reddb-io-server 1.1.0

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//! B+ Tree Node Structures
//!
//! Internal and leaf nodes for the B+ tree.

use super::version::{Snapshot, Timestamp, TxnId, VersionChain};
use std::fmt::Debug;
use std::sync::atomic::{AtomicU64, Ordering as AtomicOrdering};

/// Node ID type
pub type NodeId = u64;

/// Global node ID counter
static NODE_ID_COUNTER: AtomicU64 = AtomicU64::new(1);

/// Get next node ID
pub fn next_node_id() -> NodeId {
    NODE_ID_COUNTER.fetch_add(1, AtomicOrdering::SeqCst)
}

/// Node type discriminator
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NodeType {
    /// Internal node (keys + child pointers)
    Internal,
    /// Leaf node (keys + values)
    Leaf,
}

/// Generic B+ tree node
#[derive(Debug)]
pub enum Node<K, V>
where
    K: Clone + Ord + Debug,
    V: Clone + Debug,
{
    /// Internal node
    Internal(InternalNode<K, V>),
    /// Leaf node
    Leaf(LeafNode<K, V>),
}

impl<K, V> Node<K, V>
where
    K: Clone + Ord + Debug,
    V: Clone + Debug,
{
    /// Get node ID
    pub fn id(&self) -> NodeId {
        match self {
            Node::Internal(n) => n.id,
            Node::Leaf(n) => n.id,
        }
    }

    /// Get node type
    pub fn node_type(&self) -> NodeType {
        match self {
            Node::Internal(_) => NodeType::Internal,
            Node::Leaf(_) => NodeType::Leaf,
        }
    }

    /// Check if node is leaf
    pub fn is_leaf(&self) -> bool {
        matches!(self, Node::Leaf(_))
    }

    /// Get number of keys
    pub fn len(&self) -> usize {
        match self {
            Node::Internal(n) => n.keys.len(),
            Node::Leaf(n) => n.keys.len(),
        }
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Check if node is full
    pub fn is_full(&self, order: usize) -> bool {
        self.len() >= order - 1
    }

    /// Check if node has minimum keys
    pub fn has_min_keys(&self, order: usize) -> bool {
        let min = (order - 1) / 2;
        self.len() >= min
    }

    /// Get minimum key
    pub fn min_key(&self) -> Option<&K> {
        match self {
            Node::Internal(n) => n.keys.first(),
            Node::Leaf(n) => n.keys.first(),
        }
    }

    /// Get maximum key
    pub fn max_key(&self) -> Option<&K> {
        match self {
            Node::Internal(n) => n.keys.last(),
            Node::Leaf(n) => n.keys.last(),
        }
    }

    /// Get as internal node
    pub fn as_internal(&self) -> Option<&InternalNode<K, V>> {
        match self {
            Node::Internal(n) => Some(n),
            _ => None,
        }
    }

    /// Get as internal node mutable
    pub fn as_internal_mut(&mut self) -> Option<&mut InternalNode<K, V>> {
        match self {
            Node::Internal(n) => Some(n),
            _ => None,
        }
    }

    /// Get as leaf node
    pub fn as_leaf(&self) -> Option<&LeafNode<K, V>> {
        match self {
            Node::Leaf(n) => Some(n),
            _ => None,
        }
    }

    /// Get as leaf node mutable
    pub fn as_leaf_mut(&mut self) -> Option<&mut LeafNode<K, V>> {
        match self {
            Node::Leaf(n) => Some(n),
            _ => None,
        }
    }
}

/// Internal node (keys + child pointers)
#[derive(Debug)]
pub struct InternalNode<K, V>
where
    K: Clone + Ord + Debug,
    V: Clone + Debug,
{
    /// Node ID
    pub id: NodeId,
    /// Keys (separator keys)
    pub keys: Vec<K>,
    /// Child node IDs (len = keys.len() + 1)
    pub children: Vec<NodeId>,
    /// Phantom data for V
    _phantom: std::marker::PhantomData<V>,
}

impl<K, V> InternalNode<K, V>
where
    K: Clone + Ord + Debug,
    V: Clone + Debug,
{
    /// Create new internal node
    pub fn new() -> Self {
        Self {
            id: next_node_id(),
            keys: Vec::new(),
            children: Vec::new(),
            _phantom: std::marker::PhantomData,
        }
    }

    /// Create with initial child
    pub fn with_child(child_id: NodeId) -> Self {
        let mut node = Self::new();
        node.children.push(child_id);
        node
    }

    /// Find child index for key
    pub fn find_child_index(&self, key: &K) -> usize {
        match self.keys.binary_search(key) {
            Ok(i) => i + 1,
            Err(i) => i,
        }
    }

    /// Get child for key
    pub fn get_child(&self, key: &K) -> Option<NodeId> {
        let idx = self.find_child_index(key);
        self.children.get(idx).copied()
    }

    /// Insert key and child at position
    pub fn insert_at(&mut self, idx: usize, key: K, right_child: NodeId) {
        self.keys.insert(idx, key);
        self.children.insert(idx + 1, right_child);
    }

    /// Insert key and child in sorted order
    pub fn insert(&mut self, key: K, right_child: NodeId) {
        let idx = match self.keys.binary_search(&key) {
            Ok(i) | Err(i) => i,
        };
        self.insert_at(idx, key, right_child);
    }

    /// Remove key at index
    pub fn remove_at(&mut self, idx: usize) -> (K, NodeId) {
        let key = self.keys.remove(idx);
        let child = self.children.remove(idx + 1);
        (key, child)
    }

    /// Split node at middle
    pub fn split(&mut self) -> (K, Self) {
        let mid = self.keys.len() / 2;

        // Middle key goes up to parent
        let median_key = self.keys.remove(mid);

        // Right half becomes new node
        let right_keys: Vec<K> = self.keys.drain(mid..).collect();
        let right_children: Vec<NodeId> = self.children.drain(mid + 1..).collect();

        let mut right = Self::new();
        right.keys = right_keys;
        right.children = right_children;

        (median_key, right)
    }

    /// Merge with sibling
    pub fn merge(&mut self, separator: K, right: &mut Self) {
        self.keys.push(separator);
        self.keys.append(&mut right.keys);
        self.children.append(&mut right.children);
    }

    /// Borrow from left sibling
    ///
    /// # Invariant
    ///
    /// Caller must have verified that `left.keys.len() > MIN_KEYS` before
    /// invoking this function (see rebalance logic in `tree.rs`). Otherwise
    /// borrowing would violate the B-tree invariant on the sibling.
    pub fn borrow_from_left(&mut self, left: &mut Self, parent_key: &K) -> K {
        // Get rightmost key from left sibling
        let borrowed_key = left
            .keys
            .pop()
            .expect("invariant: borrow_from_left requires left.keys non-empty");
        let borrowed_child = left
            .children
            .pop()
            .expect("invariant: internal node has children.len() == keys.len() + 1");

        // Parent key comes down
        self.keys.insert(0, parent_key.clone());
        self.children.insert(0, borrowed_child);

        // Borrowed key goes up to parent
        borrowed_key
    }

    /// Borrow from right sibling
    pub fn borrow_from_right(&mut self, right: &mut Self, parent_key: &K) -> K {
        // Get leftmost key from right sibling
        let borrowed_key = right.keys.remove(0);
        let borrowed_child = right.children.remove(0);

        // Parent key comes down
        self.keys.push(parent_key.clone());
        self.children.push(borrowed_child);

        // Borrowed key goes up to parent
        borrowed_key
    }
}

impl<K, V> Default for InternalNode<K, V>
where
    K: Clone + Ord + Debug,
    V: Clone + Debug,
{
    fn default() -> Self {
        Self::new()
    }
}

/// Leaf node entry with MVCC versions
#[derive(Debug, Clone)]
pub struct LeafEntry<K, V>
where
    K: Clone + Ord + Debug,
    V: Clone + Debug,
{
    /// Key
    pub key: K,
    /// Version chain for the value
    pub versions: VersionChain<V>,
}

impl<K, V> LeafEntry<K, V>
where
    K: Clone + Ord + Debug,
    V: Clone + Debug,
{
    /// Create new entry
    pub fn new(key: K, value: V, txn_id: TxnId, timestamp: Timestamp) -> Self {
        Self {
            key,
            versions: VersionChain::with_value(value, txn_id, timestamp),
        }
    }

    /// Get value for snapshot
    pub fn get(&self, snapshot: &Snapshot) -> Option<&V> {
        self.versions.get(snapshot)
    }

    /// Update value
    pub fn update(&mut self, value: V, txn_id: TxnId, timestamp: Timestamp) {
        self.versions.update(value, txn_id, timestamp);
    }

    /// Delete entry
    pub fn delete(&mut self, txn_id: TxnId, timestamp: Timestamp) {
        self.versions.delete(txn_id, timestamp);
    }
}

/// Leaf node (keys + versioned values)
#[derive(Debug)]
pub struct LeafNode<K, V>
where
    K: Clone + Ord + Debug,
    V: Clone + Debug,
{
    /// Node ID
    pub id: NodeId,
    /// Keys
    pub keys: Vec<K>,
    /// Versioned values (parallel to keys)
    pub entries: Vec<LeafEntry<K, V>>,
    /// Next leaf (for range scans)
    pub next: Option<NodeId>,
    /// Previous leaf (for reverse scans)
    pub prev: Option<NodeId>,
}

impl<K, V> LeafNode<K, V>
where
    K: Clone + Ord + Debug,
    V: Clone + Debug,
{
    /// Create new leaf node
    pub fn new() -> Self {
        Self {
            id: next_node_id(),
            keys: Vec::new(),
            entries: Vec::new(),
            next: None,
            prev: None,
        }
    }

    /// Find index for key
    pub fn find_index(&self, key: &K) -> Result<usize, usize> {
        self.keys.binary_search(key)
    }

    /// Get value for key
    pub fn get(&self, key: &K, snapshot: &Snapshot) -> Option<&V> {
        match self.find_index(key) {
            Ok(idx) => self.entries[idx].get(snapshot),
            Err(_) => None,
        }
    }

    /// Check if key exists
    pub fn contains(&self, key: &K, snapshot: &Snapshot) -> bool {
        self.get(key, snapshot).is_some()
    }

    /// Insert key-value pair
    pub fn insert(&mut self, key: K, value: V, txn_id: TxnId, timestamp: Timestamp) -> bool {
        match self.find_index(&key) {
            Ok(idx) => {
                // Key exists, update
                self.entries[idx].update(value, txn_id, timestamp);
                false // No new key added
            }
            Err(idx) => {
                // New key
                let entry = LeafEntry::new(key.clone(), value, txn_id, timestamp);
                self.keys.insert(idx, key);
                self.entries.insert(idx, entry);
                true // New key added
            }
        }
    }

    /// Delete key
    pub fn delete(&mut self, key: &K, txn_id: TxnId, timestamp: Timestamp) -> bool {
        match self.find_index(key) {
            Ok(idx) => {
                self.entries[idx].delete(txn_id, timestamp);
                true
            }
            Err(_) => false,
        }
    }

    /// Split node at middle
    pub fn split(&mut self) -> (K, Self) {
        let mid = self.keys.len() / 2;

        // Right half becomes new node
        let right_keys: Vec<K> = self.keys.drain(mid..).collect();
        let right_entries: Vec<LeafEntry<K, V>> = self.entries.drain(mid..).collect();

        let mut right = Self::new();
        right.keys = right_keys.clone();
        right.entries = right_entries;

        // First key of right node is the separator
        let separator = right.keys[0].clone();

        // Link siblings
        right.next = self.next;
        right.prev = Some(self.id);
        self.next = Some(right.id);

        (separator, right)
    }

    /// Merge with right sibling
    pub fn merge(&mut self, right: &mut Self) {
        self.keys.append(&mut right.keys);
        self.entries.append(&mut right.entries);
        self.next = right.next;
    }

    /// Borrow from left sibling
    ///
    /// # Invariant
    ///
    /// Caller must have verified that `left.keys.len() > MIN_KEYS` before
    /// invoking this function. In leaf nodes `keys.len() == entries.len()`
    /// always, so one invariant check covers both pops.
    pub fn borrow_from_left(&mut self, left: &mut Self) -> K {
        let borrowed_key = left
            .keys
            .pop()
            .expect("invariant: borrow_from_left requires left.keys non-empty");
        let borrowed_entry = left
            .entries
            .pop()
            .expect("invariant: leaf node has keys.len() == entries.len()");

        self.keys.insert(0, borrowed_key.clone());
        self.entries.insert(0, borrowed_entry);

        self.keys[0].clone()
    }

    /// Borrow from right sibling
    pub fn borrow_from_right(&mut self, right: &mut Self) -> K {
        let borrowed_key = right.keys.remove(0);
        let borrowed_entry = right.entries.remove(0);

        self.keys.push(borrowed_key);
        self.entries.push(borrowed_entry);

        right.keys[0].clone()
    }

    /// Get all keys in range
    pub fn range<'a>(
        &'a self,
        start: Option<&K>,
        end: Option<&K>,
        snapshot: &'a Snapshot,
    ) -> impl Iterator<Item = (&'a K, &'a V)> {
        let start_idx = match start {
            Some(k) => match self.find_index(k) {
                Ok(i) => i,
                Err(i) => i,
            },
            None => 0,
        };

        let end_idx = match end {
            Some(k) => match self.find_index(k) {
                Ok(i) => i + 1,
                Err(i) => i,
            },
            None => self.keys.len(),
        };

        self.keys[start_idx..end_idx]
            .iter()
            .zip(self.entries[start_idx..end_idx].iter())
            .filter_map(move |(key, entry)| entry.get(snapshot).map(|v| (key, v)))
    }

    /// Garbage collect old versions
    pub fn gc(&mut self, watermark: Timestamp) -> usize {
        let mut removed = 0;
        for entry in &mut self.entries {
            removed += entry.versions.gc(watermark);
        }
        removed
    }
}

