nodedb-lite 0.1.0

Embedded edge client for NodeDB — offline vector search, graph traversal, and CRDT sync
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
//! Recording `SyncDelegate` used by the transport integration tests.
//!
//! Every callback the transport can invoke is implemented; the handful the
//! dispatch/push tests assert on record into a `std::sync::Mutex` (not
//! tokio's) so assertions can read them from outside an async context.

use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};

use nodedb_lite::LiteError;
use nodedb_lite::engine::crdt::engine::PendingDelta;
use nodedb_lite::nodedb::CollectionMeta;
use nodedb_lite::sync::array::PendingArrayOp;
use nodedb_lite::sync::{
    PendingColumnarBatch, PendingFtsDelete, PendingFtsIndex, PendingKvWrite, PendingSpatialDelete,
    PendingSpatialInsert, PendingTimeseriesBatch, PendingVectorDelete, PendingVectorInsert,
    SyncDelegate,
};

pub struct MockDelegate {
    acked_up_to: AtomicU64,
    rejected: std::sync::Mutex<Vec<u64>>,
    imported: std::sync::Mutex<Vec<(String, Vec<u8>)>>,
    applied_rows: std::sync::Mutex<Vec<(String, String, nodedb_types::sync::wire::RowOp)>>,
    imported_schemas: std::sync::Mutex<Vec<String>>,
    pending: std::sync::Mutex<Vec<PendingDelta>>,
    collection_metas: std::sync::Mutex<std::collections::HashMap<String, CollectionMeta>>,
    identity: std::sync::Mutex<nodedb_lite::identity::LiteIdentity>,
    identity_changes: AtomicU64,
    peer_id_rotations: AtomicU64,
    refuse_rows: AtomicBool,
    kv_pending: std::sync::Mutex<Vec<(Vec<u8>, PendingKvWrite)>>,
    kv_in_flight: std::sync::Mutex<Vec<u64>>,
    kv_retired: std::sync::Mutex<Vec<u64>>,
    stream_acks: std::sync::Mutex<Vec<(u64, u64)>>,
    in_flight_clears: AtomicU64,
    /// Array catch-up, snapshot, and push-queue state.
    pub array: super::mock_array::MockArrayState,
}

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

impl MockDelegate {
    pub fn new() -> Self {
        Self {
            acked_up_to: AtomicU64::new(0),
            rejected: std::sync::Mutex::new(Vec::new()),
            imported: std::sync::Mutex::new(Vec::new()),
            applied_rows: std::sync::Mutex::new(Vec::new()),
            imported_schemas: std::sync::Mutex::new(Vec::new()),
            pending: std::sync::Mutex::new(Vec::new()),
            collection_metas: std::sync::Mutex::new(std::collections::HashMap::new()),
            identity: std::sync::Mutex::new(nodedb_lite::identity::LiteIdentity {
                lite_id: "mock-lite".to_string(),
                epoch: 1,
                peer_id: nodedb_lite::identity::mint_peer_id(),
            }),
            identity_changes: AtomicU64::new(0),
            peer_id_rotations: AtomicU64::new(0),
            refuse_rows: AtomicBool::new(false),
            kv_pending: std::sync::Mutex::new(Vec::new()),
            kv_in_flight: std::sync::Mutex::new(Vec::new()),
            kv_retired: std::sync::Mutex::new(Vec::new()),
            stream_acks: std::sync::Mutex::new(Vec::new()),
            in_flight_clears: AtomicU64::new(0),
            array: super::mock_array::MockArrayState::default(),
        }
    }

    /// Make `apply_remote_row` refuse every row with a serialization error.
    pub fn refuse_rows(&self) {
        self.refuse_rows.store(true, Ordering::Relaxed);
    }

    /// Queue KV writes for the push loop, as `(durable_key, write)` pairs.
    pub fn set_pending_kv(&self, writes: Vec<(Vec<u8>, PendingKvWrite)>) {
        *self.kv_pending.lock().expect("kv_pending lock") = writes;
    }

    /// Batch ids passed to `retire_kv_write`, in order.
    pub fn retired_kv(&self) -> Vec<u64> {
        self.kv_retired.lock().expect("kv_retired lock").clone()
    }

    /// `(stream_id, applied_seq)` pairs passed to `record_stream_ack`.
    pub fn stream_acks(&self) -> Vec<(u64, u64)> {
        self.stream_acks.lock().expect("stream_acks lock").clone()
    }

    /// How many times `clear_engine_in_flight` was invoked.
    pub fn in_flight_clears(&self) -> u64 {
        self.in_flight_clears.load(Ordering::Relaxed)
    }

    /// The Loro peer id this delegate currently reports.
    pub fn peer_id(&self) -> u64 {
        self.identity.lock().expect("identity lock").peer_id
    }

