zvec 0.1.0

Rust bindings to zvec, an in-process vector database by Alibaba.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! [`Collection`] — a zvec collection.

use std::ffi::CString;
use std::ptr::NonNull;

use crate::doc::{Doc, DocRef};
use crate::error::{check, ErrorCode, Result, ZvecError};
use crate::ffi_util::{cstr_to_string, cstring};
use crate::index_params::IndexParams;
use crate::options::CollectionOptions;
use crate::query::VectorQuery;
use crate::schema::{CollectionSchema, FieldSchema};
use crate::stats::CollectionStats;
use crate::sys;

fn pks_to_c(pks: &[&str]) -> Result<(Vec<CString>, Vec<*const core::ffi::c_char>)> {
    let mut c_strings = Vec::with_capacity(pks.len());
    for pk in pks {
        c_strings.push(cstring(pk)?);
    }
    let ptrs = c_strings.iter().map(|s| s.as_ptr()).collect();
    Ok((c_strings, ptrs))
}

fn docs_to_c(docs: &[&Doc]) -> Vec<*const sys::zvec_doc_t> {
    docs.iter().map(|d| d.as_ptr()).collect()
}

/// Result summary returned by batch write APIs.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct WriteSummary {
    pub success: usize,
    pub error: usize,
}

/// Per-document status returned by the `*_with_results` batch write APIs.
#[derive(Debug, Clone)]
pub struct WriteResult {
    pub code: crate::error::ErrorCode,
    pub message: Option<String>,
}

/// Result set returned by [`Collection::query`] or [`Collection::fetch`].
///
/// The slot owns a zvec-allocated `zvec_doc_t**` array and frees it on drop.
pub struct DocSet {
    ptr: *mut *mut sys::zvec_doc_t,
    len: usize,
}

impl DocSet {
    pub fn len(&self) -> usize {
        self.len
    }

    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    pub fn get(&self, idx: usize) -> Option<DocRef<'_>> {
        if idx >= self.len {
            return None;
        }
        let p = unsafe { *self.ptr.add(idx) };
        DocRef::from_ptr(p)
    }

    pub fn iter(&self) -> impl Iterator<Item = DocRef<'_>> {
        (0..self.len).filter_map(move |i| self.get(i))
    }

    /// Convert this result set into a flat `Vec<Hit>` suitable for
    /// feeding into [`crate::rerank::RrfReRanker`] or
    /// [`crate::rerank::WeightedReRanker`]. Rows whose primary key
    /// cannot be read are skipped.
    pub fn to_hits(&self) -> Vec<crate::rerank::Hit> {
        self.iter()
            .filter_map(|r| {
                r.pk_copy().map(|pk| crate::rerank::Hit {
                    pk,
                    score: r.score(),
                })
            })
            .collect()
    }
}

impl Drop for DocSet {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe { sys::zvec_docs_free(self.ptr, self.len) };
        }
    }
}

pub struct Collection {
    ptr: NonNull<sys::zvec_collection_t>,
}

impl Collection {
    /// Create (and open) a new collection at `path` using the given schema.
    /// Passing `None` for `options` uses zvec's defaults.
    pub fn create_and_open(
        path: &str,
        schema: &CollectionSchema,
        options: Option<&CollectionOptions>,
    ) -> Result<Self> {
        let c_path = cstring(path)?;
        let options_ptr = options.map_or(core::ptr::null(), |o| o.as_ptr());
        let mut out: *mut sys::zvec_collection_t = core::ptr::null_mut();
        check(unsafe {
            sys::zvec_collection_create_and_open(
                c_path.as_ptr(),
                schema.as_ptr(),
                options_ptr,
                &mut out,
            )
        })?;
        NonNull::new(out).map(|ptr| Self { ptr }).ok_or_else(|| {
            ZvecError::with_message(
                ErrorCode::Internal,
                "zvec_collection_create_and_open returned NULL",
            )
        })
    }

    /// Open an existing collection at `path`.
    pub fn open(path: &str, options: Option<&CollectionOptions>) -> Result<Self> {
        let c_path = cstring(path)?;
        let options_ptr = options.map_or(core::ptr::null(), |o| o.as_ptr());
        let mut out: *mut sys::zvec_collection_t = core::ptr::null_mut();
        check(unsafe { sys::zvec_collection_open(c_path.as_ptr(), options_ptr, &mut out) })?;
        NonNull::new(out).map(|ptr| Self { ptr }).ok_or_else(|| {
            ZvecError::with_message(ErrorCode::Internal, "zvec_collection_open returned NULL")
        })
    }

