sqlitegraph 2.2.3

Embedded graph database with full ACID transactions, HNSW vector search, dual backend support, and comprehensive graph algorithms library
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
642
643
644
645
646
647
648
649
650
651
652
653
//! Varint (variable-length integer) encoding
//!
//! Varint encoding stores integers using a variable number of bytes.
//! Each byte uses 7 bits for data and 1 bit (MSB) as a continuation flag.
//!
//! # Encoding Format
//!
//! ```text
//! - For values < 128: 1 byte (MSB = 0)
//! - For values >= 128: Multiple bytes (MSB = 1 for all but last byte)
//!
//! Example:
//!   42     -> [0b00101010]           (1 byte)
//!   300    -> [0b10101100, 0b00000010] (2 bytes)
//!   16384  -> [0b10000000, 0b10000000, 0b00000001] (3 bytes)
//! ```
//!
//! # MSB Prefix Convention
//!
//! The Most Significant Bit (bit 7) indicates continuation:
//! - MSB = 1: More bytes follow
//! - MSB = 0: This is the last byte
//!
//! Lower 7 bits contain the actual data.

use crate::backend::native::NativeBackendError;
use crate::backend::native::NativeResult;

/// Maximum number of bytes needed to encode a u64 as varint
///
/// u64::MAX requires 10 bytes in varint encoding:
/// - 9 full bytes (7 bits each = 63 bits)
/// - 1 partial byte for remaining 1 bit
pub const MAX_VARINT_BYTES: usize = 10;

/// Maximum value that can be encoded in a single varint byte
pub const MAX_SINGLE_BYTE_VALUE: u64 = 0x7F; // 127

/// Mask for extracting 7-bit data from a byte
const DATA_MASK: u8 = 0x7F;

/// Continuation bit (MSB)
const CONTINUATION_BIT: u8 = 0x80;

/// Error type for varint decoding failures
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VarintError {
    /// Input buffer is empty
    EmptyInput,

    /// Input ends before varint is complete
    IncompleteData {
        expected_bytes: usize,
        actual_bytes: usize,
    },

    /// Varint exceeds maximum allowed length (10 bytes)
    TooLong { actual_length: usize },

    /// Malformed varint (last byte has continuation bit set)
    Malformed { position: usize },
}

impl std::fmt::Display for VarintError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::EmptyInput => write!(f, "Cannot decode varint from empty input"),
            Self::IncompleteData {
                expected_bytes,
                actual_bytes,
            } => write!(
                f,
                "Incomplete varint: expected {} bytes, found {}",
                expected_bytes, actual_bytes
            ),
            Self::TooLong { actual_length } => write!(
                f,
                "Varint too long: maximum {} bytes, found {}",
                MAX_VARINT_BYTES, actual_length
            ),
            Self::Malformed { position } => write!(
                f,
                "Malformed varint: continuation bit set at final byte {}",
                position
            ),
        }
    }
}

impl std::error::Error for VarintError {}

/// Encode a u64 value as varint
///
/// # Arguments
/// * `value` - The value to encode
///
/// # Returns
/// A Vec<u8> containing the varint-encoded value
///
/// # Examples
/// ```
/// use sqlitegraph::backend::native::v3::compression::varint::encode_varint;
///
/// // Small values fit in one byte
/// assert_eq!(encode_varint(0), vec![0x00]);
/// assert_eq!(encode_varint(42), vec![0x2A]);
/// assert_eq!(encode_varint(127), vec![0x7F]);
///
/// // Larger values use multiple bytes
/// assert_eq!(encode_varint(128), vec![0x80, 0x01]);
/// assert_eq!(encode_varint(300), vec![0xAC, 0x02]);
/// ```
///
/// # Encoding Details
///
/// Each byte (except the last) has the MSB set to indicate continuation.
/// The lower 7 bits of each byte contain actual data.
///
/// ```text
/// Value: 300 (0x12C)
///
/// Byte 0: 0x12C & 0x7F = 0x2C | 0x80 = 0xAC (44 + continuation)
/// Byte 1: (0x12C >> 7) = 0x02 | 0x00 = 0x02 (2, no continuation)
///
/// Result: [0xAC, 0x02]
/// ```
pub fn encode_varint(value: u64) -> Vec<u8> {
    if value == 0 {
        return vec![0];
    }

    let mut buffer = Vec::with_capacity(MAX_VARINT_BYTES);
    let mut remaining = value;

    while remaining > 0 {
        // Take 7 bits
        let mut byte = (remaining & DATA_MASK as u64) as u8;

        remaining >>= 7;

        // Set continuation bit if more bytes follow
        if remaining > 0 {
            byte |= CONTINUATION_BIT;
        }

        buffer.push(byte);
    }

    buffer
}

