laurus 0.10.0

Unified search library for lexical, vector, and semantic retrieval
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
//! Integration tests for crash-safe atomic writes of Flat and IVF index
//! files (Issue #889 PR-3, matching HNSW's existing #784/#786 pattern —
//! see `vector_hnsw_atomic_write_test.rs`).
//!
//! `FlatIndexWriter::write()` and `IvfIndexWriter::write()` now write to a
//! `.flat.tmp`/`.ivf.tmp` file and atomically `rename_file` it into place,
//! instead of writing `storage.create_output(&file_name)` directly. A crash
//! between writing the temp file and the rename therefore leaves the
//! previously committed file untouched.
//!
//! Unlike HNSW's segmented layout, Flat and IVF are still monolithic at
//! this point in the #889 campaign (segmentation for them lands in PR-4/
//! PR-6) — there is no manifest and no orphan sweep, so these tests only
//! assert that a stray `.tmp` file cannot corrupt or replace the
//! previously committed data, not that it gets cleaned up.

use std::cell::Cell;
use std::io::{Seek, SeekFrom, Write};
use std::sync::{Arc, Mutex};

use laurus::Result;
use laurus::storage::memory::MemoryStorage;
use laurus::storage::{FileMetadata, Storage, StorageInput, StorageOutput};
use laurus::vector::core::distance::DistanceMetric;
use laurus::vector::index::ivf::writer::IvfIndexWriter;
use laurus::vector::{
    FlatIndexConfig, FlatIndexWriter, IvfIndexConfig, Vector, VectorIndexWriter,
    VectorIndexWriterConfig,
};

/// A [`Storage`] decorator whose next `create_output` call returns a writer
/// that fails after a configured number of `write` calls — simulating a
/// crash partway through `write()`'s sequence of `write_all` calls, the way
/// a real process kill or disk-full error would.
#[derive(Debug)]
struct FlakyStorage {
    inner: Arc<dyn Storage>,
    fail_after_writes: Mutex<Option<usize>>,
}

impl FlakyStorage {
    fn new(inner: Arc<dyn Storage>) -> Self {
        Self {
            inner,
            fail_after_writes: Mutex::new(None),
        }
    }

    /// Arm the *next* `create_output` call to fail on its `(n+1)`-th write.
    fn arm_next_write_failure(&self, after_n_writes: usize) {
        *self.fail_after_writes.lock().unwrap() = Some(after_n_writes);
    }
}

/// `StorageOutput` need not be `Sync` (only `Send`), so a plain `Cell` is
/// fine here even though `FlakyStorage` itself must be `Sync`.
#[derive(Debug)]
struct FlakyOutput {
    inner: Box<dyn StorageOutput>,
    remaining: Cell<usize>,
}

impl Write for FlakyOutput {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let remaining = self.remaining.get();
        if remaining == 0 {
            return Err(std::io::Error::other("simulated crash mid-write"));
        }
        self.remaining.set(remaining - 1);
        self.inner.write(buf)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.inner.flush()
    }
}

impl Seek for FlakyOutput {
    fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
        self.inner.seek(pos)
    }
}

impl StorageOutput for FlakyOutput {
    fn flush_and_sync(&mut self) -> Result<()> {
        self.inner.flush_and_sync()
    }
    fn position(&self) -> Result<u64> {
        self.inner.position()
    }
    fn close(&mut self) -> Result<()> {
        self.inner.close()
    }
}

impl Storage for FlakyStorage {
    fn open_input(&self, name: &str) -> Result<Box<dyn StorageInput>> {
        self.inner.open_input(name)
    }

    fn create_output(&self, name: &str) -> Result<Box<dyn StorageOutput>> {
        let inner = self.inner.create_output(name)?;
        if let Some(n) = self.fail_after_writes.lock().unwrap().take() {
            Ok(Box::new(FlakyOutput {
                inner,
                remaining: Cell::new(n),
            }))
        } else {
            Ok(inner)
        }
    }

    fn create_output_append(&self, name: &str) -> Result<Box<dyn StorageOutput>> {
        self.inner.create_output_append(name)
    }

    fn file_exists(&self, name: &str) -> bool {
        self.inner.file_exists(name)
    }

    fn delete_file(&self, name: &str) -> Result<()> {
        self.inner.delete_file(name)
    }

    fn list_files(&self) -> Result<Vec<String>> {
        self.inner.list_files()
    }

    fn file_size(&self, name: &str) -> Result<u64> {
        self.inner.file_size(name)
    }

    fn metadata(&self, name: &str) -> Result<FileMetadata> {
        self.inner.metadata(name)
    }

    fn rename_file(&self, old_name: &str, new_name: &str) -> Result<()> {
        self.inner.rename_file(old_name, new_name)
    }

    fn create_temp_output(&self, prefix: &str) -> Result<(String, Box<dyn StorageOutput>)> {
        self.inner.create_temp_output(prefix)
    }

