nodedb 0.2.0

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
// SPDX-License-Identifier: BUSL-1.1

//! Integration tests for conditional update guarantees.
//!
//! Verifies:
//! - Affected row count is correctly returned for bulk updates
//! - Conditional UPDATE WHERE with predicates (stock >= N) works atomically
//! - RETURNING flag returns post-update documents
//! - TransactionBatch does not auto-abort on 0-row conditional update
//! - PointUpdate returns affected count

use nodedb::bridge::envelope::{PhysicalPlan, Status};
use nodedb::bridge::physical_plan::{DocumentOp, MetaOp};
use nodedb::bridge::scan_filter::ScanFilter;

use crate::helpers::*;

fn filter(field: &str, op: &str, value: nodedb_types::Value) -> ScanFilter {
    ScanFilter {
        field: field.into(),
        op: op.into(),
        value,
        clauses: Vec::new(),
        expr: None,
    }
}

/// Hash a string PK to a deterministic non-zero surrogate so each test
/// row lands on its own substrate key. The data plane keys redb rows by
/// `surrogate_to_doc_id(surrogate)`; with a wired catalog the assigner
/// guarantees a stable injection, but executor-direct fixtures bypass
/// the catalog and have to thread their own bindings.
fn surrogate_for(id: &str) -> nodedb_types::Surrogate {
    let mut h: u32 = 2_166_136_261;
    for &b in id.as_bytes() {
        h ^= b as u32;
        h = h.wrapping_mul(16_777_619);
    }
    // Bias away from `Surrogate::ZERO`, which the document substrate
    // reserves as a sentinel for unbound rows.
    nodedb_types::Surrogate::new(h.max(1))
}

/// Insert a product document with stock field.
fn insert_product(
    core: &mut nodedb::data::executor::core_loop::CoreLoop,
    tx: &mut nodedb_bridge::buffer::Producer<nodedb::bridge::dispatch::BridgeRequest>,
    rx: &mut nodedb_bridge::buffer::Consumer<nodedb::bridge::dispatch::BridgeResponse>,
    id: &str,
    stock: u64,
) {
    let value = format!("{{\"name\":\"product\",\"stock\":{stock}}}");
    send_ok(
        core,
        tx,
        rx,
        PhysicalPlan::Document(DocumentOp::PointPut {
            collection: "products".into(),
            document_id: id.into(),
            value: value.into_bytes(),
            surrogate: surrogate_for(id),
            pk_bytes: id.as_bytes().to_vec(),
        }),
    );
}

/// Read a product and return its stock value.
fn get_stock(
    core: &mut nodedb::data::executor::core_loop::CoreLoop,
    tx: &mut nodedb_bridge::buffer::Producer<nodedb::bridge::dispatch::BridgeRequest>,
    rx: &mut nodedb_bridge::buffer::Consumer<nodedb::bridge::dispatch::BridgeResponse>,
    id: &str,
) -> u64 {
    let payload = send_ok(
        core,
        tx,
        rx,
        PhysicalPlan::Document(DocumentOp::PointGet {
            collection: "products".into(),
            document_id: id.into(),
            rls_filters: Vec::new(),
            system_as_of_ms: None,
            valid_at_ms: None,
            surrogate: surrogate_for(id),
            pk_bytes: id.as_bytes().to_vec(),
        }),
    );
    let v = payload_value(&payload);
    v.get("stock").and_then(|s| s.as_u64()).unwrap_or_default()
}

#[test]
fn bulk_update_returns_affected_count() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    insert_product(&mut core, &mut tx, &mut rx, "p1", 10);
    insert_product(&mut core, &mut tx, &mut rx, "p2", 5);
    insert_product(&mut core, &mut tx, &mut rx, "p3", 0);

    // Bulk update: SET stock = 99 WHERE stock > 0 (should match p1 and p2).
    let filters = vec![filter("stock", "gt", nodedb_types::Value::Integer(0))];
    let filter_bytes = zerompk::to_msgpack_vec(&filters).unwrap();
    let updates = vec![(
        "stock".to_string(),
        nodedb::bridge::physical_plan::UpdateValue::Literal(
            nodedb_types::json_to_msgpack(&serde_json::json!(99)).unwrap(),
        ),
    )];

    let payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Document(DocumentOp::BulkUpdate {
            collection: "products".into(),
            filters: filter_bytes,
            updates,
            returning: None,
            ollp_predicted_surrogates: None,
        }),
    );

    let v = payload_value(&payload);
    assert_eq!(v.get("affected").and_then(|a| a.as_u64()), Some(2));

    assert_eq!(get_stock(&mut core, &mut tx, &mut rx, "p1"), 99);
    assert_eq!(get_stock(&mut core, &mut tx, &mut rx, "p2"), 99);
    assert_eq!(get_stock(&mut core, &mut tx, &mut rx, "p3"), 0);
}