    /// How many times `rotate_peer_id` was invoked.
    pub fn peer_id_rotations(&self) -> u64 {
        self.peer_id_rotations.load(Ordering::Relaxed)
    }

    /// How many times `regenerate_identity` was invoked.
    pub fn identity_changes(&self) -> u64 {
        self.identity_changes.load(Ordering::Relaxed)
    }

    /// Highest mutation id passed to `acknowledge`, or 0 if none.
    pub fn acked_up_to(&self) -> u64 {
        self.acked_up_to.load(Ordering::Relaxed)
    }

    /// Mutation ids passed to `reject` / `reject_with_policy`, in order.
    pub fn rejected(&self) -> Vec<u64> {
        self.rejected.lock().expect("rejected lock").clone()
    }

    /// `(collection, bytes)` pairs passed to `import_remote`, in order.
    pub fn imported(&self) -> Vec<(String, Vec<u8>)> {
        self.imported.lock().expect("imported lock").clone()
    }

    /// Snapshot of rows applied so far, in application order.
    pub fn applied_rows(&self) -> Vec<(String, String, nodedb_types::sync::wire::RowOp)> {
        self.applied_rows.lock().expect("applied_rows lock").clone()
    }

    /// Names of the collection schemas imported, in order.
    pub fn imported_schemas(&self) -> Vec<String> {
        self.imported_schemas
            .lock()
            .expect("imported_schemas lock")
            .clone()
    }

    pub fn set_pending(&self, deltas: Vec<PendingDelta>) {
        *self.pending.lock().expect("pending lock") = deltas;
    }

    pub fn set_collection_meta(&self, name: &str, meta: CollectionMeta) {
        self.collection_metas
            .lock()
            .expect("collection_metas lock")
            .insert(name.to_string(), meta);
    }
}

#[async_trait::async_trait]
impl SyncDelegate for MockDelegate {
    fn sync_identity(&self) -> nodedb_lite::identity::LiteIdentity {
        self.identity.lock().expect("identity lock").clone()
    }
    async fn regenerate_identity(&self) {
        let mut identity = self.identity.lock().expect("identity lock");
        identity.lite_id = format!("{}-regenerated", identity.lite_id);
        identity.epoch = 1;
        identity.peer_id = nodedb_lite::identity::mint_peer_id();
        self.identity_changes.fetch_add(1, Ordering::Relaxed);
    }
    async fn rotate_peer_id(&self) {
        let mut identity = self.identity.lock().expect("identity lock");
        identity.peer_id = nodedb_lite::identity::mint_peer_id();
        self.peer_id_rotations.fetch_add(1, Ordering::Relaxed);
    }
    fn pending_deltas(&self) -> Vec<PendingDelta> {
        self.pending.lock().expect("pending lock").clone()
    }
    fn acknowledge(&self, mutation_id: u64) {
        self.acked_up_to.store(mutation_id, Ordering::Relaxed);
    }
    async fn set_pending_delta_seq(&self, _mutation_id: u64, _seq: u64) {}
    fn reject(&self, mutation_id: u64) {
        self.rejected
            .lock()
            .expect("rejected lock")
            .push(mutation_id);
    }
    fn reject_with_policy(
        &self,
        mutation_id: u64,
        _hint: &nodedb_types::sync::compensation::CompensationHint,
    ) {
        self.rejected
            .lock()
            .expect("rejected lock")
            .push(mutation_id);
    }
    async fn apply_remote_row(
        &self,
        msg: &nodedb_types::sync::wire::RowPushMsg,
    ) -> Result<(), nodedb_types::error::NodeDbError> {
        if self.refuse_rows.load(Ordering::Relaxed) {
            return Err(nodedb_types::error::NodeDbError::serialization(
                "msgpack",
                "mock refuses every row",
            ));
        }
        self.applied_rows.lock().expect("applied_rows lock").push((
            msg.collection.clone(),
            msg.document_id.clone(),
            msg.op,
        ));
        Ok(())
    }

