Skip to content

Commit 0f5cf49

Browse files
committed
avoid throw semantics
- from nmslib/hnswlib#619
1 parent 93abb2b commit 0f5cf49

File tree

3 files changed

+274
-88
lines changed

3 files changed

+274
-88
lines changed

ThirdParty/hnswlib/include/hnswlib/bruteforce.h

Lines changed: 11 additions & 6 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>
@@ -51,7 +54,7 @@ class BruteforceSearch : public AlgorithmInterface<dist_t> {
5154
size_per_element_ = data_size_ + sizeof(labeltype);
5255
data_ = (char *) malloc(maxElements * size_per_element_);
5356
if (data_ == nullptr)
54-
throw std::runtime_error("Not enough memory: BruteforceSearch failed to allocate data");
57+
HNSWLIB_THROW_RUNTIME_ERROR("Not enough memory: BruteforceSearch failed to allocate data");
5558
cur_element_count = 0;
5659
}
5760

@@ -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+
Status addPointNoExceptions(const void *datapoint, labeltype label, bool replace_deleted = false) override {
6568
int idx;
6669
{
6770
std::unique_lock<std::mutex> lock(index_lock);
@@ -71,7 +74,7 @@ class BruteforceSearch : public AlgorithmInterface<dist_t> {
7174
idx = search->second;
7275
} else {
7376
if (cur_element_count >= maxelements_) {
74-
throw std::runtime_error("The number of elements exceeds the specified limit\n");
77+
HNSWLIB_THROW_RUNTIME_ERROR("The number of elements exceeds the specified limit\n");
7578
}
7679
idx = cur_element_count;
7780
dict_external_to_internal[label] = idx;
@@ -80,6 +83,7 @@ class BruteforceSearch : public AlgorithmInterface<dist_t> {
8083
}
8184
memcpy(data_ + size_per_element_ * idx + data_size_, &label, sizeof(labeltype));
8285
memcpy(data_ + size_per_element_ * idx, datapoint, data_size_);
86+
return OkStatus();
8387
}
8488

8589

@@ -103,8 +107,9 @@ class BruteforceSearch : public AlgorithmInterface<dist_t> {
103107
}
104108

105109

106-
std::priority_queue<std::pair<dist_t, labeltype >>
107-
searchKnn(const void *query_data, size_t k, BaseFilterFunctor* isIdAllowed = nullptr) const {
110+
using DistanceLabelPriorityQueue = typename AlgorithmInterface<dist_t>::DistanceLabelPriorityQueue;
111+
StatusOr<DistanceLabelPriorityQueue>
112+
searchKnnNoExceptions(const void *query_data, size_t k, BaseFilterFunctor* isIdAllowed = nullptr) const override {
108113
assert(k <= cur_element_count);
109114
std::priority_queue<std::pair<dist_t, labeltype >> topResults;
110115
if (cur_element_count == 0) return topResults;
@@ -163,7 +168,7 @@ class BruteforceSearch : public AlgorithmInterface<dist_t> {
163168
size_per_element_ = data_size_ + sizeof(labeltype);
164169
data_ = (char *) malloc(maxelements_ * size_per_element_);
165170
if (data_ == nullptr)
166-
throw std::runtime_error("Not enough memory: loadIndex failed to allocate data");
171+
HNSWLIB_THROW_RUNTIME_ERROR("Not enough memory: loadIndex failed to allocate data");
167172

168173
input.read(data_, maxelements_ * size_per_element_);
169174

0 commit comments

Comments
 (0)