Skip to content

Commit

Permalink
Fix: index_dense_t::make(path)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvardanian committed May 5, 2024
1 parent fa09c8b commit bcc27c4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
15 changes: 4 additions & 11 deletions include/usearch/index_dense.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,17 +592,10 @@ class index_dense_gt {
* @return An instance of ::index_dense_gt or error, wrapped in a `state_result_t`.
*/
static state_result_t make(char const* path, bool view = false) {
index_dense_metadata_result_t meta = index_dense_metadata_from_path(path);
if (!meta)
return {};
metric_punned_t metric(meta.head.dimensions, meta.head.kind_metric, meta.head.kind_scalar);
index_dense_gt result = make(metric);
if (!result)
return result;
if (view)
result.view(path);
else
result.load(path);
state_result_t result;
serialization_result_t serialization_result = view ? result.index.view(path) : result.index.load(path);
if (!serialization_result)
return result.failed(std::move(serialization_result.error));
return result;
}

Expand Down
19 changes: 12 additions & 7 deletions python/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ static dense_index_py_t make_index( //
if (metric.missing())
throw std::invalid_argument("Unsupported metric!");

return index_dense_t::make(metric, config);
using index_state_t = typename index_dense_t::state_result_t;
index_state_t state = index_dense_t::make(metric, config);
if (!state)
throw std::invalid_argument(state.error.release());

return std::move(state.index);
}

scalar_kind_t numpy_string_to_kind(std::string const& name) {
Expand Down Expand Up @@ -231,7 +236,7 @@ static void add_many_to_index( //

if (!threads)
threads = std::thread::hardware_concurrency();
if (!index.reserve(index_limits_t(ceil2(index.size() + vectors_count), threads)))
if (!index.try_reserve(index_limits_t(ceil2(index.size() + vectors_count), threads)))
throw std::invalid_argument("Out of memory!");

// clang-format off
Expand Down Expand Up @@ -263,7 +268,7 @@ static void search_typed( //

if (!threads)
threads = std::thread::hardware_concurrency();
if (!index.reserve(index_limits_t(index.size(), threads)))
if (!index.try_reserve(index_limits_t(index.size(), threads)))
throw std::invalid_argument("Out of memory!");

// Progress status
Expand Down Expand Up @@ -342,7 +347,7 @@ static void search_typed( //
limits.members = index.size();
limits.threads_add = 0;
limits.threads_search = 1;
if (!index.reserve(limits)) {
if (!index.try_reserve(limits)) {
atomic_error = "Out of memory!";
return false;
}
Expand Down Expand Up @@ -716,7 +721,7 @@ static void compact_index(dense_index_py_t& index, std::size_t threads, progress

if (!threads)
threads = std::thread::hardware_concurrency();
if (!index.reserve(index_limits_t(index.size(), threads)))
if (!index.try_reserve(index_limits_t(index.size(), threads)))
throw std::invalid_argument("Out of memory!");

index.compact(executor_default_t{threads}, progress_t{progress});
Expand Down Expand Up @@ -1050,7 +1055,7 @@ PYBIND11_MODULE(compiled, m) {

if (!threads)
threads = std::thread::hardware_concurrency();
if (!index.reserve(index_limits_t(index.size(), threads)))
if (!index.try_reserve(index_limits_t(index.size(), threads)))
throw std::invalid_argument("Out of memory!");

index.isolate(executor_default_t{threads});
Expand All @@ -1069,7 +1074,7 @@ PYBIND11_MODULE(compiled, m) {

if (!threads)
threads = std::thread::hardware_concurrency();
if (!index.reserve(index_limits_t(index.size(), threads)))
if (!index.try_reserve(index_limits_t(index.size(), threads)))
throw std::invalid_argument("Out of memory!");

index.isolate(executor_default_t{threads});
Expand Down

0 comments on commit bcc27c4

Please sign in to comment.