Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions bindings/c/include/svs/c/svs_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,8 @@ struct svs_dynamic_index_params {
///
/// @remarks Thread-safety: unless a specific function documents otherwise, handles
/// (svs_error_h, svs_index_h, svs_index_builder_h, svs_algorithm_h, svs_storage_h,
/// svs_search_params_h) are not internally synchronized. Do not operate on the same
/// handle from multiple threads concurrently without external synchronization.
/// svs_search_params_h, svs_task_h) are not internally synchronized. Do not operate on
/// the samehandle from multiple threads concurrently without external synchronization.
/// Exception: dynamic indices built or loaded with a svs_sync_kind other than
/// SVS_SYNC_KIND_NONE are internally synchronized, with the limits listed in
/// svs_sync_kind.
Expand All @@ -661,6 +661,7 @@ typedef struct svs_storage* svs_storage_h;
typedef struct svs_search_params* svs_search_params_h;
typedef struct svs_leanvec_training_data* svs_leanvec_training_data_h;
typedef struct svs_logger* svs_logger_h;
typedef struct svs_task* svs_task_h;

// Fully defined types; "_t" suffix indicates a fully defined struct
typedef enum svs_error_code svs_error_code_t;
Expand Down Expand Up @@ -1648,6 +1649,44 @@ SVS_API bool svs_index_dynamic_delete_points(
svs_error_h out_err /*=NULL*/
);

/// @brief Start deletion of points from a dynamic index,
// returns a task that finalize the deletion.
// Points aren't returnable by search after this operation.
/// @param index The dynamic index handle
/// @param ids Pointer to the vector IDs to delete (size_t array)
/// @param num_ids The number of vector IDs to delete
/// @param follow_up Receives the task on success, not written on failure.
// Free it with svs_task_free()
/// @param out_deleted_count Optional pointer to store the number of successfully deleted
/// vectors
/// @param out_err An optional error handle to capture errors
/// @return true on success, false on failure
/// @error SVS_ERROR_NOT_IMPLEMENTED if @p index doesn't use SVS_SYNC_KIND_FINE_GRAIN or
/// @p num_ids is greater than 1
SVS_API bool svs_index_dynamic_mark_points_deleted(
svs_index_h index,
const size_t* ids,
size_t num_ids,
svs_task_h* follow_up,
size_t* out_deleted_count /*=NULL*/,
svs_error_h out_err /*=NULL*/
);

/// @brief Finalize a two-stage task being started by one of the following calls:
// svs_index_dynamic_mark_points_deleted()
/// @param task The task handle
/// @param out_err An optional error handle to capture errors
/// @return true on success, false on failure
SVS_API bool svs_execute_task(
svs_task_h task, svs_error_h out_err /*=NULL*/
);

/// @brief Free the task handle
/// @param task The task handle to free
/// @remarks A task keeps its index alive: the index is released only when the index
/// handle and all of its tasks are freed.
SVS_API void svs_task_free(svs_task_h task);

/// @brief Check if a dynamic index has a specific ID
/// @param index The dynamic index handle
/// @param id The vector ID to check for
Expand Down
5 changes: 5 additions & 0 deletions bindings/c/src/index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <svs/core/data/simple.h>
#include <svs/core/query_result.h>
#include <svs/index/vamana/dynamic_index.h>
#include <svs/index/vamana/index.h>
#include <svs/lib/misc.h>

Expand Down Expand Up @@ -63,6 +64,8 @@ struct Index {
virtual size_t size() const = 0;
};

using DeleteResponse = svs::index::vamana::DeleteResponse;

struct DynamicIndex : public Index {
svs_sync_kind_t sync_kind;
mutable std::optional<std::shared_mutex> mutex;
Expand Down Expand Up @@ -92,8 +95,10 @@ struct DynamicIndex : public Index {
svs::data::ConstSimpleDataView<float> new_points, std::span<const size_t> ids
) = 0;
virtual size_t delete_points(std::span<const size_t> ids) = 0;
virtual size_t delete_points(std::span<const size_t> ids, DeleteResponse& response) = 0;
virtual bool has_id(size_t id) const = 0;
virtual void consolidate() = 0;
virtual size_t repair(const DeleteResponse& response) = 0;
virtual void compact(size_t batchsize) = 0;
};
} // namespace svs::c_runtime
39 changes: 39 additions & 0 deletions bindings/c/src/index_vamana.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,20 @@ void DynamicIndexVamana::consolidate() {
index.consolidate();
}

size_t DynamicIndexVamana::delete_points(
std::span<const size_t> /*ids*/, DeleteResponse& /*response*/
) {
throw not_implemented(
"Reporting internal IDs is only supported by FINE GRAIN dynamic indices."
);
}

size_t DynamicIndexVamana::repair(const DeleteResponse& /*response*/) {
throw not_implemented(
"Consolidating internal IDs is only supported by FINE GRAIN dynamic indices."
);
}