/// Decode a varint from a byte slice
///
/// # Arguments
/// * `bytes` - Input byte slice containing varint-encoded data
///
/// # Returns
/// A tuple of (decoded_value, bytes_read)
///
/// # Errors
/// Returns `VarintError` if:
/// - Input is empty
/// - Varint is incomplete
/// - Varint exceeds maximum length
/// - Varint is malformed
///
/// # Examples
/// ```
/// use sqlitegraph::backend::native::v3::compression::varint::decode_varint;
///
/// // Single byte values
/// assert_eq!(decode_varint(&[0x00]).unwrap(), (0, 1));
/// assert_eq!(decode_varint(&[0x2A]).unwrap(), (42, 1));
/// assert_eq!(decode_varint(&[0x7F]).unwrap(), (127, 1));
///
/// // Multi-byte values
/// assert_eq!(decode_varint(&[0x80, 0x01]).unwrap(), (128, 2));
/// assert_eq!(decode_varint(&[0xAC, 0x02]).unwrap(), (300, 2));
/// ```
pub fn decode_varint(bytes: &[u8]) -> NativeResult<(u64, usize)> {
    if bytes.is_empty() {
        return Err(NativeBackendError::InvalidHeader {
            field: "varint".to_string(),
            reason: VarintError::EmptyInput.to_string(),
        });
    }

    let mut result: u64 = 0;
    let mut shift: usize = 0;
    let mut bytes_read = 0;

    for (i, &byte) in bytes.iter().enumerate() {
        bytes_read = i + 1;

        // Check for maximum length
        if i >= MAX_VARINT_BYTES {
            return Err(NativeBackendError::InvalidHeader {
                field: "varint".to_string(),
                reason: VarintError::TooLong {
                    actual_length: i + 1,
                }
                .to_string(),
            });
        }

        // Extract 7 bits of data
        let data = (byte & DATA_MASK) as u64;
        result |= data << shift;

        // Check continuation bit
        if byte & CONTINUATION_BIT == 0 {
            // Last byte
            return Ok((result, bytes_read));
        }

        shift += 7;

        // Check for overflow (u64 has 64 bits, we use 7 per byte)
        if shift >= 64 {
            return Err(NativeBackendError::InvalidHeader {
                field: "varint".to_string(),
                reason: format!("Varint overflow: shift {} exceeds u64 capacity", shift),
            });
        }
    }

    // If we get here, input ended before varint was complete
    Err(NativeBackendError::InvalidHeader {
        field: "varint".to_string(),
        reason: VarintError::IncompleteData {
            expected_bytes: bytes_read + 1,
            actual_bytes: bytes_read,
        }
        .to_string(),
    })
}

/// Calculate the number of bytes needed to encode a value as varint
///
/// # Arguments
/// * `value` - The value to measure
///
/// # Returns
/// Number of bytes needed (1-10)
///
/// # Examples
/// ```
/// use sqlitegraph::backend::native::v3::compression::varint::varint_size;
///
/// assert_eq!(varint_size(0), 1);
/// assert_eq!(varint_size(127), 1);
/// assert_eq!(varint_size(128), 2);
/// assert_eq!(varint_size(16383), 2);
/// assert_eq!(varint_size(16384), 3);
/// ```
pub fn varint_size(value: u64) -> usize {
    if value == 0 {
        return 1;
    }

    let mut size = 0;
    let mut remaining = value;

    while remaining > 0 {
        size += 1;
        remaining >>= 7;
    }

    size
}