    pub(crate) fn as_ptr(&self) -> *const sys::zvec_collection_t {
        self.ptr.as_ptr() as *const _
    }

    /// Flush buffered writes to disk.
    pub fn flush(&self) -> Result<()> {
        check(unsafe { sys::zvec_collection_flush(self.ptr.as_ptr()) })
    }

    /// Rebuild indexes and merge segments.
    pub fn optimize(&self) -> Result<()> {
        check(unsafe { sys::zvec_collection_optimize(self.ptr.as_ptr()) })
    }

    /// Close the underlying handle. After this the collection cannot be used.
    /// Normally closing happens in `Drop`, but callers may want an explicit
    /// close to check the result.
    pub fn close(self) -> Result<()> {
        let ptr = self.ptr.as_ptr();
        core::mem::forget(self);
        let rc = unsafe { sys::zvec_collection_close(ptr) };
        let _ = unsafe { sys::zvec_collection_destroy(ptr) };
        check(rc)
    }

    // ---------- introspection ----------

    pub fn schema(&self) -> Result<CollectionSchema> {
        let mut out: *mut sys::zvec_collection_schema_t = core::ptr::null_mut();
        check(unsafe { sys::zvec_collection_get_schema(self.as_ptr(), &mut out) })?;
        CollectionSchema::from_raw(out).ok_or_else(|| {
            ZvecError::with_message(
                ErrorCode::Internal,
                "zvec_collection_get_schema returned NULL",
            )
        })
    }

    pub fn options(&self) -> Result<CollectionOptions> {
        let mut out: *mut sys::zvec_collection_options_t = core::ptr::null_mut();
        check(unsafe { sys::zvec_collection_get_options(self.as_ptr(), &mut out) })?;
        CollectionOptions::from_raw(out).ok_or_else(|| {
            ZvecError::with_message(
                ErrorCode::Internal,
                "zvec_collection_get_options returned NULL",
            )
        })
    }

    pub fn stats(&self) -> Result<CollectionStats> {
        let mut out: *mut sys::zvec_collection_stats_t = core::ptr::null_mut();
        check(unsafe { sys::zvec_collection_get_stats(self.as_ptr(), &mut out) })?;
        CollectionStats::from_raw(out).ok_or_else(|| {
            ZvecError::with_message(
                ErrorCode::Internal,
                "zvec_collection_get_stats returned NULL",
            )
        })
    }

    // ---------- index / column DDL ----------

    pub fn create_index(&self, field_name: &str, params: &IndexParams) -> Result<()> {
        let c = cstring(field_name)?;
        check(unsafe {
            sys::zvec_collection_create_index(self.ptr.as_ptr(), c.as_ptr(), params.as_ptr())
        })
    }

    pub fn drop_index(&self, field_name: &str) -> Result<()> {
        let c = cstring(field_name)?;
        check(unsafe { sys::zvec_collection_drop_index(self.ptr.as_ptr(), c.as_ptr()) })
    }

    pub fn add_column(&self, field: &FieldSchema, expression: Option<&str>) -> Result<()> {
        let expr_c = match expression {
            Some(e) => Some(cstring(e)?),
            None => None,
        };
        let expr_ptr = expr_c.as_ref().map_or(core::ptr::null(), |c| c.as_ptr());
        // The field pointer needs to be `const zvec_field_schema_t*`; our
        // wrapper exposes such a borrow via as_ptr().
        check(unsafe {
            sys::zvec_collection_add_column(self.ptr.as_ptr(), field.as_ptr() as *const _, expr_ptr)
        })
    }

    pub fn drop_column(&self, column_name: &str) -> Result<()> {
        let c = cstring(column_name)?;
        check(unsafe { sys::zvec_collection_drop_column(self.ptr.as_ptr(), c.as_ptr()) })
    }

    pub fn alter_column(
        &self,
        column_name: &str,
        new_name: Option<&str>,
        new_schema: Option<&FieldSchema>,
    ) -> Result<()> {
        let col_c = cstring(column_name)?;
        let new_name_c = match new_name {
            Some(n) => Some(cstring(n)?),
            None => None,
        };
        let new_name_ptr = new_name_c
            .as_ref()
            .map_or(core::ptr::null(), |c| c.as_ptr());
        let schema_ptr = new_schema.map_or(core::ptr::null(), |s| s.as_ptr() as *const _);
        check(unsafe {
            sys::zvec_collection_alter_column(
                self.ptr.as_ptr(),
                col_c.as_ptr(),
                new_name_ptr,
                schema_ptr,
            )
        })
    }

