nodedb 0.0.0-beta.1

Local-first, real-time, edge-to-cloud hybrid database for multi-modal workloads
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
//! Fixed-size slab allocator for KV overflow values.
//!
//! Each tier holds fixed-size slots — zero internal fragmentation, O(1) alloc/free.
//!
//! Tiers: 64B, 128B, 256B, 512B, 1KB, 2KB, 4KB.
//! Values larger than 4KB use a fallback large-object heap (Vec-based, rare).
//!
//! Design follows Linux SLAB/SLUB: size-class segregation prevents small
//! allocations from fragmenting large-allocation regions.

/// Size classes for slab tiers in bytes.
const TIER_SIZES: &[usize] = &[64, 128, 256, 512, 1024, 2048, 4096];

/// Maximum slab-managed value size. Larger values use the large-object fallback.
const MAX_SLAB_SIZE: usize = 4096;

/// Initial slots per tier (grows on demand).
const INITIAL_SLOTS_PER_TIER: usize = 64;

/// A single slab tier with fixed-size slots.
#[derive(Debug)]
struct SlabTier {
    /// Slot size in bytes (e.g., 64, 128, ...).
    slot_size: usize,
    /// Contiguous backing memory: `slots * slot_size` bytes.
    buf: Vec<u8>,
    /// Number of allocated slots.
    total_slots: usize,
    /// Free slot indices (stack-based: push on free, pop on alloc).
    free_list: Vec<u32>,
    /// Number of currently occupied slots.
    occupied: usize,
}

impl SlabTier {
    fn new(slot_size: usize, initial_slots: usize) -> Self {
        let buf = vec![0u8; slot_size * initial_slots];
        let free_list: Vec<u32> = (0..initial_slots as u32).rev().collect();
        Self {
            slot_size,
            buf,
            total_slots: initial_slots,
            free_list,
            occupied: 0,
        }
    }

    /// Allocate a slot. Returns the slot index, or None if the tier is full
    /// (caller should grow the tier).
    fn alloc(&mut self, data: &[u8]) -> Option<u32> {
        debug_assert!(data.len() <= self.slot_size);

        if let Some(slot_idx) = self.free_list.pop() {
            let offset = slot_idx as usize * self.slot_size;
            self.buf[offset..offset + data.len()].copy_from_slice(data);
            // Zero remaining bytes in the slot (deterministic reads).
            if data.len() < self.slot_size {
                self.buf[offset + data.len()..offset + self.slot_size].fill(0);
            }
            self.occupied += 1;
            Some(slot_idx)
        } else {
            None // Tier full — caller must grow.
        }
    }

    /// Grow the tier by doubling its capacity.
    fn grow(&mut self) {
        let new_slots = self.total_slots;
        let old_total = self.total_slots;
        self.total_slots += new_slots;
        self.buf.resize(self.total_slots * self.slot_size, 0);
        // Push new slot indices onto the free list (reverse order for stack semantics).
        for i in (old_total..self.total_slots).rev() {
            self.free_list.push(i as u32);
        }
    }

    /// Free a slot by index.
    fn free(&mut self, slot_idx: u32) {
        debug_assert!((slot_idx as usize) < self.total_slots);
        self.free_list.push(slot_idx);
        self.occupied -= 1;
    }

    /// Read data from a slot.
    fn get(&self, slot_idx: u32, len: usize) -> &[u8] {
        let offset = slot_idx as usize * self.slot_size;
        &self.buf[offset..offset + len]
    }

    /// Total memory consumed by this tier (backing buffer).
    fn capacity_bytes(&self) -> usize {
        self.buf.len()
    }

    /// Memory actively occupied (occupied_slots * slot_size).
    fn occupied_bytes(&self) -> usize {
        self.occupied * self.slot_size
    }

    /// Fragmentation ratio: capacity / occupied. 1.0 = no fragmentation.
    /// Returns 0.0 if nothing is occupied.
    fn fragmentation_ratio(&self) -> f64 {
        if self.occupied == 0 {
            return 0.0;
        }
        self.buf.len() as f64 / self.occupied_bytes() as f64
    }
}

/// Large-object entry for values exceeding `MAX_SLAB_SIZE`.
#[derive(Debug)]
struct LargeObject {
    data: Vec<u8>,
}

