velesdb-core 5.1.0

High-performance vector database engine written in Rust
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
//! Sparse index WAL replay logic.
//!
//! Extracted from `persistence.rs` to reduce NLOC below the 500 threshold.

use super::inverted_index::SparseInvertedIndex;
use super::persistence_generation::committed_generation_for_wal;
use super::types::SparseVector;
use crate::error::{Error, Result};
use crate::storage::atomic_write::{atomic_write, sync_parent_directory};

use std::io::{BufWriter, Read, Write};
use std::path::Path;

const WAL_OP_UPSERT: u8 = 0x01;
const WAL_OP_DELETE: u8 = 0x02;
const WAL_HEADER_MAGIC: [u8; 4] = *b"VSWL";
const WAL_HEADER_VERSION: u8 = 1;
const WAL_HEADER_LEN: usize = 13;

// ---------------------------------------------------------------------------
// Byte-parsing helpers
// ---------------------------------------------------------------------------

/// Reads a little-endian `u64` from `data[pos..pos+8]`.
#[inline]
pub(super) fn read_le_u64(data: &[u8], pos: usize, context: &str) -> Result<u64> {
    data[pos..pos + 8]
        .try_into()
        .map(u64::from_le_bytes)
        .map_err(|_| Error::SparseIndexError(format!("{context} at offset {pos}")))
}

/// Reads a little-endian `u32` from `data[pos..pos+4]`.
#[inline]
pub(super) fn read_le_u32(data: &[u8], pos: usize, context: &str) -> Result<u32> {
    data[pos..pos + 4]
        .try_into()
        .map(u32::from_le_bytes)
        .map_err(|_| Error::SparseIndexError(format!("{context} at offset {pos}")))
}

/// Reads a little-endian `f32` from `data[pos..pos+4]`.
#[inline]
pub(super) fn read_le_f32(data: &[u8], pos: usize, context: &str) -> Result<f32> {
    data[pos..pos + 4]
        .try_into()
        .map(f32::from_le_bytes)
        .map_err(|_| Error::SparseIndexError(format!("{context} at offset {pos}")))
}

// ---------------------------------------------------------------------------
// WAL write operations
// ---------------------------------------------------------------------------

/// Appends an upsert entry to the sparse WAL.
///
/// # Errors
///
/// Returns an error if the WAL file cannot be opened or written.
pub fn wal_append_upsert(wal_path: &Path, point_id: u64, vector: &SparseVector) -> Result<()> {
    #[allow(clippy::cast_possible_truncation)] // nnz bounded by sparse vector dimension count
    let nnz = vector.nnz() as u32;
    let total_len = compute_upsert_entry_len(nnz)?;

    let mut w = open_wal_writer(wal_path)?;
    write_upsert_header(&mut w, total_len, point_id, nnz)?;
    write_term_value_pairs(&mut w, &vector.indices, &vector.values)?;
    flush_wal(&mut w, wal_path)
}

/// Writes the upsert WAL entry header (length prefix, opcode, point ID, nnz).
fn write_upsert_header(
    w: &mut BufWriter<std::fs::File>,
    total_len: u32,
    point_id: u64,
    nnz: u32,
) -> Result<()> {
    wal_write(w, &total_len.to_le_bytes())?;
    wal_write(w, &[WAL_OP_UPSERT])?;
    wal_write(w, &point_id.to_le_bytes())?;
    wal_write(w, &nnz.to_le_bytes())
}

/// Writes sparse vector term-value pairs to the WAL.
fn write_term_value_pairs(
    w: &mut BufWriter<std::fs::File>,
    indices: &[u32],
    values: &[f32],
) -> Result<()> {
    for (&idx, &val) in indices.iter().zip(values.iter()) {
        wal_write(w, &idx.to_le_bytes())?;
        wal_write(w, &val.to_le_bytes())?;
    }
    Ok(())
}