#[test]
fn conditional_decrement_stops_at_zero() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    insert_product(&mut core, &mut tx, &mut rx, "flash-deal", 5);

    let mut successes = 0u64;
    for i in 0..10 {
        let current_stock = get_stock(&mut core, &mut tx, &mut rx, "flash-deal");

        let filters = vec![filter("stock", "gte", nodedb_types::Value::Integer(1))];
        let filter_bytes = zerompk::to_msgpack_vec(&filters).unwrap();

        let new_stock = current_stock.saturating_sub(1);
        let updates = vec![(
            "stock".to_string(),
            nodedb::bridge::physical_plan::UpdateValue::Literal(
                nodedb_types::json_to_msgpack(&serde_json::json!(new_stock)).unwrap(),
            ),
        )];

        let payload = send_ok(
            &mut core,
            &mut tx,
            &mut rx,
            PhysicalPlan::Document(DocumentOp::BulkUpdate {
                collection: "products".into(),
                filters: filter_bytes,
                updates,
                returning: None,
                ollp_predicted_surrogates: None,
            }),
        );

        let v = payload_value(&payload);
        let affected = v.get("affected").and_then(|a| a.as_u64()).unwrap_or(0);
        if affected > 0 {
            successes += 1;
        }

        if i >= 5 {
            assert_eq!(
                affected, 0,
                "iteration {i}: expected 0 affected after stock depleted"
            );
        }
    }

    assert_eq!(successes, 5, "exactly 5 decrements should succeed");
    assert_eq!(get_stock(&mut core, &mut tx, &mut rx, "flash-deal"), 0);
}

#[test]
fn bulk_update_zero_match_returns_zero_affected() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    insert_product(&mut core, &mut tx, &mut rx, "p1", 0);

    let filters = vec![filter("stock", "gte", nodedb_types::Value::Integer(100))];
    let filter_bytes = zerompk::to_msgpack_vec(&filters).unwrap();
    let updates = vec![(
        "stock".to_string(),
        nodedb::bridge::physical_plan::UpdateValue::Literal(
            nodedb_types::json_to_msgpack(&serde_json::json!(999)).unwrap(),
        ),
    )];

    let payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Document(DocumentOp::BulkUpdate {
            collection: "products".into(),
            filters: filter_bytes,
            updates,
            returning: None,
            ollp_predicted_surrogates: None,
        }),
    );

    let v = payload_value(&payload);
    assert_eq!(v.get("affected").and_then(|a| a.as_u64()), Some(0));
}

#[test]
fn bulk_update_returning_returns_updated_documents() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    insert_product(&mut core, &mut tx, &mut rx, "r1", 10);
    insert_product(&mut core, &mut tx, &mut rx, "r2", 20);

    let filters = vec![filter("stock", "gt", nodedb_types::Value::Integer(0))];
    let filter_bytes = zerompk::to_msgpack_vec(&filters).unwrap();
    let updates = vec![(
        "stock".to_string(),
        nodedb::bridge::physical_plan::UpdateValue::Literal(
            nodedb_types::json_to_msgpack(&serde_json::json!(0)).unwrap(),
        ),
    )];

    let payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Document(DocumentOp::BulkUpdate {
            collection: "products".into(),
            filters: filter_bytes,
            updates,
            returning: None,
            ollp_predicted_surrogates: None,
        }),
    );

    let v = payload_value(&payload);
    assert_eq!(v.get("affected").and_then(|a| a.as_u64()), Some(2));
}

