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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
// store/sqlite.rs — SQLite persistence layer for VectorRecord and Collection metadata.
use std::{collections::HashMap, path::Path};

use sqlx::{
    sqlite::{SqliteConnectOptions, SqlitePoolOptions},
    QueryBuilder, Sqlite, SqlitePool,
};
use uuid::Uuid;

use crate::{
    error::{VectorError, VectorResult},
    types::{Collection, CollectionStats, DistanceMetric, IndexType, VectorRecord},
};

/// Manages all SQLite read/write operations for collections and vector records.
pub struct VectorStore {
    pool: SqlitePool,
}

/// Backward-compatible alias for [`VectorStore`].
pub type SqliteStore = VectorStore;

impl VectorStore {
    /// Open (or create) the SQLite database at `db_path`, applying schema migrations.
    pub async fn new(db_path: &Path) -> VectorResult<Self> {
        if let Some(parent) = db_path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        let options = SqliteConnectOptions::new()
            .filename(db_path)
            .create_if_missing(true)
            .foreign_keys(true);

        let pool = SqlitePoolOptions::new()
            .max_connections(8)
            .connect_with(options)
            .await?;

        sqlx::query("PRAGMA journal_mode = WAL")
            .execute(&pool)
            .await?;
        sqlx::query("PRAGMA synchronous = NORMAL")
            .execute(&pool)
            .await?;
        sqlx::query("PRAGMA temp_store = MEMORY")
            .execute(&pool)
            .await?;

        sqlx::migrate!()
            .run(&pool)
            .await
            .map_err(|err| VectorError::Index(format!("failed to run SQLite migrations: {err}")))?;

        Ok(VectorStore { pool })
    }

    /// Alias for [`VectorStore::new`].
    pub async fn open(path: &Path) -> VectorResult<Self> {
        Self::new(path).await
    }

    /// Return the underlying SQLx connection pool.
    pub fn pool(&self) -> &SqlitePool {
        &self.pool
    }

    /// Persist a new collection definition (upsert).
    pub async fn create_collection(
        &self,
        workspace_id: &str,
        col: &Collection,
    ) -> VectorResult<()> {
        sqlx::query(
            r#"INSERT INTO collections
               (workspace_id, name, dimensions, distance, index_type, ef_construction, m_connections,
                created_at, vector_count, metadata)
               VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"#,
        )
        .bind(workspace_id)
        .bind(&col.name)
        .bind(col.dimensions as i64)
        .bind(distance_to_db(col.distance))
        .bind(index_type_to_db(col.index_type))
        .bind(col.ef_construction as i64)
        .bind(col.m_connections as i64)
        .bind(col.created_at.to_rfc3339())
        .bind(col.vector_count as i64)
        .bind(normalize_metadata(&col.metadata)?)
        .execute(&self.pool)
        .await?;
        Ok(())
    }

    /// Alias for [`VectorStore::create_collection`].
    pub async fn save_collection(&self, workspace_id: &str, col: &Collection) -> VectorResult<()> {
        self.create_collection(workspace_id, col).await
    }

    /// Retrieve a collection by name.
    pub async fn get_collection(&self, workspace_id: &str, name: &str) -> VectorResult<Collection> {
        let row = sqlx::query_as::<_, CollectionRow>(
            "SELECT workspace_id, name, dimensions, distance, index_type, ef_construction, m_connections, \
             created_at, vector_count, metadata FROM collections WHERE workspace_id = ? AND name = ?",
        )
        .bind(workspace_id)
        .bind(name)
        .fetch_optional(&self.pool)
        .await?;

        match row {
            Some(row) => collection_from_row(row),
            None => Err(VectorError::NotFound {
                entity: "collection".into(),
                id: name.to_string(),
            }),
        }
    }

    /// Delete a collection by name.
    pub async fn delete_collection(&self, workspace_id: &str, name: &str) -> VectorResult<()> {
        let mut tx = self.pool.begin().await?;
        sqlx::query("DELETE FROM vector_records WHERE workspace_id = ? AND collection = ?")
            .bind(workspace_id)
            .bind(name)
            .execute(&mut *tx)
            .await?;
        sqlx::query("DELETE FROM collections WHERE workspace_id = ? AND name = ?")
            .bind(workspace_id)
            .bind(name)
            .execute(&mut *tx)
            .await?;
        tx.commit().await?;
        Ok(())
    }

