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
//! Column statistics for cost-based query optimization.
//!
//! Maintains per-collection, per-field statistics in redb metadata tables,
//! updated incrementally on writes. Used by the CBO to select join strategies,
//! estimate result cardinality, and choose scan methods.

use std::sync::Arc;

use redb::{Database, ReadableTable, TableDefinition, WriteTransaction};
use serde::{Deserialize, Serialize};

/// Redb table for column statistics.
/// Key: "{tenant}:{collection}:{field}" → Value: serialized ColumnStats.
const COLUMN_STATS: TableDefinition<&str, &[u8]> = TableDefinition::new("column_stats");

/// Statistics for a single column in a collection.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ColumnStats {
    /// Total number of documents observed (including those without this field).
    pub row_count: u64,
    /// Number of documents that have this field with a non-null value.
    pub non_null_count: u64,
    /// Number of null values (field absent or explicitly null).
    pub null_count: u64,
    /// Approximate number of distinct values (HyperLogLog estimate).
    pub distinct_count: u64,
    /// Minimum value observed (as JSON string for cross-type comparison).
    pub min_value: Option<String>,
    /// Maximum value observed (as JSON string for cross-type comparison).
    pub max_value: Option<String>,
    /// HyperLogLog registers for cardinality estimation.
    /// 256 registers (m=256) give ~6.5% standard error, good enough for CBO.
    pub hll_registers: Vec<u8>,
}

/// Default number of HLL registers. 256 = 2^8, giving ~6.5% standard error.
/// Sourced from `SparseTuning::hll_registers` at runtime.
pub(crate) const DEFAULT_HLL_M: usize = 256;
/// Default HLL precision bits (log2 of `DEFAULT_HLL_M`).
/// Sourced from `SparseTuning::hll_precision` at runtime.
pub(crate) const DEFAULT_HLL_P: u32 = 8;

impl ColumnStats {
    /// Create empty statistics for a new column.
    pub fn new() -> Self {
        Self {
            row_count: 0,
            non_null_count: 0,
            null_count: 0,
            distinct_count: 0,
            min_value: None,
            max_value: None,
            hll_registers: vec![0u8; DEFAULT_HLL_M],
        }
    }

    /// Update statistics with a new observed value.
    ///
    /// Call this on every write (PointPut) for each field in the document.
    pub fn observe(&mut self, value: Option<&serde_json::Value>) {
        self.row_count += 1;

        match value {
            None | Some(serde_json::Value::Null) => {
                self.null_count += 1;
            }
            Some(val) => {
                self.non_null_count += 1;

                // Update min/max.
                let val_str = match val {
                    serde_json::Value::String(s) => s.clone(),
                    other => other.to_string(),
                };
                match &self.min_value {
                    None => self.min_value = Some(val_str.clone()),
                    Some(min) if val_str < *min => self.min_value = Some(val_str.clone()),
                    _ => {}
                }
                match &self.max_value {
                    None => self.max_value = Some(val_str.clone()),
                    Some(max) if val_str > *max => self.max_value = Some(val_str.clone()),
                    _ => {}
                }

                // Update HyperLogLog for cardinality estimation.
                let hash = Self::hash_value(&val_str);
                let register_idx = (hash as usize) & (DEFAULT_HLL_M - 1);
                let remaining = hash >> DEFAULT_HLL_P;
                let leading_zeros = if remaining == 0 {
                    (64 - DEFAULT_HLL_P) as u8
                } else {
                    remaining.trailing_zeros() as u8 + 1
                };
                if leading_zeros > self.hll_registers[register_idx] {
                    self.hll_registers[register_idx] = leading_zeros;
                }

                // Re-estimate distinct count from HLL registers.
                self.distinct_count = self.hll_estimate();
            }
        }
    }

    /// HyperLogLog cardinality estimate.
    fn hll_estimate(&self) -> u64 {
        let m = self.hll_registers.len() as f64;
        // Alpha constant for m=256.
        let alpha = 0.7213 / (1.0 + 1.079 / m);
        let raw: f64 = alpha * m * m
            / self
                .hll_registers
                .iter()
                .map(|&r| 2.0_f64.powi(-(r as i32)))
                .sum::<f64>();

        if raw <= 2.5 * m {
            // Small range correction.
            let zeros = self.hll_registers.iter().filter(|&&r| r == 0).count() as f64;
            if zeros > 0.0 {
                (m * (m / zeros).ln()) as u64
            } else {
                raw as u64
            }
        } else {
            raw as u64
        }
    }