    fn import_remote(&self, collection: &str, data: &[u8]) {
        self.imported
            .lock()
            .expect("imported lock")
            .push((collection.to_string(), data.to_vec()));
    }
    async fn import_definition(&self, _msg: &nodedb_types::sync::wire::DefinitionSyncMsg) {}
    async fn import_collection_schema(
        &self,
        msg: &nodedb_types::sync::wire::CollectionSchemaSyncMsg,
    ) {
        self.imported_schemas
            .lock()
            .expect("imported_schemas lock")
            .push(msg.descriptor.name.clone());
    }
    async fn handle_array_delta(
        &self,
        _msg: &nodedb_types::sync::wire::ArrayDeltaMsg,
    ) -> Option<nodedb_types::sync::wire::ArrayAckMsg> {
        None
    }
    async fn handle_array_delta_batch(
        &self,
        _msg: &nodedb_types::sync::wire::ArrayDeltaBatchMsg,
    ) -> Option<nodedb_types::sync::wire::ArrayAckMsg> {
        None
    }
    async fn handle_array_reject(
        &self,
        msg: &nodedb_types::sync::wire::ArrayRejectMsg,
    ) -> Option<nodedb_types::sync::wire::ArrayCatchupRequestMsg> {
        self.array.reject(msg)
    }
    async fn handle_array_snapshot(&self, msg: &nodedb_types::sync::wire::ArraySnapshotMsg) {
        self.array.record_header(msg);
    }
    async fn handle_array_snapshot_chunk(
        &self,
        msg: &nodedb_types::sync::wire::ArraySnapshotChunkMsg,
    ) -> Option<nodedb_types::sync::wire::ArrayAckMsg> {
        self.array.record_chunk(msg)
    }
    fn begin_array_catchup(&self) -> Vec<nodedb_types::sync::wire::ArrayCatchupRequestMsg> {
        self.array.begin_catchup()
    }
    async fn pending_array_ops(
        &self,
        after: Option<nodedb_array::sync::hlc::Hlc>,
        limit: usize,
    ) -> Result<Vec<PendingArrayOp>, LiteError> {
        Ok(self.array.pending_after(after, limit))
    }
    async fn persist_array_op_seq(&self, op: &PendingArrayOp) -> Result<(), LiteError> {
        self.array.persist_seq(op);
        Ok(())
    }
    async fn retire_array_ops(
        &self,
        hlcs: &[nodedb_array::sync::hlc::Hlc],
    ) -> Result<(), LiteError> {
        self.array.retire(hlcs);
        Ok(())
    }
    fn array_schema_announce(
        &self,
        array: &str,
    ) -> Result<Option<nodedb_types::sync::wire::ArraySchemaSyncMsg>, LiteError> {
        Ok(self.array.schema(array))
    }

    async fn pending_columnar_batches(&self) -> Vec<(Vec<u8>, PendingColumnarBatch)> {
        Vec::new()
    }
    async fn mark_columnar_batch_in_flight(&self, _batch_id: u64, _durable_key: Vec<u8>) {}
    async fn ack_columnar_batch_in_flight(&self, _batch_id: u64) {}
    async fn acknowledge_columnar_batch(&self, _durable_key: Vec<u8>) {}

    async fn pending_vector_inserts(&self) -> Vec<(Vec<u8>, PendingVectorInsert)> {
        Vec::new()
    }
    async fn mark_vector_insert_in_flight(&self, _batch_id: u64, _durable_key: Vec<u8>) {}
    async fn ack_vector_insert_in_flight(&self, _batch_id: u64) {}
    async fn acknowledge_vector_insert(&self, _durable_key: Vec<u8>) {}

    async fn pending_vector_deletes(&self) -> Vec<(Vec<u8>, PendingVectorDelete)> {
        Vec::new()
    }
    async fn mark_vector_delete_in_flight(&self, _batch_id: u64, _durable_key: Vec<u8>) {}
    async fn ack_vector_delete_in_flight(&self, _batch_id: u64) {}
    async fn acknowledge_vector_delete(&self, _durable_key: Vec<u8>) {}

    async fn pending_fts_indexes(&self) -> Result<Vec<(Vec<u8>, PendingFtsIndex)>, LiteError> {
        Ok(Vec::new())
    }
    async fn mark_fts_index_in_flight(&self, _batch_id: u64, _durable_key: Vec<u8>) {}
    async fn ack_fts_index_in_flight(&self, _batch_id: u64) {}
    async fn acknowledge_fts_index(&self, _durable_key: Vec<u8>) {}

    async fn pending_fts_deletes(&self) -> Result<Vec<(Vec<u8>, PendingFtsDelete)>, LiteError> {
        Ok(Vec::new())
    }
    async fn mark_fts_delete_in_flight(&self, _batch_id: u64, _durable_key: Vec<u8>) {}
    async fn ack_fts_delete_in_flight(&self, _batch_id: u64) {}
    async fn acknowledge_fts_delete(&self, _durable_key: Vec<u8>) {}

    async fn pending_spatial_inserts(&self) -> Vec<(Vec<u8>, PendingSpatialInsert)> {
        Vec::new()
    }
    async fn mark_spatial_insert_in_flight(&self, _batch_id: u64, _durable_key: Vec<u8>) {}
    async fn ack_spatial_insert_in_flight(&self, _batch_id: u64) {}
    async fn acknowledge_spatial_insert(&self, _durable_key: Vec<u8>) {}

    async fn pending_spatial_deletes(&self) -> Vec<(Vec<u8>, PendingSpatialDelete)> {
        Vec::new()
    }
    async fn mark_spatial_delete_in_flight(&self, _batch_id: u64, _durable_key: Vec<u8>) {}
    async fn ack_spatial_delete_in_flight(&self, _batch_id: u64) {}
    async fn acknowledge_spatial_delete(&self, _durable_key: Vec<u8>) {}