/// Encode a u32 value as varint (convenience wrapper)
///
/// # Arguments
/// * `value` - The u32 value to encode
///
/// # Returns
/// A Vec<u8> containing the varint-encoded value
pub fn encode_varint_u32(value: u32) -> Vec<u8> {
    encode_varint(value as u64)
}

/// Decode a u32 varint from a byte slice
///
/// # Arguments
/// * `bytes` - Input byte slice
///
/// # Returns
/// A tuple of (decoded_u32_value, bytes_read)
///
/// # Errors
/// Returns an error if the varint value exceeds u32::MAX
pub fn decode_varint_u32(bytes: &[u8]) -> NativeResult<(u32, usize)> {
    let (value, bytes_read) = decode_varint(bytes)?;

    if value > u32::MAX as u64 {
        return Err(NativeBackendError::InvalidHeader {
            field: "varint_u32".to_string(),
            reason: format!("Value {} exceeds u32::MAX", value),
        });
    }

    Ok((value as u32, bytes_read))
}

/// Encode a u16 value as varint (convenience wrapper)
///
/// # Arguments
/// * `value` - The u16 value to encode
///
/// # Returns
/// A Vec<u8> containing the varint-encoded value
pub fn encode_varint_u16(value: u16) -> Vec<u8> {
    encode_varint(value as u64)
}

/// Decode a u16 varint from a byte slice
///
/// # Arguments
/// * `bytes` - Input byte slice
///
/// # Returns
/// A tuple of (decoded_u16_value, bytes_read)
///
/// # Errors
/// Returns an error if the varint value exceeds u16::MAX
pub fn decode_varint_u16(bytes: &[u8]) -> NativeResult<(u16, usize)> {
    let (value, bytes_read) = decode_varint(bytes)?;

    if value > u16::MAX as u64 {
        return Err(NativeBackendError::InvalidHeader {
            field: "varint_u16".to_string(),
            reason: format!("Value {} exceeds u16::MAX", value),
        });
    }

    Ok((value as u16, bytes_read))
}

#[cfg(test)]
mod tests {
    use super::*;

    // === Encoding Tests ===

    #[test]
    fn test_encode_varint_zero() {
        assert_eq!(encode_varint(0), vec![0x00]);
    }

    #[test]
    fn test_encode_varint_single_byte() {
        // Values 0-127 fit in one byte
        assert_eq!(encode_varint(1), vec![0x01]);
        assert_eq!(encode_varint(42), vec![0x2A]);
        assert_eq!(encode_varint(127), vec![0x7F]);
    }

    #[test]
    fn test_encode_varint_two_bytes() {
        // Values 128-16383 fit in two bytes
        assert_eq!(encode_varint(128), vec![0x80, 0x01]);
        assert_eq!(encode_varint(300), vec![0xAC, 0x02]);
        assert_eq!(encode_varint(16383), vec![0xFF, 0x7F]);
    }

    #[test]
    fn test_encode_varint_three_bytes() {
        // Values 16384-2097151 fit in three bytes
        assert_eq!(encode_varint(16384), vec![0x80, 0x80, 0x01]);
        assert_eq!(encode_varint(2097151), vec![0xFF, 0xFF, 0x7F]);
    }

    #[test]
    fn test_encode_varint_max_u64() {
        // u64::MAX requires 10 bytes
        let encoded = encode_varint(u64::MAX);
        assert_eq!(encoded.len(), 10);

        // Verify MSB pattern: first 9 bytes have MSB set, last byte doesn't
        for (i, &byte) in encoded.iter().enumerate() {
            if i < 9 {
                assert!(
                    byte & CONTINUATION_BIT != 0,
                    "Byte {} should have MSB set",
                    i
                );
            } else {
                assert!(
                    byte & CONTINUATION_BIT == 0,
                    "Byte {} should not have MSB set",
                    i
                );
            }
        }
    }

    #[test]
    fn test_encode_varint_u16() {
        // u16::MAX should encode efficiently
        let encoded = encode_varint_u16(u16::MAX);
        assert_eq!(encoded, vec![0xFF, 0xFF, 0x03]);

        // Small value
        assert_eq!(encode_varint_u16(100), vec![100]);
    }

