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
//! Global Secondary Indexes (GSI) for cross-shard index lookups.
//!
//! A GSI is a hidden redb table sharded by the indexed column's value.
//! It maps `{index_name}:{value}` → `Vec<(tenant_id, collection, doc_id, shard_location)>`.
//!
//! This allows queries like `WHERE email = 'alice@example.com'` to be
//! resolved via a single index lookup instead of scanning all shards.
//!
//! Constraints: max 4 GSIs per collection to limit write amplification
//! (each document write updates all GSIs).

use std::sync::Arc;

use redb::{Database, ReadableTable, TableDefinition};
use tracing::debug;

/// GSI table: key = "{index_name}:{value}" → MessagePack Vec<GsiEntry>.
const GSI_TABLE: TableDefinition<&str, &[u8]> = TableDefinition::new("gsi.entries");

/// GSI metadata: key = "{tenant}:{collection}:{index_name}" → MessagePack GsiMeta.
const GSI_META: TableDefinition<&str, &[u8]> = TableDefinition::new("gsi.meta");

/// Default maximum GSIs per collection. Sourced from `SparseTuning::max_gsis_per_collection`.
pub const DEFAULT_MAX_GSIS_PER_COLLECTION: usize = 4;

/// A single GSI entry pointing to the document's primary location.
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
pub struct GsiEntry {
    pub tenant_id: u32,
    pub collection: String,
    pub document_id: String,
    /// vShard where the primary document lives.
    pub shard_id: u16,
}

/// GSI metadata for a declared index.
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
pub struct GsiMeta {
    pub index_name: String,
    pub collection: String,
    pub field: String,
    /// Additional payload columns stored in the GSI for covering-index
    /// queries. If `payload_fields` covers all projected columns, the
    /// query can be served directly from the GSI without a primary hop.
    pub payload_fields: Vec<String>,
    pub tenant_id: u32,
}

/// GSI store backed by redb.
pub struct GsiStore {
    db: Arc<Database>,
    /// Maximum GSIs per collection. Set from `SparseTuning::max_gsis_per_collection`.
    max_gsis: usize,
}

impl GsiStore {
    /// Open or create the GSI store.
    pub fn open(db: Arc<Database>) -> crate::Result<Self> {
        let write_txn = db.begin_write().map_err(|e| crate::Error::Storage {
            engine: "gsi".into(),
            detail: format!("open: {e}"),
        })?;
        {
            let _ = write_txn.open_table(GSI_TABLE);
            let _ = write_txn.open_table(GSI_META);
        }
        write_txn.commit().map_err(|e| crate::Error::Storage {
            engine: "gsi".into(),
            detail: format!("commit: {e}"),
        })?;
        Ok(Self {
            db,
            max_gsis: DEFAULT_MAX_GSIS_PER_COLLECTION,
        })
    }

    /// Declare a new GSI on a collection field.
    ///
    /// Enforces the max 4 GSIs per collection limit.
    pub fn create_index(
        &self,
        tenant_id: u32,
        collection: &str,
        index_name: &str,
        field: &str,
        payload_fields: Vec<String>,
    ) -> crate::Result<()> {
        // Check limit.
        let existing = self.list_indexes(tenant_id, collection)?;
        if existing.len() >= self.max_gsis {
            return Err(crate::Error::Storage {
                engine: "gsi".into(),
                detail: format!(
                    "collection '{collection}' already has {} GSIs (max)",
                    self.max_gsis
                ),
            });
        }

        let meta = GsiMeta {
            index_name: index_name.to_string(),
            collection: collection.to_string(),
            field: field.to_string(),
            payload_fields,
            tenant_id,
        };
        let meta_key = format!("{tenant_id}:{collection}:{index_name}");
        let meta_bytes =
            rmp_serde::to_vec_named(&meta).map_err(|e| crate::Error::Serialization {
                format: "msgpack".into(),
                detail: format!("gsi meta: {e}"),
            })?;

        let write_txn = self.db.begin_write().map_err(|e| crate::Error::Storage {
            engine: "gsi".into(),
            detail: format!("write: {e}"),
        })?;
        {
            let mut table = write_txn
                .open_table(GSI_META)
                .map_err(|e| crate::Error::Storage {
                    engine: "gsi".into(),
                    detail: format!("open meta: {e}"),
                })?;
            table
                .insert(meta_key.as_str(), meta_bytes.as_slice())
                .map_err(|e| crate::Error::Storage {
                    engine: "gsi".into(),
                    detail: format!("insert meta: {e}"),
                })?;
        }
        write_txn.commit().map_err(|e| crate::Error::Storage {
            engine: "gsi".into(),
            detail: format!("commit: {e}"),
        })?;

        debug!(tenant_id, collection, index_name, field, "GSI created");
        Ok(())
    }

