cqlite-core 0.11.0

Core engine for CQLite — read Apache Cassandra 5.0 SSTables locally without a cluster
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
//! VInt (Variable-length integer) encoder for Cassandra compatibility
//!
//! Implements byte-identical encoding to Cassandra's VIntCoding.java.
//! All SSTable component writers depend on this producing correct output.
//!
//! Encoding format:
//! - Single byte: values 0-127 (unsigned) or -64 to 63 (signed)
//! - Multi-byte: first byte encodes length via leading 1-bits, remaining bytes are big-endian
//! - Signed values use ZigZag encoding: 0→0, -1→1, 1→2, -2→3, etc.
//!
//! References:
//! - Cassandra VIntCoding.java: org.apache.cassandra.utils.vint.VIntCoding
//! - Appendix B: docs/sstables-definitive-guide/chapters/appendix-b-encodings-cheat-sheet.md

/// ZigZag encode a signed integer to unsigned
///
/// Maps signed integers to unsigned so that small absolute values
/// have small encodings: 0→0, -1→1, 1→2, -2→3, 2→4, -3→5, ...
///
/// Formula: (n << 1) ^ (n >> 63)
/// - For positive n: left shift gives 2n, right shift gives 0, so result is 2n
/// - For negative n: left shift gives 2n, right shift gives -1 (all 1s), so XOR inverts
#[inline]
fn zigzag_encode(n: i64) -> u64 {
    ((n << 1) ^ (n >> 63)) as u64
}

/// Encode a signed i64 to VInt bytes (Cassandra-compatible)
///
/// Uses ZigZag encoding to efficiently encode small negative numbers.
///
/// # Arguments
///
/// * `value` - Signed 64-bit integer to encode
/// * `buf` - Target buffer to write VInt bytes
///
/// # Examples
///
/// ```
/// # use cqlite_core::storage::serialization::vint::encode_signed;
/// let mut buf = Vec::new();
/// encode_signed(0, &mut buf);
/// assert_eq!(buf, vec![0x00]); // Single-byte zero
///
/// buf.clear();
/// encode_signed(-1, &mut buf);
/// assert_eq!(buf, vec![0x01]); // ZigZag encoded -1
///
/// buf.clear();
/// encode_signed(64, &mut buf);
/// assert_eq!(buf, vec![0x80, 0x80]); // Two-byte format
/// ```
pub fn encode_signed(value: i64, buf: &mut Vec<u8>) {
    let unsigned_value = zigzag_encode(value);
    encode_unsigned(unsigned_value, buf);
}

/// Encode an unsigned u64 to VInt bytes
///
/// Encoding format (standard Cassandra unsigned VInt):
/// - 1 byte: 0xxxxxxx (values 0-127)
/// - 2 bytes: 10xxxxxx xxxxxxxx (values 128-16383, 14 bits total: 6+8)
/// - 3 bytes: 110xxxxx xxxxxxxx xxxxxxxx (values 16384-2097151, 21 bits: 5+8+8)
/// - ... up to 9 bytes total
///
/// The first byte encodes the number of extra bytes via leading 1-bits,
/// followed by a 0 separator bit, then data bits.
///
/// # Arguments
///
/// * `value` - Unsigned 64-bit integer to encode
/// * `buf` - Target buffer to write VInt bytes
///
/// # Examples
///
/// ```
/// # use cqlite_core::storage::serialization::vint::encode_unsigned;
/// let mut buf = Vec::new();
/// encode_unsigned(0, &mut buf);
/// assert_eq!(buf, vec![0x00]);
///
/// buf.clear();
/// encode_unsigned(127, &mut buf);
/// assert_eq!(buf, vec![0x7F]);
///
/// buf.clear();
/// encode_unsigned(128, &mut buf);
/// assert_eq!(buf, vec![0x80, 0x80]); // 10xxxxxx xxxxxxxx
/// ```
pub fn encode_unsigned(value: u64, buf: &mut Vec<u8>) {
    let size = unsigned_len_value(value);

    if size == 1 {
        // Single byte: 0xxxxxxx (values 0-127)
        buf.push(value as u8);
    } else if size == 9 {
        // 9 bytes: 0xFF followed by full 8-byte long
        buf.push(0xFF);
        buf.extend_from_slice(&value.to_be_bytes());
    } else {
        // Multi-byte (2-8 bytes): [leading 1s][0][data bits][remaining bytes]
        let extra_bytes = size - 1;

        // Create first byte with leading 1-bits pattern
        // For 2 bytes (1 extra): 0x80 | data_bits (10xxxxxx)
        // For 3 bytes (2 extra): 0xC0 | data_bits (110xxxxx)
        let mask = encode_extra_bytes_to_read(extra_bytes);

        // Calculate how many data bits fit in the first byte
        let first_byte_data_bits = 8 - extra_bytes - 1;

        // Extract data bits for first byte
        let shift = extra_bytes * 8;
        let first_byte_data = ((value >> shift) & ((1 << first_byte_data_bits) - 1)) as u8;
        buf.push(mask | first_byte_data);

        // Add remaining bytes in big-endian order
        for i in (0..extra_bytes).rev() {
            buf.push(((value >> (i * 8)) & 0xFF) as u8);
        }
    }
}

