a3s-vec 0.1.8

Native Rust in-process vector database with zvec-compatible capabilities
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
//! Persistence primitives used by the collection handle.

mod derived_file;
mod diskann_file;
mod fault;
mod index_cache;
mod lock;
pub mod manifest;
pub mod snapshot;
pub mod wal;

use crate::config::{ConfigBuilder, Durability};
use crate::doc::Doc;
use crate::error::{Error, Result};
use crate::schema::CollectionSchema;
use crate::storage_ceilings::StorageCeilings;
use fault::FaultInjector;
use lock::CollectionLock;
use manifest::Manifest;
use std::fs;
use std::path::{Path, PathBuf};

pub(crate) use derived_file::{PositionedFile, RandomAccessReader};
#[cfg(test)]
pub(crate) use fault::StallGate;
pub use wal::WalOperation;

/// Open storage state. The lock is held for the lifetime of the collection.
#[derive(Debug)]
pub(crate) struct StorageHandle {
    pub root: PathBuf,
    pub manifest: Manifest,
    pub lock: CollectionLock,
    pub ceilings: StorageCeilings,
    faults: FaultInjector,
}

impl StorageHandle {
    pub fn create(
        path: &Path,
        schema: &CollectionSchema,
        read_only: bool,
        ceilings: StorageCeilings,
    ) -> Result<Self> {
        if read_only {
            return Err(Error::permission_denied(
                "cannot create a collection through a read-only handle",
            ));
        }
        schema.validate()?;
        if path.exists() && (path.join("manifest.json").exists() || path.join("segments").exists())
        {
            return Err(Error::already_exists(format!(
                "collection already exists at {}",
                path.display()
            )));
        }
        fs::create_dir_all(path)
            .map_err(|e| Error::internal(format!("create collection path: {e}")))?;
        let lock = CollectionLock::acquire(path, false)?;
        let faults = FaultInjector::default();
        let mut manifest = Manifest::new(schema.name.clone(), schema.digest());
        manifest.generation = 1;
        manifest.docs_checksum = snapshot::write_with_faults(
            path,
            schema,
            &[],
            manifest.generation,
            0,
            true,
            &faults,
            ceilings.max_snapshot_bytes(),
        )?;
        manifest.wal_active_seq = 1;
        manifest.wal_checkpoint_seq = 0;
        manifest.revision = 0;
        manifest.checkpoint_revision = 0;
        manifest.wal_ops_since_checkpoint = 0;
        manifest.wal_bytes_since_checkpoint = 0;
        manifest::write_with_faults(path, &manifest, true, &faults)?;
        Ok(Self {
            root: path.to_path_buf(),
            manifest,
            lock,
            ceilings,
            faults,
        })
    }

    pub fn open(
        path: &Path,
        read_only: bool,
        ceilings: StorageCeilings,
    ) -> Result<(Self, CollectionSchema, Vec<Doc>)> {
        if !path.exists() {
            return Err(Error::not_found(format!(
                "collection path does not exist: {}",
                path.display()
            )));
        }
        let lock = CollectionLock::acquire(path, read_only)?;
        let manifest = manifest::read(path)?;
        let (mut schema, mut docs) =
            snapshot::read(path, &manifest, ceilings.max_snapshot_bytes())?;
        let records = wal::replay(
            path,
            manifest.wal_checkpoint_seq.saturating_add(1),
            manifest.wal_active_seq,
            manifest.wal_bytes_since_checkpoint,
            ceilings.max_wal_replay_bytes(),
        )?;
        let mut recovered_revision = manifest.checkpoint_revision;
        for record in records {
            let expected_revision = recovered_revision
                .checked_add(1)
                .ok_or_else(|| Error::resource_exhausted("collection revision overflow"))?;
            if record.revision != expected_revision {
                return Err(Error::internal(format!(
                    "non-monotonic WAL revision: expected {expected_revision}, got {}",
                    record.revision
                )));
            }
            apply_operation(&mut docs, &mut schema, record.operation)?;
            recovered_revision = record.revision;
        }
        if recovered_revision != manifest.revision {
            return Err(Error::internal(format!(
                "manifest revision {} is not recoverable from checkpoint revision {} and committed WAL",
                manifest.revision, manifest.checkpoint_revision
            )));
        }
        schema.validate()?;
        Ok((
            Self {
                root: path.to_path_buf(),
                manifest,
                lock,
                ceilings,
                faults: FaultInjector::default(),
            },
            schema,
            docs,
        ))
    }