    fn sync(&self) -> Result<()> {
        self.inner.sync()
    }

    fn close(&mut self) -> Result<()> {
        Ok(())
    }
}

const DIM: usize = 4;
const N: u64 = 20;

fn doc_vec(i: u64) -> Vector {
    let t = i as f32 * 0.1;
    Vector::new(vec![t.cos(), t.sin(), (t * 2.0).cos(), (t * 2.0).sin()])
}

fn build_and_commit_flat(storage: Arc<dyn Storage>, name: &str) {
    let config = FlatIndexConfig {
        dimension: DIM,
        distance_metric: DistanceMetric::Cosine,
        ..FlatIndexConfig::default()
    };
    let mut writer =
        FlatIndexWriter::with_storage(config, VectorIndexWriterConfig::default(), name, storage)
            .unwrap();
    let vectors: Vec<_> = (0..N).map(|i| (i, "v".to_string(), doc_vec(i))).collect();
    writer.add_vectors(vectors).unwrap();
    writer.finalize().unwrap();
    writer.write().unwrap();
}

fn build_and_commit_ivf(storage: Arc<dyn Storage>, name: &str) {
    let config = IvfIndexConfig {
        dimension: DIM,
        distance_metric: DistanceMetric::Cosine,
        n_clusters: 2,
        n_probe: 2,
        normalize_vectors: false,
        ..IvfIndexConfig::default()
    };
    let mut writer =
        IvfIndexWriter::with_storage(config, VectorIndexWriterConfig::default(), name, storage)
            .unwrap();
    let vectors: Vec<_> = (0..N).map(|i| (i, "v".to_string(), doc_vec(i))).collect();
    writer.add_vectors(vectors).unwrap();
    writer.finalize().unwrap();
    writer.write().unwrap();
}

fn read_all_flat(storage: Arc<dyn Storage>, name: &str) -> Vec<u64> {
    use laurus::vector::reader::VectorIndexReader;
    let reader = laurus::vector::index::flat::reader::FlatVectorIndexReader::load(
        storage,
        name,
        DistanceMetric::Cosine,
    )
    .unwrap();
    let mut ids: Vec<u64> = reader
        .vector_ids()
        .unwrap()
        .into_iter()
        .map(|(d, _)| d)
        .collect();
    ids.sort_unstable();
    ids
}

fn read_all_ivf(storage: Arc<dyn Storage>, name: &str) -> Vec<u64> {
    use laurus::vector::reader::VectorIndexReader;
    let reader = laurus::vector::index::ivf::reader::IvfIndexReader::load(
        storage,
        name,
        DistanceMetric::Cosine,
    )
    .unwrap();
    let mut ids: Vec<u64> = reader
        .vector_ids()
        .unwrap()
        .into_iter()
        .map(|(d, _)| d)
        .collect();
    ids.sort_unstable();
    ids
}

#[test]
fn flat_commit_leaves_no_temp_file() {
    let storage: Arc<dyn Storage> = Arc::new(MemoryStorage::default());
    build_and_commit_flat(storage.clone(), "flat_atomic");

    assert!(
        storage.file_exists("flat_atomic.flat"),
        "committed file must exist"
    );
    let temps: Vec<String> = storage
        .list_files()
        .unwrap()
        .into_iter()
        .filter(|f| f.ends_with(".tmp"))
        .collect();
    assert!(
        temps.is_empty(),
        "a successful write must not leave any .tmp behind, got {temps:?}"
    );
    assert_eq!(read_all_flat(storage, "flat_atomic").len(), N as usize);
}

#[test]
fn ivf_commit_leaves_no_temp_file() {
    let storage: Arc<dyn Storage> = Arc::new(MemoryStorage::default());
    build_and_commit_ivf(storage.clone(), "ivf_atomic");

    assert!(
        storage.file_exists("ivf_atomic.ivf"),
        "committed file must exist"
    );
    let temps: Vec<String> = storage
        .list_files()
        .unwrap()
        .into_iter()
        .filter(|f| f.ends_with(".tmp"))
        .collect();
    assert!(
        temps.is_empty(),
        "a successful write must not leave any .tmp behind, got {temps:?}"
    );
    assert_eq!(read_all_ivf(storage, "ivf_atomic").len(), N as usize);
}