/// Calculate the encoded length of a signed i64
///
/// Returns the number of bytes that would be needed to encode this value.
///
/// # Examples
///
/// ```
/// # use cqlite_core::storage::serialization::vint::signed_len;
/// assert_eq!(signed_len(0), 1);
/// assert_eq!(signed_len(63), 1);
/// assert_eq!(signed_len(-64), 1);
/// assert_eq!(signed_len(64), 2);
/// assert_eq!(signed_len(-65), 2);
/// ```
#[inline]
pub fn signed_len(value: i64) -> usize {
    let unsigned_value = zigzag_encode(value);
    unsigned_len_value(unsigned_value)
}

/// Calculate the encoded length of an unsigned u64
///
/// Returns the number of bytes that would be needed to encode this value.
///
/// # Examples
///
/// ```
/// # use cqlite_core::storage::serialization::vint::unsigned_len;
/// assert_eq!(unsigned_len(0), 1);
/// assert_eq!(unsigned_len(127), 1);
/// assert_eq!(unsigned_len(128), 2);
/// assert_eq!(unsigned_len(16384), 3);
/// ```
#[inline]
pub fn unsigned_len(value: u64) -> usize {
    unsigned_len_value(value)
}

/// Compute the number of bytes needed to encode an unsigned VInt
///
/// Matches Cassandra's computeUnsignedVIntSize algorithm:
/// Uses bit manipulation to calculate size based on the number of leading zeros.
///
/// Formula: (639 - magnitude * 9) >> 6
/// where magnitude = numberOfLeadingZeros(value | 1)
#[inline]
fn unsigned_len_value(value: u64) -> usize {
    // | with 1 ensures magnitude <= 63, so (63 - 1) / 7 <= 8
    let magnitude = (value | 1).leading_zeros();
    // Hand-picked formula from Cassandra that matches: 9 - ((magnitude - 1) / 7)
    ((639 - (magnitude * 9)) >> 6) as usize
}