    pub fn append(
        &mut self,
        revision: u64,
        operation: WalOperation,
        config: &ConfigBuilder,
    ) -> Result<u64> {
        if !self.lock.is_exclusive() {
            return Err(Error::permission_denied("collection is read-only"));
        }
        let expected_revision = self
            .manifest
            .revision
            .checked_add(1)
            .ok_or_else(|| Error::resource_exhausted("collection revision overflow"))?;
        if revision != expected_revision {
            return Err(Error::failed_precondition(format!(
                "WAL revision must advance from {} to {expected_revision}, got {revision}",
                self.manifest.revision
            )));
        }
        let record = wal::WalRecord::new(revision, operation)?;
        let sync = matches!(config.durability, Durability::Always);
        let bytes = wal::append_with_faults(
            &self.root,
            self.manifest.wal_active_seq,
            self.manifest.wal_bytes_since_checkpoint,
            &record,
            sync,
            &self.faults,
        )?;
        let mut next_manifest = self.manifest.clone();
        next_manifest.revision = revision;
        next_manifest.wal_ops_since_checkpoint = next_manifest
            .wal_ops_since_checkpoint
            .checked_add(1)
            .ok_or_else(|| Error::resource_exhausted("WAL operation count overflow"))?;
        next_manifest.wal_bytes_since_checkpoint = next_manifest
            .wal_bytes_since_checkpoint
            .checked_add(bytes)
            .ok_or_else(|| Error::resource_exhausted("WAL byte count overflow"))?;
        manifest::write_with_faults(&self.root, &next_manifest, sync, &self.faults)?;
        self.manifest = next_manifest;
        Ok(bytes)
    }

    pub fn checkpoint(
        &mut self,
        schema: &CollectionSchema,
        docs: &(impl snapshot::SnapshotDocs + ?Sized),
        revision: u64,
        sync: bool,
    ) -> Result<()> {
        if !self.lock.is_exclusive() {
            return Err(Error::permission_denied("collection is read-only"));
        }
        if revision < self.manifest.revision {
            return Err(Error::failed_precondition(format!(
                "checkpoint revision {revision} precedes committed revision {}",
                self.manifest.revision
            )));
        }
        let generation = self
            .manifest
            .generation
            .checked_add(1)
            .ok_or_else(|| Error::resource_exhausted("snapshot generation overflow"))?;
        let Some(checksum) =
            self.next_snapshot_checksum(schema, docs, generation, revision, sync)?
        else {
            return Ok(());
        };
        let mut next_manifest = self.manifest.clone();
        next_manifest.format_version = manifest::FORMAT_VERSION;
        next_manifest.collection_name.clone_from(&schema.name);
        next_manifest.schema_digest = schema.digest();
        next_manifest.generation = generation;
        next_manifest.revision = revision;
        next_manifest.checkpoint_revision = revision;
        next_manifest.docs_checksum = checksum;
        next_manifest.wal_checkpoint_seq = self.manifest.wal_active_seq;
        next_manifest.wal_active_seq = self
            .manifest
            .wal_active_seq
            .checked_add(1)
            .ok_or_else(|| Error::resource_exhausted("WAL sequence overflow"))?;
        next_manifest.wal_ops_since_checkpoint = 0;
        next_manifest.wal_bytes_since_checkpoint = 0;
        manifest::write_with_faults(&self.root, &next_manifest, sync, &self.faults)?;
        self.manifest = next_manifest;

        // The manifest is already committed. Old generations are harmless,
        // so cleanup is best-effort: reporting a prune error here would make
        // callers believe the transaction failed after it actually committed.
        // If WAL cleanup is interrupted, stop this maintenance pass so the
        // remaining files match an actual crash at that boundary. Both prune
        // operations are optional after manifest publication and can be
        // retried by a later checkpoint.
        if wal::prune_with_faults(&self.root, self.manifest.wal_checkpoint_seq, &self.faults)
            .is_ok()
        {
            let _snapshot_prune = snapshot::prune_with_faults(
                &self.root,
                self.manifest.generation,
                self.manifest.docs_checksum,
                self.ceilings.max_snapshot_bytes(),
                &self.faults,
            );
        }
        Ok(())
    }

