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
// SPDX-License-Identifier: Apache-2.0
//! DocumentOp dispatch for the Lite physical visitor.

use nodedb_physical::physical_plan::DocumentOp;
use nodedb_types::RlsWriteCheck;

use crate::error::LiteError;
use crate::query::document_ops;
use crate::query::engine::LiteQueryEngine;
use crate::storage::engine::StorageEngine;

use crate::query::physical_visitor::adapter::LitePhysicalFut;
use crate::query::physical_visitor::adapter::policy::deny_policy;

use super::row_source::{copy_source, join_source};

pub(super) fn dispatch<'a, S: StorageEngine + 'a>(
    engine: &'a LiteQueryEngine<S>,
    permit: Option<&'a crate::engine::fts::coordinator::TextMutationPermit>,
    op: &DocumentOp,
) -> Result<LitePhysicalFut<'a>, LiteError> {
    match op {
        DocumentOp::PointPut {
            collection,
            document_id,
            value,
            returning,
            rls_filters,
            ..
        } => {
            // PointPut has no rls_write_check slot: unconditional-overwrite
            // upsert semantics carry no write gate.
            deny_policy(
                "DocumentOp::PointPut",
                returning.as_ref(),
                &[rls_filters.as_slice()],
                &RlsWriteCheck::NoPolicyApplies,
            )?;
            let col = collection.clone();
            let doc_id = document_id.clone();
            let val = value.clone();
            Ok(Box::pin(async move {
                document_ops::writes::point_put_coordinated(
                    engine,
                    permit,
                    col.as_str(),
                    &doc_id,
                    &val,
                )
                .await
            }))
        }

        DocumentOp::PointInsert {
            collection,
            document_id,
            value,
            if_absent,
            returning,
            rls_filters,
            ..
        } => {
            // PointInsert has no rls_write_check slot: an unconditional
            // first write carries no write gate.
            deny_policy(
                "DocumentOp::PointInsert",
                returning.as_ref(),
                &[rls_filters.as_slice()],
                &RlsWriteCheck::NoPolicyApplies,
            )?;
            let col = collection.clone();
            let doc_id = document_id.clone();
            let val = value.clone();
            let if_absent = *if_absent;
            Ok(Box::pin(async move {
                document_ops::writes::point_insert_coordinated(
                    engine,
                    permit,
                    col.as_str(),
                    &doc_id,
                    &val,
                    if_absent,
                )
                .await
            }))
        }

        DocumentOp::PointUpdate {
            collection,
            document_id,
            updates,
            returning,
            rls_filters,
            rls_write_check,
            ..
        } => {
            deny_policy(
                "DocumentOp::PointUpdate",
                returning.as_ref(),
                &[rls_filters.as_slice()],
                rls_write_check,
            )?;
            let col = collection.clone();
            let doc_id = document_id.clone();
            let updates = updates.clone();
            Ok(Box::pin(async move {
                document_ops::writes::point_update_coordinated(
                    engine,
                    permit,
                    col.as_str(),
                    &doc_id,
                    &updates,
                )
                .await
            }))
        }

        DocumentOp::PointDelete {
            collection,
            document_id,
            returning,
            rls_filters,
            rls_write_check,
            ..
        } => {
            deny_policy(
                "DocumentOp::PointDelete",
                returning.as_ref(),
                &[rls_filters.as_slice()],
                rls_write_check,
            )?;
            let col = collection.clone();
            let doc_id = document_id.clone();
            Ok(Box::pin(async move {
                document_ops::writes::point_delete_coordinated(
                    engine,
                    permit,
                    col.as_str(),
                    &doc_id,
                )
                .await
            }))
        }

        DocumentOp::BatchInsert {
            collection,
            documents,
            if_absent,
            returning,
            rls_filters,
            ..
        } => {
            // BatchInsert has no rls_write_check slot: an unconditional
            // batch of first writes carries no write gate.
            deny_policy(
                "DocumentOp::BatchInsert",
                returning.as_ref(),
                &[rls_filters.as_slice()],
                &RlsWriteCheck::NoPolicyApplies,
            )?;
            let col = collection.clone();
            let docs = documents.clone();
            let if_absent = *if_absent;
            Ok(Box::pin(async move {
                document_ops::writes::batch_insert_coordinated(
                    engine,
                    permit,
                    col.as_str(),
                    &docs,
                    if_absent,
                )
                .await
            }))
        }

        // Every row of the page must land, or none. Lite's upsert writes one
        // row at a time, so a later row's refusal would leave earlier rows
        // written.
        DocumentOp::BatchUpsert { collection, .. } => Err(LiteError::Unsupported {
            detail: format!(
                "DocumentOp::BatchUpsert on '{collection}': an all-or-nothing multi-row \
                 upsert page is not supported in Lite"
            ),
        }),

        DocumentOp::Upsert {
            collection,
            document_id,
            value,
            on_conflict_updates,
            rls_write_check,
            returning,
            rls_filters,
            ..
        } => {
            deny_policy(
                "DocumentOp::Upsert",
                returning.as_ref(),
                &[rls_filters.as_slice()],
                rls_write_check,
            )?;
            let col = collection.clone();
            let doc_id = document_id.clone();
            let val = value.clone();
            let conflict_updates = on_conflict_updates.clone();
            Ok(Box::pin(async move {
                document_ops::writes::upsert_coordinated(
                    engine,
                    permit,
                    col.as_str(),
                    &doc_id,
                    &val,
                    &conflict_updates,
                )
                .await
            }))
        }

        DocumentOp::Truncate {
            collection,
            restart_identity,
            ..
        } => {
            let col = collection.clone();
            let restart = *restart_identity;
            Ok(Box::pin(async move {
                let result =
                    document_ops::writes::truncate_coordinated(engine, permit, col.as_str())
                        .await?;
                crate::query::truncate::restart_identity(engine, col.as_str(), restart);
                Ok(result)
            }))
        }

        DocumentOp::BulkUpdate {
            collection,
            updates,
            returning,
            rls_filters,
            rls_write_check,
            ..
        } => {
            deny_policy(
                "DocumentOp::BulkUpdate",
                returning.as_ref(),
                &[rls_filters.as_slice()],
                rls_write_check,
            )?;
            let col = collection.clone();
            let updates = updates.clone();
            Ok(Box::pin(async move {
                document_ops::writes::bulk_update_coordinated(
                    engine,
                    permit,
                    col.as_str(),
                    &updates,
                )
                .await
            }))
        }

        DocumentOp::BulkDelete {
            collection,
            returning,
            rls_filters,
            rls_write_check,
            ..
        } => {
            deny_policy(
                "DocumentOp::BulkDelete",
                returning.as_ref(),
                &[rls_filters.as_slice()],
                rls_write_check,
            )?;
            let col = collection.clone();
            Ok(Box::pin(async move {
                document_ops::writes::bulk_delete(engine, col.as_str()).await
            }))
        }

        DocumentOp::InsertSelect {
            target_collection,
            source,
        } => {
            let (source, limit) = copy_source("DocumentOp::InsertSelect", source)?;
            let target = target_collection.clone();
            let source = source.to_string();
            let limit = limit.unwrap_or(usize::MAX);
            Ok(Box::pin(async move {
                document_ops::sets::insert_select_coordinated(
                    engine,
                    permit,
                    target.as_str(),
                    &source,
                    limit,
                )
                .await
            }))
        }

        DocumentOp::UpdateFromJoin {
            target_collection,
            source,
            source_alias,
            target_join_col,
            source_join_col,
            updates,
            target_filters,
            returning,
            source_rows,
            rls_filters,
            rls_write_check,
            ..
        } => {
            deny_policy(
                "DocumentOp::UpdateFromJoin",
                returning.as_ref(),
                &[rls_filters.as_slice()],
                rls_write_check,
            )?;
            // The join reads every target row. A target-only WHERE would
            // narrow the rows it updates, which this path cannot apply.
            if !target_filters.is_empty() {
                return Err(LiteError::Unsupported {
                    detail: "DocumentOp::UpdateFromJoin with target filters is not supported \
                             in Lite"
                        .into(),
                });
            }
            let source = join_source("DocumentOp::UpdateFromJoin", source, source_rows.as_deref())?
                .to_string();
            let target = target_collection.clone();
            let alias = source_alias.clone();
            let target_join = target_join_col.clone();
            let source_join = source_join_col.clone();
            let updates = updates.clone();
            Ok(Box::pin(async move {
                document_ops::sets::update_from_join_coordinated(
                    engine,
                    permit,
                    document_ops::sets::DocumentJoin {
                        target_collection: target.as_str(),
                        source_collection: source.as_str(),
                        source_alias: &alias,
                        target_join_col: &target_join,
                        source_join_col: &source_join,
                    },
                    &updates,
                )
                .await
            }))
        }

        DocumentOp::Merge {
            target_collection,
            source,
            source_alias,
            target_join_col,
            source_join_col,
            clauses,
            returning,
            source_rows,
            rls_filters,
            rls_write_check,
            ..
        } => {
            deny_policy(
                "DocumentOp::Merge",
                returning.as_ref(),
                &[rls_filters.as_slice()],
                rls_write_check,
            )?;
            let source =
                join_source("DocumentOp::Merge", source, source_rows.as_deref())?.to_string();
            let target = target_collection.clone();
            let alias = source_alias.clone();
            let target_join = target_join_col.clone();
            let source_join = source_join_col.clone();
            let clauses = clauses.clone();
            Ok(Box::pin(async move {
                document_ops::sets::merge_coordinated(
                    engine,
                    permit,
                    document_ops::sets::DocumentJoin {
                        target_collection: target.as_str(),
                        source_collection: source.as_str(),
                        source_alias: &alias,
                        target_join_col: &target_join,
                        source_join_col: &source_join,
                    },
                    &clauses,
                )
                .await
            }))
        }

        DocumentOp::PointGet { .. }
        | DocumentOp::Scan { .. }
        | DocumentOp::RangeScan { .. }
        | DocumentOp::IndexedFetch { .. }
        | DocumentOp::IndexLookup { .. }
        | DocumentOp::EstimateCount { .. }
        | DocumentOp::Register { .. }
        | DocumentOp::DropIndex { .. }
        | DocumentOp::BackfillIndex { .. }
        | DocumentOp::MaterializeScan { .. }
        | DocumentOp::ApplyBalanceDelta { .. }
        | DocumentOp::ResolveWrite(_)
        | DocumentOp::ResolvedWrite { .. } => Err(LiteError::BadRequest {
            detail: "document write dispatch received a non-write operation".into(),
        }),
    }
}