/// Encode the number of extra bytes to read in the first byte
///
/// For a VInt with N extra bytes, this returns the bit pattern
/// with N leading 1-bits followed by a 0 separator.
///
/// Examples:
/// - 0 extra bytes: 0x00 (00000000)
/// - 1 extra byte:  0x80 (10000000)
/// - 2 extra bytes: 0xC0 (11000000)
/// - 3 extra bytes: 0xE0 (11100000)
#[inline]
fn encode_extra_bytes_to_read(extra_bytes: usize) -> u8 {
    if extra_bytes == 0 {
        0x00
    } else if extra_bytes >= 8 {
        0xFF
    } else {
        // Generate mask with N leading 1-bits followed by 0s
        // For 1 extra byte: 0x80 (10000000)
        // For 2 extra bytes: 0xC0 (11000000)
        // Formula: ~((1 << (8 - extra_bytes)) - 1)
        let shift = 8 - extra_bytes;
        0xFF_u8 << shift
    }
}

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

    #[test]
    fn test_zigzag_encode() {
        assert_eq!(zigzag_encode(0), 0);
        assert_eq!(zigzag_encode(-1), 1);
        assert_eq!(zigzag_encode(1), 2);
        assert_eq!(zigzag_encode(-2), 3);
        assert_eq!(zigzag_encode(2), 4);
        assert_eq!(zigzag_encode(-3), 5);
        assert_eq!(zigzag_encode(63), 126);
        assert_eq!(zigzag_encode(-64), 127);
        assert_eq!(zigzag_encode(64), 128);
    }

    #[test]
    fn test_encode_signed_test_vectors() {
        // Test vectors from Issue #363
        let test_cases = vec![
            (0i64, vec![0x00]),
            (1i64, vec![0x02]),
            (-1i64, vec![0x01]),
            (63i64, vec![0x7E]),
            (-64i64, vec![0x7F]),
            (64i64, vec![0x80, 0x80]),
        ];

        for (value, expected) in test_cases {
            let mut buf = Vec::new();
            encode_signed(value, &mut buf);
            assert_eq!(
                buf, expected,
                "encode_signed({}) failed: expected {:?}, got {:?}",
                value, expected, buf
            );
        }
    }

    #[test]
    fn test_encode_unsigned_boundaries() {
        let test_cases = vec![
            (0u64, vec![0x00]),
            (127u64, vec![0x7F]),               // Max single byte
            (128u64, vec![0x80, 0x80]),         // Min two bytes
            (16383u64, vec![0xBF, 0xFF]),       // Max two bytes (14 bits: 6+8)
            (16384u64, vec![0xC0, 0x40, 0x00]), // Min three bytes
        ];

        for (value, expected) in test_cases {
            let mut buf = Vec::new();
            encode_unsigned(value, &mut buf);
            assert_eq!(
                buf, expected,
                "encode_unsigned({}) failed: expected {:?}, got {:?}",
                value, expected, buf
            );
        }
    }

    #[test]
    fn test_signed_len() {
        assert_eq!(signed_len(0), 1);
        assert_eq!(signed_len(1), 1);
        assert_eq!(signed_len(-1), 1);
        assert_eq!(signed_len(63), 1);
        assert_eq!(signed_len(-64), 1);
        assert_eq!(signed_len(64), 2);
        assert_eq!(signed_len(-65), 2);
        assert_eq!(signed_len(127), 2);
        assert_eq!(signed_len(-128), 2);
    }

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

    #[test]
    fn test_encode_extra_bytes() {
        assert_eq!(encode_extra_bytes_to_read(0), 0x00);
        assert_eq!(encode_extra_bytes_to_read(1), 0x80);
        assert_eq!(encode_extra_bytes_to_read(2), 0xC0);
        assert_eq!(encode_extra_bytes_to_read(3), 0xE0);
        assert_eq!(encode_extra_bytes_to_read(4), 0xF0);
        assert_eq!(encode_extra_bytes_to_read(5), 0xF8);
        assert_eq!(encode_extra_bytes_to_read(6), 0xFC);
        assert_eq!(encode_extra_bytes_to_read(7), 0xFE);
        assert_eq!(encode_extra_bytes_to_read(8), 0xFF);
    }

    #[test]
    fn test_roundtrip_with_decoder() {
        // Test roundtrip encode → decode using standard Cassandra VInt format
        use crate::parser::vint::parse_vint;

        let test_values = vec![
            0,
            1,
            -1,
            63,
            -64,
            64,
            -65,
            127,
            -128,
            255,
            -255,
            1000,
            -1000,
            32767,
            -32768,
            1048576,
            -1048576,
            i32::MAX as i64,
            i32::MIN as i64,
        ];

        for value in test_values {
            // Test our encoder
            let mut buf = Vec::new();
            encode_signed(value, &mut buf);

            // Test decoder can parse it
            let (remaining, decoded) = parse_vint(&buf).unwrap();
            assert!(
                remaining.is_empty(),
                "Decoder should consume all bytes for value {}",
                value
            );
            assert_eq!(
                decoded, value,
                "Roundtrip failed for value {}: encoded as {:?}",
                value, buf
            );
        }
    }

    #[test]
    fn test_large_values() {
        // Test values requiring different byte sizes
        let test_cases = vec![
            (1u64 << 7, 2),  // 128 - 2 bytes
            (1u64 << 14, 3), // 16384 - 3 bytes
            (1u64 << 21, 4), // 2097152 - 4 bytes
            (1u64 << 28, 5), // 268435456 - 5 bytes
            (1u64 << 35, 6), // 6 bytes
            (1u64 << 42, 7), // 7 bytes
            (1u64 << 49, 8), // 8 bytes
            (1u64 << 56, 9), // 9 bytes
        ];

        for (value, expected_size) in test_cases {
            assert_eq!(
                unsigned_len(value),
                expected_size,
                "Value {} should encode to {} bytes",
                value,
                expected_size
            );

            let mut buf = Vec::new();
            encode_unsigned(value, &mut buf);
            assert_eq!(
                buf.len(),
                expected_size,
                "Encoded {} to {:?}, expected {} bytes",
                value,
                buf,
                expected_size
            );
        }
    }

    #[test]
    fn test_performance_target() {
        // Performance target: <100ns per encode
        // This is a rough check that we're not doing anything obviously slow
        use std::time::Instant;

        let iterations = 10_000;
        let mut buf = Vec::with_capacity(9);

        let start = Instant::now();
        for i in 0..iterations {
            buf.clear();
            encode_signed(i, &mut buf);
        }
        let elapsed = start.elapsed();

        let avg_ns = elapsed.as_nanos() / iterations as u128;
        println!("Average encode time: {} ns", avg_ns);

        // Sanity check: should be much faster than 1µs per encode
        assert!(
            avg_ns < 1000,
            "Encoding is too slow: {} ns per operation",
            avg_ns
        );
    }

    #[test]
    fn test_cassandra_compatibility() {
        // Test specific patterns from standard Cassandra VInt encoding
        // These use ZigZag encoding: signed → unsigned → VInt bytes
        let test_cases = vec![
            (0i64, vec![0x00], "Zero value"), // zigzag(0)=0, vint(0)=[0x00]
            (1i64, vec![0x02], "Single byte positive"), // zigzag(1)=2, vint(2)=[0x02]
            (63i64, vec![0x7E], "Maximum single byte positive"), // zigzag(63)=126, vint(126)=[0x7E]
            (64i64, vec![0x80, 0x80], "Two byte encoding start"), // zigzag(64)=128, vint(128)=[0x80,0x80]
            (127i64, vec![0x80, 0xFE], "Two byte positive"), // zigzag(127)=254, vint(254)=[0x80,0xFE]
            (-1i64, vec![0x01], "Single byte negative"),     // zigzag(-1)=1, vint(1)=[0x01]
            (-64i64, vec![0x7F], "Single byte negative boundary"), // zigzag(-64)=127, vint(127)=[0x7F]
            (-65i64, vec![0x80, 0x81], "Two byte negative"), // zigzag(-65)=129, vint(129)=[0x80,0x81]
        ];

        for (value, expected_bytes, description) in test_cases {
            let mut buf = Vec::new();
            encode_signed(value, &mut buf);
            assert_eq!(
                buf, expected_bytes,
                "{}: encode_signed({}) failed: expected {:?}, got {:?}",
                description, value, expected_bytes, buf
            );
        }
    }
}