    /// `Ok(None)` means the committed snapshot already matches this revision.
    fn next_snapshot_checksum(
        &self,
        schema: &CollectionSchema,
        docs: &(impl snapshot::SnapshotDocs + ?Sized),
        generation: u64,
        revision: u64,
        sync: bool,
    ) -> Result<Option<u32>> {
        // A schema change cannot be a document delta. Reading the previous
        // snapshot only to throw it away is the whole decode of every body.
        let previous = if self.manifest.schema_digest == schema.digest()
            && self.manifest.format_version >= manifest::FORMAT_VERSION
            && self.manifest.generation > 0
        {
            snapshot::read(
                &self.root,
                &self.manifest,
                self.ceilings.max_snapshot_bytes(),
            )
            .ok()
        } else {
            None
        };
        if let Some((previous_schema, previous_docs)) = previous.as_ref() {
            // A second flush of an already checkpointed revision has no new
            // bytes to publish. Leaving the existing snapshot in place keeps
            // one authoritative generation instead of an empty delta.
            if previous_schema.digest() == schema.digest()
                && self.manifest.revision == revision
                && self.manifest.wal_ops_since_checkpoint == 0
                && snapshot::documents_unchanged(previous_docs, docs)
            {
                return Ok(None);
            }
            if !previous_docs.is_empty() && previous_schema.digest() == schema.digest() {
                return Ok(Some(snapshot::write_delta_with_faults(
                    &self.root,
                    schema,
                    docs,
                    previous_docs,
                    generation,
                    revision,
                    self.manifest.generation,
                    self.manifest.docs_checksum,
                    sync,
                    &self.faults,
                    self.ceilings.max_snapshot_bytes(),
                )?));
            }
        }
        Ok(Some(snapshot::write_with_faults(
            &self.root,
            schema,
            docs,
            generation,
            revision,
            sync,
            &self.faults,
            self.ceilings.max_snapshot_bytes(),
        )?))
    }

    pub fn should_checkpoint(&self, config: &ConfigBuilder) -> bool {
        config
            .wal_max_ops
            .is_some_and(|v| self.manifest.wal_ops_since_checkpoint >= v)
            || config
                .wal_max_bytes
                .is_some_and(|v| self.manifest.wal_bytes_since_checkpoint >= v)
    }

    pub(crate) fn read_index_cache(&self) -> Result<Option<Vec<u8>>> {
        index_cache::read(&self.root, self.ceilings.max_index_cache_file_bytes())
    }

    pub(crate) fn write_index_cache(&self, bytes: &[u8], sync: bool) -> Result<()> {
        if !self.lock.is_exclusive() {
            return Err(Error::permission_denied(
                "cannot write an index cache through a read-only handle",
            ));
        }
        index_cache::write(
            &self.root,
            bytes,
            sync,
            self.ceilings.max_index_cache_file_bytes(),
        )
    }

    pub(crate) fn open_diskann_file(&self) -> Result<Option<PositionedFile>> {
        diskann_file::open(&self.root, self.ceilings.max_diskann_file_bytes())
    }

    pub(crate) fn write_diskann_file(&self, bytes: &[u8], sync: bool) -> Result<()> {
        if !self.lock.is_exclusive() {
            return Err(Error::permission_denied(
                "cannot write a DiskANN sidecar through a read-only handle",
            ));
        }
        self.faults.hit(fault::FaultPoint::DiskannWritten)?;
        diskann_file::write(
            &self.root,
            bytes,
            sync,
            self.ceilings.max_diskann_file_bytes(),
        )
    }

