Skip to content

Commit

Permalink
Fix VoxelStreamSQLite wasn't loading anything with key cache enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed May 10, 2024
1 parent 05834b7 commit 67245c4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 deletions.
3 changes: 3 additions & 0 deletions doc/source/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Semver is not yet in place, so each version can have breaking changes, although
- Added project setting `voxel/ownership_checks` to turn off sanity checks done by certain virtual functions that pass an object (such as `_generate_block`). Relevant for C#, where the garbage collection model prevents such checks from working properly.
- `VoxelViewer`: added `view_distance_vertical_ratio` to use different vertical view distance proportionally to the horizontal distance

- Fixes
- `VoxelStreamSQLite`: fixed `set_key_cache_enabled(true)` caused nothing to load


1.2 - 20/04/2024 - branch `1.2` - tag `v1.2.0`
------------------------------------------------
Expand Down
9 changes: 6 additions & 3 deletions streams/sqlite/voxel_stream_sqlite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,11 @@ void VoxelStreamSQLite::save_voxel_block(VoxelStream::VoxelQueryData &q) {
void VoxelStreamSQLite::load_voxel_blocks(Span<VoxelStream::VoxelQueryData> p_blocks) {
ZN_PROFILE_SCOPE();

// Getting connection first to allow the key cache to load if enabled.
// This should be quick after the first call because the connection is cached.
VoxelStreamSQLiteInternal *con = get_connection();
ERR_FAIL_COND(con == nullptr);

// Check the cache first
StdVector<unsigned int> blocks_to_load;
for (unsigned int i = 0; i < p_blocks.size(); ++i) {
Expand All @@ -757,12 +762,10 @@ void VoxelStreamSQLite::load_voxel_blocks(Span<VoxelStream::VoxelQueryData> p_bl

if (blocks_to_load.size() == 0) {
// Everything was cached, no need to query the database
recycle_connection(con);
return;
}

VoxelStreamSQLiteInternal *con = get_connection();
ERR_FAIL_COND(con == nullptr);

// TODO We should handle busy return codes
ERR_FAIL_COND(con->begin_transaction() == false);

Expand Down
2 changes: 2 additions & 0 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "voxel/test_octree.h"
#include "voxel/test_region_file.h"
#include "voxel/test_storage_funcs.h"
#include "voxel/test_stream_sqlite.h"
#include "voxel/test_voxel_buffer.h"
#include "voxel/test_voxel_data_map.h"
#include "voxel/test_voxel_graph.h"
Expand Down Expand Up @@ -118,6 +119,7 @@ void run_voxel_tests() {
VOXEL_TEST(test_spatial_lock_spam);
VOXEL_TEST(test_spatial_lock_dependent_map_chunks);
VOXEL_TEST(test_discord_soakil_copypaste);
VOXEL_TEST(test_voxel_stream_sqlite_basic);

print_line("------------ Voxel tests end -------------");
}
Expand Down
64 changes: 64 additions & 0 deletions tests/voxel/test_stream_sqlite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "test_stream_sqlite.h"
#include "../../streams/sqlite/voxel_stream_sqlite.h"
#include "../testing.h"

namespace zylann::voxel::tests {

namespace {
void test_voxel_stream_sqlite_basic(bool with_key_cache) {
zylann::testing::TestDirectory test_dir;
ZN_TEST_ASSERT(test_dir.is_valid());

const String database_path = test_dir.get_path().path_join("database.sqlite");

VoxelBuffer vb1(VoxelBuffer::ALLOCATOR_DEFAULT);
vb1.create(Vector3i(16, 16, 16));
vb1.fill_area(1, Vector3i(5, 5, 5), Vector3i(10, 11, 12), 0);
const Vector3i vb1_pos(1, 2, -3);

{
Ref<VoxelStreamSQLite> stream;
stream.instantiate();
stream->set_key_cache_enabled(with_key_cache);
stream->set_database_path(database_path);
// Save block
{
VoxelStreamSQLite::VoxelQueryData q{ vb1, vb1_pos, 0, VoxelStream::RESULT_ERROR };
stream->save_voxel_block(q);
// Result is not set currently for saves...
// ZN_TEST_ASSERT(q.result == VoxelStream::RESULT_BLOCK_FOUND);
}
// Load it back (caching might take effect)
{
VoxelBuffer loaded_vb1(VoxelBuffer::ALLOCATOR_DEFAULT);
VoxelStreamSQLite::VoxelQueryData q{ loaded_vb1, vb1_pos, 0, VoxelStream::RESULT_ERROR };
stream->load_voxel_block(q);
ZN_TEST_ASSERT(q.result == VoxelStream::RESULT_BLOCK_FOUND);
ZN_TEST_ASSERT(loaded_vb1.equals(vb1));
}
// Flush before the stream object is destroyed
stream->flush();
}
{
// Create new stream object and reopen the database (avoids caching effects).
Ref<VoxelStreamSQLite> stream;
stream.instantiate();
stream->set_key_cache_enabled(with_key_cache);
stream->set_database_path(database_path);
{
VoxelBuffer loaded_vb1(VoxelBuffer::ALLOCATOR_DEFAULT);
VoxelStreamSQLite::VoxelQueryData q{ loaded_vb1, vb1_pos, 0, VoxelStream::RESULT_ERROR };
stream->load_voxel_block(q);
ZN_TEST_ASSERT(q.result == VoxelStream::RESULT_BLOCK_FOUND);
ZN_TEST_ASSERT(loaded_vb1.equals(vb1));
}
}
}
} // namespace

void test_voxel_stream_sqlite_basic() {
test_voxel_stream_sqlite_basic(false);
test_voxel_stream_sqlite_basic(true);
}

} // namespace zylann::voxel::tests
10 changes: 10 additions & 0 deletions tests/voxel/test_stream_sqlite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef VOXEL_TESTS_STREAM_SQLITE_H
#define VOXEL_TESTS_STREAM_SQLITE_H

namespace zylann::voxel::tests {

void test_voxel_stream_sqlite_basic();

} // namespace zylann::voxel::tests

#endif // VOXEL_TESTS_STREAM_SQLITE_H

0 comments on commit 67245c4

Please sign in to comment.