minimemory 3.0.0

Embedded vector database library for Rust - like SQLite for vectors
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
//! Bindings C/FFI para minimemory.
//!
//! Estos bindings permiten usar minimemory desde cualquier lenguaje
//! que soporte FFI con C, incluyendo PHP, Ruby, etc.
//!
//! ## Uso en PHP
//!
//! ```php
//! <?php
//! $ffi = FFI::cdef("
//!     typedef struct MiniMemoryDB MiniMemoryDB;
//!     typedef struct SearchResult {
//!         char* id;
//!         float distance;
//!     } SearchResult;
//!
//!     MiniMemoryDB* mmdb_new(uint32_t dimensions, const char* distance, const char* index_type);
//!     void mmdb_free(MiniMemoryDB* db);
//!     int mmdb_insert(MiniMemoryDB* db, const char* id, const float* vector, uint32_t len);
//!     SearchResult* mmdb_search(MiniMemoryDB* db, const float* query, uint32_t len, uint32_t k, uint32_t* result_count);
//!     void mmdb_free_results(SearchResult* results, uint32_t count);
//!     int mmdb_save(MiniMemoryDB* db, const char* path);
//!     MiniMemoryDB* mmdb_load(const char* path);
//! ", "libminimemory.so");
//!
//! // Crear base de datos
//! $db = $ffi->mmdb_new(384, "cosine", "flat");
//!
//! // Insertar vector
//! $vector = FFI::new("float[384]");
//! for ($i = 0; $i < 384; $i++) $vector[$i] = 0.1;
//! $ffi->mmdb_insert($db, "doc1", $vector, 384);
//!
//! // Buscar
//! $count = FFI::new("uint32_t");
//! $results = $ffi->mmdb_search($db, $vector, 384, 10, FFI::addr($count));
//!
//! // Limpiar
//! $ffi->mmdb_free_results($results, $count->cdata);
//! $ffi->mmdb_free($db);
//! ?>
//! ```

use std::ffi::{c_char, c_float, c_int, CStr, CString};
use std::ptr;
use std::sync::Arc;

use crate::{
    Config as RustConfig, Distance as RustDistance, IndexType as RustIndexType,
    VectorDB as RustVectorDB,
};

/// Estructura opaca para la base de datos
pub struct MiniMemoryDB {
    inner: Arc<RustVectorDB>,
}

/// Resultado de búsqueda para FFI
#[repr(C)]
pub struct SearchResult {
    pub id: *mut c_char,
    pub distance: c_float,
}

/// Crea una nueva base de datos.
///
/// # Argumentos
/// * `dimensions` - Número de dimensiones
/// * `distance` - "cosine", "euclidean", o "dot"
/// * `index_type` - "flat" o "hnsw"
///
/// # Retorna
/// Puntero a la base de datos o NULL si hay error.
#[no_mangle]
pub extern "C" fn mmdb_new(
    dimensions: u32,
    distance: *const c_char,
    index_type: *const c_char,
) -> *mut MiniMemoryDB {
    let dist_str = unsafe {
        if distance.is_null() {
            "cosine"
        } else {
            match CStr::from_ptr(distance).to_str() {
                Ok(s) => s,
                Err(_) => return ptr::null_mut(),
            }
        }
    };

    let idx_str = unsafe {
        if index_type.is_null() {
            "flat"
        } else {
            match CStr::from_ptr(index_type).to_str() {
                Ok(s) => s,
                Err(_) => return ptr::null_mut(),
            }
        }
    };

    let dist = match dist_str {
        "cosine" | "cos" => RustDistance::Cosine,
        "euclidean" | "l2" => RustDistance::Euclidean,
        "dot" | "dot_product" => RustDistance::DotProduct,
        _ => return ptr::null_mut(),
    };

    let idx = match idx_str {
        "flat" => RustIndexType::Flat,
        "hnsw" => RustIndexType::HNSW {
            m: 16,
            ef_construction: 200,
        },
        _ => return ptr::null_mut(),
    };

    let config = RustConfig::new(dimensions as usize)
        .with_distance(dist)
        .with_index(idx);

    match RustVectorDB::new(config) {
        Ok(db) => Box::into_raw(Box::new(MiniMemoryDB {
            inner: Arc::new(db),
        })),
        Err(_) => ptr::null_mut(),
    }
}

