claw-vector 0.1.1

The semantic memory engine for ClawDB — HNSW vector indexing and storage
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// collections.rs — collection management and persistence orchestration.
use std::{collections::HashMap, path::PathBuf, sync::Arc};

use chrono::Utc;
use tokio::sync::RwLock;
use tracing::{instrument, warn};
use uuid::Uuid;

use crate::{
    config::VectorConfig,
    error::{VectorError, VectorResult},
    index::selector::IndexSelector,
    store::{mmap::MmapVectorFile, sqlite::VectorStore},
    types::{Collection, DistanceMetric, IndexType, VectorRecord},
};

/// Coordinates collection metadata, vector files, and in-memory indexes.
pub struct CollectionManager {
    /// Runtime configuration shared by all collections.
    pub config: VectorConfig,
    /// Persistent metadata store.
    pub store: Arc<VectorStore>,
    /// In-memory index implementations keyed by collection name.
    pub indexes: Arc<RwLock<HashMap<String, IndexSelector>>>,
    /// Memory-mapped vector files keyed by collection name.
    pub mmap_files: Arc<RwLock<HashMap<String, MmapVectorFile>>>,
}

impl CollectionManager {
    /// Build a manager and restore all persisted collections.
    #[instrument(skip(store))]
    pub async fn new(config: VectorConfig, store: Arc<VectorStore>) -> VectorResult<Self> {
        std::fs::create_dir_all(&config.index_dir)?;

        let manager = CollectionManager {
            config,
            store,
            indexes: Arc::new(RwLock::new(HashMap::new())),
            mmap_files: Arc::new(RwLock::new(HashMap::new())),
        };

        let collections = manager.store.list_all_collections().await?;
        for collection in collections {
            manager.restore_collection(&collection).await?;
        }

        Ok(manager)
    }

    /// Create a new collection and initialize its index and vector file.
    #[instrument(skip(self))]
    pub async fn create_collection(
        &self,
        workspace_id: &str,
        name: &str,
        dimensions: usize,
        distance: DistanceMetric,
    ) -> VectorResult<Collection> {
        if name.trim().is_empty() {
            return Err(VectorError::Collection {
                name: name.to_string(),
                reason: "name must not be empty".into(),
            });
        }

        let collection = Collection {
            workspace_id: workspace_id.to_string(),
            name: name.to_string(),
            dimensions,
            distance,
            index_type: IndexType::Flat,
            created_at: Utc::now(),
            vector_count: 0,
            metadata: serde_json::json!({}),
            ef_construction: self.config.ef_construction,
            m_connections: self.config.m_connections,
        };

        let dir = self.collection_dir(workspace_id, name);
        std::fs::create_dir_all(&dir)?;
        let mmap = MmapVectorFile::create(
            &self.vector_file_path(workspace_id, name),
            dimensions,
            self.config.max_elements.max(1),
        )?;
        let index = IndexSelector::new(dimensions, distance, &self.config);

        self.store
            .create_collection(workspace_id, &collection)
            .await?;

        let key = self.collection_key(workspace_id, name);
        self.indexes.write().await.insert(key.clone(), index);
        self.mmap_files.write().await.insert(key, mmap);

        Ok(collection)
    }

    /// Fetch a collection definition by name.
    #[instrument(skip(self))]
    pub async fn get_collection(&self, workspace_id: &str, name: &str) -> VectorResult<Collection> {
        self.store.get_collection(workspace_id, name).await
    }

    /// Delete a collection and all of its persisted state.
    #[instrument(skip(self))]
    pub async fn delete_collection(&self, workspace_id: &str, name: &str) -> VectorResult<()> {
        let key = self.collection_key(workspace_id, name);
        let removed_index = self.indexes.write().await.remove(&key);
        let removed_mmap = self.mmap_files.write().await.remove(&key);

        if removed_index.is_none() || removed_mmap.is_none() {
            let exists = self.store.get_collection(workspace_id, name).await.is_ok();
            if !exists {
                return Err(VectorError::NotFound {
                    entity: "collection".into(),
                    id: format!("{workspace_id}/{name}"),
                });
            }
        }

        if let Err(err) = self.store.delete_collection(workspace_id, name).await {
            if let Some(index) = removed_index {
                self.indexes.write().await.insert(key.clone(), index);
            }
            if let Some(mmap) = removed_mmap {
                self.mmap_files.write().await.insert(key.clone(), mmap);
            }
            return Err(err);
        }

        let collection_dir = self.collection_dir(workspace_id, name);
        if collection_dir.exists() {
            std::fs::remove_dir_all(collection_dir)?;
        }

        Ok(())
    }

