Skip to content

Commit c7b2bcb

Browse files
ahuber21rfsaliev
authored andcommitted
Replace macros with runtime_error_wrapper() for error handling (#214)
I think this produces more readable code. And since `runtime_error_wrapper()` is an internal function, there shouldn't be any disadvantages for the shared lib.
1 parent 1de691a commit c7b2bcb

File tree

4 files changed

+79
-105
lines changed

4 files changed

+79
-105
lines changed

bindings/cpp/src/dynamic_vamana_index.cpp

Lines changed: 50 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,25 @@ struct DynamicVamanaIndexManagerBase : public DynamicVamanaIndex {
5555
~DynamicVamanaIndexManagerBase() override = default;
5656

5757
Status add(size_t n, const size_t* labels, const float* x) noexcept override {
58-
SVS_RUNTIME_TRY_BEGIN
59-
svs::data::ConstSimpleDataView<float> data{x, n, impl_->dimensions()};
60-
std::span<const size_t> lbls(labels, n);
61-
impl_->add(data, lbls);
62-
return Status_Ok;
63-
SVS_RUNTIME_TRY_END
58+
return runtime_error_wrapper([&] {
59+
svs::data::ConstSimpleDataView<float> data{x, n, impl_->dimensions()};
60+
std::span<const size_t> lbls(labels, n);
61+
impl_->add(data, lbls);
62+
});
6463
}
6564

6665
Status
6766
remove_selected(size_t* num_removed, const IDFilter& selector) noexcept override {
68-
SVS_RUNTIME_TRY_BEGIN
69-
*num_removed = impl_->remove_selected(selector);
70-
return Status_Ok;
71-
SVS_RUNTIME_TRY_END
67+
return runtime_error_wrapper([&] {
68+
*num_removed = impl_->remove_selected(selector);
69+
});
7270
}
7371

7472
Status remove(size_t n, const size_t* labels) noexcept override {
75-
SVS_RUNTIME_TRY_BEGIN
76-
std::span<const size_t> lbls(labels, n);
77-
impl_->remove(lbls);
78-
return Status_Ok;
79-
SVS_RUNTIME_TRY_END
73+
return runtime_error_wrapper([&] {
74+
std::span<const size_t> lbls(labels, n);
75+
impl_->remove(lbls);
76+
});
8077
}
8178

8279
Status search(
@@ -88,12 +85,11 @@ struct DynamicVamanaIndexManagerBase : public DynamicVamanaIndex {
8885
const SearchParams* params = nullptr,
8986
IDFilter* filter = nullptr
9087
) const noexcept override {
91-
SVS_RUNTIME_TRY_BEGIN
92-
// TODO wrap arguments into proper data structures in DynamicVamanaIndexImpl and
93-
// here
94-
impl_->search(n, x, k, distances, labels, params, filter);
95-
return Status_Ok;
96-
SVS_RUNTIME_TRY_END
88+
return runtime_error_wrapper([&] {
89+
// TODO wrap arguments into proper data structures in DynamicVamanaIndexImpl and
90+
// here
91+
impl_->search(n, x, k, distances, labels, params, filter);
92+
});
9793
}
9894

9995
Status range_search(
@@ -104,26 +100,19 @@ struct DynamicVamanaIndexManagerBase : public DynamicVamanaIndex {
104100
const SearchParams* params = nullptr,
105101
IDFilter* filter = nullptr
106102
) const noexcept override {
107-
SVS_RUNTIME_TRY_BEGIN
108-
// TODO wrap arguments into proper data structures in DynamicVamanaIndexImpl and
109-
// here
110-
impl_->range_search(n, x, radius, results, params, filter);
111-
return Status_Ok;
112-
SVS_RUNTIME_TRY_END
103+
return runtime_error_wrapper([&] {
104+
// TODO wrap arguments into proper data structures in DynamicVamanaIndexImpl and
105+
// here
106+
impl_->range_search(n, x, radius, results, params, filter);
107+
});
113108
}
114109

115110
Status reset() noexcept override {
116-
SVS_RUNTIME_TRY_BEGIN
117-
impl_->reset();
118-
return Status_Ok;
119-
SVS_RUNTIME_TRY_END
111+
return runtime_error_wrapper([&] { impl_->reset(); });
120112
}
121113

122114
Status save(std::ostream& out) const noexcept override {
123-
SVS_RUNTIME_TRY_BEGIN
124-
impl_->save(out);
125-
return Status_Ok;
126-
SVS_RUNTIME_TRY_END
115+
return runtime_error_wrapper([&] { impl_->save(out); });
127116
}
128117
};
129118

@@ -146,20 +135,16 @@ Status DynamicVamanaIndex::build(
146135
const DynamicVamanaIndex::SearchParams& default_search_params
147136
) noexcept {
148137
*index = nullptr;
149-
SVS_RUNTIME_TRY_BEGIN
150-
auto impl = std::make_unique<DynamicVamanaIndexImpl>(
151-
dim, metric, storage_kind, params, default_search_params
152-
);
153-
*index = new DynamicVamanaIndexManager{std::move(impl)};
154-
return Status_Ok;
155-
SVS_RUNTIME_TRY_END
138+
return runtime_error_wrapper([&] {
139+
auto impl = std::make_unique<DynamicVamanaIndexImpl>(
140+
dim, metric, storage_kind, params, default_search_params
141+
);
142+
*index = new DynamicVamanaIndexManager{std::move(impl)};
143+
});
156144
}
157145

158146
Status DynamicVamanaIndex::destroy(DynamicVamanaIndex* index) noexcept {
159-
SVS_RUNTIME_TRY_BEGIN
160-
delete index;
161-
return Status_Ok;
162-
SVS_RUNTIME_TRY_END
147+
return runtime_error_wrapper([&] { delete index; });
163148
}
164149

165150
Status DynamicVamanaIndex::load(
@@ -169,12 +154,11 @@ Status DynamicVamanaIndex::load(
169154
StorageKind storage_kind
170155
) noexcept {
171156
*index = nullptr;
172-
SVS_RUNTIME_TRY_BEGIN
173-
std::unique_ptr<DynamicVamanaIndexImpl> impl{
174-
DynamicVamanaIndexImpl::load(in, metric, storage_kind)};
175-
*index = new DynamicVamanaIndexManager{std::move(impl)};
176-
return Status_Ok;
177-
SVS_RUNTIME_TRY_END
157+
return runtime_error_wrapper([&] {
158+
std::unique_ptr<DynamicVamanaIndexImpl> impl{
159+
DynamicVamanaIndexImpl::load(in, metric, storage_kind)};
160+
*index = new DynamicVamanaIndexManager{std::move(impl)};
161+
});
178162
}
179163

180164
// Specialization to build LeanVec-based Vamana index with specified leanvec dims
@@ -188,13 +172,12 @@ Status DynamicVamanaIndexLeanVec::build(
188172
const DynamicVamanaIndex::SearchParams& default_search_params
189173
) noexcept {
190174
*index = nullptr;
191-
SVS_RUNTIME_TRY_BEGIN
192-
auto impl = std::make_unique<DynamicVamanaIndexLeanVecImpl>(
193-
dim, metric, storage_kind, leanvec_dims, params, default_search_params
194-
);
195-
*index = new DynamicVamanaIndexLeanVecImplManager{std::move(impl)};
196-
return Status_Ok;
197-
SVS_RUNTIME_TRY_END
175+
return runtime_error_wrapper([&] {
176+
auto impl = std::make_unique<DynamicVamanaIndexLeanVecImpl>(
177+
dim, metric, storage_kind, leanvec_dims, params, default_search_params
178+
);
179+
*index = new DynamicVamanaIndexLeanVecImplManager{std::move(impl)};
180+
});
198181
}
199182

200183
// Specialization to build LeanVec-based Vamana index with provided training data
@@ -208,15 +191,14 @@ Status DynamicVamanaIndexLeanVec::build(
208191
const DynamicVamanaIndex::SearchParams& default_search_params
209192
) noexcept {
210193
*index = nullptr;
211-
SVS_RUNTIME_TRY_BEGIN
212-
auto training_data_impl =
213-
static_cast<const LeanVecTrainingDataManager*>(training_data)->impl_;
214-
auto impl = std::make_unique<DynamicVamanaIndexLeanVecImpl>(
215-
dim, metric, storage_kind, training_data_impl, params, default_search_params
216-
);
217-
*index = new DynamicVamanaIndexLeanVecImplManager{std::move(impl)};
218-
return Status_Ok;
219-
SVS_RUNTIME_TRY_END
194+
return runtime_error_wrapper([&] {
195+
auto training_data_impl =
196+
static_cast<const LeanVecTrainingDataManager*>(training_data)->impl_;
197+
auto impl = std::make_unique<DynamicVamanaIndexLeanVecImpl>(
198+
dim, metric, storage_kind, training_data_impl, params, default_search_params
199+
);
200+
*index = new DynamicVamanaIndexLeanVecImplManager{std::move(impl)};
201+
});
220202
}
221203

222204
} // namespace runtime

bindings/cpp/src/svs_runtime_utils.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121

2222
// TODO remove unused includes
2323
#include <algorithm>
24+
#include <concepts>
25+
#include <functional>
2426
#include <memory>
27+
#include <type_traits>
2528
#include <variant>
2629
#include <vector>
2730

@@ -62,26 +65,23 @@ class StatusException : public svs::lib::ANNException {
6265
svs::runtime::ErrorCode errcode_;
6366
};
6467

65-
#define SVS_RUNTIME_TRY_BEGIN try {
66-
#define SVS_RUNTIME_TRY_END \
67-
} \
68-
catch (const svs::runtime::StatusException& ex) { \
69-
return svs::runtime::Status(ex.code(), ex.what()); \
70-
} \
71-
catch (const std::invalid_argument& ex) { \
72-
return svs::runtime::Status(svs::runtime::ErrorCode::INVALID_ARGUMENT, ex.what()); \
73-
} \
74-
catch (const std::runtime_error& ex) { \
75-
return svs::runtime::Status(svs::runtime::ErrorCode::RUNTIME_ERROR, ex.what()); \
76-
} \
77-
catch (const std::exception& ex) { \
78-
return svs::runtime::Status(svs::runtime::ErrorCode::UNKNOWN_ERROR, ex.what()); \
79-
} \
80-
catch (...) { \
81-
return svs::runtime::Status( \
82-
svs::runtime::ErrorCode::UNKNOWN_ERROR, "An unknown error has occurred." \
83-
); \
68+
template <typename Callable>
69+
inline auto runtime_error_wrapper(Callable&& func) noexcept -> Status {
70+
try {
71+
func();
72+
return Status_Ok;
73+
} catch (const svs::runtime::StatusException& ex) {
74+
return Status(ex.code(), ex.what());
75+
} catch (const std::invalid_argument& ex) {
76+
return Status(ErrorCode::INVALID_ARGUMENT, ex.what());
77+
} catch (const std::runtime_error& ex) {
78+
return Status(ErrorCode::RUNTIME_ERROR, ex.what());
79+
} catch (const std::exception& ex) {
80+
return Status(ErrorCode::UNKNOWN_ERROR, ex.what());
81+
} catch (...) {
82+
return Status(ErrorCode::UNKNOWN_ERROR, "An unknown error has occurred.");
8483
}
84+
}
8585

8686
using LeanVecMatricesType = svs::leanvec::LeanVecMatrices<svs::Dynamic>;
8787

bindings/cpp/src/training.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,22 @@ Status LeanVecTrainingData::build(
2828
const float* x,
2929
size_t leanvec_dims
3030
) noexcept {
31-
SVS_RUNTIME_TRY_BEGIN
32-
const auto data = svs::data::ConstSimpleDataView<float>(x, n, dim);
33-
*training_data =
34-
new LeanVecTrainingDataManager{LeanVecTrainingDataImpl{data, leanvec_dims}};
35-
return Status_Ok;
36-
SVS_RUNTIME_TRY_END
31+
return runtime_error_wrapper([&] {
32+
const auto data = svs::data::ConstSimpleDataView<float>(x, n, dim);
33+
*training_data =
34+
new LeanVecTrainingDataManager{LeanVecTrainingDataImpl{data, leanvec_dims}};
35+
});
3736
}
3837

3938
Status LeanVecTrainingData::destroy(LeanVecTrainingData* training_data) noexcept {
40-
SVS_RUNTIME_TRY_BEGIN
41-
delete training_data;
42-
return Status_Ok;
43-
SVS_RUNTIME_TRY_END
39+
return runtime_error_wrapper([&] { delete training_data; });
4440
}
4541

4642
Status
4743
LeanVecTrainingData::load(LeanVecTrainingData** training_data, std::istream& in) noexcept {
48-
SVS_RUNTIME_TRY_BEGIN
49-
*training_data = new LeanVecTrainingDataManager{LeanVecTrainingDataImpl::load(in)};
50-
return Status_Ok;
51-
SVS_RUNTIME_TRY_END
44+
return runtime_error_wrapper([&] {
45+
*training_data = new LeanVecTrainingDataManager{LeanVecTrainingDataImpl::load(in)};
46+
});
5247
}
5348
} // namespace runtime
5449
} // namespace svs

bindings/cpp/src/training_impl.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,7 @@ struct LeanVecTrainingDataManager : public svs::runtime::LeanVecTrainingData {
8484
: impl_{std::move(impl)} {}
8585

8686
Status save(std::ostream& out) const noexcept override {
87-
SVS_RUNTIME_TRY_BEGIN
88-
impl_.save(out);
89-
return Status_Ok;
90-
SVS_RUNTIME_TRY_END
87+
return runtime_error_wrapper([&] { impl_.save(out); });
9188
}
9289

9390
LeanVecTrainingDataImpl impl_;

0 commit comments

Comments
 (0)