/// Fixed-size slab allocator with tiered size classes.
///
/// Drop-in replacement for `OverflowPool` with O(1) alloc/free and
/// zero internal fragmentation within each tier.
#[derive(Debug)]
pub struct SlabAllocator {
    /// One tier per size class (indexed by `tier_index()`).
    tiers: Vec<SlabTier>,
    /// Large objects that don't fit in any slab tier.
    /// Indexed by a monotonic ID (stored in the `index` field of `KvValue::Overflow`).
    large_objects: Vec<Option<LargeObject>>,
    /// Free large-object slots for reuse.
    large_free: Vec<u32>,
    /// Next large-object ID (monotonic).
    next_large_id: u32,
}

impl SlabAllocator {
    pub fn new() -> Self {
        let tiers = TIER_SIZES
            .iter()
            .map(|&size| SlabTier::new(size, INITIAL_SLOTS_PER_TIER))
            .collect();
        Self {
            tiers,
            large_objects: Vec::new(),
            large_free: Vec::new(),
            next_large_id: 0,
        }
    }

    /// Allocate space for a value. Returns `(handle, len)`.
    ///
    /// The handle encodes the tier index and slot index (for slab-managed values)
    /// or a large-object ID (for oversized values). The caller stores these in
    /// `KvValue::Overflow { index: handle, len }`.
    pub fn alloc(&mut self, data: &[u8]) -> (u32, u32) {
        let len = data.len();

        if len <= MAX_SLAB_SIZE {
            let tier_idx = tier_index(len);
            let tier = &mut self.tiers[tier_idx];

            // Try to allocate in the current tier; grow and retry if full.
            let slot_idx = match tier.alloc(data) {
                Some(idx) => idx,
                None => {
                    tier.grow();
                    // grow() doubles capacity, guaranteeing free slots exist.
                    // If alloc still fails, it means a logic bug in grow() —
                    // fall back to large-object path rather than panicking.
                    match tier.alloc(data) {
                        Some(idx) => idx,
                        None => {
                            // Defensive: fall back to large-object path rather than panicking.
                            let id = self.next_large_id;
                            self.next_large_id += 1;
                            self.large_objects.push(Some(LargeObject {
                                data: data.to_vec(),
                            }));
                            let handle = encode_large_handle(id);
                            return (handle, len as u32);
                        }
                    }
                }
            };
            let handle = encode_handle(tier_idx as u8, slot_idx);
            return (handle, len as u32);
        }

        // Large object fallback.
        let id = if let Some(free_id) = self.large_free.pop() {
            self.large_objects[free_id as usize] = Some(LargeObject {
                data: data.to_vec(),
            });
            free_id
        } else {
            let id = self.next_large_id;
            self.next_large_id += 1;
            self.large_objects.push(Some(LargeObject {
                data: data.to_vec(),
            }));
            id
        };

        let handle = encode_large_handle(id);
        (handle, len as u32)
    }

    /// Read a value by handle and length.
    pub fn get(&self, handle: u32, len: u32) -> &[u8] {
        if is_large_handle(handle) {
            let id = decode_large_handle(handle);
            let obj = self.large_objects[id as usize]
                .as_ref()
                .expect("large object read: slot empty");
            &obj.data[..len as usize]
        } else {
            let (tier_idx, slot_idx) = decode_handle(handle);
            self.tiers[tier_idx as usize].get(slot_idx, len as usize)
        }
    }

    /// Free a value by handle and length.
    pub fn free(&mut self, handle: u32, _len: u32) {
        if is_large_handle(handle) {
            let id = decode_large_handle(handle);
            self.large_objects[id as usize] = None;
            self.large_free.push(id);
        } else {
            let (tier_idx, slot_idx) = decode_handle(handle);
            self.tiers[tier_idx as usize].free(slot_idx);
        }
    }

    /// Total memory capacity across all tiers + large objects.
    pub fn capacity(&self) -> usize {
        let slab: usize = self.tiers.iter().map(|t| t.capacity_bytes()).sum();
        let large: usize = self
            .large_objects
            .iter()
            .filter_map(|o| o.as_ref().map(|lo| lo.data.len()))
            .sum();
        slab + large
    }