    /// List all collections.
    pub async fn list_collections(&self, workspace_id: &str) -> VectorResult<Vec<Collection>> {
        let rows = sqlx::query_as::<_, CollectionRow>(
            "SELECT workspace_id, name, dimensions, distance, index_type, ef_construction, m_connections, \
             created_at, vector_count, metadata FROM collections WHERE workspace_id = ? ORDER BY name",
        )
        .bind(workspace_id)
        .fetch_all(&self.pool)
        .await?;

        rows.into_iter().map(collection_from_row).collect()
    }

    /// List all collections across all workspaces.
    pub async fn list_all_collections(&self) -> VectorResult<Vec<Collection>> {
        let rows = sqlx::query_as::<_, CollectionRow>(
            "SELECT workspace_id, name, dimensions, distance, index_type, ef_construction, m_connections, \
             created_at, vector_count, metadata FROM collections ORDER BY workspace_id, name",
        )
        .fetch_all(&self.pool)
        .await?;

        rows.into_iter().map(collection_from_row).collect()
    }

    /// Persist a vector record, linking it to the given `internal_id`.
    pub async fn insert_record(
        &self,
        workspace_id: &str,
        record: &VectorRecord,
        internal_id: usize,
    ) -> VectorResult<()> {
        sqlx::query(
            r#"INSERT INTO vector_records
               (id, internal_id, workspace_id, collection, text, metadata, created_at)
               VALUES (?, ?, ?, ?, ?, ?, ?)"#,
        )
        .bind(record.id.to_string())
        .bind(internal_id as i64)
        .bind(workspace_id)
        .bind(&record.collection)
        .bind(&record.text)
        .bind(normalize_metadata(&record.metadata)?)
        .bind(record.created_at.to_rfc3339())
        .execute(&self.pool)
        .await?;
        Ok(())
    }

    /// Alias for [`VectorStore::insert_record`].
    pub async fn save_record(
        &self,
        workspace_id: &str,
        record: &VectorRecord,
        internal_id: usize,
    ) -> VectorResult<()> {
        self.insert_record(workspace_id, record, internal_id).await
    }

    /// Retrieve a record and its internal identifier by UUID.
    pub async fn get_record(
        &self,
        workspace_id: &str,
        id: Uuid,
    ) -> VectorResult<(VectorRecord, usize)> {
        let row = sqlx::query_as::<_, RecordRow>(
            "SELECT id, internal_id, workspace_id, collection, text, metadata, created_at \
             FROM vector_records WHERE workspace_id = ? AND id = ?",
        )
        .bind(workspace_id)
        .bind(id.to_string())
        .fetch_optional(&self.pool)
        .await?;

        match row {
            Some(row) => record_from_row(row),
            None => Err(VectorError::NotFound {
                entity: "record".into(),
                id: id.to_string(),
            }),
        }
    }

    /// Delete a vector record by id and return its previous internal id when found.
    pub async fn delete_record(&self, workspace_id: &str, id: Uuid) -> VectorResult<Option<usize>> {
        let mut tx = self.pool.begin().await?;
        let internal_id = sqlx::query_scalar::<_, i64>(
            "SELECT internal_id FROM vector_records WHERE workspace_id = ? AND id = ?",
        )
        .bind(workspace_id)
        .bind(id.to_string())
        .fetch_optional(&mut *tx)
        .await?
        .map(|value| value as usize);

        if internal_id.is_some() {
            sqlx::query("DELETE FROM vector_records WHERE workspace_id = ? AND id = ?")
                .bind(workspace_id)
                .bind(id.to_string())
                .execute(&mut *tx)
                .await?;
        }

        tx.commit().await?;
        Ok(internal_id)
    }

    /// Insert multiple vector records in a single transaction.
    pub async fn batch_insert_records(
        &self,
        workspace_id: &str,
        records: &[(VectorRecord, usize)],
    ) -> VectorResult<()> {
        let mut tx = self.pool.begin().await?;
        for (record, internal_id) in records {
            sqlx::query(
                r#"INSERT INTO vector_records
                   (id, internal_id, workspace_id, collection, text, metadata, created_at)
                   VALUES (?, ?, ?, ?, ?, ?, ?)"#,
            )
            .bind(record.id.to_string())
            .bind(*internal_id as i64)
            .bind(workspace_id)
            .bind(&record.collection)
            .bind(&record.text)
            .bind(normalize_metadata(&record.metadata)?)
            .bind(record.created_at.to_rfc3339())
            .execute(&mut *tx)
            .await?;
        }
        tx.commit().await?;
        Ok(())
    }