    pub(crate) fn index_cache_identity(&self) -> String {
        let manifest = &self.manifest;
        format!(
            "v2:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}",
            manifest.format_version,
            manifest.collection_name.len(),
            manifest.collection_name,
            manifest.schema_digest,
            manifest.generation,
            manifest.revision,
            manifest.checkpoint_revision,
            manifest.wal_active_seq,
            manifest.wal_checkpoint_seq,
            manifest.wal_ops_since_checkpoint,
            manifest.wal_bytes_since_checkpoint,
            manifest.docs_checksum,
        )
    }

    #[cfg(test)]
    fn arm_fault(&self, point: fault::FaultPoint) {
        self.faults.arm(point);
    }

    #[cfg(test)]
    pub(crate) fn arm_wal_sync_stall(&self) -> StallGate {
        self.faults.arm_stall(fault::FaultPoint::WalSynced)
    }

    #[cfg(test)]
    pub(crate) fn arm_diskann_write_fault(&self) {
        self.faults.arm(fault::FaultPoint::DiskannWritten);
    }

    #[cfg(test)]
    fn fault_fired(&self, point: fault::FaultPoint) -> bool {
        self.faults.fired(point)
    }

    #[cfg(test)]
    pub(crate) fn diskann_write_fault_fired(&self) -> bool {
        self.fault_fired(fault::FaultPoint::DiskannWritten)
    }
}

fn apply_operation(
    docs: &mut Vec<Doc>,
    schema: &mut CollectionSchema,
    operation: WalOperation,
) -> Result<()> {
    match operation {
        WalOperation::Insert { docs: incoming } => {
            for doc in incoming {
                if let Some(pk) = doc.get_pk() {
                    if let Some(existing) = docs.iter_mut().find(|d| d.get_pk() == Some(pk)) {
                        *existing = doc;
                    } else {
                        docs.push(doc);
                    }
                }
            }
        }
        WalOperation::Upsert { docs: incoming } => {
            for doc in incoming {
                let pk = doc
                    .get_pk()
                    .ok_or_else(|| {
                        Error::invalid_argument("WAL upsert document has no primary key")
                    })?
                    .to_string();
                if let Some(existing) = docs.iter_mut().find(|d| d.get_pk() == Some(pk.as_str())) {
                    *existing = doc;
                } else {
                    docs.push(doc);
                }
            }
        }
        WalOperation::Update { docs: patches } => {
            for patch in patches {
                let pk = patch
                    .get_pk()
                    .ok_or_else(|| {
                        Error::invalid_argument("WAL update document has no primary key")
                    })?
                    .to_string();
                let existing = docs
                    .iter_mut()
                    .find(|d| d.get_pk() == Some(pk.as_str()))
                    .ok_or_else(|| {
                        Error::not_found(format!("document '{pk}' not found during WAL replay"))
                    })?;
                for (name, value) in patch.fields() {
                    existing.set_field_value(name, value.clone())?;
                }
                for (name, value) in patch.vectors() {
                    existing.set_vector_value(name, value.clone())?;
                }
                if patch.get_score() != 0.0 {
                    existing.set_score(patch.get_score())?;
                }
            }
        }
        WalOperation::Delete { ids } => {
            docs.retain(|doc| !doc.get_pk().is_some_and(|pk| ids.iter().any(|id| id == pk)));
        }
        WalOperation::Schema {
            schema: next,
            docs: next_docs,
        } => {
            next.validate()?;
            if next.name != schema.name {
                return Err(Error::internal("WAL schema name mismatch"));
            }
            *schema = next;
            *docs = next_docs;
        }
        WalOperation::SchemaOnly { schema: next } => {
            next.validate()?;
            if next.name != schema.name {
                return Err(Error::internal("WAL schema name mismatch"));
            }
            *schema = next;
        }
    }
    Ok(())
}

#[cfg(test)]
mod fault_tests;
#[cfg(test)]
mod recovery_fuzz_tests;
#[cfg(test)]
mod test_support;
#[cfg(test)]
mod tests;