    /// List all persisted collections.
    #[instrument(skip(self))]
    pub async fn list_collections(&self, workspace_id: &str) -> VectorResult<Vec<Collection>> {
        self.store.list_collections(workspace_id).await
    }

    /// Insert a single vector record into its collection.
    #[instrument(skip(self, record))]
    pub async fn insert_vector(
        &self,
        workspace_id: &str,
        record: VectorRecord,
    ) -> VectorResult<Uuid> {
        let collection = self
            .store
            .get_collection(workspace_id, &record.collection)
            .await?;
        if record.vector.len() != collection.dimensions {
            return Err(VectorError::DimensionMismatch {
                expected: collection.dimensions,
                got: record.vector.len(),
            });
        }

        let internal_id = self
            .store
            .next_internal_id(workspace_id, &record.collection)
            .await?;
        let record_id = record.id;
        self.apply_in_memory_insert(workspace_id, &record, internal_id)
            .await?;

        if let Err(err) = self
            .store
            .insert_record(workspace_id, &record, internal_id)
            .await
        {
            self.rollback_in_memory_insert(workspace_id, &record.collection, internal_id)
                .await;
            return Err(err);
        }

        if let Err(err) = self
            .store
            .increment_vector_count(workspace_id, &record.collection, 1)
            .await
        {
            let _ = self.store.delete_record(workspace_id, record.id).await;
            self.rollback_in_memory_insert(workspace_id, &record.collection, internal_id)
                .await;
            return Err(err);
        }

        self.sync_collection_index_type(workspace_id, &record.collection)
            .await?;

        Ok(record_id)
    }

    /// Insert multiple vector records atomically.
    #[instrument(skip(self, records))]
    pub async fn insert_batch(
        &self,
        workspace_id: &str,
        records: Vec<VectorRecord>,
    ) -> VectorResult<Vec<Uuid>> {
        if records.is_empty() {
            return Ok(Vec::new());
        }

        let mut next_ids = HashMap::<String, usize>::new();
        let mut deltas = HashMap::<String, i64>::new();
        let mut staged = Vec::with_capacity(records.len());
        let mut ids = Vec::with_capacity(records.len());

        for record in records {
            let collection = self
                .store
                .get_collection(workspace_id, &record.collection)
                .await?;
            if record.vector.len() != collection.dimensions {
                return Err(VectorError::DimensionMismatch {
                    expected: collection.dimensions,
                    got: record.vector.len(),
                });
            }

            let next_id = if let Some(next_id) = next_ids.get_mut(&record.collection) {
                let current = *next_id;
                *next_id += 1;
                current
            } else {
                let current = self
                    .store
                    .next_internal_id(workspace_id, &record.collection)
                    .await?;
                next_ids.insert(record.collection.clone(), current + 1);
                current
            };

            *deltas.entry(record.collection.clone()).or_insert(0) += 1;
            ids.push(record.id);
            staged.push((record, next_id));
        }

        for (record, internal_id) in &staged {
            if let Err(err) = self
                .apply_in_memory_insert(workspace_id, record, *internal_id)
                .await
            {
                self.rollback_batch_in_memory(workspace_id, &staged).await;
                return Err(err);
            }
        }

        if let Err(err) = self.store.batch_insert_records(workspace_id, &staged).await {
            self.rollback_batch_in_memory(workspace_id, &staged).await;
            return Err(err);
        }

        for (collection, delta) in deltas {
            if let Err(err) = self
                .store
                .increment_vector_count(workspace_id, &collection, delta)
                .await
            {
                for (record, _) in &staged {
                    let _ = self.store.delete_record(workspace_id, record.id).await;
                }
                self.rollback_batch_in_memory(workspace_id, &staged).await;
                return Err(err);
            }
            self.sync_collection_index_type(workspace_id, &collection)
                .await?;
        }

        Ok(ids)
    }