impl<K, V> Default for LeafNode<K, V>
where
    K: Clone + Ord + Debug,
    V: Clone + Debug,
{
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_internal_node_basic() {
        let mut node: InternalNode<i32, String> = InternalNode::new();
        assert!(node.keys.is_empty());
        assert!(node.children.is_empty());

        // Add a child first
        node.children.push(100);

        // Insert separator keys with children
        node.insert(5, 101);
        node.insert(10, 102);
        node.insert(15, 103);

        assert_eq!(node.keys, vec![5, 10, 15]);
        assert_eq!(node.children, vec![100, 101, 102, 103]);
    }

    #[test]
    fn test_internal_node_find_child() {
        let mut node: InternalNode<i32, String> = InternalNode::new();
        node.children.push(100);
        node.insert(10, 101);
        node.insert(20, 102);
        node.insert(30, 103);

        // Key < 10 goes to child 0
        assert_eq!(node.find_child_index(&5), 0);
        // Key = 10 goes to child 1
        assert_eq!(node.find_child_index(&10), 1);
        // Key 15 (10 < x < 20) goes to child 1
        assert_eq!(node.find_child_index(&15), 1);
        // Key >= 30 goes to child 3
        assert_eq!(node.find_child_index(&35), 3);
    }

    #[test]
    fn test_internal_node_split() {
        let mut node: InternalNode<i32, String> = InternalNode::new();
        node.children.push(100);
        for i in 1..=6 {
            node.insert(i * 10, 100 + i as u64);
        }

        let (median, right) = node.split();

        // Median goes up
        assert!(node.keys.len() < 6);
        assert!(!right.keys.is_empty());
    }

    #[test]
    fn test_leaf_node_basic() {
        let mut node: LeafNode<i32, String> = LeafNode::new();
        let snapshot = Snapshot::new(TxnId::ZERO, Timestamp(100));

        assert!(node.keys.is_empty());

        // Insert values
        node.insert(10, "ten".to_string(), TxnId(1), Timestamp(1));
        node.insert(20, "twenty".to_string(), TxnId(1), Timestamp(2));
        node.insert(5, "five".to_string(), TxnId(1), Timestamp(3));

        assert_eq!(node.keys, vec![5, 10, 20]);
        assert_eq!(node.get(&10, &snapshot), Some(&"ten".to_string()));
        assert_eq!(node.get(&15, &snapshot), None);
    }

    #[test]
    fn test_leaf_node_update() {
        let mut node: LeafNode<i32, String> = LeafNode::new();
        let snapshot = Snapshot::new(TxnId::ZERO, Timestamp(100));

        node.insert(10, "v1".to_string(), TxnId(1), Timestamp(1));
        assert_eq!(node.get(&10, &snapshot), Some(&"v1".to_string()));

        // Update same key
        node.insert(10, "v2".to_string(), TxnId(2), Timestamp(2));
        assert_eq!(node.get(&10, &snapshot), Some(&"v2".to_string()));

        // Only one key
        assert_eq!(node.keys.len(), 1);
    }

    #[test]
    fn test_leaf_node_delete() {
        let mut node: LeafNode<i32, String> = LeafNode::new();
        let snapshot = Snapshot::new(TxnId::ZERO, Timestamp(100));

        node.insert(10, "ten".to_string(), TxnId(1), Timestamp(1));
        assert!(node.contains(&10, &snapshot));

        node.delete(&10, TxnId(2), Timestamp(2));
        assert!(!node.contains(&10, &snapshot));
    }

    #[test]
    fn test_leaf_node_split() {
        let mut node: LeafNode<i32, String> = LeafNode::new();

        for i in 1..=6 {
            node.insert(i * 10, format!("v{}", i), TxnId(1), Timestamp(i as u64));
        }

        let (separator, right) = node.split();

        // Keys are split
        assert!(node.keys.len() < 6);
        assert!(!right.keys.is_empty());

        // Separator is first key of right
        assert_eq!(separator, right.keys[0]);

        // Siblings are linked
        assert_eq!(node.next, Some(right.id));
        assert_eq!(right.prev, Some(node.id));
    }

    #[test]
    fn test_leaf_node_range() {
        let mut node: LeafNode<i32, String> = LeafNode::new();
        let snapshot = Snapshot::new(TxnId::ZERO, Timestamp(100));

        for i in 1..=10 {
            node.insert(i * 10, format!("v{}", i), TxnId(1), Timestamp(i as u64));
        }

        // Range 25..75
        let results: Vec<_> = node.range(Some(&25), Some(&75), &snapshot).collect();
        assert_eq!(results.len(), 5); // 30, 40, 50, 60, 70
    }

    #[test]
    fn test_node_enum() {
        let leaf: Node<i32, String> = Node::Leaf(LeafNode::new());
        assert!(leaf.is_leaf());
        assert!(leaf.as_leaf().is_some());
        assert!(leaf.as_internal().is_none());

        let internal: Node<i32, String> = Node::Internal(InternalNode::new());
        assert!(!internal.is_leaf());
        assert!(internal.as_internal().is_some());
        assert!(internal.as_leaf().is_none());
    }
}