#[test]
fn bulk_update_returning_zero_match_returns_affected_zero() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    insert_product(&mut core, &mut tx, &mut rx, "p1", 0);

    let filters = vec![filter("stock", "gte", nodedb_types::Value::Integer(100))];
    let filter_bytes = zerompk::to_msgpack_vec(&filters).unwrap();
    let updates = vec![(
        "stock".to_string(),
        nodedb::bridge::physical_plan::UpdateValue::Literal(
            nodedb_types::json_to_msgpack(&serde_json::json!(999)).unwrap(),
        ),
    )];

    let payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Document(DocumentOp::BulkUpdate {
            collection: "products".into(),
            filters: filter_bytes,
            updates,
            returning: None,
            ollp_predicted_surrogates: None,
        }),
    );

    let v = payload_value(&payload);
    assert_eq!(v.get("affected").and_then(|a| a.as_u64()), Some(0));
}

#[test]
fn point_update_returns_affected_count() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    insert_product(&mut core, &mut tx, &mut rx, "pu1", 10);

    let updates = vec![(
        "stock".to_string(),
        nodedb::bridge::physical_plan::UpdateValue::Literal(
            nodedb_types::json_to_msgpack(&serde_json::json!(5)).unwrap(),
        ),
    )];

    let payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Document(DocumentOp::PointUpdate {
            collection: "products".into(),
            document_id: "pu1".into(),
            updates,
            returning: None,
            surrogate: surrogate_for("pu1"),
            pk_bytes: b"pu1".to_vec(),
        }),
    );

    let v = payload_value(&payload);
    assert_eq!(v.get("affected").and_then(|a| a.as_u64()), Some(1));
    assert_eq!(get_stock(&mut core, &mut tx, &mut rx, "pu1"), 5);
}

#[test]
fn point_update_returning_returns_updated_document() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    insert_product(&mut core, &mut tx, &mut rx, "pu2", 10);

    let updates = vec![(
        "stock".to_string(),
        nodedb::bridge::physical_plan::UpdateValue::Literal(
            nodedb_types::json_to_msgpack(&serde_json::json!(7)).unwrap(),
        ),
    )];

    let payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Document(DocumentOp::PointUpdate {
            collection: "products".into(),
            document_id: "pu2".into(),
            updates,
            returning: None,
            surrogate: surrogate_for("pu2"),
            pk_bytes: b"pu2".to_vec(),
        }),
    );

    let v = payload_value(&payload);
    assert_eq!(v.get("affected").and_then(|a| a.as_u64()), Some(1));
}

#[test]
fn transaction_batch_does_not_abort_on_zero_row_update() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    insert_product(&mut core, &mut tx, &mut rx, "t1", 1);
    insert_product(&mut core, &mut tx, &mut rx, "t2", 0);

    // Transaction: first update matches (stock >= 1), second doesn't (stock >= 100).
    // Batch should NOT auto-abort on 0-row update.
    let filters_match = zerompk::to_msgpack_vec(&vec![filter(
        "stock",
        "gte",
        nodedb_types::Value::Integer(1),
    )])
    .unwrap();

    let filters_nomatch = zerompk::to_msgpack_vec(&vec![filter(
        "stock",
        "gte",
        nodedb_types::Value::Integer(100),
    )])
    .unwrap();

    let resp = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Meta(MetaOp::TransactionBatch {
            plans: vec![
                PhysicalPlan::Document(DocumentOp::BulkUpdate {
                    collection: "products".into(),
                    filters: filters_match,
                    updates: vec![(
                        "stock".to_string(),
                        nodedb::bridge::physical_plan::UpdateValue::Literal(
                            nodedb_types::json_to_msgpack(&serde_json::json!(0)).unwrap(),
                        ),
                    )],
                    returning: None,
                    ollp_predicted_surrogates: None,
                }),
                PhysicalPlan::Document(DocumentOp::BulkUpdate {
                    collection: "products".into(),
                    filters: filters_nomatch,
                    updates: vec![(
                        "stock".to_string(),
                        nodedb::bridge::physical_plan::UpdateValue::Literal(
                            nodedb_types::json_to_msgpack(&serde_json::json!(999)).unwrap(),
                        ),
                    )],
                    returning: None,
                    ollp_predicted_surrogates: None,
                }),
            ],
        }),
    );

    assert_eq!(
        resp.status,
        Status::Ok,
        "transaction should not abort on 0-row update: {:?}",
        resp.error_code
    );

    // t1 updated to 0, t2 unchanged at 0.
    assert_eq!(get_stock(&mut core, &mut tx, &mut rx, "t1"), 0);
    assert_eq!(get_stock(&mut core, &mut tx, &mut rx, "t2"), 0);
}