Skip to content

Commit 5733855

Browse files
committed
feat(c_api): add Error_t instead bool as return result
Signed-off-by: LHT129 <tianlan.lht@antgroup.com>
1 parent d9ac223 commit 5733855

File tree

3 files changed

+83
-27
lines changed

3 files changed

+83
-27
lines changed

include/vsag/vsag_c_api.h

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,40 @@
1717

1818
extern "C" {
1919
#include <stdint.h>
20+
21+
int VSAG_SUCCESS = 0;
22+
int VSAG_UNKNOWN_ERROR = -1;
23+
int VSAG_INTERNAL_ERROR = -2;
24+
int VSAG_INVALID_ARGUMENT = -3;
25+
int VSAG_WRONG_STATUS = -4;
26+
int VSAG_BUILD_TWICE = -5;
27+
int VSAG_INDEX_NOT_EMPTY = -6;
28+
int VSAG_UNSUPPORTED_INDEX = -7;
29+
int VSAG_UNSUPPORTED_INDEX_OPERATION = -8;
30+
int VSAG_DIMENSION_NOT_EQUAL = -9;
31+
int VSAG_INDEX_EMPTY = -10;
32+
int VSAG_NO_ENOUGH_MEMORY = -11;
33+
int VSAG_READ_ERROR = -12;
34+
int VSAG_MISSING_FILE = -13;
35+
int VSAG_INVALID_BINARY = -14;
36+
37+
typedef struct Error {
38+
int code;
39+
char message[1024];
40+
} Error_t;
41+
2042
typedef void* vsag_index_t;
2143
vsag_index_t
2244
vsag_index_factory(const char* index_name, const char* index_param);
2345

24-
bool
46+
Error_t
2547
vsag_index_destroy(vsag_index_t index);
2648

27-
bool
49+
Error_t
2850
vsag_index_build(
2951
vsag_index_t index, const float* data, const int64_t* ids, uint64_t dim, uint64_t count);
3052

31-
bool
53+
Error_t
3254
vsag_index_knn_search(vsag_index_t index,
3355
const float* query,
3456
uint64_t dim,
@@ -37,9 +59,9 @@ vsag_index_knn_search(vsag_index_t index,
3759
float* results,
3860
int64_t* ids);
3961

40-
bool
62+
Error_t
4163
vsag_serialize_file(vsag_index_t index, const char* file_path);
4264

43-
bool
65+
Error_t
4466
vsag_deserialize_file(vsag_index_t index, const char* file_path);
4567
}

src/vsag_c_api.cpp

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
#include <vsag/index.h>
1818
#include <vsag/vsag_c_api.h>
1919

20+
#include <cstring>
2021
#include <fstream>
2122

23+
Error_t SUCCESS = {VSAG_SUCCESS, "success"};
24+
2225
class VsagIndex {
2326
public:
2427
VsagIndex(std::shared_ptr<vsag::Index> index) : index_(std::move(index)) {
@@ -27,6 +30,26 @@ class VsagIndex {
2730
std::shared_ptr<vsag::Index> index_;
2831
};
2932

33+
static Error_t
34+
make_error(const vsag::Error& error) {
35+
Error_t err;
36+
err.code = static_cast<int>(error.type);
37+
const auto& msg = error.message;
38+
memcpy(err.message, msg.c_str(), msg.size() + 1);
39+
err.message[msg.size()] = '\0';
40+
return err;
41+
}
42+
43+
static Error_t
44+
make_error(const std::exception& e) {
45+
Error_t err;
46+
err.code = static_cast<int>(vsag::ErrorType::INTERNAL_ERROR);
47+
const auto* msg = e.what();
48+
memcpy(err.message, msg, strlen(msg) + 1);
49+
err.message[strlen(msg)] = '\0';
50+
return err;
51+
}
52+
3053
extern "C" {
3154
vsag_index_t
3255
vsag_index_factory(const char* index_name, const char* index_param) {
@@ -42,18 +65,18 @@ vsag_index_factory(const char* index_name, const char* index_param) {
4265
}
4366
}
4467

45-
bool
68+
Error_t
4669
vsag_index_destroy(vsag_index_t index) {
4770
try {
4871
auto* vsag_index = static_cast<VsagIndex*>(index);
4972
delete vsag_index;
50-
return true;
73+
return SUCCESS;
5174
} catch (const std::exception& e) {
52-
return false;
75+
return make_error(e);
5376
}
5477
}
5578

56-
bool
79+
Error_t
5780
vsag_index_build(
5881
vsag_index_t index, const float* data, const int64_t* ids, uint64_t dim, uint64_t count) {
5982
try {
@@ -65,15 +88,20 @@ vsag_index_build(
6588
->NumElements(static_cast<int64_t>(count))
6689
->Ids(ids)
6790
->Float32Vectors(data);
68-
vsag_index->index_->Build(dataset);
91+
auto build_result = vsag_index->index_->Build(dataset);
92+
if (build_result.has_value()) {
93+
return SUCCESS;
94+
}
95+
96+
return make_error(build_result.error());
6997
}
70-
return true;
98+
return SUCCESS;
7199
} catch (const std::exception& e) {
72-
return false;
100+
return make_error(e);
73101
}
74102
}
75103

76-
bool
104+
Error_t
77105
vsag_index_knn_search(vsag_index_t index,
78106
const float* query,
79107
uint64_t dim,
@@ -98,30 +126,36 @@ vsag_index_knn_search(vsag_index_t index,
98126
ids[i] = ids_view[i];
99127
results[i] = dists_view[i];
100128
}
129+
} else {
130+
return make_error(result.error());
101131
}
102132
}
103-
return true;
133+
return SUCCESS;
104134
} catch (const std::exception& e) {
105-
return false;
135+
return make_error(e);
106136
}
107137
}
108138

109-
bool
139+
Error_t
110140
vsag_serialize_file(vsag_index_t index, const char* file_path) {
111141
try {
112142
auto* vsag_index = static_cast<VsagIndex*>(index);
113143
if (vsag_index != nullptr) {
114144
std::ofstream file(file_path, std::ios::binary);
115-
vsag_index->index_->Serialize(file);
145+
auto serialize_result = vsag_index->index_->Serialize(file);
116146
file.close();
147+
if (serialize_result.has_value()) {
148+
return SUCCESS;
149+
}
150+
return make_error(serialize_result.error());
117151
}
118-
return true;
152+
return SUCCESS;
119153
} catch (const std::exception& e) {
120-
return false;
154+
return make_error(e);
121155
}
122156
}
123157

124-
bool
158+
Error_t
125159
vsag_deserialize_file(vsag_index_t index, const char* file_path) {
126160
try {
127161
auto* vsag_index = static_cast<VsagIndex*>(index);
@@ -130,9 +164,9 @@ vsag_deserialize_file(vsag_index_t index, const char* file_path) {
130164
vsag_index->index_->Deserialize(file);
131165
file.close();
132166
}
133-
return true;
167+
return SUCCESS;
134168
} catch (const std::exception& e) {
135-
return false;
169+
return make_error(e);
136170
}
137171
}
138172
}

