Skip to content

Commit 4aa1934

Browse files
committed
Support error handling without exceptions
1 parent c1b9b79 commit 4aa1934

16 files changed

+269
-115
lines changed

examples/cpp/example_epsilon_search.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ int main() {
5353
}
5454
std::cout << "Query #" << i << "\n";
5555
hnswlib::EpsilonSearchStopCondition<dist_t> stop_condition(epsilon2, min_num_candidates, max_elements);
56-
std::vector<std::pair<float, hnswlib::labeltype>> result =
57-
alg_hnsw->searchStopConditionClosest(query_data, stop_condition);
56+
HNSWLIB_ASSIGN_VALUE_OR_THROW_IN_TEST(result, alg_hnsw->searchStopConditionClosest(query_data, stop_condition));
5857
size_t num_vectors = result.size();
5958
std::cout << "Found " << num_vectors << " vectors\n";
6059
delete[] query_data;

examples/cpp/example_filter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ int main() {
4545
// Query the elements for themselves with filter and check returned labels
4646
int k = 10;
4747
for (int i = 0; i < max_elements; i++) {
48-
std::vector<std::pair<float, hnswlib::labeltype>> result = alg_hnsw->searchKnnCloserFirst(data + i * dim, k, &pickIdsDivisibleByTwo);
48+
HNSWLIB_ASSIGN_VALUE_OR_THROW_IN_TEST(
49+
result, alg_hnsw->searchKnnCloserFirst(data + i * dim, k, &pickIdsDivisibleByTwo));
4950
for (auto item: result) {
5051
if (item.second % 2 == 1) std::cout << "Error: found odd label\n";
5152
}

examples/cpp/example_mt_filter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ int main() {
106106
int k = 10;
107107
std::vector<hnswlib::labeltype> neighbors(max_elements * k);
108108
ParallelFor(0, max_elements, num_threads, [&](size_t row, size_t threadId) {
109-
std::priority_queue<std::pair<float, hnswlib::labeltype>> result = alg_hnsw->searchKnn(data + dim * row, k, &pickIdsDivisibleByTwo);
109+
HNSWLIB_ASSIGN_VALUE_OR_THROW_IN_TEST(
110+
result,
111+
alg_hnsw->searchKnn(data + dim * row, k, &pickIdsDivisibleByTwo));
110112
for (int i = 0; i < k; i++) {
111113
hnswlib::labeltype label = result.top().second;
112114
result.pop();

examples/cpp/example_mt_search.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ int main() {
8989
// Query the elements for themselves and measure recall
9090
std::vector<hnswlib::labeltype> neighbors(max_elements);
9191
ParallelFor(0, max_elements, num_threads, [&](size_t row, size_t threadId) {
92-
std::priority_queue<std::pair<float, hnswlib::labeltype>> result = alg_hnsw->searchKnn(data + dim * row, 1);
92+
HNSWLIB_ASSIGN_VALUE_OR_THROW_IN_TEST(result, alg_hnsw->searchKnn(data + dim * row, 1));
9393
hnswlib::labeltype label = result.top().second;
9494
neighbors[row] = label;
9595
});

examples/cpp/example_multivector_search.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ int main() {
6363
}
6464
std::cout << "Query #" << i << "\n";
6565
hnswlib::MultiVectorSearchStopCondition<docidtype, dist_t> stop_condition(space, num_docs, ef_collection);
66-
std::vector<std::pair<float, hnswlib::labeltype>> result =
67-
alg_hnsw->searchStopConditionClosest(query_data, stop_condition);
66+
HNSWLIB_ASSIGN_VALUE_OR_THROW_IN_TEST(
67+
result,
68+
alg_hnsw->searchStopConditionClosest(query_data, stop_condition));
6869
size_t num_vectors = result.size();
6970

7071
std::unordered_map<docidtype, size_t> doc_counter;

examples/cpp/example_search.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int main() {
2929
// Query the elements for themselves and measure recall
3030
float correct = 0;
3131
for (int i = 0; i < max_elements; i++) {
32-
std::priority_queue<std::pair<float, hnswlib::labeltype>> result = alg_hnsw->searchKnn(data + i * dim, 1);
32+
HNSWLIB_ASSIGN_VALUE_OR_THROW_IN_TEST(result, alg_hnsw->searchKnn(data + i * dim, 1));
3333
hnswlib::labeltype label = result.top().second;
3434
if (label == i) correct++;
3535
}
@@ -45,7 +45,7 @@ int main() {
4545
alg_hnsw = new hnswlib::HierarchicalNSW<float>(&space, hnsw_path);
4646
correct = 0;
4747
for (int i = 0; i < max_elements; i++) {
48-
std::priority_queue<std::pair<float, hnswlib::labeltype>> result = alg_hnsw->searchKnn(data + i * dim, 1);
48+
HNSWLIB_ASSIGN_VALUE_OR_THROW_IN_TEST(result, alg_hnsw->searchKnn(data + i * dim, 1));
4949
hnswlib::labeltype label = result.top().second;
5050
if (label == i) correct++;
5151
}

hnswlib/bruteforce.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#pragma once
2+
3+
#include "hnswlib.h"
4+
25
#include <unordered_map>
36
#include <fstream>
47
#include <mutex>
@@ -61,7 +64,7 @@ class BruteforceSearch : public AlgorithmInterface<dist_t> {
6164
}
6265

6366

64-
void addPoint(const void *datapoint, labeltype label, bool replace_deleted = false) {
67+
HNSWLIB_STATUS_TYPE addPoint(const void *datapoint, labeltype label, bool replace_deleted = false) override {
6568
int idx;
6669
{
6770
std::unique_lock<std::mutex> lock(index_lock);
@@ -103,8 +106,9 @@ class BruteforceSearch : public AlgorithmInterface<dist_t> {
103106
}
104107

105108

106-
std::priority_queue<std::pair<dist_t, labeltype >>
107-
searchKnn(const void *query_data, size_t k, BaseFilterFunctor* isIdAllowed = nullptr) const {
109+
using DistanceLabelPriorityQueue = typename AlgorithmInterface<dist_t>::DistanceLabelPriorityQueue;
110+
HNSWLIB_STATUS_OR_TYPE(DistanceLabelPriorityQueue)
111+
searchKnn(const void *query_data, size_t k, BaseFilterFunctor* isIdAllowed = nullptr) const override {
108112
assert(k <= cur_element_count);
109113
std::priority_queue<std::pair<dist_t, labeltype >> topResults;
110114
if (cur_element_count == 0) return topResults;

0 commit comments

Comments
 (0)