    /// Resolve a record UUID to its internal id.
    pub async fn uuid_to_internal(&self, workspace_id: &str, id: Uuid) -> VectorResult<usize> {
        let internal_id = sqlx::query_scalar::<_, i64>(
            "SELECT internal_id FROM vector_records WHERE workspace_id = ? AND id = ?",
        )
        .bind(workspace_id)
        .bind(id.to_string())
        .fetch_optional(&self.pool)
        .await?
        .ok_or_else(|| VectorError::NotFound {
            entity: "record".into(),
            id: id.to_string(),
        })?;
        Ok(internal_id as usize)
    }

    /// Resolve a collection-scoped internal id to its UUID.
    pub async fn internal_to_uuid(
        &self,
        workspace_id: &str,
        collection: &str,
        internal_id: usize,
    ) -> VectorResult<Uuid> {
        let id = sqlx::query_scalar::<_, String>(
            "SELECT id FROM vector_records WHERE workspace_id = ? AND collection = ? AND internal_id = ?",
        )
        .bind(workspace_id)
        .bind(collection)
        .bind(internal_id as i64)
        .fetch_optional(&self.pool)
        .await?
        .ok_or_else(|| VectorError::NotFound {
            entity: "record".into(),
            id: format!("{collection}:{internal_id}"),
        })?;
        Uuid::parse_str(&id)
            .map_err(|err| VectorError::Index(format!("invalid UUID stored in SQLite: {err}")))
    }

    /// Bulk-resolve collection-scoped internal ids to stored vector metadata.
    pub async fn bulk_internal_to_uuid(
        &self,
        workspace_id: &str,
        collection: &str,
        ids: &[usize],
    ) -> VectorResult<Vec<(usize, VectorRecord)>> {
        if ids.is_empty() {
            return Ok(Vec::new());
        }

        let mut builder = QueryBuilder::<Sqlite>::new(
            "SELECT id, internal_id, workspace_id, collection, text, metadata, created_at FROM vector_records WHERE workspace_id = ",
        );
        builder.push_bind(workspace_id);
        builder.push(" AND collection = ");
        builder.push_bind(collection);
        builder.push(" AND internal_id IN (");
        let mut separated = builder.separated(", ");
        for id in ids {
            separated.push_bind(*id as i64);
        }
        separated.push_unseparated(") ORDER BY internal_id ASC");

        let rows = builder
            .build_query_as::<RecordRow>()
            .fetch_all(&self.pool)
            .await?;

        let resolved = rows
            .into_iter()
            .map(record_from_row)
            .collect::<VectorResult<Vec<_>>>()?;

        let mut by_id = HashMap::with_capacity(resolved.len());
        for (record, internal_id) in resolved {
            by_id.insert(internal_id, record);
        }

        Ok(ids
            .iter()
            .filter_map(|id| by_id.remove(id).map(|record| (*id, record)))
            .collect())
    }

    /// Increment a collection's stored vector count.
    pub async fn increment_vector_count(
        &self,
        workspace_id: &str,
        collection: &str,
        delta: i64,
    ) -> VectorResult<()> {
        sqlx::query(
            "UPDATE collections SET vector_count = MAX(vector_count + ?, 0) WHERE workspace_id = ? AND name = ?",
        )
        .bind(delta)
        .bind(workspace_id)
        .bind(collection)
        .execute(&self.pool)
        .await?;
        Ok(())
    }

    /// Update the persisted index type for a collection.
    pub async fn update_collection_index_type(
        &self,
        workspace_id: &str,
        collection: &str,
        index_type: IndexType,
    ) -> VectorResult<()> {
        sqlx::query("UPDATE collections SET index_type = ? WHERE workspace_id = ? AND name = ?")
            .bind(index_type_to_db(index_type))
            .bind(workspace_id)
            .bind(collection)
            .execute(&self.pool)
            .await?;
        Ok(())
    }

