MCPcopy Create free account
hub / github.com/alibaba/zvec / main

Function main

examples/c/basic_example.c:129–267  ·  view source on GitHub ↗

* @brief Basic C API usage example */

Source from the content-addressed store, hash-verified

127 * @brief Basic C API usage example
128 */
129int main() {
130 printf("=== ZVec C API Basic Example ===\n\n");
131
132 zvec_error_code_t error;
133
134 // Create collection using simplified function
135 zvec_collection_t *collection = NULL;
136 error = create_simple_test_collection(&collection);
137 if (handle_error(error, "creating collection") != ZVEC_OK) {
138 return 1;
139 }
140 printf("✓ Collection created successfully\n");
141
142 // Prepare test data
143 float vector1[] = {0.1f, 0.2f, 0.3f};
144 float vector2[] = {0.4f, 0.5f, 0.6f};
145
146 zvec_doc_t *docs[2];
147 for (int i = 0; i < 2; ++i) {
148 docs[i] = zvec_doc_create();
149 if (!docs[i]) {
150 fprintf(stderr, "Failed to create document %d\n", i);
151 // Cleanup allocated resources
152 for (int j = 0; j < i; ++j) {
153 zvec_doc_destroy(docs[j]);
154 }
155 return ZVEC_ERROR_INTERNAL_ERROR;
156 }
157 }
158
159 // Manually add fields to document 1
160 zvec_doc_set_pk(docs[0], "doc1");
161 zvec_doc_add_field_by_value(docs[0], "id", ZVEC_DATA_TYPE_STRING, "doc1",
162 strlen("doc1"));
163 zvec_doc_add_field_by_value(docs[0], "text", ZVEC_DATA_TYPE_STRING,
164 "First document", strlen("First document"));
165 zvec_doc_add_field_by_value(docs[0], "embedding", ZVEC_DATA_TYPE_VECTOR_FP32,
166 vector1, 3 * sizeof(float));
167
168 // Manually add fields to document 2
169 zvec_doc_set_pk(docs[1], "doc2");
170 zvec_doc_add_field_by_value(docs[1], "id", ZVEC_DATA_TYPE_STRING, "doc2",
171 strlen("doc2"));
172 zvec_doc_add_field_by_value(docs[1], "text", ZVEC_DATA_TYPE_STRING,
173 "Second document", strlen("Second document"));
174 zvec_doc_add_field_by_value(docs[1], "embedding", ZVEC_DATA_TYPE_VECTOR_FP32,
175 vector2, 3 * sizeof(float));
176
177 // Insert documents
178 size_t success_count = 0;
179 size_t error_count = 0;
180 error = zvec_collection_insert(collection, (const zvec_doc_t **)docs, 2,
181 &success_count, &error_count);
182 if (handle_error(error, "inserting documents") != ZVEC_OK) {
183 zvec_collection_destroy(collection);
184 return 1;
185 }
186 printf("✓ Documents inserted - Success: %zu, Failed: %zu\n", success_count,

Callers

nothing calls this directly

Calls 15

zvec_doc_destroyFunction · 0.85
zvec_doc_set_pkFunction · 0.85
zvec_collection_insertFunction · 0.85
zvec_collection_destroyFunction · 0.85
zvec_collection_flushFunction · 0.85
zvec_vector_query_createFunction · 0.85

Tested by

no test coverage detected