/// Libera la memoria de la base de datos.
#[no_mangle]
pub extern "C" fn mmdb_free(db: *mut MiniMemoryDB) {
    if !db.is_null() {
        unsafe {
            drop(Box::from_raw(db));
        }
    }
}

/// Inserta un vector en la base de datos.
///
/// # Retorna
/// 0 si éxito, -1 si error.
#[no_mangle]
pub extern "C" fn mmdb_insert(
    db: *mut MiniMemoryDB,
    id: *const c_char,
    vector: *const c_float,
    len: u32,
) -> c_int {
    if db.is_null() || id.is_null() || vector.is_null() {
        return -1;
    }

    let db = unsafe { &*db };
    let id_str = unsafe {
        match CStr::from_ptr(id).to_str() {
            Ok(s) => s,
            Err(_) => return -1,
        }
    };

    let vec: Vec<f32> = unsafe { std::slice::from_raw_parts(vector, len as usize).to_vec() };

    match db.inner.insert(id_str, &vec, None) {
        Ok(_) => 0,
        Err(_) => -1,
    }
}

/// Busca los k vectores más similares.
///
/// # Argumentos
/// * `db` - Base de datos
/// * `query` - Vector de consulta
/// * `len` - Longitud del vector
/// * `k` - Número de resultados
/// * `result_count` - Puntero donde se escribirá el número de resultados
///
/// # Retorna
/// Array de SearchResult. Debe liberarse con mmdb_free_results.
#[no_mangle]
pub extern "C" fn mmdb_search(
    db: *mut MiniMemoryDB,
    query: *const c_float,
    len: u32,
    k: u32,
    result_count: *mut u32,
) -> *mut SearchResult {
    if db.is_null() || query.is_null() || result_count.is_null() {
        return ptr::null_mut();
    }

    let db = unsafe { &*db };
    let query_vec: Vec<f32> = unsafe { std::slice::from_raw_parts(query, len as usize).to_vec() };

    match db.inner.search(&query_vec, k as usize) {
        Ok(results) => {
            let count = results.len();
            unsafe { *result_count = count as u32 };

            if count == 0 {
                return ptr::null_mut();
            }

            let mut ffi_results: Vec<SearchResult> = results
                .into_iter()
                .map(|r| {
                    let id_cstring = CString::new(r.id).unwrap_or_default();
                    SearchResult {
                        id: id_cstring.into_raw(),
                        distance: r.distance,
                    }
                })
                .collect();

            let ptr = ffi_results.as_mut_ptr();
            std::mem::forget(ffi_results);
            ptr
        }
        Err(_) => {
            unsafe { *result_count = 0 };
            ptr::null_mut()
        }
    }
}

/// Libera los resultados de búsqueda.
#[no_mangle]
pub extern "C" fn mmdb_free_results(results: *mut SearchResult, count: u32) {
    if results.is_null() || count == 0 {
        return;
    }

    unsafe {
        let slice = std::slice::from_raw_parts_mut(results, count as usize);
        for result in slice.iter() {
            if !result.id.is_null() {
                drop(CString::from_raw(result.id));
            }
        }
        drop(Vec::from_raw_parts(results, count as usize, count as usize));
    }
}

/// Obtiene un vector por su ID.
///
/// # Retorna
/// Puntero al vector (debe liberarse con mmdb_free_vector) o NULL si no existe.
#[no_mangle]
pub extern "C" fn mmdb_get(
    db: *mut MiniMemoryDB,
    id: *const c_char,
    len: *mut u32,
) -> *mut c_float {
    if db.is_null() || id.is_null() || len.is_null() {
        return ptr::null_mut();
    }

    let db = unsafe { &*db };
    let id_str = unsafe {
        match CStr::from_ptr(id).to_str() {
            Ok(s) => s,
            Err(_) => return ptr::null_mut(),
        }
    };

    match db.inner.get(id_str) {
        Ok(Some((vector, _))) => {
            unsafe { *len = vector.len() as u32 };
            let mut boxed = vector.into_boxed_slice();
            let ptr = boxed.as_mut_ptr();
            std::mem::forget(boxed);
            ptr
        }
        _ => {
            unsafe { *len = 0 };
            ptr::null_mut()
        }
    }
}