    /// Return collection storage statistics as tracked in SQLite.
    pub async fn collection_stats(
        &self,
        workspace_id: &str,
        name: &str,
    ) -> VectorResult<CollectionStats> {
        let vector_count = sqlx::query_scalar::<_, i64>(
            "SELECT vector_count FROM collections WHERE workspace_id = ? AND name = ?",
        )
        .bind(workspace_id)
        .bind(name)
        .fetch_optional(&self.pool)
        .await?
        .ok_or_else(|| VectorError::NotFound {
            entity: "collection".into(),
            id: name.to_string(),
        })?;

        let record_bytes = sqlx::query_scalar::<_, i64>(
            "SELECT COALESCE(SUM(LENGTH(id) + LENGTH(IFNULL(text, '')) + LENGTH(metadata) + LENGTH(created_at) + 8), 0) FROM vector_records WHERE workspace_id = ? AND collection = ?",
        )
        .bind(workspace_id)
        .bind(name)
        .fetch_one(&self.pool)
        .await?;

        let collection_bytes = sqlx::query_scalar::<_, i64>(
            "SELECT LENGTH(name) + LENGTH(distance) + LENGTH(index_type) + LENGTH(created_at) + LENGTH(metadata) + 32 FROM collections WHERE workspace_id = ? AND name = ?",
        )
        .bind(workspace_id)
        .bind(name)
        .fetch_one(&self.pool)
        .await?;

        Ok(CollectionStats {
            vector_count: vector_count as u64,
            size_bytes: (record_bytes + collection_bytes.max(0)) as u64,
        })
    }

    /// Return the next available internal id for a collection.
    pub async fn next_internal_id(
        &self,
        workspace_id: &str,
        collection: &str,
    ) -> VectorResult<usize> {
        let max_internal_id = sqlx::query_scalar::<_, Option<i64>>(
            "SELECT MAX(internal_id) FROM vector_records WHERE workspace_id = ? AND collection = ?",
        )
        .bind(workspace_id)
        .bind(collection)
        .fetch_one(&self.pool)
        .await?;
        Ok(max_internal_id.map(|value| value as usize + 1).unwrap_or(0))
    }

    /// Load all persisted records for a collection, ordered by internal id.
    pub async fn list_records_for_collection(
        &self,
        workspace_id: &str,
        collection: &str,
    ) -> VectorResult<Vec<(VectorRecord, usize)>> {
        let rows = sqlx::query_as::<_, RecordRow>(
            "SELECT id, internal_id, workspace_id, collection, text, metadata, created_at FROM vector_records WHERE workspace_id = ? AND collection = ? ORDER BY internal_id ASC",
        )
        .bind(workspace_id)
        .bind(collection)
        .fetch_all(&self.pool)
        .await?;

        rows.into_iter().map(record_from_row).collect()
    }

    /// Search full-text records for a collection using SQLite FTS5.
    pub async fn keyword_search(
        &self,
        workspace_id: &str,
        collection: &str,
        query: &str,
        limit: usize,
    ) -> VectorResult<Vec<(usize, VectorRecord, f32)>> {
        if query.trim().is_empty() || limit == 0 {
            return Ok(Vec::new());
        }

        let rows = sqlx::query_as::<_, KeywordRow>(
            r#"
                 SELECT vr.id, vr.internal_id, vr.workspace_id, vr.collection, vr.text, vr.metadata, vr.created_at,
                   CAST(bm25(vector_records_fts) AS REAL) AS rank
            FROM vector_records_fts
            JOIN vector_records AS vr ON vr.rowid = vector_records_fts.rowid
            WHERE vr.workspace_id = ? AND vr.collection = ? AND vector_records_fts MATCH ?
            ORDER BY rank ASC
            LIMIT ?
            "#,
        )
        .bind(workspace_id)
        .bind(collection)
        .bind(query)
        .bind(limit as i64)
        .fetch_all(&self.pool)
        .await?;

        rows.into_iter()
            .map(|row| {
                let rank = row.rank.unwrap_or(0.0);
                let record_row = RecordRow {
                    id: row.id,
                    internal_id: row.internal_id,
                    workspace_id: row.workspace_id,
                    collection: row.collection,
                    text: row.text,
                    metadata: row.metadata,
                    created_at: row.created_at,
                };
                let (record, internal_id) = record_from_row(record_row)?;
                Ok((internal_id, record, rank))
            })
            .collect()
    }

