diff --git a/bindings/c/docs/C_API_Design.md b/bindings/c/docs/C_API_Design.md index dfa70e486..e9310016b 100644 --- a/bindings/c/docs/C_API_Design.md +++ b/bindings/c/docs/C_API_Design.md @@ -278,7 +278,7 @@ Invalid parameters are rejected with `SVS_ERROR_INVALID_ARGUMENT`; unlike the le |-----------------|----------| | `SVS_SYNC_KIND_NONE` | No internal synchronization (default); the caller must serialize access | | `SVS_SYNC_KIND_GLOBAL` | Index-wide reader/writer lock: read-only operations (search, `has_id`, `get_distance`, reconstruct, `get_size`, `get_num_threads`, memory queries, use as a conversion source) run concurrently; mutating operations (add/delete points, consolidate, compact, save, `set_num_threads`) are exclusive | -| `SVS_SYNC_KIND_FINE_GRAIN` | Backed by the concurrent Vamana index, which synchronizes updates itself: read-only operations and add/delete points, consolidate, compact all take a shared lock and run in parallel (compact still waits inside the index). Save, `set_num_threads`, memory queries and use as a conversion source are exclusive | +| `SVS_SYNC_KIND_FINE_GRAIN` | Backed by the concurrent Vamana index, which synchronizes updates itself: read-only operations and add/delete points, consolidate, compact all take a shared lock and run in parallel (compact still waits inside the index). Save takes the shared lock too: it runs alongside searches and makes writers wait. `set_num_threads`, memory queries and use as a conversion source are exclusive | Synchronization limits: - `svs_index_free()` must not run concurrently with any other call on the same handle. diff --git a/bindings/c/include/svs/c/svs_c.h b/bindings/c/include/svs/c/svs_c.h index e8b69c30d..96b7d6638 100644 --- a/bindings/c/include/svs/c/svs_c.h +++ b/bindings/c/include/svs/c/svs_c.h @@ -597,7 +597,8 @@ struct svs_memory_breakdown { /// With SVS_SYNC_KIND_FINE_GRAIN, the handle is backed by a concurrent index that /// synchronizes updates itself: add_points, delete_points, consolidate and compact take a /// shared lock and run alongside searches and each other (compact still waits for -/// in-flight operations inside the index). Only save, set_num_threads, memory queries +/// in-flight operations inside the index). save also takes a shared lock: it runs +/// alongside searches and makes writers wait. Only set_num_threads, memory queries /// and use as a conversion source take an exclusive lock. /// /// @remarks Synchronization covers operations on the index only: diff --git a/bindings/c/src/index_vamana.hpp b/bindings/c/src/index_vamana.hpp index 2a193981e..7aa643d27 100644 --- a/bindings/c/src/index_vamana.hpp +++ b/bindings/c/src/index_vamana.hpp @@ -158,8 +158,9 @@ struct DynamicIndexVamana : public DynamicIndex { /// Dynamic index backed by svs::ConcurrentDynamicVamana. /// /// The index synchronizes add/delete/consolidate/compact itself, so they take the shared -/// lock alongside searches. Operations that are not thread-safe in the index (save, -/// set_num_threads) and whole-index reads (memory breakdown, use as a conversion source) +/// lock alongside searches. save() also takes the shared lock: the index excludes its own +/// writers while saving. Operations that are not thread-safe in the index +/// (set_num_threads) and whole-index reads (memory breakdown, use as a conversion source) /// take the exclusive lock. struct ConcurrentIndexVamana : public DynamicIndexVamana { ConcurrentIndexVamana( @@ -179,6 +180,16 @@ struct ConcurrentIndexVamana : public DynamicIndexVamana { size_t delete_points(std::span ids) override; + void save(const std::filesystem::path& directory) override { + auto lock = read_lock(); + index.save(directory / "config", directory / "graph", directory / "data"); + } + + void save(std::ostream& stream) override { + auto lock = read_lock(); + index.save(stream); + } + void consolidate() override { auto lock = read_lock(); index.consolidate(); diff --git a/include/svs/concurrent/README.md b/include/svs/concurrent/README.md index a8ba73bf0..4d9cb2a83 100644 --- a/include/svs/concurrent/README.md +++ b/include/svs/concurrent/README.md @@ -82,14 +82,14 @@ share nothing. You may call `compact()` at any time, but it blocks all other work. It makes the storage smaller, so it must wait for every reader to finish first. It is the only caller that takes `compact_mutex_` exclusive. It runs `consolidate_locked()` -before it compacts. +before it compacts. `save()` takes `save_gatekeeper_` and +`save_mutex_` exclusive, so writers wait while searches keep running. It writes the +slots as they are. ### 1d. Not safe at all Call these only when no other thread uses the index: -- `save()` - it holds no lock while it writes the files. It calls `consolidate()` - and `compact()` first, but it releases both locks before writing. - `set_alpha`, `set_prune_to`, `set_max_candidates`, `set_construction_window_size`, `set_threadpool` - plain writes, no lock. @@ -142,6 +142,8 @@ There are three groups: | Lock | Protects | Taken exclusive by | | --- | --- | --- | +| `save_mutex_` | the index state stays unchanged while `save()` writes it | `save()` only | +| `save_gatekeeper_` | new writers wait behind a pending `save()` | `save()` only | | `compact_mutex_` | the storage stays alive | `compact()` only | | `translator_mutex_` | the two id hash maps | `add_points`, `consolidate`, `compact`, `replace_external_id` | | `pending_insertions_mutex_` | `pending_insertions_` | `add_points` | @@ -159,6 +161,7 @@ so it waits for all of them. deadlock. ``` +save_gatekeeper_ -> save_mutex_ -> compact_gatekeeper_ -> compact_mutex_ compact_mutex_ -> slot_alloc_mutex_ compact_mutex_ -> translator_mutex_ l2e_mutex_ -> e2l_mutex_ (multi index) @@ -247,7 +250,7 @@ The dataset must use a `SegmentedBlocked` allocator; this is checked at compile time. `distance` may be a functor or a `svs::DistanceType`. `batch_iterator()` returns this index's own `BatchIterator`, so it takes the locks described in 1a. -The rules from section 1 apply unchanged: `save()`, `set_threadpool()` and the +The rules from section 1 apply unchanged: `set_threadpool()` and the build-parameter setters still need exclusive access. ```cpp diff --git a/include/svs/concurrent/dynamic_index.h b/include/svs/concurrent/dynamic_index.h index 1fc1a8980..da0538dc4 100644 --- a/include/svs/concurrent/dynamic_index.h +++ b/include/svs/concurrent/dynamic_index.h @@ -182,6 +182,7 @@ class MutableVamanaIndex { static constexpr bool supports_deletions = true; static constexpr bool supports_saving = true; static constexpr bool needs_id_translation = true; + static constexpr bool saves_slot_status = true; // Type Aliases using Idx = typename Graph::index_type; @@ -250,7 +251,8 @@ class MutableVamanaIndex { // compact() holds it exclusive. // // Lock acquisition order across the codebase: - // compact_gatekeeper_ -> compact_mutex_ (never reversed) + // save_gatekeeper_ -> save_mutex_ -> compact_gatekeeper_ -> compact_mutex_ + // (never reversed) // compact_mutex_ -> slot_alloc_mutex_ (never reversed) // compact_mutex_ -> translator_mutex_ (never reversed) // slot_alloc_mutex_ and translator_mutex_ are never held simultaneously @@ -263,6 +265,13 @@ class MutableVamanaIndex { std::make_unique()}; // Iterators check this under compact_mutex_ before reusing cached internal IDs. size_t compaction_epoch_ = 0; + // Outermost lock. Writers (add_points, delete_entries, consolidate, compact, + // replace_external_id) hold it shared; save() holds it exclusive. Searches never take + // it. + std::unique_ptr save_mutex_{std::make_unique()}; + // Once save() owns this gate, new writers cannot keep it waiting on save_mutex_. + std::unique_ptr save_gatekeeper_{ + std::make_unique()}; // Writer-only mutex serializing slot allocation in add_points std::unique_ptr slot_alloc_mutex_{std::make_unique()}; @@ -447,7 +456,7 @@ class MutableVamanaIndex { /// /// * data.size() == graph.n_nodes() == status.size(). /// * ``status`` contains no ``Pending`` slots. - /// * ``translator`` maps exactly the ``Valid`` slots of ``status``. + /// * ``translator`` maps every ``Valid`` slot and no ``Empty`` slot of ``status``. template MutableVamanaIndex( const VamanaIndexParameters& config, @@ -513,17 +522,12 @@ class MutableVamanaIndex { status_[i] = s; } - if (translator_.size() != num_valid) { - throw ANNEXCEPTION( - "Translator has {} IDs but status has {} valid slots", - translator_.size(), - num_valid - ); - } for (auto pair : translator_) { - if (pair.second >= n || status[pair.second] != SlotMetadata::Valid) { + if (pair.second >= n || (status[pair.second] != SlotMetadata::Valid && + status[pair.second] != SlotMetadata::Deleted)) { throw ANNEXCEPTION( - "Translator maps external id {} to non-valid slot {}", + "Translator maps external id {} to slot {} that is not valid or " + "deleted", pair.first, pair.second ); @@ -826,6 +830,7 @@ class MutableVamanaIndex { /// @param new_id The external ID to assign. Must not be mapped at all. /// ReplaceExternalIdResult replace_external_id(size_t old_id, size_t new_id) { + auto save_lock = lock_save_shared(); auto compact_lock = lock_storage_shared(); std::lock_guard lock{*translator_mutex_}; @@ -882,6 +887,12 @@ class MutableVamanaIndex { // This is an internal method, mostly used to help implement the batch iterator. ValidBuilder internal_search_builder() const { return ValidBuilder{status_}; } + /// @brief Shared lock taken first by every writer; waits behind a pending save(). + [[nodiscard]] std::shared_lock lock_save_shared() const { + std::shared_lock gatekeeper_lock{*save_gatekeeper_}; + return std::shared_lock(*save_mutex_); + } + /// @brief RAII shared lock guarding data_/graph_ against compact()'s shrink /// (which frees segments). Used by searches, iterators and mutations that /// may overlap each other but must exclude compaction. @@ -1108,6 +1119,8 @@ class MutableVamanaIndex { return {}; } + auto save_lock = lock_save_shared(); + // Reserve slot ownership against compact(). Held for the entire call, // including the lock-free Phase 2-4 below; compact() takes this // exclusive and so will block until every in-flight add finishes. @@ -1315,6 +1328,7 @@ class MutableVamanaIndex { /// graph. /// template size_t delete_entries(const T& ids) { + auto save_lock = lock_save_shared(); auto compact_lock = lock_storage_shared(); std::shared_lock lock{*translator_mutex_}; size_t deleted = 0; @@ -1400,6 +1414,7 @@ class MutableVamanaIndex { /// improve performance but requires more working memory. /// void compact(Idx batch_size = 1'000) { + auto save_lock = lock_save_shared(); std::lock_guard gatekeeper_lock{*compact_gatekeeper_}; std::lock_guard compact_lock{*compact_mutex_}; @@ -1613,6 +1628,7 @@ class MutableVamanaIndex { ///// Mutation void consolidate() { + auto save_lock = lock_save_shared(); auto compact_lock = lock_storage_shared(); consolidate_locked(); } @@ -1702,6 +1718,7 @@ class MutableVamanaIndex { /// @returns The number of listed IDs that were consolidated. /// template size_t consolidate(const T& ids) { + auto save_lock = lock_save_shared(); auto compact_lock = lock_storage_shared(); // Collect the internal slots of the listed, already-soft-deleted IDs. @@ -1741,18 +1758,24 @@ class MutableVamanaIndex { get_search_parameters()}; } - static constexpr lib::Version save_version = lib::Version(0, 0, 0); + static constexpr lib::Version save_version = lib::Version(0, 0, 1); void save( const std::filesystem::path& config_directory, const std::filesystem::path& graph_directory, const std::filesystem::path& data_directory ) { + std::lock_guard gatekeeper_lock{*save_gatekeeper_}; + std::lock_guard save_lock{*save_mutex_}; vamana::detail::save_dynamic_index( *this, config_directory, graph_directory, data_directory ); } - void save(std::ostream& os) { vamana::detail::save_dynamic_index(*this, os); } + void save(std::ostream& os) { + std::lock_guard gatekeeper_lock{*save_gatekeeper_}; + std::lock_guard save_lock{*save_mutex_}; + vamana::detail::save_dynamic_index(*this, os); + } ///// ///// Calibrate @@ -2323,6 +2346,7 @@ auto auto_dynamic_assemble( // Read the combined TOML (parameters + translation) // and the translator binary data. auto table = lib::detail::read_metadata(is); + auto version = table.template cast().version(); auto parameters = lib::load( table.template cast().at("parameters").template cast() @@ -2333,6 +2357,13 @@ auto auto_dynamic_assemble( auto translator = IDTranslator::load(translation, is); + auto status = std::vector(); + if (version == lib::Version(0, 0, 1)) { + status.resize(lib::load_at(table.template cast(), "num_status") + ); + lib::read_binary(is, status); + } + auto data = data_loader(); auto graph = graph_loader(); @@ -2344,11 +2375,8 @@ auto auto_dynamic_assemble( ); } - auto translator_size = translator.size(); - if (translator_size != datasize) { - throw ANNEXCEPTION( - "Translator has {} IDs but should have {}", translator_size, datasize - ); + if (version != lib::Version(0, 0, 1)) { + status.assign(datasize, SlotMetadata::Valid); } auto threadpool = threads::as_threadpool(std::move(threadpool_proto)); @@ -2357,7 +2385,7 @@ auto auto_dynamic_assemble( std::move(data), std::move(graph), std::move(distance), - std::vector(datasize, SlotMetadata::Valid), + std::move(status), std::move(translator), std::move(threadpool), std::move(logger)}; diff --git a/include/svs/index/vamana/dynamic_index.h b/include/svs/index/vamana/dynamic_index.h index 8a9a64dd1..24c025d02 100644 --- a/include/svs/index/vamana/dynamic_index.h +++ b/include/svs/index/vamana/dynamic_index.h @@ -118,16 +118,27 @@ void save_dynamic_index( const std::filesystem::path& graph_directory, const std::filesystem::path& data_directory ) { - // Post-consolidation, all entries should be "valid". - // Therefore, we don't need to save the slot metadata. - index.consolidate(); - index.compact(); + if constexpr (!Index::saves_slot_status) { + // Post-consolidation, all entries should be "valid". + // Therefore, we don't need to save the slot metadata. + index.consolidate(); + index.compact(); + } + std::vector status; + if constexpr (Index::saves_slot_status) { + for (auto slot : index.status_snapshot()) { + if (slot == decltype(slot)::Pending) { + throw ANNEXCEPTION("Cannot save an index with pending slots"); + } + status.push_back(static_cast(slot)); + } + } // Save auxiliary data structures. lib::save_to_disk( lib::SaveOverride([&](const lib::SaveContext& ctx) { // Save the construction parameters. - return lib::SaveTable( + auto table = lib::SaveTable( "vamana_dynamic_auxiliary_parameters", Index::save_version, { @@ -136,6 +147,10 @@ void save_dynamic_index( {"translation", lib::save(index.view_translator(), ctx)}, } ); + if constexpr (Index::saves_slot_status) { + table.insert("status", lib::save(lib::BinaryBlobSaver(status), ctx)); + } + return table; }), config_directory ); @@ -147,10 +162,21 @@ void save_dynamic_index( } template void save_dynamic_index(Index& index, std::ostream& os) { - // Post-consolidation, all entries should be "valid". - // Therefore, we don't need to save the slot metadata. - index.consolidate(); - index.compact(); + if constexpr (!Index::saves_slot_status) { + // Post-consolidation, all entries should be "valid". + // Therefore, we don't need to save the slot metadata. + index.consolidate(); + index.compact(); + } + std::vector status; + if constexpr (Index::saves_slot_status) { + for (auto slot : index.status_snapshot()) { + if (slot == decltype(slot)::Pending) { + throw ANNEXCEPTION("Cannot save an index with pending slots"); + } + status.push_back(static_cast(slot)); + } + } lib::begin_serialization(os); auto save_table = lib::SaveTable( @@ -162,8 +188,14 @@ template void save_dynamic_index(Index& index, std::ostream& os {"translation", lib::detail::exit_hook(index.view_translator().metadata())}, } ); + if constexpr (Index::saves_slot_status) { + save_table.insert("num_status", lib::save(status.size())); + } lib::save_to_stream(save_table, os); index.view_translator().save(os); + if constexpr (Index::saves_slot_status) { + lib::write_binary(os, status); + } // Save the dataset. lib::save_to_stream(index.view_data(), os); @@ -223,6 +255,7 @@ class MutableVamanaIndex { static constexpr bool supports_deletions = true; static constexpr bool supports_saving = true; static constexpr bool needs_id_translation = true; + static constexpr bool saves_slot_status = false; // Type Aliases using Idx = typename Graph::index_type; @@ -1509,7 +1542,7 @@ template struct VamanaStateLoader { // We provide the option to load from a static index. return VamanaIndexParameters::check_load_compatibility(schema, version) || (schema == "vamana_dynamic_auxiliary_parameters" && - version == lib::Version(0, 0, 0)); + (version == lib::Version(0, 0, 0) || version == lib::Version(0, 0, 1))); } // Provide a compatibility path for loading static datasets. @@ -1522,10 +1555,20 @@ template struct VamanaStateLoader { std::vector(assume_datasize, Metadata::Valid)}; } + auto status = std::vector(assume_datasize, Metadata::Valid); + if (table.version() == lib::Version(0, 0, 1)) { + std::vector raw = + lib::load_at>(table, "status"); + status.resize(raw.size()); + std::transform(raw.begin(), raw.end(), status.begin(), [](uint8_t s) { + return static_cast(s); + }); + } + return VamanaStateLoader{ SVS_LOAD_MEMBER_AT_(table, parameters), svs::lib::load_at(table, "translation"), - std::vector(assume_datasize, Metadata::Valid), + std::move(status), }; } @@ -1661,6 +1704,16 @@ auto auto_dynamic_assemble( ); } + // A file saved by the concurrent index can still contain deleted IDs. The dynamic + // index checks IDs only in the translator (has_id), so remove them. + auto to_drop = std::vector(); + for (auto pair : translator) { + if (pair.second < datasize && status[pair.second] == SlotMetadata::Deleted) { + to_drop.push_back(pair.second); + } + } + translator.delete_internal(to_drop, false); + // The translator must cover exactly the valid slots; copied state may contain // deleted or empty slots. size_t num_valid = 0; @@ -1710,6 +1763,7 @@ auto auto_dynamic_assemble( // Read the combined TOML (parameters + translation) // and the translator binary data. auto table = lib::detail::read_metadata(is); + auto version = table.template cast().version(); auto parameters = lib::load( table.template cast().at("parameters").template cast() @@ -1720,6 +1774,13 @@ auto auto_dynamic_assemble( auto translator = IDTranslator::load(translation, is); + auto status = std::vector(); + if (version == lib::Version(0, 0, 1)) { + status.resize(lib::load_at(table.template cast(), "num_status") + ); + lib::read_binary(is, status); + } + auto data = data_loader(); auto graph = graph_loader(); @@ -1731,14 +1792,29 @@ auto auto_dynamic_assemble( ); } + if (version != lib::Version(0, 0, 1)) { + status.assign(datasize, SlotMetadata::Valid); + } + + // A file saved by the concurrent index can still contain deleted IDs. The dynamic + // index checks IDs only in the translator (has_id), so remove them. + auto to_drop = std::vector(); + for (auto pair : translator) { + if (pair.second < status.size() && status[pair.second] == SlotMetadata::Deleted) { + to_drop.push_back(pair.second); + } + } + translator.delete_internal(to_drop, false); + + auto num_valid = + static_cast(std::count(status.begin(), status.end(), SlotMetadata::Valid)); auto translator_size = translator.size(); - if (translator_size != datasize) { + if (translator_size != num_valid) { throw ANNEXCEPTION( - "Translator has {} IDs but should have {}", translator_size, datasize + "Translator has {} IDs but should have {}", translator_size, num_valid ); } - auto status = std::vector(datasize, SlotMetadata::Valid); auto threadpool = threads::as_threadpool(std::move(threadpool_proto)); return MutableVamanaIndex{ parameters, diff --git a/include/svs/orchestrators/concurrent_dynamic_vamana.h b/include/svs/orchestrators/concurrent_dynamic_vamana.h index 884e67804..fd89eac28 100644 --- a/include/svs/orchestrators/concurrent_dynamic_vamana.h +++ b/include/svs/orchestrators/concurrent_dynamic_vamana.h @@ -38,8 +38,9 @@ namespace svs { /// The wrapped index may be searched and mutated from several threads at once: search, /// batch iteration, ``add_points``, ``delete_points``, ``consolidate``, ``compact``, ID /// inspection, ``get_distance`` and ``reconstruct_at`` are safe to call concurrently. -/// ``save``, ``set_threadpool`` and the build-parameter setters are not; call them only -/// when no other thread uses the index. See ``include/svs/concurrent/README.md``. +/// ``save`` is safe alongside searches and blocks writers while it runs. +/// ``set_threadpool`` and the build-parameter setters are not safe; call them only when no +/// other thread uses the index. See ``include/svs/concurrent/README.md``. /// /// The dataset must use a ``svs::index::vamana::concurrent::SegmentedBlocked`` allocator so /// that growing it never relocates storage under concurrent readers. diff --git a/tests/svs/concurrent/dynamic_index.cpp b/tests/svs/concurrent/dynamic_index.cpp index 214dec205..8fef4f2f7 100644 --- a/tests/svs/concurrent/dynamic_index.cpp +++ b/tests/svs/concurrent/dynamic_index.cpp @@ -565,6 +565,127 @@ CATCH_TEST_CASE( } } } + + CATCH_SECTION("Save keeps deleted and empty slots") { + // Ids [0, 100) are consolidated (Empty slots) but the index is not compacted; + // ids [200, 300) are soft-deleted (Deleted slots). + std::vector consolidated(100); + std::iota(consolidated.begin(), consolidated.end(), 0); + index.delete_entries(consolidated); + index.consolidate(); + std::vector soft_deleted(100); + std::iota(soft_deleted.begin(), soft_deleted.end(), 200); + index.delete_entries(soft_deleted); + + const auto status_before = index.status_snapshot(); + const auto num_slots = index.view_data().size(); + const auto size_before = index.size(); + CATCH_REQUIRE( + std::count( + status_before.begin(), status_before.end(), cc::SlotMetadata::Empty + ) > 0 + ); + CATCH_REQUIRE( + std::count( + status_before.begin(), status_before.end(), cc::SlotMetadata::Deleted + ) > 0 + ); + + std::stringstream stream; + index.save(stream); + + // Saving does not compact the source. + CATCH_REQUIRE(index.view_data().size() == num_slots); + CATCH_REQUIRE(index.size() == size_before); + CATCH_REQUIRE(index.status_snapshot() == status_before); + + auto deserializer = svs::lib::detail::Deserializer::build(stream); + CATCH_REQUIRE(deserializer.is_native()); + auto loaded = cc::auto_dynamic_assemble( + stream, + [&]() -> ConcurrentGraph { return ConcurrentGraph::load(stream); }, + [&]() -> ConcurrentData { + return svs::lib::load_from_stream(stream); + }, + Distance(), + num_threads + ); + + CATCH_REQUIRE(loaded.view_data().size() == num_slots); + CATCH_REQUIRE(loaded.size() == size_before); + CATCH_REQUIRE(loaded.status_snapshot() == status_before); + for (size_t i = 0; i < 400; ++i) { + const bool removed = (i < 100) || (i >= 200 && i < 300); + CATCH_REQUIRE(loaded.has_id(i) == !removed); + } + + auto expected = svs::QueryResult(queries.size(), num_neighbors); + index.search(expected.view(), queries.cview(), search_params); + auto actual = svs::QueryResult(queries.size(), num_neighbors); + loaded.search(actual.view(), queries.cview(), search_params); + for (size_t q = 0; q < queries.size(); ++q) { + for (size_t i = 0; i < num_neighbors; ++i) { + CATCH_REQUIRE(actual.index(q, i) == expected.index(q, i)); + } + } + + // The same save must also load into the dynamic index: same size, same + // visible ids, same search results. + auto check_loaded = [&](auto& dynamic) { + CATCH_REQUIRE(dynamic.size() == size_before); + for (size_t i = 0; i < 400; ++i) { + const bool removed = (i < 100) || (i >= 200 && i < 300); + CATCH_REQUIRE(dynamic.has_id(i) == !removed); + } + auto got = svs::QueryResult(queries.size(), num_neighbors); + dynamic.search(got.view(), queries.cview(), search_params); + for (size_t q = 0; q < queries.size(); ++q) { + for (size_t i = 0; i < num_neighbors; ++i) { + CATCH_REQUIRE(got.index(q, i) == expected.index(q, i)); + } + } + }; + + stream.clear(); + stream.seekg(0); + auto stream_deserializer = svs::lib::detail::Deserializer::build(stream); + CATCH_REQUIRE(stream_deserializer.is_native()); + std::istream& is = stream; // select the `std::istream&` overload + auto from_stream = svs::index::vamana::auto_dynamic_assemble( + is, + svs::lib::Lazy([&]() { + return svs::graphs::SimpleBlockedGraph::load(stream); + }), + svs::lib::Lazy([&]() { + return svs::lib::load_from_stream>(stream); + }), + Distance(), + num_threads + ); + check_loaded(from_stream); + + svs::lib::UniqueTempDirectory tempdir{"svs_concurrent_save_to_dynamic"}; + std::filesystem::create_directories(tempdir.get() / "config"); + std::filesystem::create_directories(tempdir.get() / "graph"); + std::filesystem::create_directories(tempdir.get() / "data"); + index.save( + tempdir.get() / "config", tempdir.get() / "graph", tempdir.get() / "data" + ); + auto from_directory = svs::index::vamana::auto_dynamic_assemble( + tempdir.get() / "config", + svs::lib::Lazy([&]() { + return svs::graphs::SimpleBlockedGraph::load( + tempdir.get() / "graph" + ); + }), + svs::lib::Lazy([&]() { + return svs::data::BlockedData::load(tempdir.get() / "data"); + }), + Distance(), + num_threads + ); + check_loaded(from_directory); + } } CATCH_TEST_CASE(