reddb-io-server 1.0.7

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
//! Block-level zone maps (a.k.a. min/max summaries, SMA indexes).
//!
//! A zone map is a small, per-block summary used to *skip* entire blocks
//! during scans. Given a query predicate, the planner consults the zone map
//! first; if the block cannot possibly contain a match, it is not read.
//! Data warehouses like Parquet, Clickhouse, and Snowflake rely on this for
//! order-of-magnitude pruning.
//!
//! RedDB already tracks `min_ts`/`max_ts` on timeseries chunks, but nothing
//! richer — and tables/graphs get nothing at all. This module provides a
//! single reusable [`ZoneMap`] that every segment can embed via
//! [`crate::storage::index::HasBloom`] semantics.
//!
//! # What it tracks
//!
//! - `min_key` / `max_key` — lexicographic bounds, lets range queries skip
//! - `total_count` — rows observed
//! - `null_count` — rows with a null in the indexed column
//! - `distinct_estimate` — via [`HyperLogLog`] (16 KB, ~0.81% std error)
//! - `bloom` — fast negative point lookup via [`BloomSegment`]
//!
//! # What it does not
//!
//! - Value distributions (histograms) — not yet.
//! - Row-level positions — zone maps are *block summaries*, not indexes.
//!
//! # Integration
//!
//! Blocks call [`ZoneMap::observe`] / [`ZoneMap::observe_null`] on every
//! insert. Planners call [`ZoneMap::block_skip`] with a predicate to decide
//! whether to read the block. Zone maps are mergeable via [`ZoneMap::union`]
//! so higher-level aggregates (segment → collection → shard) can be built
//! cheaply.

use crate::storage::index::bloom_segment::BloomSegment;
use crate::storage::index::stats::{IndexKind, IndexStats};
use crate::storage::index::{HasBloom, IndexBase};
use crate::storage::primitives::HyperLogLog;

/// Predicate the planner asks a zone map to evaluate.
#[derive(Debug, Clone)]
pub enum ZonePredicate<'a> {
    /// Equality: `column == key`
    Equals(&'a [u8]),
    /// Range: `start <= column <= end`. `None` on either side means open.
    Range {
        start: Option<&'a [u8]>,
        end: Option<&'a [u8]>,
    },
    /// Is null check: block must contain at least one null.
    IsNull,
    /// Is not null: block must contain at least one non-null value.
    IsNotNull,
}

/// Outcome of evaluating a zone map against a predicate.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ZoneDecision {
    /// The block *might* contain matching rows — read it.
    MustRead,
    /// The block cannot possibly contain matches — skip it.
    Skip,
}

/// Reusable per-block zone map.
pub struct ZoneMap {
    min_key: Option<Vec<u8>>,
    max_key: Option<Vec<u8>>,
    total_count: u64,
    null_count: u64,
    hll: HyperLogLog,
    bloom: BloomSegment,
}

impl ZoneMap {
    /// Create a zone map sized for `expected_rows`. The bloom is tuned to
    /// the row estimate; HLL is fixed-size (~16 KB).
    pub fn with_capacity(expected_rows: usize) -> Self {
        Self {
            min_key: None,
            max_key: None,
            total_count: 0,
            null_count: 0,
            hll: HyperLogLog::new(),
            bloom: BloomSegment::with_capacity(expected_rows.max(64)),
        }
    }

    /// Default: sized for 4 KB table pages (~128 rows).
    pub fn new() -> Self {
        Self::with_capacity(128)
    }

    /// Record a non-null value observation.
    pub fn observe(&mut self, key: &[u8]) {
        self.total_count = self.total_count.saturating_add(1);
        self.hll.add(key);
        self.bloom.insert(key);

        match &self.min_key {
            None => self.min_key = Some(key.to_vec()),
            Some(cur) if key < cur.as_slice() => self.min_key = Some(key.to_vec()),
            _ => {}
        }
        match &self.max_key {
            None => self.max_key = Some(key.to_vec()),
            Some(cur) if key > cur.as_slice() => self.max_key = Some(key.to_vec()),
            _ => {}
        }
    }

    /// Record a null observation. Does not touch the bloom / HLL since
    /// there is no key to hash.
    pub fn observe_null(&mut self) {
        self.total_count = self.total_count.saturating_add(1);
        self.null_count = self.null_count.saturating_add(1);
    }

    /// Minimum observed key, if any.
    pub fn min(&self) -> Option<&[u8]> {
        self.min_key.as_deref()
    }

    /// Maximum observed key, if any.
    pub fn max(&self) -> Option<&[u8]> {
        self.max_key.as_deref()
    }

    /// Total rows observed (including nulls).
    pub fn total_count(&self) -> u64 {
        self.total_count
    }

    /// Rows observed as null.
    pub fn null_count(&self) -> u64 {
        self.null_count
    }

    /// Rows observed as non-null.
    pub fn non_null_count(&self) -> u64 {
        self.total_count.saturating_sub(self.null_count)
    }

    /// Estimated distinct non-null values.
    pub fn distinct_estimate(&self) -> u64 {
        self.hll.count()
    }