    /// Simple hash function for HLL (FNV-1a 64-bit).
    fn hash_value(s: &str) -> u64 {
        let mut hash: u64 = 0xcbf29ce484222325;
        for byte in s.as_bytes() {
            hash ^= *byte as u64;
            hash = hash.wrapping_mul(0x100000001b3);
        }
        hash
    }

    /// Selectivity estimate for equality predicate (1 / distinct_count).
    pub fn eq_selectivity(&self) -> f64 {
        if self.distinct_count == 0 {
            1.0
        } else {
            1.0 / self.distinct_count as f64
        }
    }

    /// Selectivity estimate for range predicate (heuristic: 0.33).
    pub fn range_selectivity(&self) -> f64 {
        0.33
    }
}

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

/// Column statistics store backed by redb.
pub struct StatsStore {
    db: Arc<Database>,
}

impl StatsStore {
    /// Open or create the stats store sharing a redb database.
    pub fn open(db: Arc<Database>) -> crate::Result<Self> {
        // Ensure the table exists.
        let write_txn = db.begin_write().map_err(|e| crate::Error::Storage {
            engine: "stats".into(),
            detail: format!("open write txn: {e}"),
        })?;
        {
            let _ = write_txn.open_table(COLUMN_STATS);
        }
        write_txn.commit().map_err(|e| crate::Error::Storage {
            engine: "stats".into(),
            detail: format!("commit: {e}"),
        })?;
        Ok(Self { db })
    }

    /// Load statistics for a column.
    pub fn get(
        &self,
        tenant_id: u32,
        collection: &str,
        field: &str,
    ) -> crate::Result<Option<ColumnStats>> {
        let key = format!("{tenant_id}:{collection}:{field}");
        let read_txn = self.db.begin_read().map_err(|e| crate::Error::Storage {
            engine: "stats".into(),
            detail: format!("read txn: {e}"),
        })?;
        let table = read_txn
            .open_table(COLUMN_STATS)
            .map_err(|e| crate::Error::Storage {
                engine: "stats".into(),
                detail: format!("open table: {e}"),
            })?;
        match table.get(key.as_str()) {
            Ok(Some(guard)) => {
                let bytes = guard.value();
                let stats: ColumnStats =
                    rmp_serde::from_slice(bytes).map_err(|e| crate::Error::Storage {
                        engine: "stats".into(),
                        detail: format!("deserialize: {e}"),
                    })?;
                Ok(Some(stats))
            }
            Ok(None) => Ok(None),
            Err(e) => Err(crate::Error::Storage {
                engine: "stats".into(),
                detail: format!("get: {e}"),
            }),
        }
    }

    /// Persist updated statistics for a column.
    pub fn put(
        &self,
        tenant_id: u32,
        collection: &str,
        field: &str,
        stats: &ColumnStats,
    ) -> crate::Result<()> {
        let key = format!("{tenant_id}:{collection}:{field}");
        let bytes = rmp_serde::to_vec_named(stats).map_err(|e| crate::Error::Storage {
            engine: "stats".into(),
            detail: format!("serialize: {e}"),
        })?;
        let write_txn = self.db.begin_write().map_err(|e| crate::Error::Storage {
            engine: "stats".into(),
            detail: format!("write txn: {e}"),
        })?;
        {
            let mut table =
                write_txn
                    .open_table(COLUMN_STATS)
                    .map_err(|e| crate::Error::Storage {
                        engine: "stats".into(),
                        detail: format!("open table: {e}"),
                    })?;
            table
                .insert(key.as_str(), bytes.as_slice())
                .map_err(|e| crate::Error::Storage {
                    engine: "stats".into(),
                    detail: format!("insert: {e}"),
                })?;
        }
        write_txn.commit().map_err(|e| crate::Error::Storage {
            engine: "stats".into(),
            detail: format!("commit: {e}"),
        })?;
        Ok(())
    }