    /// Close the underlying SQLx pool.
    pub async fn close(&self) {
        self.pool.close().await;
    }
}

#[derive(Debug, sqlx::FromRow)]
struct CollectionRow {
    workspace_id: String,
    name: String,
    dimensions: i64,
    distance: String,
    index_type: String,
    ef_construction: i64,
    m_connections: i64,
    created_at: String,
    vector_count: i64,
    metadata: String,
}

#[derive(Debug, sqlx::FromRow)]
struct RecordRow {
    id: String,
    internal_id: i64,
    #[allow(dead_code)]
    workspace_id: String,
    collection: String,
    text: Option<String>,
    metadata: String,
    created_at: String,
}

#[derive(Debug, sqlx::FromRow)]
struct KeywordRow {
    id: String,
    internal_id: i64,
    workspace_id: String,
    collection: String,
    text: Option<String>,
    metadata: String,
    created_at: String,
    rank: Option<f32>,
}

/// Convert a raw database row into a [`Collection`], parsing JSON and RFC-3339 fields.
fn collection_from_row(row: CollectionRow) -> VectorResult<Collection> {
    Ok(Collection {
        workspace_id: row.workspace_id,
        name: row.name,
        dimensions: row.dimensions as usize,
        distance: distance_from_db(&row.distance)?,
        index_type: index_type_from_db(&row.index_type)?,
        ef_construction: row.ef_construction as usize,
        m_connections: row.m_connections as usize,
        created_at: chrono::DateTime::parse_from_rfc3339(&row.created_at)
            .map_err(|e| VectorError::Index(format!("invalid timestamp in DB: {e}")))?
            .with_timezone(&chrono::Utc),
        vector_count: row.vector_count as u64,
        metadata: parse_metadata(&row.metadata)?,
    })
}

fn record_from_row(row: RecordRow) -> VectorResult<(VectorRecord, usize)> {
    let id = Uuid::parse_str(&row.id).map_err(|err| {
        VectorError::Index(format!(
            "invalid UUID stored in vector_records table: {err}"
        ))
    })?;
    let record = VectorRecord {
        id,
        collection: row.collection,
        vector: Vec::new(),
        metadata: parse_metadata(&row.metadata)?,
        text: row.text,
        created_at: chrono::DateTime::parse_from_rfc3339(&row.created_at)
            .map_err(|e| VectorError::Index(format!("invalid timestamp in DB: {e}")))?
            .with_timezone(&chrono::Utc),
    };
    Ok((record, row.internal_id as usize))
}

fn normalize_metadata(metadata: &serde_json::Value) -> VectorResult<String> {
    if metadata.is_null() {
        Ok("{}".to_string())
    } else {
        serde_json::to_string(metadata).map_err(Into::into)
    }
}

fn parse_metadata(metadata: &str) -> VectorResult<serde_json::Value> {
    if metadata.trim().is_empty() {
        Ok(serde_json::json!({}))
    } else {
        Ok(serde_json::from_str(metadata)?)
    }
}

fn distance_to_db(distance: DistanceMetric) -> &'static str {
    match distance {
        DistanceMetric::Cosine => "cosine",
        DistanceMetric::Euclidean => "euclidean",
        DistanceMetric::DotProduct => "dot_product",
    }
}

fn distance_from_db(distance: &str) -> VectorResult<DistanceMetric> {
    match distance.trim_matches('"') {
        "cosine" => Ok(DistanceMetric::Cosine),
        "euclidean" => Ok(DistanceMetric::Euclidean),
        "dot_product" => Ok(DistanceMetric::DotProduct),
        other => Err(VectorError::Index(format!(
            "unsupported distance metric '{other}'"
        ))),
    }
}

fn index_type_to_db(index_type: IndexType) -> &'static str {
    match index_type {
        IndexType::HNSW => "hnsw",
        IndexType::Flat => "flat",
    }
}

fn index_type_from_db(index_type: &str) -> VectorResult<IndexType> {
    match index_type.trim_matches('"') {
        "hnsw" => Ok(IndexType::HNSW),
        "flat" => Ok(IndexType::Flat),
        other => Err(VectorError::Index(format!(
            "unsupported index type '{other}'"
        ))),
    }
}