/// Flushes AND fsyncs the WAL writer, mapping I/O errors to `SparseIndexError`.
///
/// A sparse upsert/delete is acknowledged only after this returns `Ok`, and the
/// sparse vectors live nowhere else — a lost WAL entry is not rebuilt from any
/// snapshot or payload. So the acked append path must be durable against power
/// loss, exactly like the BM25 and edge WALs: this delegates to the shared
/// `wal_framing::flush_wal` (a single `flush` + `sync_all`) rather than a
/// buffer-only `flush`, which left acknowledged writes in the page cache. The
/// framing helper's `Error::Index` is remapped to this module's error contract.
fn flush_wal(w: &mut BufWriter<std::fs::File>, wal_path: &Path) -> Result<()> {
    crate::index::wal_framing::flush_wal(w, wal_path, "sparse WAL")
        .map_err(|e| Error::SparseIndexError(format!("WAL flush/fsync failed: {e}")))
}

/// Computes the total byte length of an upsert WAL entry using checked arithmetic.
fn compute_upsert_entry_len(nnz: u32) -> Result<u32> {
    nnz.checked_mul(8)
        .and_then(|pairs_len| {
            1u32.checked_add(8)
                .and_then(|h| h.checked_add(4))
                .and_then(|h| h.checked_add(pairs_len))
        })
        .ok_or_else(|| {
            Error::SparseIndexError(format!(
                "WAL entry too large: nnz={nnz} would overflow u32 length prefix"
            ))
        })
}

/// Opens a WAL file for appending with buffered I/O.
fn open_wal_writer(wal_path: &Path) -> Result<BufWriter<std::fs::File>> {
    if let Some(generation) = committed_generation_for_wal(wal_path)? {
        align_wal_generation(wal_path, generation)?;
    }
    let file = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(wal_path)
        .map_err(|e| Error::SparseIndexError(format!("WAL open failed: {e}")))?;
    Ok(BufWriter::new(file))
}

/// Prevents post-compaction writes from extending a stale WAL generation.
fn align_wal_generation(wal_path: &Path, expected: u64) -> Result<()> {
    if read_wal_generation(wal_path)? != Some(expected) {
        reset_wal(wal_path, expected)?;
    }
    Ok(())
}

fn read_wal_generation(wal_path: &Path) -> Result<Option<u64>> {
    let file = match std::fs::File::open(wal_path) {
        Ok(file) => file,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(error) => {
            return Err(Error::SparseIndexError(format!(
                "WAL generation read open failed: {error}"
            )))
        }
    };
    let mut bytes = Vec::with_capacity(WAL_HEADER_LEN);
    file.take(WAL_HEADER_LEN as u64)
        .read_to_end(&mut bytes)
        .map_err(|error| Error::SparseIndexError(format!("WAL generation read failed: {error}")))?;
    read_wal_header(&bytes).map(|header| header.map(|(generation, _)| generation))
}

/// Writes bytes to a WAL writer, mapping I/O errors to `SparseIndexError`.
fn wal_write(w: &mut BufWriter<std::fs::File>, bytes: &[u8]) -> Result<()> {
    w.write_all(bytes)
        .map_err(|e| Error::SparseIndexError(format!("WAL write failed: {e}")))
}

/// Appends a delete entry to the sparse WAL.
///
/// # Errors
///
/// Returns an error if the WAL file cannot be opened or written.
pub fn wal_append_delete(wal_path: &Path, point_id: u64) -> Result<()> {
    let total_len: u32 = 1 + 8;

    let mut w = open_wal_writer(wal_path)?;
    wal_write(&mut w, &total_len.to_le_bytes())?;
    wal_write(&mut w, &[WAL_OP_DELETE])?;
    wal_write(&mut w, &point_id.to_le_bytes())?;
    flush_wal(&mut w, wal_path)
}

// ---------------------------------------------------------------------------
// WAL replay
// ---------------------------------------------------------------------------

/// Replays a sparse WAL into the given index. Returns the number of entries replayed.
///
/// # Errors
///
/// Returns an error if the WAL file cannot be read or byte sequences are corrupt.
pub fn wal_replay(wal_path: &Path, index: &SparseInvertedIndex) -> Result<u64> {
    let data = read_wal_file(wal_path)?;
    let Some(data) = data else {
        return Ok(0);
    };
    let start = read_wal_header(&data)?.map_or(0, |(_, offset)| offset);
    replay_wal_bytes(&data, start, index)
}