    // ---------- DML ----------

    pub fn insert(&self, docs: &[&Doc]) -> Result<WriteSummary> {
        let mut c_docs = docs_to_c(docs);
        let mut success = 0usize;
        let mut error = 0usize;
        check(unsafe {
            sys::zvec_collection_insert(
                self.ptr.as_ptr(),
                c_docs.as_mut_ptr(),
                c_docs.len(),
                &mut success,
                &mut error,
            )
        })?;
        Ok(WriteSummary { success, error })
    }

    pub fn insert_with_results(&self, docs: &[&Doc]) -> Result<Vec<WriteResult>> {
        self.batch_results(docs, sys::zvec_collection_insert_with_results)
    }

    pub fn update(&self, docs: &[&Doc]) -> Result<WriteSummary> {
        let mut c_docs = docs_to_c(docs);
        let mut success = 0usize;
        let mut error = 0usize;
        check(unsafe {
            sys::zvec_collection_update(
                self.ptr.as_ptr(),
                c_docs.as_mut_ptr(),
                c_docs.len(),
                &mut success,
                &mut error,
            )
        })?;
        Ok(WriteSummary { success, error })
    }

    pub fn update_with_results(&self, docs: &[&Doc]) -> Result<Vec<WriteResult>> {
        self.batch_results(docs, sys::zvec_collection_update_with_results)
    }

    pub fn upsert(&self, docs: &[&Doc]) -> Result<WriteSummary> {
        let mut c_docs = docs_to_c(docs);
        let mut success = 0usize;
        let mut error = 0usize;
        check(unsafe {
            sys::zvec_collection_upsert(
                self.ptr.as_ptr(),
                c_docs.as_mut_ptr(),
                c_docs.len(),
                &mut success,
                &mut error,
            )
        })?;
        Ok(WriteSummary { success, error })
    }

    pub fn upsert_with_results(&self, docs: &[&Doc]) -> Result<Vec<WriteResult>> {
        self.batch_results(docs, sys::zvec_collection_upsert_with_results)
    }

    /// Stream [`Doc`]s from an iterator into the collection in batches of
    /// `batch_size`. Accumulates per-batch [`WriteSummary`]s into a single
    /// returned summary; stops on the first error.
    ///
    /// Useful for loading large corpora where collecting every `Doc` into a
    /// `Vec` up front would be wasteful or impossible.
    pub fn insert_iter<I>(&self, docs: I, batch_size: usize) -> Result<WriteSummary>
    where
        I: IntoIterator<Item = Doc>,
    {
        self.batched_write(docs, batch_size, Collection::insert)
    }

    /// As [`Self::insert_iter`] but using [`Self::update`] for each batch.
    pub fn update_iter<I>(&self, docs: I, batch_size: usize) -> Result<WriteSummary>
    where
        I: IntoIterator<Item = Doc>,
    {
        self.batched_write(docs, batch_size, Collection::update)
    }

    /// As [`Self::insert_iter`] but using [`Self::upsert`] for each batch.
    pub fn upsert_iter<I>(&self, docs: I, batch_size: usize) -> Result<WriteSummary>
    where
        I: IntoIterator<Item = Doc>,
    {
        self.batched_write(docs, batch_size, Collection::upsert)
    }

    fn batched_write<I, F>(&self, docs: I, batch_size: usize, op: F) -> Result<WriteSummary>
    where
        I: IntoIterator<Item = Doc>,
        F: Fn(&Collection, &[&Doc]) -> Result<WriteSummary>,
    {
        assert!(batch_size > 0, "batch_size must be > 0");
        let mut total = WriteSummary::default();
        let mut batch: Vec<Doc> = Vec::with_capacity(batch_size);
        for doc in docs {
            batch.push(doc);
            if batch.len() >= batch_size {
                let refs: Vec<&Doc> = batch.iter().collect();
                let s = op(self, &refs)?;
                total.success += s.success;
                total.error += s.error;
                batch.clear();
            }
        }
        if !batch.is_empty() {
            let refs: Vec<&Doc> = batch.iter().collect();
            let s = op(self, &refs)?;
            total.success += s.success;
            total.error += s.error;
        }
        Ok(total)
    }

