Conversation
|
@greptile |
Greptile SummaryThis PR adds comprehensive C language bindings to the zvec project, including a C API wrapper ( Key additions:
Critical issue:
The implementation is otherwise well-structured with thread-safe error handling, proper API versioning, and comprehensive functionality coverage. Confidence Score: 1/5
Important Files Changed
Class Diagram%%{init: {'theme': 'neutral'}}%%
classDiagram
class ZVec_C_API {
+zvec_initialize()
+zvec_shutdown()
+zvec_get_version()
}
class Collection_API {
+zvec_collection_create()
+zvec_collection_open()
+zvec_collection_insert()
+zvec_collection_query()
+zvec_collection_flush()
}
class Schema_API {
+zvec_collection_schema_create()
+zvec_field_schema_create()
+zvec_field_schema_set_hnsw_index()
+zvec_field_schema_set_invert_index()
}
class Document_API {
+zvec_doc_create()
+zvec_doc_set_pk()
+zvec_doc_add_field()
+zvec_doc_get_pk_copy()
}
class Index_API {
+zvec_index_params_hnsw_create()
+zvec_index_params_invert_create()
}
class Config_API {
+zvec_config_data_create()
+zvec_config_log_create()
}
class Memory_Management {
+zvec_free()
+zvec_free_string()
+zvec_free_str()
+zvec_docs_free()
}
ZVec_C_API --> Collection_API
ZVec_C_API --> Config_API
Collection_API --> Schema_API
Collection_API --> Document_API
Schema_API --> Index_API
Document_API --> Memory_Management
Collection_API --> Memory_Management
Last reviewed commit: 67ff247 |
format c_api.h fix some code format c_api code build rpm fix some code fix some code remove rpm build Add GitHub Actions release workflow for C API add release linux-arm64 remove c api version fix some code fix some code fix some code refact some code format some code format some code format some code add api reference
src/binding/c/c_api.cc
Outdated
|
|
||
| // RAII guard for array allocated with new[] | ||
| template <typename T> | ||
| struct DeleteArrayGuard { |
There was a problem hiding this comment.
Those guards are not used? Can they be removed?
BTW, you can used unique_ptr instead of Delete(Array)Guard.
| /** | ||
| * @brief Mutable string structure (owns memory) | ||
| */ | ||
| typedef struct { |
There was a problem hiding this comment.
Use c99 flexible array to reduce malloc count(2->1)
struct A {
int a;
char data[]; // flexible array member
};
There was a problem hiding this comment.
This approach restricts usage to the end of a struct only, making it inconvenient to use within other structs.
src/c_api/c_api.cc
Outdated
| set_last_error(status.message()); | ||
| return ZVEC_ERROR_INTERNAL_ERROR; | ||
| } | ||
| } g_initialized.store(true); |
src/c_api/c_api.cc
Outdated
| cpp_config.log_config = log_config; | ||
| } | ||
| // Initialize global configuration | ||
| auto status = zvec::GlobalConfig::Instance().Initialize(cpp_config); |
There was a problem hiding this comment.
Initialize and error check common logic could be extracted out of if/else.
src/c_api/c_api.cc
Outdated
| return ZVEC_OK;) | ||
| } | ||
|
|
||
| ZVecErrorCode zvec_is_initialized(bool *initialized) { |
There was a problem hiding this comment.
Update to bool zvec_is_initialized() to simplify usage?
src/c_api/c_api.cc
Outdated
|
|
||
| void zvec_config_file_log_destroy(ZVecFileLogConfig *config) { | ||
| if (config) { | ||
| if (config->dir.data) free((void *)config->dir.data); |
There was a problem hiding this comment.
cast is unnecessary, void * can be implicitly casted.
src/c_api/c_api.cc
Outdated
| static char *copy_string(const std::string &str) { | ||
| if (str.empty()) return nullptr; | ||
| char *copy = static_cast<char *>(malloc(str.length() + 1)); | ||
| strcpy(copy, str.c_str()); |
There was a problem hiding this comment.
Use strncpy to support binary str input.
src/c_api/c_api.cc
Outdated
| for (size_t i = 0; i < *result_count; ++i) { | ||
| const std::string pk = i < pks.size() ? pks[i] : std::string(); | ||
| const std::string message = statuses[i].message(); | ||
| (*results)[i].pk = copy_string(pk); |
There was a problem hiding this comment.
Can we use ordered style so that pk does not need to be stored in result?
| ZVecDoc *zvec_doc_create(void) { | ||
| ZVEC_TRY_RETURN_NULL("Failed to create document", { | ||
| auto doc_ptr = | ||
| new std::shared_ptr<zvec::Doc>(std::make_shared<zvec::Doc>()); |
There was a problem hiding this comment.
Could we just use plain Doc?
auto* doc = new Doc();
There was a problem hiding this comment.
Switching to raw pointers would make interoperability with C++ code more difficult: it would require frequent conversions between shared_ptr and raw pointers, and it would be prone to memory leaks. It's recommended to keep using shared_ptr.
src/c_api/c_api.cc
Outdated
| set_last_error("Invalid value size for vector_fp64 type"); | ||
| return error_code; | ||
| } | ||
| (*doc_ptr)->set(name, vec); |
There was a problem hiding this comment.
move(vec) for better performance
src/c_api/c_api.cc
Outdated
| reinterpret_cast<const int8_t *>(field->value.vector_value.data), | ||
| reinterpret_cast<const int8_t *>(field->value.vector_value.data) + | ||
| field->value.vector_value.length); | ||
| (*doc_ptr)->set(name, vec); |
There was a problem hiding this comment.
move(vec) for better performance
| include_directories(${PROJECT_ROOT_DIR}/src/include) | ||
| include_directories(${PROJECT_ROOT_DIR}/src) | ||
| # Add generated headers to global include path | ||
| include_directories(${PROJECT_BINARY_DIR}/src/generated) |
There was a problem hiding this comment.
这里可以不用设置为全局的include路径吧?
| @@ -0,0 +1,193 @@ | |||
| name: Release | |||
There was a problem hiding this comment.
在macos/linux的ci里面也加上c_api的执行逻辑吧,可以参考c++ example的写法
Summary
Add C language bindings.
Changes
src/c_api/c_api.ccsrc/include/zvec/c_api.hexamples/c_api/Testing
tests/c_api/c_api_test.c