    /// Access the underlying bloom (for cross-structure helpers).
    pub fn bloom(&self) -> &BloomSegment {
        &self.bloom
    }

    /// Decide whether to skip a block given a predicate.
    ///
    /// Safe by default: when uncertain, returns [`ZoneDecision::MustRead`].
    pub fn block_skip(&self, predicate: &ZonePredicate<'_>) -> ZoneDecision {
        // Empty block → trivially skippable.
        if self.total_count == 0 {
            return ZoneDecision::Skip;
        }

        match predicate {
            ZonePredicate::Equals(key) => {
                // Outside [min, max] window → skip.
                if let (Some(min), Some(max)) = (self.min(), self.max()) {
                    if *key < min || *key > max {
                        return ZoneDecision::Skip;
                    }
                }
                // Bloom says definitely absent → skip.
                if self.bloom.definitely_absent(key) {
                    return ZoneDecision::Skip;
                }
                ZoneDecision::MustRead
            }
            ZonePredicate::Range { start, end } => {
                if let (Some(a), Some(qend)) = (self.min(), end) {
                    if *qend < a {
                        return ZoneDecision::Skip;
                    }
                }
                if let (Some(b), Some(qstart)) = (self.max(), start) {
                    if *qstart > b {
                        return ZoneDecision::Skip;
                    }
                }
                ZoneDecision::MustRead
            }
            ZonePredicate::IsNull => {
                if self.null_count == 0 {
                    ZoneDecision::Skip
                } else {
                    ZoneDecision::MustRead
                }
            }
            ZonePredicate::IsNotNull => {
                if self.non_null_count() == 0 {
                    ZoneDecision::Skip
                } else {
                    ZoneDecision::MustRead
                }
            }
        }
    }

    /// Merge another zone map into this one (e.g. aggregating block-level
    /// maps into a segment-level summary).
    pub fn union(&mut self, other: &ZoneMap) {
        self.total_count = self.total_count.saturating_add(other.total_count);
        self.null_count = self.null_count.saturating_add(other.null_count);

        match (&self.min_key, &other.min_key) {
            (None, Some(o)) => self.min_key = Some(o.clone()),
            (Some(s), Some(o)) if o < s => self.min_key = Some(o.clone()),
            _ => {}
        }
        match (&self.max_key, &other.max_key) {
            (None, Some(o)) => self.max_key = Some(o.clone()),
            (Some(s), Some(o)) if o > s => self.max_key = Some(o.clone()),
            _ => {}
        }

        self.hll.merge(&other.hll);
        // BloomSegment::union_inplace fails when sizes differ; in that case
        // callers that care should rebuild the bloom. Zone-map union is a
        // best-effort aggregate.
        let _ = self.bloom.union_inplace(&other.bloom);
    }

    /// Reset to the empty state.
    pub fn clear(&mut self) {
        self.min_key = None;
        self.max_key = None;
        self.total_count = 0;
        self.null_count = 0;
        self.hll.clear();
        // Bloom can't be selectively cleared; replace it.
        self.bloom = BloomSegment::with_capacity(128);
    }
}

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

impl HasBloom for ZoneMap {
    fn bloom_segment(&self) -> Option<&BloomSegment> {
        Some(&self.bloom)
    }
}

impl IndexBase for ZoneMap {
    fn name(&self) -> &str {
        "zone_map"
    }

    fn kind(&self) -> IndexKind {
        IndexKind::ZoneMap
    }

    fn stats(&self) -> IndexStats {
        IndexStats {
            entries: self.total_count as usize,
            distinct_keys: self.distinct_estimate() as usize,
            approx_bytes: self.bloom.filter().byte_size() + 16 * 1024, // HLL fixed
            kind: IndexKind::ZoneMap,
            has_bloom: true,
            index_correlation: 0.0,
        }
    }