void DynamicIndexVamana::compact(size_t batchsize) {
auto lock = write_lock();
// Ensure the index is consolidated before compacting.
Expand Down Expand Up @@ -320,4 +334,29 @@ size_t ConcurrentIndexVamana::delete_points(std::span<const size_t> ids) {
return delete_points_unlocked(ids);
}

size_t ConcurrentIndexVamana::delete_points(
std::span<const size_t> ids, DeleteResponse& response
) {
assert(sync_kind == SVS_SYNC_KIND_FINE_GRAIN);
if (ids.size() > 1) {
throw not_implemented(
"FINE GRAIN dynamic index only supports deleting one point at a time."
);
}
auto lock = read_lock();
return index.delete_points(ids, response);
}

size_t ConcurrentIndexVamana::repair(const DeleteResponse& response) {
assert(sync_kind == SVS_SYNC_KIND_FINE_GRAIN);
if (response.internal_ids.size() > 1) {
throw not_implemented(
"FINE GRAIN dynamic index only supports repairment one point at a time."
);
}
auto lock = read_lock();
// The index skips the repair if it was compacted since the delete.
return index.repair_incoming_connections(response);
}

} // namespace svs::c_runtime
8 changes: 8 additions & 0 deletions bindings/c/src/index_vamana.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ struct DynamicIndexVamana : public DynamicIndex {

void consolidate() override;

size_t delete_points(std::span<const size_t> ids, DeleteResponse& response) override;

size_t repair(const DeleteResponse& response) override;

void compact(size_t batchsize) override;

size_t get_num_threads() const override {
Expand Down Expand Up @@ -184,6 +188,10 @@ struct ConcurrentIndexVamana : public DynamicIndexVamana {
index.consolidate();
}

size_t delete_points(std::span<const size_t> ids, DeleteResponse& response) override;

size_t repair(const DeleteResponse& response) override;

void compact(size_t batchsize) override {
// exclusive lock prevents potential deadlock in case of custom threadpool takes
// internal mutex in parallel_for method
Expand Down
86 changes: 86 additions & 0 deletions bindings/c/src/svs_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,92 @@ extern "C" bool svs_index_dynamic_delete_points(
);
}

struct svs_task {
using IndexPtr_t = std::shared_ptr<svs::c_runtime::DynamicIndex>;
class AbstractTask {
protected:
IndexPtr_t index_;

public:
AbstractTask(IndexPtr_t index)
: index_(index){};

virtual ~AbstractTask() = default;

virtual void execute() const = 0;
};

class RepairTask : public AbstractTask {
svs::c_runtime::DeleteResponse response_;

public:
RepairTask(IndexPtr_t index)
: AbstractTask(index){};

void execute() const override { index_->repair(response_); };

svs::c_runtime::DeleteResponse& get_response() { return response_; }
};

void execute() { task->execute(); }

svs_task(std::unique_ptr<AbstractTask> _task)
: task(std::move(_task)) {}

private:
std::unique_ptr<AbstractTask> task;
};

extern "C" bool svs_index_dynamic_mark_points_deleted(
svs_index_h index,
const size_t* ids,
size_t num_ids,
svs_task_h* follow_up,
size_t* out_deleted_count,
svs_error_h out_err
) {
using namespace svs::c_runtime;
return wrap_exceptions(
[&]() {
EXPECT_ARG_NOT_NULL(index);
EXPECT_ARG_NOT_NULL(ids);
EXPECT_ARG_GT_THAN(num_ids, 0);
EXPECT_ARG_NOT_NULL(follow_up);
auto dynamic_index_ptr = std::dynamic_pointer_cast<DynamicIndex>(index->impl);
INVALID_ARGUMENT_IF(
dynamic_index_ptr == nullptr, "Index does not support dynamic updates"
);
auto task = std::make_unique<svs_task::RepairTask>(dynamic_index_ptr);
auto& response = task->get_response();
auto output = std::make_unique<svs_task>(svs_task{std::move(task)});

auto deleted_count =
dynamic_index_ptr->delete_points(std::span(ids, num_ids), response);
if (out_deleted_count) {
*out_deleted_count = deleted_count;
}

*follow_up = output.release();
return true;
},
out_err
);
}

extern "C" bool svs_execute_task(svs_task_h task, svs_error_h out_err) {
using namespace svs::c_runtime;
return wrap_exceptions(
[&]() {
EXPECT_ARG_NOT_NULL(task);
task->execute();
return true;
},
out_err
);
}

extern "C" void svs_task_free(svs_task_h task) { delete task; }

extern "C" bool svs_index_dynamic_has_id(
svs_index_h index, size_t id, bool* out_has_id, svs_error_h out_err
) {
Expand Down
Loading
Loading