    #[test]
    fn test_encode_varint_u32() {
        // u32::MAX requires 5 bytes
        let encoded = encode_varint_u32(u32::MAX);
        assert_eq!(encoded.len(), 5);
        assert_eq!(encoded, vec![0xFF, 0xFF, 0xFF, 0xFF, 0x0F]);
    }

    // === Decoding Tests ===

    #[test]
    fn test_decode_varint_zero() {
        assert_eq!(decode_varint(&[0x00]).unwrap(), (0, 1));
    }

    #[test]
    fn test_decode_varint_single_byte() {
        assert_eq!(decode_varint(&[0x01]).unwrap(), (1, 1));
        assert_eq!(decode_varint(&[0x2A]).unwrap(), (42, 1));
        assert_eq!(decode_varint(&[0x7F]).unwrap(), (127, 1));
    }

    #[test]
    fn test_decode_varint_two_bytes() {
        assert_eq!(decode_varint(&[0x80, 0x01]).unwrap(), (128, 2));
        assert_eq!(decode_varint(&[0xAC, 0x02]).unwrap(), (300, 2));
        assert_eq!(decode_varint(&[0xFF, 0x7F]).unwrap(), (16383, 2));
    }

    #[test]
    fn test_decode_varint_three_bytes() {
        assert_eq!(decode_varint(&[0x80, 0x80, 0x01]).unwrap(), (16384, 3));
        assert_eq!(decode_varint(&[0xFF, 0xFF, 0x7F]).unwrap(), (2097151, 3));
    }

    #[test]
    fn test_decode_varint_with_extra_data() {
        // Should only read the varint bytes
        let data = vec![0x80, 0x01, 0xFF, 0xFF, 0xFF];
        assert_eq!(decode_varint(&data).unwrap(), (128, 2));
    }

    #[test]
    fn test_decode_varint_u16() {
        // Single-byte value (127 max)
        assert_eq!(decode_varint_u16(&[0x7F]).unwrap(), (127, 1));
        // Two-byte value (255)
        assert_eq!(decode_varint_u16(&[0xFF, 0x01]).unwrap(), (255, 2));
        // Max u16
        assert_eq!(
            decode_varint_u16(&[0xFF, 0xFF, 0x03]).unwrap(),
            (u16::MAX, 3)
        );
    }

    #[test]
    fn test_decode_varint_u16_incomplete() {
        // Test incomplete varint - should error
        let result = decode_varint_u16(&[0x80]);
        assert!(result.is_err());
    }

    #[test]
    fn test_decode_varint_u32() {
        // Single-byte value (127 max)
        assert_eq!(decode_varint_u32(&[0x7F]).unwrap(), (127, 1));
        // Two-byte value (255)
        assert_eq!(decode_varint_u32(&[0xFF, 0x01]).unwrap(), (255, 2));
        // Max u32
        assert_eq!(
            decode_varint_u32(&[0xFF, 0xFF, 0xFF, 0xFF, 0x0F]).unwrap(),
            (u32::MAX, 5)
        );
    }

    #[test]
    fn test_decode_varint_u32_incomplete() {
        // Test incomplete varint - should error
        let result = decode_varint_u32(&[0x80]);
        assert!(result.is_err());
    }

    #[test]
    fn test_decode_varint_u32_overflow() {
        // Value > u32::MAX should error
        let large_value = encode_varint(u32::MAX as u64 + 1);
        assert!(decode_varint_u32(&large_value).is_err());
    }

    #[test]
    fn test_decode_varint_u16_overflow() {
        // Value > u16::MAX should error
        let large_value = encode_varint(u16::MAX as u64 + 1);
        assert!(decode_varint_u16(&large_value).is_err());
    }

    // === Error Cases ===

    #[test]
    fn test_decode_varint_empty_input() {
        let result = decode_varint(&[]);
        assert!(result.is_err());
    }

    #[test]
    fn test_decode_varint_incomplete() {
        // Two-byte varint but only one byte provided
        let result = decode_varint(&[0x80]);
        assert!(result.is_err());

        // Three-byte varint but only two bytes provided
        let result = decode_varint(&[0x80, 0x80]);
        assert!(result.is_err());
    }