    /// Index a document's field value in all applicable GSIs.
    ///
    /// Called on every PointPut. For each GSI declared on this collection,
    /// extracts the indexed field value and inserts/updates the GSI entry.
    pub fn index_document(
        &self,
        tenant_id: u32,
        collection: &str,
        document_id: &str,
        shard_id: u16,
        doc: &serde_json::Value,
        indexes: &[GsiMeta],
    ) -> crate::Result<()> {
        if indexes.is_empty() {
            return Ok(());
        }

        let write_txn = self.db.begin_write().map_err(|e| crate::Error::Storage {
            engine: "gsi".into(),
            detail: format!("write: {e}"),
        })?;
        {
            let mut table = write_txn
                .open_table(GSI_TABLE)
                .map_err(|e| crate::Error::Storage {
                    engine: "gsi".into(),
                    detail: format!("open entries: {e}"),
                })?;

            for idx_meta in indexes {
                let value = doc.get(&idx_meta.field).and_then(|v| match v {
                    serde_json::Value::String(s) => Some(s.clone()),
                    serde_json::Value::Number(n) => Some(n.to_string()),
                    serde_json::Value::Bool(b) => Some(b.to_string()),
                    _ => None,
                });
                let Some(value_str) = value else { continue };

                let key = format!("{}:{}", idx_meta.index_name, value_str);
                let mut entries: Vec<GsiEntry> = match table.get(key.as_str()) {
                    Ok(Some(guard)) => match rmp_serde::from_slice(guard.value()) {
                        Ok(v) => v,
                        Err(e) => {
                            tracing::warn!(index = %key, error = %e, "GSI entry deserialization failed, starting fresh");
                            Vec::new()
                        }
                    },
                    _ => Vec::new(),
                };

                // Remove existing entry for this doc (update case).
                entries.retain(|e| e.document_id != document_id);
                entries.push(GsiEntry {
                    tenant_id,
                    collection: collection.to_string(),
                    document_id: document_id.to_string(),
                    shard_id,
                });

                let bytes =
                    rmp_serde::to_vec_named(&entries).map_err(|e| crate::Error::Serialization {
                        format: "msgpack".into(),
                        detail: format!("gsi entries: {e}"),
                    })?;
                table.insert(key.as_str(), bytes.as_slice()).map_err(|e| {
                    crate::Error::Storage {
                        engine: "gsi".into(),
                        detail: format!("insert entry: {e}"),
                    }
                })?;
            }
        }
        write_txn.commit().map_err(|e| crate::Error::Storage {
            engine: "gsi".into(),
            detail: format!("commit: {e}"),
        })?;
        Ok(())
    }

    /// Look up documents by GSI value. Returns entries pointing to primary locations.
    pub fn lookup(&self, index_name: &str, value: &str) -> crate::Result<Vec<GsiEntry>> {
        let key = format!("{index_name}:{value}");
        let read_txn = self.db.begin_read().map_err(|e| crate::Error::Storage {
            engine: "gsi".into(),
            detail: format!("read: {e}"),
        })?;
        let table = read_txn
            .open_table(GSI_TABLE)
            .map_err(|e| crate::Error::Storage {
                engine: "gsi".into(),
                detail: format!("open entries: {e}"),
            })?;

        match table.get(key.as_str()) {
            Ok(Some(guard)) => {
                let entries: Vec<GsiEntry> = match rmp_serde::from_slice(guard.value()) {
                    Ok(v) => v,
                    Err(e) => {
                        tracing::warn!(index = %key, error = %e, "GSI lookup deserialization failed");
                        Vec::new()
                    }
                };
                Ok(entries)
            }
            Ok(None) => Ok(Vec::new()),
            Err(e) => Err(crate::Error::Storage {
                engine: "gsi".into(),
                detail: format!("get: {e}"),
            }),
        }
    }