    /// Total bytes actively in use.
    pub fn used_bytes(&self) -> usize {
        let slab: usize = self.tiers.iter().map(|t| t.occupied_bytes()).sum();
        let large: usize = self
            .large_objects
            .iter()
            .filter_map(|o| o.as_ref().map(|lo| lo.data.len()))
            .sum();
        slab + large
    }

    /// Per-tier fragmentation statistics.
    pub fn tier_stats(&self) -> Vec<SlabTierStats> {
        self.tiers
            .iter()
            .map(|t| SlabTierStats {
                slot_size: t.slot_size,
                total_slots: t.total_slots,
                occupied_slots: t.occupied,
                capacity_bytes: t.capacity_bytes(),
                occupied_bytes: t.occupied_bytes(),
                fragmentation_ratio: t.fragmentation_ratio(),
            })
            .collect()
    }

    /// Overall fragmentation ratio: capacity / used_bytes. 1.0 = perfect.
    pub fn fragmentation_ratio(&self) -> f64 {
        let used = self.used_bytes();
        if used == 0 {
            return 0.0;
        }
        self.capacity() as f64 / used as f64
    }
}

impl Default for SlabAllocator {
    fn default() -> Self {
        Self::new()
    }
}

/// Per-tier statistics for observability.
#[derive(Debug, Clone)]
pub struct SlabTierStats {
    pub slot_size: usize,
    pub total_slots: usize,
    pub occupied_slots: usize,
    pub capacity_bytes: usize,
    pub occupied_bytes: usize,
    /// Ratio of capacity to occupied. 1.0 = no waste. Higher = more waste.
    pub fragmentation_ratio: f64,
}

// ---------------------------------------------------------------------------
// Handle encoding: pack tier_index + slot_index into a u32.
//
// Layout: bit 31 = large flag, bits 30:28 = tier_index (0-6), bits 27:0 = slot_index.
// Large objects: bit 31 = 1, bits 30:0 = large_object_id.
// ---------------------------------------------------------------------------

const LARGE_FLAG: u32 = 1 << 31;
const TIER_SHIFT: u32 = 28;
const SLOT_MASK: u32 = (1 << 28) - 1;

fn encode_handle(tier_idx: u8, slot_idx: u32) -> u32 {
    debug_assert!(tier_idx < 8);
    debug_assert!(slot_idx <= SLOT_MASK);
    ((tier_idx as u32) << TIER_SHIFT) | slot_idx
}

fn decode_handle(handle: u32) -> (u8, u32) {
    let tier_idx = ((handle >> TIER_SHIFT) & 0x7) as u8;
    let slot_idx = handle & SLOT_MASK;
    (tier_idx, slot_idx)
}

fn encode_large_handle(id: u32) -> u32 {
    LARGE_FLAG | id
}

fn decode_large_handle(handle: u32) -> u32 {
    handle & !LARGE_FLAG
}

fn is_large_handle(handle: u32) -> bool {
    handle & LARGE_FLAG != 0
}