    /// Delete a vector from a collection by UUID.
    #[instrument(skip(self))]
    pub async fn delete_vector(
        &self,
        workspace_id: &str,
        collection: &str,
        id: Uuid,
    ) -> VectorResult<bool> {
        let (record, internal_id) = match self.store.get_record(workspace_id, id).await {
            Ok(value) => value,
            Err(VectorError::NotFound { .. }) => return Ok(false),
            Err(err) => return Err(err),
        };

        if record.collection != collection {
            return Ok(false);
        }

        {
            let mut indexes = self.indexes.write().await;
            let key = self.collection_key(workspace_id, collection);
            let index = indexes.get_mut(&key).ok_or_else(|| VectorError::NotFound {
                entity: "collection".into(),
                id: format!("{workspace_id}/{collection}"),
            })?;
            index.delete(internal_id)?;
        }

        {
            let mut mmap_files = self.mmap_files.write().await;
            let key = self.collection_key(workspace_id, collection);
            let mmap = mmap_files
                .get_mut(&key)
                .ok_or_else(|| VectorError::NotFound {
                    entity: "collection".into(),
                    id: format!("{workspace_id}/{collection}"),
                })?;
            mmap.delete_vector(internal_id)?;
            mmap.flush()?;
        }

        self.store.delete_record(workspace_id, id).await?;
        self.store
            .increment_vector_count(workspace_id, collection, -1)
            .await?;
        Ok(true)
    }

    /// Load a full vector record, including its raw vector from the mmap file.
    #[instrument(skip(self))]
    pub async fn get_vector(
        &self,
        workspace_id: &str,
        collection: &str,
        id: Uuid,
    ) -> VectorResult<VectorRecord> {
        let (mut record, internal_id) = self.store.get_record(workspace_id, id).await?;
        if record.collection != collection {
            return Err(VectorError::NotFound {
                entity: "record".into(),
                id: id.to_string(),
            });
        }

        let mmap_files = self.mmap_files.read().await;
        let key = self.collection_key(workspace_id, collection);
        let mmap = mmap_files.get(&key).ok_or_else(|| VectorError::NotFound {
            entity: "collection".into(),
            id: format!("{workspace_id}/{collection}"),
        })?;
        record.vector = mmap.read_vector(internal_id)?;
        Ok(record)
    }

    /// Persist all loaded indexes to disk.
    #[instrument(skip(self))]
    pub async fn persist_indexes(&self) -> VectorResult<()> {
        let indexes = self.indexes.read().await;
        for (key, index) in indexes.iter() {
            if let Some((workspace_id, name)) = key.split_once("::") {
                index.save(&self.config.index_dir, workspace_id, name)?;
            }
        }
        Ok(())
    }

    /// Read a raw vector by collection and internal id.
    pub async fn read_vector_by_internal_id(
        &self,
        workspace_id: &str,
        collection: &str,
        internal_id: usize,
    ) -> VectorResult<Vec<f32>> {
        let mmap_files = self.mmap_files.read().await;
        let key = self.collection_key(workspace_id, collection);
        let mmap = mmap_files.get(&key).ok_or_else(|| VectorError::NotFound {
            entity: "collection".into(),
            id: format!("{workspace_id}/{collection}"),
        })?;
        mmap.read_vector(internal_id)
    }

    /// Return the number of loaded indexes.
    pub async fn loaded_index_count(&self) -> usize {
        self.indexes.read().await.len()
    }

    /// Return the number of loaded mmap vector files.
    pub async fn loaded_mmap_count(&self) -> usize {
        self.mmap_files.read().await.len()
    }