    /// List all GSIs declared for a collection.
    pub fn list_indexes(&self, tenant_id: u32, collection: &str) -> crate::Result<Vec<GsiMeta>> {
        let prefix = format!("{tenant_id}:{collection}:");
        let end = format!("{tenant_id}:{collection}:\u{ffff}");

        let read_txn = self.db.begin_read().map_err(|e| crate::Error::Storage {
            engine: "gsi".into(),
            detail: format!("read: {e}"),
        })?;
        let table = read_txn
            .open_table(GSI_META)
            .map_err(|e| crate::Error::Storage {
                engine: "gsi".into(),
                detail: format!("open meta: {e}"),
            })?;

        let range =
            table
                .range(prefix.as_str()..end.as_str())
                .map_err(|e| crate::Error::Storage {
                    engine: "gsi".into(),
                    detail: format!("range: {e}"),
                })?;

        let mut result = Vec::new();
        for entry in range {
            let entry = entry.map_err(|e| crate::Error::Storage {
                engine: "gsi".into(),
                detail: format!("entry: {e}"),
            })?;
            match rmp_serde::from_slice::<GsiMeta>(entry.1.value()) {
                Ok(meta) => result.push(meta),
                Err(e) => {
                    tracing::warn!(
                        key = %entry.0.value(),
                        error = %e,
                        "GSI metadata deserialization failed, skipping entry"
                    );
                }
            }
        }
        Ok(result)
    }

    /// Check if a GSI covers all projected columns (covering index query).
    ///
    /// A covering index includes the indexed field + all payload_fields.
    /// If the query projection is a subset, we can serve the query from
    /// the GSI without a primary document fetch.
    pub fn is_covering(meta: &GsiMeta, projection: &[String]) -> bool {
        if projection.is_empty() {
            return false; // SELECT * always needs primary.
        }
        projection.iter().all(|col| {
            col == &meta.field
                || col == "id"
                || col == "document_id"
                || meta.payload_fields.contains(col)
        })
    }
}

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

    fn open_store() -> (GsiStore, tempfile::TempDir) {
        let dir = tempfile::tempdir().unwrap();
        let db = Arc::new(Database::create(dir.path().join("gsi.redb")).unwrap());
        let store = GsiStore::open(db).unwrap();
        (store, dir)
    }

    #[test]
    fn create_and_lookup() {
        let (store, _dir) = open_store();
        store
            .create_index(1, "users", "email_idx", "email", vec![])
            .unwrap();

        let doc = serde_json::json!({"email": "alice@example.com", "name": "Alice"});
        let indexes = store.list_indexes(1, "users").unwrap();
        store
            .index_document(1, "users", "u1", 0, &doc, &indexes)
            .unwrap();

        let results = store.lookup("email_idx", "alice@example.com").unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].document_id, "u1");
    }

    #[test]
    fn max_gsi_limit() {
        let (store, _dir) = open_store();
        for i in 0..DEFAULT_MAX_GSIS_PER_COLLECTION {
            store
                .create_index(1, "t", &format!("idx{i}"), &format!("f{i}"), vec![])
                .unwrap();
        }
        // One over the limit should fail.
        let result = store.create_index(1, "t", "idx_extra", "f_extra", vec![]);
        assert!(result.is_err());
    }

    #[test]
    fn covering_index_check() {
        let meta = GsiMeta {
            index_name: "email_idx".into(),
            collection: "users".into(),
            field: "email".into(),
            payload_fields: vec!["name".into(), "status".into()],
            tenant_id: 1,
        };
        assert!(GsiStore::is_covering(
            &meta,
            &["email".into(), "name".into()]
        ));
        assert!(GsiStore::is_covering(
            &meta,
            &["id".into(), "email".into(), "status".into()]
        ));
        assert!(!GsiStore::is_covering(
            &meta,
            &["email".into(), "age".into()]
        ));
        assert!(!GsiStore::is_covering(&meta, &[])); // SELECT *
    }

    #[test]
    fn update_replaces_entry() {
        let (store, _dir) = open_store();
        store
            .create_index(1, "users", "email_idx", "email", vec![])
            .unwrap();
        let indexes = store.list_indexes(1, "users").unwrap();

        let doc1 = serde_json::json!({"email": "old@example.com"});
        store
            .index_document(1, "users", "u1", 0, &doc1, &indexes)
            .unwrap();

        let doc2 = serde_json::json!({"email": "new@example.com"});
        store
            .index_document(1, "users", "u1", 0, &doc2, &indexes)
            .unwrap();

        // Old value still has the stale entry because we only deduplicate
        // within the same key. A separate cleanup handles stale entries.
        let _old = store.lookup("email_idx", "old@example.com").unwrap();
        // The new value should have the entry.
        let new = store.lookup("email_idx", "new@example.com").unwrap();
        assert_eq!(new.len(), 1);
        assert_eq!(new[0].document_id, "u1");
    }
}