/// Libera un vector obtenido con mmdb_get.
#[no_mangle]
pub extern "C" fn mmdb_free_vector(vector: *mut c_float, len: u32) {
    if !vector.is_null() && len > 0 {
        unsafe {
            drop(Vec::from_raw_parts(vector, len as usize, len as usize));
        }
    }
}

/// Elimina un vector por su ID.
///
/// # Retorna
/// 1 si fue eliminado, 0 si no existía, -1 si error.
#[no_mangle]
pub extern "C" fn mmdb_delete(db: *mut MiniMemoryDB, id: *const c_char) -> c_int {
    if db.is_null() || id.is_null() {
        return -1;
    }

    let db = unsafe { &*db };
    let id_str = unsafe {
        match CStr::from_ptr(id).to_str() {
            Ok(s) => s,
            Err(_) => return -1,
        }
    };

    match db.inner.delete(id_str) {
        Ok(true) => 1,
        Ok(false) => 0,
        Err(_) => -1,
    }
}

/// Verifica si un vector existe.
///
/// # Retorna
/// 1 si existe, 0 si no.
#[no_mangle]
pub extern "C" fn mmdb_contains(db: *mut MiniMemoryDB, id: *const c_char) -> c_int {
    if db.is_null() || id.is_null() {
        return 0;
    }

    let db = unsafe { &*db };
    let id_str = unsafe {
        match CStr::from_ptr(id).to_str() {
            Ok(s) => s,
            Err(_) => return 0,
        }
    };

    if db.inner.contains(id_str) {
        1
    } else {
        0
    }
}

/// Guarda la base de datos a un archivo.
///
/// # Retorna
/// 0 si éxito, -1 si error.
#[no_mangle]
pub extern "C" fn mmdb_save(db: *mut MiniMemoryDB, path: *const c_char) -> c_int {
    if db.is_null() || path.is_null() {
        return -1;
    }

    let db = unsafe { &*db };
    let path_str = unsafe {
        match CStr::from_ptr(path).to_str() {
            Ok(s) => s,
            Err(_) => return -1,
        }
    };

    match db.inner.save(path_str) {
        Ok(_) => 0,
        Err(_) => -1,
    }
}

/// Carga una base de datos desde archivo.
///
/// # Retorna
/// Puntero a la base de datos o NULL si error.
#[no_mangle]
pub extern "C" fn mmdb_load(path: *const c_char) -> *mut MiniMemoryDB {
    if path.is_null() {
        return ptr::null_mut();
    }

    let path_str = unsafe {
        match CStr::from_ptr(path).to_str() {
            Ok(s) => s,
            Err(_) => return ptr::null_mut(),
        }
    };

    match RustVectorDB::open(path_str) {
        Ok(db) => Box::into_raw(Box::new(MiniMemoryDB {
            inner: Arc::new(db),
        })),
        Err(_) => ptr::null_mut(),
    }
}

/// Obtiene el número de vectores.
#[no_mangle]
pub extern "C" fn mmdb_len(db: *mut MiniMemoryDB) -> u32 {
    if db.is_null() {
        return 0;
    }
    let db = unsafe { &*db };
    db.inner.len() as u32
}

/// Obtiene las dimensiones.
#[no_mangle]
pub extern "C" fn mmdb_dimensions(db: *mut MiniMemoryDB) -> u32 {
    if db.is_null() {
        return 0;
    }
    let db = unsafe { &*db };
    db.inner.dimensions() as u32
}

/// Limpia todos los vectores.
#[no_mangle]
pub extern "C" fn mmdb_clear(db: *mut MiniMemoryDB) {
    if !db.is_null() {
        let db = unsafe { &*db };
        db.inner.clear();
    }
}