pub(super) fn wal_replay_for_generation(
    wal_path: &Path,
    index: &SparseInvertedIndex,
    expected_generation: Option<u64>,
) -> Result<u64> {
    let Some(data) = read_wal_file(wal_path)? else {
        return Ok(0);
    };
    let header = read_wal_header(&data)?;
    let Some(expected) = expected_generation else {
        if header.is_some() {
            return Err(Error::SparseIndexError(
                "sparse WAL has a generation header but no snapshot manifest".to_string(),
            ));
        }
        return replay_wal_bytes(&data, 0, index);
    };
    match header {
        Some((generation, offset)) if generation == expected => {
            replay_wal_bytes(&data, offset, index)
        }
        Some(_) | None => Ok(0),
    }
}

pub(super) fn reset_wal(wal_path: &Path, generation: u64) -> Result<()> {
    atomic_write(wal_path, &wal_header(generation))
        .map_err(|e| Error::SparseIndexError(format!("sparse WAL durable reset: {e}")))
}

pub(super) fn sync_wal(wal_path: &Path) -> Result<()> {
    let file = match std::fs::OpenOptions::new()
        .read(true)
        .write(true)
        .open(wal_path)
    {
        Ok(file) => file,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
        Err(error) => {
            return Err(Error::SparseIndexError(format!(
                "compact WAL sync open: {error}"
            )))
        }
    };
    file.sync_all()
        .and_then(|()| sync_parent_directory(wal_path))
        .map_err(|error| Error::SparseIndexError(format!("compact WAL sync: {error}")))
}

fn replay_wal_bytes(data: &[u8], mut pos: usize, index: &SparseInvertedIndex) -> Result<u64> {
    let mut count = 0_u64;

    while pos < data.len() {
        let Some((body_start, total_len)) = read_wal_entry_header(data, pos) else {
            break;
        };
        pos += 4;

        if pos + total_len > data.len() {
            tracing::warn!(
                "Sparse WAL truncated at offset {body_start}: declared {total_len} bytes but only {} remain",
                data.len() - pos
            );
            break;
        }

        let op = data[pos];
        pos += 1;

        let advanced = replay_single_entry(data, op, pos, body_start, total_len, index)?;
        if let Some((new_pos, counted)) = advanced {
            pos = new_pos;
            count += counted;
        } else {
            break;
        }

        advance_past_entry(&mut pos, body_start + total_len);
    }

    Ok(count)
}

fn wal_header(generation: u64) -> [u8; WAL_HEADER_LEN] {
    let mut header = [0_u8; WAL_HEADER_LEN];
    header[..4].copy_from_slice(&WAL_HEADER_MAGIC);
    header[4] = WAL_HEADER_VERSION;
    header[5..].copy_from_slice(&generation.to_le_bytes());
    header
}

fn read_wal_header(data: &[u8]) -> Result<Option<(u64, usize)>> {
    if !data.starts_with(&WAL_HEADER_MAGIC) {
        return Ok(None);
    }
    if data.len() < WAL_HEADER_LEN {
        return Err(Error::SparseIndexError(
            "sparse WAL generation header is truncated".to_string(),
        ));
    }
    if data[4] != WAL_HEADER_VERSION {
        return Err(Error::SparseIndexError(format!(
            "unsupported sparse WAL header version: {}",
            data[4]
        )));
    }
    let generation = read_le_u64(data, 5, "sparse WAL generation bytes")?;
    if generation == 0 {
        return Err(Error::SparseIndexError(
            "sparse WAL generation must be non-zero".to_string(),
        ));
    }
    Ok(Some((generation, WAL_HEADER_LEN)))
}

/// Reads the WAL file, returning `None` for missing files.
fn read_wal_file(wal_path: &Path) -> Result<Option<Vec<u8>>> {
    match std::fs::read(wal_path) {
        Ok(d) => Ok(Some(d)),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(e) => Err(Error::SparseIndexError(format!("WAL read failed: {e}"))),
    }
}