    #[test]
    fn test_decode_varint_too_long() {
        // Create a varint longer than 10 bytes
        let too_long = vec![0x80u8; 11];
        let result = decode_varint(&too_long);
        assert!(result.is_err());
    }

    // === Round-trip Tests ===

    #[test]
    fn test_round_trip_small_values() {
        let test_values = vec![0, 1, 42, 127, 128, 255, 256, 1000];

        for value in test_values {
            let encoded = encode_varint(value);
            let (decoded, bytes_read) = decode_varint(&encoded).unwrap();

            assert_eq!(decoded, value, "Round-trip failed for {}", value);
            assert_eq!(
                bytes_read,
                encoded.len(),
                "Byte count mismatch for {}",
                value
            );
        }
    }

    #[test]
    fn test_round_trip_edge_values() {
        let test_values = vec![
            0,
            1,
            127,
            128,
            16383,
            16384,
            2097151,
            2097152,
            u16::MAX as u64,
            u32::MAX as u64,
            u64::MAX,
        ];

        for value in test_values {
            let encoded = encode_varint(value);
            let (decoded, bytes_read) = decode_varint(&encoded).unwrap();

            assert_eq!(decoded, value, "Round-trip failed for {}", value);
            assert_eq!(bytes_read, encoded.len());
        }
    }

    #[test]
    fn test_round_trip_u16() {
        for value in [0, 1, 100, u16::MAX / 2, u16::MAX - 1, u16::MAX] {
            let encoded = encode_varint_u16(value);
            let (decoded, bytes_read) = decode_varint_u16(&encoded).unwrap();

            assert_eq!(decoded, value);
            assert_eq!(bytes_read, encoded.len());
        }
    }

    #[test]
    fn test_round_trip_u32() {
        for value in [0, 1, 1000, u16::MAX as u32, u32::MAX / 2, u32::MAX] {
            let encoded = encode_varint_u32(value);
            let (decoded, bytes_read) = decode_varint_u32(&encoded).unwrap();

            assert_eq!(decoded, value);
            assert_eq!(bytes_read, encoded.len());
        }
    }

    // === varint_size Tests ===

    #[test]
    fn test_varint_size_calculation() {
        assert_eq!(varint_size(0), 1);
        assert_eq!(varint_size(127), 1);
        assert_eq!(varint_size(128), 2);
        assert_eq!(varint_size(16383), 2);
        assert_eq!(varint_size(16384), 3);
        assert_eq!(varint_size(2097151), 3);
        assert_eq!(varint_size(2097152), 4);

        // Verify matches actual encoding
        for value in [0, 1, 127, 128, 16383, 16384, u32::MAX as u64, u64::MAX] {
            assert_eq!(
                varint_size(value),
                encode_varint(value).len(),
                "varint_size mismatch for {}",
                value
            );
        }
    }

    // === Data Integrity Tests ===

    #[test]
    fn test_varint_7_bit_chunks() {
        // Verify that each byte correctly extracts 7 bits
        let value = 0b111_1111_1111_1111_1111_1111; // 22 bits

        let encoded = encode_varint(value);
        assert_eq!(encoded.len(), 4); // ceil(22/7) = 4

        // Verify bit patterns
        assert_eq!(encoded[0] & !DATA_MASK, CONTINUATION_BIT); // Has MSB
        assert_eq!(encoded[0] & DATA_MASK, 0b1111111); // Lower 7 bits

        assert_eq!(encoded[3] & CONTINUATION_BIT, 0); // Last byte, no MSB
    }

    #[test]
    fn test_varint_efficiency_for_small_values() {
        // Most node offsets and counts are small (< 128)
        assert_eq!(encode_varint(0).len(), 1);
        assert_eq!(encode_varint(10).len(), 1);
        assert_eq!(encode_varint(42).len(), 1);
        assert_eq!(encode_varint(100).len(), 1);
        assert_eq!(encode_varint(127).len(), 1);

        // 128+ requires 2 bytes
        assert_eq!(encode_varint(128).len(), 2);
        assert_eq!(encode_varint(255).len(), 2);
        assert_eq!(encode_varint(256).len(), 2);
        assert_eq!(encode_varint(1000).len(), 2);
    }
}