    /// Update statistics incrementally for a document's fields.
    ///
    /// Called on every PointPut. Loads existing stats for each field,
    /// observes the new value, and persists.
    pub fn observe_document(
        &self,
        tenant_id: u32,
        collection: &str,
        doc: &serde_json::Value,
    ) -> crate::Result<()> {
        if let Some(obj) = doc.as_object() {
            for (field, value) in obj {
                let mut stats = self.get(tenant_id, collection, field)?.unwrap_or_default();
                stats.observe(Some(value));
                self.put(tenant_id, collection, field, &stats)?;
            }
        }
        Ok(())
    }

    /// Update statistics within an externally-owned write transaction.
    ///
    /// Opens the COLUMN_STATS table once and reads/writes all fields in a
    /// single table open, eliminating per-field transaction overhead.
    pub fn observe_document_in_txn(
        &self,
        txn: &WriteTransaction,
        tenant_id: u32,
        collection: &str,
        doc: &serde_json::Value,
    ) -> crate::Result<()> {
        let Some(obj) = doc.as_object() else {
            return Ok(());
        };
        if obj.is_empty() {
            return Ok(());
        }

        let mut table = txn
            .open_table(COLUMN_STATS)
            .map_err(|e| crate::Error::Storage {
                engine: "stats".into(),
                detail: format!("open table: {e}"),
            })?;

        for (field, value) in obj {
            let key = format!("{tenant_id}:{collection}:{field}");

            // Read existing stats from the same write transaction.
            let mut stats: ColumnStats = table
                .get(key.as_str())
                .ok()
                .flatten()
                .and_then(|guard| rmp_serde::from_slice(guard.value()).ok())
                .unwrap_or_default();

            stats.observe(Some(value));

            let bytes = rmp_serde::to_vec_named(&stats).map_err(|e| crate::Error::Storage {
                engine: "stats".into(),
                detail: format!("serialize: {e}"),
            })?;
            table
                .insert(key.as_str(), bytes.as_slice())
                .map_err(|e| crate::Error::Storage {
                    engine: "stats".into(),
                    detail: format!("insert: {e}"),
                })?;
        }

        Ok(())
    }
}

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

    #[test]
    fn hll_cardinality_estimate() {
        let mut stats = ColumnStats::new();
        for i in 0..1000 {
            stats.observe(Some(&serde_json::Value::String(format!("value_{i}"))));
        }
        // HLL with 256 registers should be within ~20% of 1000.
        assert!(
            stats.distinct_count > 700,
            "too low: {}",
            stats.distinct_count
        );
        assert!(
            stats.distinct_count < 1400,
            "too high: {}",
            stats.distinct_count
        );
    }

    #[test]
    fn min_max_tracking() {
        let mut stats = ColumnStats::new();
        for v in &["charlie", "alice", "bob"] {
            stats.observe(Some(&serde_json::Value::String(v.to_string())));
        }
        assert_eq!(stats.min_value.as_deref(), Some("alice"));
        assert_eq!(stats.max_value.as_deref(), Some("charlie"));
        assert_eq!(stats.non_null_count, 3);
        assert_eq!(stats.null_count, 0);
    }

    #[test]
    fn null_tracking() {
        let mut stats = ColumnStats::new();
        stats.observe(None);
        stats.observe(Some(&serde_json::Value::Null));
        stats.observe(Some(&serde_json::Value::String("val".into())));
        assert_eq!(stats.null_count, 2);
        assert_eq!(stats.non_null_count, 1);
        assert_eq!(stats.row_count, 3);
    }

    #[test]
    fn eq_selectivity() {
        let mut stats = ColumnStats::new();
        for i in 0..100 {
            stats.observe(Some(&serde_json::Value::String(format!("v{i}"))));
        }
        let sel = stats.eq_selectivity();
        assert!(sel > 0.005 && sel < 0.02, "selectivity: {sel}");
    }

    #[test]
    fn stats_store_roundtrip() {
        let dir = tempfile::tempdir().unwrap();
        let db = Arc::new(Database::create(dir.path().join("stats.redb")).unwrap());
        let store = StatsStore::open(db).unwrap();

        let mut stats = ColumnStats::new();
        stats.observe(Some(&serde_json::Value::String("hello".into())));
        store.put(1, "users", "name", &stats).unwrap();

        let loaded = store.get(1, "users", "name").unwrap().unwrap();
        assert_eq!(loaded.row_count, 1);
        assert_eq!(loaded.non_null_count, 1);
        assert_eq!(loaded.min_value, Some("hello".to_string()));
    }
}