    fn bloom(&self) -> Option<&crate::storage::primitives::BloomFilter> {
        Some(self.bloom.filter())
    }
}

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

    #[test]
    fn tracks_min_max() {
        let mut zm = ZoneMap::with_capacity(64);
        zm.observe(b"delta");
        zm.observe(b"alpha");
        zm.observe(b"charlie");
        zm.observe(b"beta");
        assert_eq!(zm.min(), Some(b"alpha".as_slice()));
        assert_eq!(zm.max(), Some(b"delta".as_slice()));
        assert_eq!(zm.total_count(), 4);
        assert_eq!(zm.null_count(), 0);
    }

    #[test]
    fn tracks_nulls_separately() {
        let mut zm = ZoneMap::with_capacity(64);
        zm.observe(b"a");
        zm.observe_null();
        zm.observe_null();
        zm.observe(b"b");
        assert_eq!(zm.total_count(), 4);
        assert_eq!(zm.null_count(), 2);
        assert_eq!(zm.non_null_count(), 2);
    }

    #[test]
    fn distinct_estimate_approximates_cardinality() {
        let mut zm = ZoneMap::with_capacity(2000);
        for i in 0..1000 {
            zm.observe(format!("user{i}").as_bytes());
        }
        // Insert duplicates — should not inflate the estimate.
        for i in 0..1000 {
            zm.observe(format!("user{i}").as_bytes());
        }
        let est = zm.distinct_estimate();
        // HLL is ~0.81% std error — give it slack.
        assert!(est > 900 && est < 1100, "estimate={est}");
    }

    #[test]
    fn block_skip_equality_out_of_range() {
        let mut zm = ZoneMap::with_capacity(64);
        zm.observe(b"mango");
        zm.observe(b"orange");
        zm.observe(b"peach");
        // Below min → skip.
        assert_eq!(
            zm.block_skip(&ZonePredicate::Equals(b"apple")),
            ZoneDecision::Skip
        );
        // Above max → skip.
        assert_eq!(
            zm.block_skip(&ZonePredicate::Equals(b"strawberry")),
            ZoneDecision::Skip
        );
        // In range, inserted → must read.
        assert_eq!(
            zm.block_skip(&ZonePredicate::Equals(b"mango")),
            ZoneDecision::MustRead
        );
    }

    #[test]
    fn block_skip_equality_bloom_prune() {
        let mut zm = ZoneMap::with_capacity(1024);
        zm.observe(b"alpha");
        zm.observe(b"zulu");
        // "needle" is inside [alpha, zulu] lexicographically, so range
        // check alone can't skip — bloom must prove absence.
        let decision = zm.block_skip(&ZonePredicate::Equals(b"needle"));
        // Bloom is probabilistic; it *usually* prunes an unseen key.
        // Either outcome is safe, but must-read for an absent key is
        // still correct behavior.
        assert!(matches!(
            decision,
            ZoneDecision::Skip | ZoneDecision::MustRead
        ));
    }

    #[test]
    fn block_skip_range_non_overlapping() {
        let mut zm = ZoneMap::with_capacity(64);
        zm.observe(&10u32.to_be_bytes());
        zm.observe(&50u32.to_be_bytes());
        zm.observe(&100u32.to_be_bytes());

        let lo = 200u32.to_be_bytes();
        let hi = 300u32.to_be_bytes();
        assert_eq!(
            zm.block_skip(&ZonePredicate::Range {
                start: Some(&lo),
                end: Some(&hi),
            }),
            ZoneDecision::Skip
        );

        let qlo = 40u32.to_be_bytes();
        let qhi = 60u32.to_be_bytes();
        assert_eq!(
            zm.block_skip(&ZonePredicate::Range {
                start: Some(&qlo),
                end: Some(&qhi),
            }),
            ZoneDecision::MustRead
        );
    }

    #[test]
    fn block_skip_null_predicates() {
        let mut empty_nulls = ZoneMap::with_capacity(64);
        empty_nulls.observe(b"x");
        assert_eq!(
            empty_nulls.block_skip(&ZonePredicate::IsNull),
            ZoneDecision::Skip
        );
        assert_eq!(
            empty_nulls.block_skip(&ZonePredicate::IsNotNull),
            ZoneDecision::MustRead
        );

        let mut all_nulls = ZoneMap::with_capacity(64);
        all_nulls.observe_null();
        all_nulls.observe_null();
        assert_eq!(
            all_nulls.block_skip(&ZonePredicate::IsNull),
            ZoneDecision::MustRead
        );
        assert_eq!(
            all_nulls.block_skip(&ZonePredicate::IsNotNull),
            ZoneDecision::Skip
        );
    }

    #[test]
    fn empty_block_skips_everything() {
        let zm = ZoneMap::with_capacity(64);
        assert_eq!(
            zm.block_skip(&ZonePredicate::Equals(b"whatever")),
            ZoneDecision::Skip
        );
        assert_eq!(
            zm.block_skip(&ZonePredicate::Range {
                start: None,
                end: None,
            }),
            ZoneDecision::Skip
        );
    }

    #[test]
    fn union_merges_bounds_and_counts() {
        let mut a = ZoneMap::with_capacity(256);
        a.observe(b"cherry");
        a.observe(b"apple");
        a.observe_null();

        let mut b = ZoneMap::with_capacity(256);
        b.observe(b"zebra");
        b.observe(b"banana");

        a.union(&b);
        assert_eq!(a.min(), Some(b"apple".as_slice()));
        assert_eq!(a.max(), Some(b"zebra".as_slice()));
        assert_eq!(a.total_count(), 5);
        assert_eq!(a.null_count(), 1);
        assert!(a.distinct_estimate() >= 4);
    }

    #[test]
    fn stats_match_observation_counts() {
        let mut zm = ZoneMap::with_capacity(64);
        for i in 0..50u32 {
            zm.observe(&i.to_be_bytes());
        }
        let s = zm.stats();
        assert_eq!(s.entries, 50);
        assert_eq!(s.kind, IndexKind::ZoneMap);
        assert!(s.has_bloom);
    }

    #[test]
    fn clear_resets_all_state() {
        let mut zm = ZoneMap::with_capacity(64);
        zm.observe(b"x");
        zm.observe_null();
        zm.clear();
        assert_eq!(zm.total_count(), 0);
        assert_eq!(zm.null_count(), 0);
        assert_eq!(zm.min(), None);
        assert_eq!(zm.max(), None);
    }
}