src/vsag_c_api_test.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ TEST_CASE("vsag_c_api basic test", "[vsag_c_api][ut]") {
4848
for (int64_t i = 0; i < dim * num_vectors; ++i) {
4949
datas[i] = distrib_real(rng);
5050
}
51-
bool ret = vsag_index_build(index, datas.data(), ids.data(), dim, num_vectors);
52-
REQUIRE(ret);
51+
Error_t ret = vsag_index_build(index, datas.data(), ids.data(), dim, num_vectors);
52+
REQUIRE(ret.code == VSAG_SUCCESS);
5353
auto func = [&](vsag_index_t index1) {
5454
const char* hgraph_search_parameters = R"(
5555
{
@@ -69,7 +69,7 @@ TEST_CASE("vsag_c_api basic test", "[vsag_c_api][ut]") {
6969
hgraph_search_parameters,
7070
scores.data(),
7171
results.data());
72-
REQUIRE(ret);
72+
REQUIRE(ret.code == VSAG_SUCCESS);
7373
bool in_results = false;
7474
for (int64_t j = 0; j < topk; ++j) {
7575
if (results[j] == i) {
@@ -84,10 +84,10 @@ TEST_CASE("vsag_c_api basic test", "[vsag_c_api][ut]") {
8484

8585
const char* file_name = "/tmp/test_c_api.vsag";
8686
ret = vsag_serialize_file(index, file_name);
87-
REQUIRE(ret);
87+
REQUIRE(ret.code == VSAG_SUCCESS);
8888
vsag_index_t index2 = vsag_index_factory(index_name, index_param);
8989
ret = vsag_deserialize_file(index2, file_name);
90-
REQUIRE(ret);
90+
REQUIRE(ret.code == VSAG_SUCCESS);
9191
REQUIRE(index2 != nullptr);
9292

9393
func(index2);

0 commit comments

Comments
 (0)