    fn batch_results(
        &self,
        docs: &[&Doc],
        call: unsafe extern "C" fn(
            *mut sys::zvec_collection_t,
            *mut *const sys::zvec_doc_t,
            usize,
            *mut *mut sys::zvec_write_result_t,
            *mut usize,
        ) -> sys::zvec_error_code_t::Type,
    ) -> Result<Vec<WriteResult>> {
        let mut c_docs = docs_to_c(docs);
        let mut results: *mut sys::zvec_write_result_t = core::ptr::null_mut();
        let mut count: usize = 0;
        check(unsafe {
            call(
                self.ptr.as_ptr(),
                c_docs.as_mut_ptr(),
                c_docs.len(),
                &mut results,
                &mut count,
            )
        })?;
        let mut out = Vec::with_capacity(count);
        for i in 0..count {
            let r = unsafe { &*results.add(i) };
            out.push(WriteResult {
                code: crate::error::ErrorCode::from_raw(r.code),
                message: unsafe { cstr_to_string(r.message) },
            });
        }
        if !results.is_null() {
            unsafe { sys::zvec_write_results_free(results, count) };
        }
        Ok(out)
    }

    pub fn delete(&self, pks: &[&str]) -> Result<WriteSummary> {
        let (keep, ptrs) = pks_to_c(pks)?;
        let mut success = 0usize;
        let mut error = 0usize;
        let rc = unsafe {
            sys::zvec_collection_delete(
                self.ptr.as_ptr(),
                ptrs.as_ptr(),
                ptrs.len(),
                &mut success,
                &mut error,
            )
        };
        drop(keep);
        check(rc)?;
        Ok(WriteSummary { success, error })
    }

    pub fn delete_with_results(&self, pks: &[&str]) -> Result<Vec<WriteResult>> {
        let (keep, ptrs) = pks_to_c(pks)?;
        let mut results: *mut sys::zvec_write_result_t = core::ptr::null_mut();
        let mut count: usize = 0;
        let rc = unsafe {
            sys::zvec_collection_delete_with_results(
                self.ptr.as_ptr(),
                ptrs.as_ptr(),
                ptrs.len(),
                &mut results,
                &mut count,
            )
        };
        drop(keep);
        check(rc)?;
        let mut out = Vec::with_capacity(count);
        for i in 0..count {
            let r = unsafe { &*results.add(i) };
            out.push(WriteResult {
                code: crate::error::ErrorCode::from_raw(r.code),
                message: unsafe { cstr_to_string(r.message) },
            });
        }
        if !results.is_null() {
            unsafe { sys::zvec_write_results_free(results, count) };
        }
        Ok(out)
    }

    pub fn delete_by_filter(&self, filter: &str) -> Result<()> {
        let c = cstring(filter)?;
        check(unsafe { sys::zvec_collection_delete_by_filter(self.ptr.as_ptr(), c.as_ptr()) })
    }

    // ---------- DQL ----------

    pub fn query(&self, query: &VectorQuery) -> Result<DocSet> {
        let mut results: *mut *mut sys::zvec_doc_t = core::ptr::null_mut();
        let mut count: usize = 0;
        check(unsafe {
            sys::zvec_collection_query(self.as_ptr(), query.as_ptr(), &mut results, &mut count)
        })?;
        Ok(DocSet {
            ptr: results,
            len: count,
        })
    }

    pub fn fetch(&self, pks: &[&str]) -> Result<DocSet> {
        let (keep, ptrs) = pks_to_c(pks)?;
        let mut results: *mut *mut sys::zvec_doc_t = core::ptr::null_mut();
        let mut count: usize = 0;
        let rc = unsafe {
            sys::zvec_collection_fetch(
                self.ptr.as_ptr(),
                ptrs.as_ptr(),
                ptrs.len(),
                &mut results,
                &mut count,
            )
        };
        drop(keep);
        check(rc)?;
        Ok(DocSet {
            ptr: results,
            len: count,
        })
    }
}

impl Drop for Collection {
    fn drop(&mut self) {
        // zvec_collection_destroy both closes and destroys the underlying
        // handle. It is always safe to call even if close() was called.
        unsafe { sys::zvec_collection_destroy(self.ptr.as_ptr()) };
    }
}

// SAFETY: A `Collection` is an in-process handle wrapping a
// `std::shared_ptr<zvec::Collection>`. zvec is documented as an embedded
// engine usable from multiple threads; all of our mutating APIs (insert /
// update / delete / flush / optimize) go through the C API, which is
// responsible for its own synchronisation.
unsafe impl Send for Collection {}
unsafe impl Sync for Collection {}

// SAFETY: A `DocSet` owns a zvec-allocated array of non-null document
// pointers. Sending to another thread is fine. We do not implement `Sync`
// because `zvec_docs_free` runs in `Drop` and the underlying documents are
// accessed by pointer.
unsafe impl Send for DocSet {}