    async fn restore_collection(&self, collection: &Collection) -> VectorResult<()> {
        let dir = self.collection_dir(&collection.workspace_id, &collection.name);
        std::fs::create_dir_all(&dir)?;

        let mmap_path = self.vector_file_path(&collection.workspace_id, &collection.name);
        let mmap = if mmap_path.exists() {
            MmapVectorFile::open(&mmap_path)?
        } else {
            MmapVectorFile::create(
                &mmap_path,
                collection.dimensions,
                self.config
                    .max_elements
                    .max(collection.vector_count as usize + 1),
            )?
        };

        let index = match IndexSelector::load(
            &self.config.index_dir,
            &collection.workspace_id,
            &collection.name,
            &self.config,
            collection.distance,
            collection.dimensions,
        ) {
            Ok(index) => index,
            Err(err) => {
                warn!(
                    collection = %collection.name,
                    error = %err,
                    "failed to load persisted index, rebuilding from mmap"
                );
                self.rebuild_index(collection, &mmap).await?
            }
        };

        let key = self.collection_key(&collection.workspace_id, &collection.name);
        self.indexes.write().await.insert(key.clone(), index);
        self.mmap_files.write().await.insert(key, mmap);
        self.sync_collection_index_type(&collection.workspace_id, &collection.name)
            .await?;
        Ok(())
    }

    async fn rebuild_index(
        &self,
        collection: &Collection,
        mmap: &MmapVectorFile,
    ) -> VectorResult<IndexSelector> {
        let mut index =
            IndexSelector::new(collection.dimensions, collection.distance, &self.config);
        let records = self
            .store
            .list_records_for_collection(&collection.workspace_id, &collection.name)
            .await?;
        let mut items = Vec::with_capacity(records.len());
        for (_, internal_id) in records {
            let vector = mmap.read_vector(internal_id)?;
            items.push((internal_id, vector));
        }
        index.insert_batch(items, &self.config)?;
        Ok(index)
    }

    async fn apply_in_memory_insert(
        &self,
        workspace_id: &str,
        record: &VectorRecord,
        internal_id: usize,
    ) -> VectorResult<()> {
        {
            let mut mmap_files = self.mmap_files.write().await;
            let key = self.collection_key(workspace_id, &record.collection);
            let mmap = mmap_files
                .get_mut(&key)
                .ok_or_else(|| VectorError::NotFound {
                    entity: "collection".into(),
                    id: format!("{workspace_id}/{}", record.collection),
                })?;
            mmap.write_vector(internal_id, &record.vector)?;
            mmap.flush()?;
        }

        let mut indexes = self.indexes.write().await;
        let key = self.collection_key(workspace_id, &record.collection);
        let index = indexes.get_mut(&key).ok_or_else(|| VectorError::NotFound {
            entity: "collection".into(),
            id: format!("{workspace_id}/{}", record.collection),
        })?;
        index.insert(internal_id, record.vector.clone(), &self.config)?;
        Ok(())
    }

    async fn rollback_in_memory_insert(
        &self,
        workspace_id: &str,
        collection: &str,
        internal_id: usize,
    ) {
        let key = self.collection_key(workspace_id, collection);
        if let Some(index) = self.indexes.write().await.get_mut(&key) {
            let _ = index.delete(internal_id);
        }
        if let Some(mmap) = self.mmap_files.write().await.get_mut(&key) {
            let _ = mmap.delete_vector(internal_id);
            let _ = mmap.flush();
        }
    }

    async fn rollback_batch_in_memory(&self, workspace_id: &str, staged: &[(VectorRecord, usize)]) {
        for (record, internal_id) in staged.iter().rev() {
            self.rollback_in_memory_insert(workspace_id, &record.collection, *internal_id)
                .await;
        }
    }

    fn collection_dir(&self, workspace_id: &str, name: &str) -> PathBuf {
        self.config.index_dir.join(workspace_id).join(name)
    }

    fn vector_file_path(&self, workspace_id: &str, name: &str) -> PathBuf {
        self.collection_dir(workspace_id, name).join("vectors.bin")
    }

    async fn sync_collection_index_type(
        &self,
        workspace_id: &str,
        collection: &str,
    ) -> VectorResult<()> {
        let current_type = {
            let indexes = self.indexes.read().await;
            let key = self.collection_key(workspace_id, collection);
            let index = indexes.get(&key).ok_or_else(|| VectorError::NotFound {
                entity: "collection".into(),
                id: format!("{workspace_id}/{collection}"),
            })?;
            if index.is_hnsw() {
                IndexType::HNSW
            } else {
                IndexType::Flat
            }
        };
        self.store
            .update_collection_index_type(workspace_id, collection, current_type)
            .await
    }

    fn collection_key(&self, workspace_id: &str, name: &str) -> String {
        format!("{workspace_id}::{name}")
    }
}