/// Find the tier index for a given value size.
///
/// Returns the index of the smallest tier that can hold `size` bytes.
fn tier_index(size: usize) -> usize {
    for (i, &tier_size) in TIER_SIZES.iter().enumerate() {
        if size <= tier_size {
            return i;
        }
    }
    // Should not reach here for sizes <= MAX_SLAB_SIZE.
    TIER_SIZES.len() - 1
}

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

    #[test]
    fn tier_index_selection() {
        assert_eq!(tier_index(1), 0); // 1 byte → 64B tier
        assert_eq!(tier_index(64), 0); // 64 bytes → 64B tier
        assert_eq!(tier_index(65), 1); // 65 bytes → 128B tier
        assert_eq!(tier_index(128), 1);
        assert_eq!(tier_index(129), 2); // → 256B
        assert_eq!(tier_index(256), 2);
        assert_eq!(tier_index(512), 3);
        assert_eq!(tier_index(1024), 4);
        assert_eq!(tier_index(2048), 5);
        assert_eq!(tier_index(4096), 6);
    }

    #[test]
    fn handle_encoding_roundtrip() {
        for tier in 0..7u8 {
            for slot in [0, 1, 100, SLOT_MASK] {
                let handle = encode_handle(tier, slot);
                assert!(!is_large_handle(handle));
                let (t, s) = decode_handle(handle);
                assert_eq!(t, tier);
                assert_eq!(s, slot);
            }
        }
    }

    #[test]
    fn large_handle_encoding() {
        let handle = encode_large_handle(42);
        assert!(is_large_handle(handle));
        assert_eq!(decode_large_handle(handle), 42);
    }

    #[test]
    fn basic_alloc_get_free() {
        let mut slab = SlabAllocator::new();

        // Small value (fits in 64B tier).
        let (h1, l1) = slab.alloc(b"hello");
        assert_eq!(slab.get(h1, l1), b"hello");

        // Medium value (fits in 256B tier).
        let data = vec![0xAB; 200];
        let (h2, l2) = slab.alloc(&data);
        assert_eq!(slab.get(h2, l2), &data);

        // Free and realloc.
        slab.free(h1, l1);
        let (h3, l3) = slab.alloc(b"world");
        assert_eq!(slab.get(h3, l3), b"world");
        // Should reuse the freed slot.
        assert_eq!(h3, h1);
    }

    #[test]
    fn large_object_fallback() {
        let mut slab = SlabAllocator::new();

        let big = vec![0xFF; 8192]; // > 4KB → large object.
        let (h, l) = slab.alloc(&big);
        assert!(is_large_handle(h));
        assert_eq!(slab.get(h, l), &big);

        slab.free(h, l);
    }

    #[test]
    fn tier_grows_on_demand() {
        let mut slab = SlabAllocator::new();

        // Fill the 64B tier (initial 64 slots).
        let mut handles = Vec::new();
        for i in 0..100u32 {
            let data = i.to_be_bytes();
            let (h, l) = slab.alloc(&data);
            handles.push((h, l));
        }

        // All values readable.
        for (i, &(h, l)) in handles.iter().enumerate() {
            let expected = (i as u32).to_be_bytes();
            assert_eq!(slab.get(h, l), &expected);
        }
    }

    #[test]
    fn fragmentation_ratio_is_reasonable() {
        let mut slab = SlabAllocator::new();

        for i in 0..50u32 {
            slab.alloc(&i.to_be_bytes());
        }

        // 50 × 4 bytes stored in 64B slots → some waste expected.
        // Ratio includes all pre-allocated tiers (7 tiers × 64 slots each).
        let ratio = slab.fragmentation_ratio();
        assert!(ratio > 1.0); // Some waste expected (4B in 64B slots + pre-alloc'd empty tiers).
        // With 7 tiers × 64 slots = 28KB pre-allocated, and 50 × 64B = 3.2KB occupied,
        // the ratio can be up to ~150. This is expected for a freshly-initialized slab
        // with minimal usage — the pre-allocated tiers amortize across future allocations.
        assert!(ratio < 200.0);
    }

    #[test]
    fn free_and_reuse_large_objects() {
        let mut slab = SlabAllocator::new();

        let big1 = vec![1u8; 5000];
        let (h1, l1) = slab.alloc(&big1);
        slab.free(h1, l1);

        let big2 = vec![2u8; 6000];
        let (h2, _l2) = slab.alloc(&big2);
        // Should reuse the freed large-object slot ID.
        assert_eq!(decode_large_handle(h2), decode_large_handle(h1));
    }

    #[test]
    fn tier_stats_populated() {
        let mut slab = SlabAllocator::new();
        slab.alloc(b"small"); // 64B tier
        slab.alloc(&[0; 200]); // 256B tier

        let stats = slab.tier_stats();
        assert_eq!(stats.len(), TIER_SIZES.len());
        assert_eq!(stats[0].occupied_slots, 1); // 64B tier
        assert_eq!(stats[2].occupied_slots, 1); // 256B tier
        assert_eq!(stats[1].occupied_slots, 0); // 128B tier unused
    }

    #[test]
    fn used_bytes_and_capacity() {
        let mut slab = SlabAllocator::new();
        let initial_cap = slab.capacity();
        assert!(initial_cap > 0); // Pre-allocated tiers.
        assert_eq!(slab.used_bytes(), 0);

        slab.alloc(b"test");
        assert_eq!(slab.used_bytes(), 64); // 4 bytes in a 64B slot.
    }
}