Skip to content

Commit ff3d8c8

Browse files
authored
Check for nullptr index_ in all api/index methods (#258)
1 parent 553e7a9 commit ff3d8c8

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/include/api/flat_l2_index.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,36 @@ class IndexFlatL2 {
9191
// @todo query() or search() -- or both?
9292
[[nodiscard]] auto query(
9393
const QueryVectorArray& vectors, size_t top_k) const {
94+
if (!index_) {
95+
throw std::runtime_error("Cannot query() because there is no index.");
96+
}
9497
return index_->query(vectors, top_k);
9598
}
9699

97100
void update(
98101
const FeatureVectorArray& vectors,
99102
const std::optional<IdVector>& ids = std::nullopt,
100103
const std::optional<UpdateOptions>& options = std::nullopt) const {
104+
if (!index_) {
105+
throw std::runtime_error("Cannot update() because there is no index.");
106+
}
101107
index_->update(vectors, ids, options);
102108
}
103109

104110
void update(
105111
const URI& vectors_uri,
106112
const std::optional<IdVector>& ids = std::nullopt,
107113
const std::optional<UpdateOptions>& options = std::nullopt) const {
114+
if (!index_) {
115+
throw std::runtime_error("Cannot update() because there is no index.");
116+
}
108117
index_->update(vectors_uri, ids, options);
109118
}
110119

111120
void remove(const IdVector& ids) const {
121+
if (!index_) {
122+
throw std::runtime_error("Cannot remove() because there is no index.");
123+
}
112124
index_->remove(ids);
113125
}
114126

src/include/api/ivf_flat_index.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ class IndexIVFFlat {
338338
datatype_to_string(feature_datatype_) +
339339
" != " + datatype_to_string(data_set.feature_type()));
340340
}
341+
if (!index_) {
342+
throw std::runtime_error("Cannot add() because there is no index.");
343+
}
341344
index_->add(data_set);
342345
}
343346

@@ -348,6 +351,10 @@ class IndexIVFFlat {
348351
// todo query() or search() -- or both?
349352
[[nodiscard]] auto query_infinite_ram(
350353
const QueryVectorArray& vectors, size_t top_k, size_t nprobe) const {
354+
if (!index_) {
355+
throw std::runtime_error(
356+
"Cannot query_infinite_ram() because there is no index.");
357+
}
351358
return index_->query_infinite_ram(vectors, top_k, nprobe);
352359
}
353360

@@ -356,31 +363,48 @@ class IndexIVFFlat {
356363
size_t top_k,
357364
size_t nprobe,
358365
size_t upper_bound = 0) const {
366+
if (!index_) {
367+
throw std::runtime_error(
368+
"Cannot query_finite_ram() because there is no index.");
369+
}
359370
return index_->query_finite_ram(vectors, top_k, nprobe, upper_bound);
360371
}
361372

362373
void update(
363374
const FeatureVectorArray& vectors,
364375
const std::optional<IdVector>& ids = std::nullopt,
365376
const std::optional<UpdateOptions>& options = std::nullopt) const {
377+
if (!index_) {
378+
throw std::runtime_error("Cannot update() because there is no index.");
379+
}
366380
index_->update(vectors, ids, options);
367381
}
368382

369383
void update(
370384
const URI& vectors_uri,
371385
const std::optional<IdVector>& ids = std::nullopt,
372386
const std::optional<UpdateOptions>& options = std::nullopt) const {
387+
if (!index_) {
388+
throw std::runtime_error("Cannot update() because there is no index.");
389+
}
373390
index_->update(vectors_uri, ids, options);
374391
}
375392

376393
void remove(const IdVector& ids) const {
394+
if (!index_) {
395+
throw std::runtime_error("Cannot remove() because there is no index.");
396+
}
377397
index_->remove(ids);
378398
}
379399

380400
void write_index(
381401
const tiledb::Context& ctx,
382402
const std::string& group_uri,
383403
bool overwrite = false) const {
404+
if (!index_) {
405+
throw std::runtime_error(
406+
"Cannot write_index() because there is no index.");
407+
}
384408
index_->write_index(ctx, group_uri, overwrite);
385409
}
386410

src/include/api/vamana_index.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ class IndexVamana {
215215
datatype_to_string(feature_datatype_) +
216216
" != " + datatype_to_string(data_set.feature_type()));
217217
}
218+
if (!index_) {
219+
throw std::runtime_error("Cannot add() because there is no index.");
220+
}
218221
index_->add(data_set);
219222
}
220223

@@ -223,13 +226,20 @@ class IndexVamana {
223226
const QueryVectorArray& vectors,
224227
size_t top_k,
225228
std::optional<size_t> opt_L = std::nullopt) {
229+
if (!index_) {
230+
throw std::runtime_error("Cannot query() because there is no index.");
231+
}
226232
return index_->query(vectors, top_k, opt_L);
227233
}
228234

229235
void write_index(
230236
const tiledb::Context& ctx,
231237
const std::string& group_uri,
232238
bool overwrite = false) const {
239+
if (!index_) {
240+
throw std::runtime_error(
241+
"Cannot write_index() because there is no index.");
242+
}
233243
index_->write_index(ctx, group_uri, overwrite);
234244
}
235245

0 commit comments

Comments
 (0)