/// Replays a single WAL entry by opcode.
fn replay_single_entry(
    data: &[u8],
    op: u8,
    pos: usize,
    body_start: usize,
    total_len: usize,
    index: &SparseInvertedIndex,
) -> Result<Option<(usize, u64)>> {
    match op {
        WAL_OP_UPSERT => {
            let Some(new_pos) = replay_upsert_entry(data, pos, body_start, total_len, index)?
            else {
                return Ok(None);
            };
            Ok(Some((new_pos, 1)))
        }
        WAL_OP_DELETE => {
            let point_id = read_le_u64(data, pos, "WAL entry corrupted: bad point_id bytes")?;
            index.delete(point_id);
            Ok(Some((pos + 8, 1)))
        }
        unknown => {
            tracing::warn!("Sparse WAL unknown op 0x{unknown:02x} at offset {body_start}");
            Ok(Some((body_start + total_len, 0)))
        }
    }
}

/// Advances `pos` to at least `expected_end`.
fn advance_past_entry(pos: &mut usize, expected_end: usize) {
    if *pos < expected_end {
        *pos = expected_end;
    }
}

/// Reads the WAL entry length prefix.
fn read_wal_entry_header(data: &[u8], pos: usize) -> Option<(usize, usize)> {
    if pos + 4 > data.len() {
        tracing::warn!("Sparse WAL truncated at offset {pos}: not enough bytes for length prefix");
        return None;
    }
    let total_len =
        read_le_u32(data, pos, "WAL entry corrupted: bad length-prefix bytes").ok()? as usize;
    Some((pos + 4, total_len))
}

/// Replays a single upsert WAL entry.
fn replay_upsert_entry(
    data: &[u8],
    mut pos: usize,
    body_start: usize,
    total_len: usize,
    index: &SparseInvertedIndex,
) -> Result<Option<usize>> {
    if total_len < 1 + 8 + 4 {
        tracing::warn!("Sparse WAL upsert entry too short at offset {body_start}");
        return Ok(None);
    }
    let point_id = read_le_u64(data, pos, "WAL entry corrupted: bad point_id bytes")?;
    pos += 8;
    let nnz = read_le_u32(data, pos, "WAL entry corrupted: bad nnz bytes")? as usize;
    pos += 4;

    // Untrusted `nnz`: each pair is 8 bytes (u32 term_id + f32 weight). Compute
    // in u64 with checked/saturating arithmetic so a crafted `nnz` cannot wrap on
    // a 32-bit target and defeat the truncation guard below. `read_term_weight_pairs`
    // then bounds its own `Vec::with_capacity` against the validated `nnz`.
    let Some(payload_bytes) = (nnz as u64).checked_mul(8) else {
        tracing::warn!("Sparse WAL upsert entry nnz overflow at offset {body_start}");
        return Ok(None);
    };
    let entry_end = (body_start as u64).saturating_add(total_len as u64);
    if entry_end < (pos as u64).saturating_add(payload_bytes) {
        tracing::warn!("Sparse WAL upsert entry truncated at offset {body_start}");
        return Ok(None);
    }

    let pairs = read_term_weight_pairs(data, &mut pos, nnz)?;
    let vector = SparseVector::new(pairs);
    index.insert(point_id, &vector);
    Ok(Some(pos))
}

/// Reads `nnz` (`term_id`, weight) pairs from the data buffer.
fn read_term_weight_pairs(data: &[u8], pos: &mut usize, nnz: usize) -> Result<Vec<(u32, f32)>> {
    // Defensive cap: a pair is 8 bytes, so the data buffer can hold at most
    // `data.len() / 8` pairs. Clamp the pre-allocation so an untrusted `nnz`
    // cannot pre-reserve far beyond what the buffer could ever supply.
    let mut pairs = Vec::with_capacity(nnz.min(data.len() / 8));
    for _ in 0..nnz {
        let idx = read_le_u32(data, *pos, "WAL entry corrupted: bad term-index bytes")?;
        *pos += 4;
        let val = read_le_f32(data, *pos, "WAL entry corrupted: bad weight bytes")?;
        *pos += 4;
        pairs.push((idx, val));
    }
    Ok(pairs)
}