    async fn pending_timeseries_batches(&self) -> Vec<(Vec<u8>, PendingTimeseriesBatch)> {
        Vec::new()
    }
    async fn mark_timeseries_batch_in_flight(
        &self,
        _stream_seq: u64,
        _batch_id: u64,
        _durable_key: Vec<u8>,
    ) {
    }
    async fn ack_timeseries_batches_through_seq(&self, _applied_seq: u64) {}
    async fn ack_timeseries_batch_by_id(&self, _batch_id: u64) {}
    async fn acknowledge_timeseries_batch(&self, _durable_key: Vec<u8>) {}
    async fn clear_engine_in_flight(&self) {
        self.in_flight_clears.fetch_add(1, Ordering::Relaxed);
        self.kv_in_flight.lock().expect("kv_in_flight lock").clear();
    }

    async fn pending_kv_writes(&self) -> Result<Vec<(Vec<u8>, PendingKvWrite)>, LiteError> {
        let in_flight = self.kv_in_flight.lock().expect("kv_in_flight lock").clone();
        let retired = self.kv_retired.lock().expect("kv_retired lock").clone();
        Ok(self
            .kv_pending
            .lock()
            .expect("kv_pending lock")
            .iter()
            .filter(|(key, _)| {
                let id = u64::from_be_bytes(key.as_slice().try_into().expect("8-byte key"));
                !in_flight.contains(&id) && !retired.contains(&id)
            })
            .cloned()
            .collect())
    }
    async fn persist_kv_write_seq(
        &self,
        key: &[u8],
        write: &PendingKvWrite,
    ) -> Result<(), LiteError> {
        let mut pending = self.kv_pending.lock().expect("kv_pending lock");
        if let Some(entry) = pending.iter_mut().find(|(k, _)| k.as_slice() == key) {
            entry.1 = write.clone();
        }
        Ok(())
    }
    async fn mark_kv_write_in_flight(&self, batch_id: u64) {
        self.kv_in_flight
            .lock()
            .expect("kv_in_flight lock")
            .push(batch_id);
    }
    async fn retire_kv_write(&self, batch_id: u64) -> Result<(), LiteError> {
        self.kv_in_flight
            .lock()
            .expect("kv_in_flight lock")
            .retain(|id| *id != batch_id);
        self.kv_retired
            .lock()
            .expect("kv_retired lock")
            .push(batch_id);
        Ok(())
    }

    async fn persist_producer_state(&self, _producer_id: u64, _accepted_epoch: u64) {}
    async fn load_producer_state(&self) -> (u64, u64) {
        (0, 0)
    }
    async fn next_stream_seq(&self, _stream_id: u64) -> u64 {
        0
    }
    async fn record_stream_ack(&self, stream_id: u64, applied_seq: u64) {
        self.stream_acks
            .lock()
            .expect("stream_acks lock")
            .push((stream_id, applied_seq));
    }

    async fn get_collection_meta(&self, name: &str) -> Option<CollectionMeta> {
        self.collection_metas
            .lock()
            .expect("collection_metas lock")
            .get(name)
            .cloned()
    }

    async fn persist_columnar_seq(
        &self,
        _key: &[u8],
        _batch: &PendingColumnarBatch,
    ) -> Result<(), LiteError> {
        Ok(())
    }
    async fn persist_timeseries_seq(
        &self,
        _key: &[u8],
        _batch: &PendingTimeseriesBatch,
    ) -> Result<(), LiteError> {
        Ok(())
    }
    async fn persist_vector_insert_seq(
        &self,
        _key: &[u8],
        _insert: &PendingVectorInsert,
    ) -> Result<(), LiteError> {
        Ok(())
    }
    async fn persist_vector_delete_seq(
        &self,
        _key: &[u8],
        _delete: &PendingVectorDelete,
    ) -> Result<(), LiteError> {
        Ok(())
    }
    async fn persist_fts_index_seq(
        &self,
        _key: &[u8],
        _entry: &PendingFtsIndex,
    ) -> Result<(), LiteError> {
        Ok(())
    }
    async fn persist_fts_delete_seq(
        &self,
        _key: &[u8],
        _entry: &PendingFtsDelete,
    ) -> Result<(), LiteError> {
        Ok(())
    }
    async fn persist_spatial_insert_seq(
        &self,
        _key: &[u8],
        _insert: &PendingSpatialInsert,
    ) -> Result<(), LiteError> {
        Ok(())
    }
    async fn persist_spatial_delete_seq(
        &self,
        _key: &[u8],
        _delete: &PendingSpatialDelete,
    ) -> Result<(), LiteError> {
        Ok(())
    }
}