/// The strongest proof of atomicity: a real interruption *during* the
/// second write's byte stream (not just a stray leftover `.tmp` file).
/// Under the pre-#889 code (`create_output(&file_name)` directly), this
/// early-returns via `?` and the writer's `Drop` still flushes/closes the
/// partially-written buffer, publishing truncated garbage over the
/// previously-committed file. Under the fix, only `{name}.flat.tmp` is
/// affected — `{name}.flat` is untouched until the final `rename_file`,
/// which never happens because `write()` returned an error first.
#[test]
fn flat_second_write_failure_does_not_corrupt_first_commit() {
    let base: Arc<dyn Storage> = Arc::new(MemoryStorage::default());
    build_and_commit_flat(base.clone(), "flat_atomic");
    let before = read_all_flat(base.clone(), "flat_atomic");
    assert_eq!(before.len(), N as usize);

    let flaky = Arc::new(FlakyStorage::new(base.clone()));
    flaky.arm_next_write_failure(5);
    let config = FlatIndexConfig {
        dimension: DIM,
        distance_metric: DistanceMetric::Cosine,
        ..FlatIndexConfig::default()
    };
    // Reload (picks up the 20 committed docs), add one more, then attempt a
    // second write that fails partway through.
    let mut writer = FlatIndexWriter::with_storage(
        config,
        VectorIndexWriterConfig::default(),
        "flat_atomic",
        flaky as Arc<dyn Storage>,
    )
    .unwrap();
    writer
        .add_vectors(vec![(N, "v".to_string(), doc_vec(N))])
        .unwrap();
    writer.finalize().unwrap();
    let write_result = writer.write();
    assert!(
        write_result.is_err(),
        "the simulated mid-write failure must propagate"
    );
    drop(writer);

    assert!(
        base.file_exists("flat_atomic.flat"),
        "the previously committed file must still exist"
    );
    let after = read_all_flat(base, "flat_atomic");
    assert_eq!(
        after, before,
        "a write failure partway through must leave the previously committed \
         data completely intact — not truncated or partially overwritten"
    );
}

/// IVF analogue of [`flat_second_write_failure_does_not_corrupt_first_commit`].
#[test]
fn ivf_second_write_failure_does_not_corrupt_first_commit() {
    let base: Arc<dyn Storage> = Arc::new(MemoryStorage::default());
    build_and_commit_ivf(base.clone(), "ivf_atomic");
    let before = read_all_ivf(base.clone(), "ivf_atomic");
    assert_eq!(before.len(), N as usize);

    let flaky = Arc::new(FlakyStorage::new(base.clone()));
    flaky.arm_next_write_failure(5);
    let config = IvfIndexConfig {
        dimension: DIM,
        distance_metric: DistanceMetric::Cosine,
        n_clusters: 2,
        n_probe: 2,
        normalize_vectors: false,
        ..IvfIndexConfig::default()
    };
    let mut writer = IvfIndexWriter::with_storage(
        config,
        VectorIndexWriterConfig::default(),
        "ivf_atomic",
        flaky as Arc<dyn Storage>,
    )
    .unwrap();
    writer
        .add_vectors(vec![(N, "v".to_string(), doc_vec(N))])
        .unwrap();
    writer.finalize().unwrap();
    let write_result = writer.write();
    assert!(
        write_result.is_err(),
        "the simulated mid-write failure must propagate"
    );
    drop(writer);

    assert!(
        base.file_exists("ivf_atomic.ivf"),
        "the previously committed file must still exist"
    );
    let after = read_all_ivf(base, "ivf_atomic");
    assert_eq!(
        after, before,
        "a write failure partway through must leave the previously committed \
         data completely intact — not truncated or partially overwritten"
    );
}

#[test]
fn flat_orphaned_temp_from_crashed_write_does_not_corrupt_committed_data() {
    let storage: Arc<dyn Storage> = Arc::new(MemoryStorage::default());
    build_and_commit_flat(storage.clone(), "flat_atomic");
    let before = read_all_flat(storage.clone(), "flat_atomic");
    assert_eq!(before.len(), N as usize);

    // Simulate a crash *during* a later write: the temp file was created
    // but the atomic rename never happened.
    {
        let mut out = storage.create_output("flat_atomic.flat.tmp").unwrap();
        out.write_all(b"partially-written-garbage-from-a-crashed-write")
            .unwrap();
        out.close().unwrap();
    }
    assert!(
        storage.file_exists("flat_atomic.flat"),
        "committed file still present"
    );

    let after = read_all_flat(storage, "flat_atomic");
    assert_eq!(
        after, before,
        "the committed data must survive an orphaned temp file from a crashed write"
    );
}

#[test]
fn ivf_orphaned_temp_from_crashed_write_does_not_corrupt_committed_data() {
    let storage: Arc<dyn Storage> = Arc::new(MemoryStorage::default());
    build_and_commit_ivf(storage.clone(), "ivf_atomic");
    let before = read_all_ivf(storage.clone(), "ivf_atomic");
    assert_eq!(before.len(), N as usize);

    {
        let mut out = storage.create_output("ivf_atomic.ivf.tmp").unwrap();
        out.write_all(b"partially-written-garbage-from-a-crashed-write")
            .unwrap();
        out.close().unwrap();
    }
    assert!(
        storage.file_exists("ivf_atomic.ivf"),
        "committed file still present"
    );

    let after = read_all_ivf(storage, "ivf_atomic");
    assert_eq!(
        after, before,
        "the committed data must survive an orphaned temp file from a crashed write"
    );
}