meshdb-executor 0.2.0

Physical operators and query execution for Mesh
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
use crate::error::Result;
use meshdb_core::{Edge, EdgeId, Node, NodeId};
use meshdb_storage::{
    ConstraintScope, PropertyConstraintKind, PropertyConstraintSpec, StorageEngine,
};

/// `(label, property)` pair identifying a single-property node point
/// / spatial index. Always length-1 on `property` side for now —
/// composite spatial indexes are a separate design and get their own
/// spec shape if they ship.
pub type PointIndexSpec = (String, String);

/// `(label, properties)` pair identifying a node property index.
/// `properties` is a `Vec<String>` so composite indexes round-trip
/// through the reader/writer boundary without truncating —
/// previously this was `(String, String)` and `SHOW INDEXES`
/// silently dropped everything past the first property.
pub type NodeIndexSpec = (String, Vec<String>);

/// `(edge_type, properties)` pair identifying an edge property
/// index. Relationship-scope analogue of [`NodeIndexSpec`].
pub type EdgeIndexSpec = (String, Vec<String>);

/// Sink for mutating graph operations produced by the executor. Isolates
/// write-side concerns from read-side traversal so we can plug in either a
/// direct-to-storage writer (single-node mode) or a Raft-backed writer that
/// proposes each mutation through consensus (cluster mode).
///
/// Methods are sync because the executor's iterator model is sync.
/// Async-backed implementations (e.g. the Raft writer) bridge via
/// `Handle::block_on`; callers must run the executor inside
/// `spawn_blocking` so they don't stall the tokio runtime.
pub trait GraphWriter {
    fn put_node(&self, node: &Node) -> Result<()>;
    fn put_edge(&self, edge: &Edge) -> Result<()>;
    fn delete_edge(&self, id: EdgeId) -> Result<()>;
    fn detach_delete_node(&self, id: NodeId) -> Result<()>;

    /// Declare a new property index. `properties` is a slice so the
    /// composite form (`CREATE INDEX FOR (n:L) ON (n.a, n.b)`) fits
    /// the same surface as single-property. Default impl errors so
    /// remote writers that don't yet support cluster-aware DDL
    /// surface the limitation immediately; storage-backed writers
    /// override via the blanket impl.
    fn create_property_index(&self, _label: &str, _properties: &[String]) -> Result<()> {
        Err(crate::error::Error::Unsupported(
            "property-index DDL is not supported by this writer".into(),
        ))
    }

    /// Tear down a property index. Mirrors [`Self::create_property_index`].
    fn drop_property_index(&self, _label: &str, _properties: &[String]) -> Result<()> {
        Err(crate::error::Error::Unsupported(
            "property-index DDL is not supported by this writer".into(),
        ))
    }

    /// Snapshot the currently-registered property indexes as
    /// `(label, property)` pairs for `SHOW INDEXES`. Default impl
    /// returns an empty list — remote writers will wire real
    /// fan-out in Phase C.
    fn list_property_indexes(&self) -> Result<Vec<NodeIndexSpec>> {
        Ok(Vec::new())
    }

    /// Relationship-scope analogue of
    /// [`Self::create_property_index`].
    fn create_edge_property_index(&self, _edge_type: &str, _properties: &[String]) -> Result<()> {
        Err(crate::error::Error::Unsupported(
            "edge-property-index DDL is not supported by this writer".into(),
        ))
    }

    /// Tear down an edge property index. Mirrors
    /// [`Self::create_edge_property_index`].
    fn drop_edge_property_index(&self, _edge_type: &str, _properties: &[String]) -> Result<()> {
        Err(crate::error::Error::Unsupported(
            "edge-property-index DDL is not supported by this writer".into(),
        ))
    }

    /// Snapshot the currently-registered edge property indexes as
    /// `(edge_type, property)` pairs. Default impl returns an empty
    /// list.
    fn list_edge_property_indexes(&self) -> Result<Vec<EdgeIndexSpec>> {
        Ok(Vec::new())
    }

    /// Declare a point / spatial index on `(label, property)`.
    /// Default impl errors — remote writers opt in via the blanket
    /// `StorageEngine` impl or a cluster-aware override.
    fn create_point_index(&self, _label: &str, _property: &str) -> Result<()> {
        Err(crate::error::Error::Unsupported(
            "point-index DDL is not supported by this writer".into(),
        ))
    }

    /// Tear down a point index. Mirrors
    /// [`Self::create_point_index`].
    fn drop_point_index(&self, _label: &str, _property: &str) -> Result<()> {
        Err(crate::error::Error::Unsupported(
            "point-index DDL is not supported by this writer".into(),
        ))
    }

    /// Snapshot the currently-registered point indexes as
    /// `(label, property)` pairs. Default impl returns empty.
    fn list_point_indexes(&self) -> Result<Vec<PointIndexSpec>> {
        Ok(Vec::new())
    }

    /// Relationship-scope analogue of
    /// [`Self::create_point_index`].
    fn create_edge_point_index(&self, _edge_type: &str, _property: &str) -> Result<()> {
        Err(crate::error::Error::Unsupported(
            "edge-point-index DDL is not supported by this writer".into(),
        ))
    }

    /// Tear down an edge point index. Mirrors
    /// [`Self::create_edge_point_index`].
    fn drop_edge_point_index(&self, _edge_type: &str, _property: &str) -> Result<()> {
        Err(crate::error::Error::Unsupported(
            "edge-point-index DDL is not supported by this writer".into(),
        ))
    }

    /// Snapshot the currently-registered edge point indexes as
    /// `(edge_type, property)` pairs.
    fn list_edge_point_indexes(&self) -> Result<Vec<PointIndexSpec>> {
        Ok(Vec::new())
    }

    /// Declare a new property constraint. Default impl errors so
    /// remote writers that haven't plumbed constraint DDL yet surface
    /// the limitation immediately — storage-backed writers override
    /// via the blanket impl. `properties` is a list to accommodate
    /// composite kinds (`NodeKey`); single-property kinds pass a
    /// one-element slice.
    fn create_property_constraint(
        &self,
        _name: Option<&str>,
        _scope: &ConstraintScope,
        _properties: &[String],
        _kind: PropertyConstraintKind,
        _if_not_exists: bool,
    ) -> Result<PropertyConstraintSpec> {
        Err(crate::error::Error::Unsupported(
            "constraint DDL is not supported by this writer".into(),
        ))
    }

    /// Tear down a constraint by name. Mirrors
    /// [`Self::create_property_constraint`].
    fn drop_property_constraint(&self, _name: &str, _if_exists: bool) -> Result<()> {
        Err(crate::error::Error::Unsupported(
            "constraint DDL is not supported by this writer".into(),
        ))
    }

    /// Snapshot the currently-registered constraints for
    /// `SHOW CONSTRAINTS`. Default impl returns an empty list.
    fn list_property_constraints(&self) -> Result<Vec<PropertyConstraintSpec>> {
        Ok(Vec::new())
    }

    /// Install (or replace) an `apoc.trigger.*` registration.
    /// `spec_blob` is the serde-encoded trigger spec — opaque
    /// to the writer; the storage layer just persists the
    /// bytes. Cluster-aware writers buffer this as a
    /// `GraphCommand::InstallTrigger` so the commit path
    /// replicates it; direct-to-storage writers persist
    /// immediately.
    fn install_trigger(&self, _name: &str, _spec_blob: &[u8]) -> Result<()> {
        Err(crate::error::Error::Unsupported(
            "trigger DDL is not supported by this writer".into(),
        ))
    }

    /// Drop a registered trigger by name. Idempotent — a
    /// missing name is not an error so routing-mode rollback
    /// can re-issue partially-applied DROPs.
    fn drop_trigger(&self, _name: &str) -> Result<()> {
        Err(crate::error::Error::Unsupported(
            "trigger DDL is not supported by this writer".into(),
        ))
    }
}

/// Blanket impl: any **sized** type that implements [`StorageEngine`]
/// is automatically a [`GraphWriter`]. See the matching
/// [`crate::reader::GraphReader`] blanket for rationale, and
/// [`StorageWriterAdapter`] for the trait-object adapter.
impl<T: StorageEngine> GraphWriter for T {
    fn put_node(&self, node: &Node) -> Result<()> {
        StorageEngine::put_node(self, node)?;
        Ok(())
    }

    fn put_edge(&self, edge: &Edge) -> Result<()> {
        StorageEngine::put_edge(self, edge)?;
        Ok(())
    }

    fn delete_edge(&self, id: EdgeId) -> Result<()> {
        if StorageEngine::get_edge(self, id)?.is_some() {
            StorageEngine::delete_edge(self, id)?;
        }
        Ok(())
    }

    fn detach_delete_node(&self, id: NodeId) -> Result<()> {
        StorageEngine::detach_delete_node(self, id)?;
        Ok(())
    }

    fn create_property_index(&self, label: &str, properties: &[String]) -> Result<()> {
        StorageEngine::create_property_index_composite(self, label, properties)?;
        Ok(())
    }

    fn drop_property_index(&self, label: &str, properties: &[String]) -> Result<()> {
        StorageEngine::drop_property_index_composite(self, label, properties)?;
        Ok(())
    }

    fn list_property_indexes(&self) -> Result<Vec<NodeIndexSpec>> {
        Ok(StorageEngine::list_property_indexes(self)
            .into_iter()
            .map(|s| (s.label, s.properties))
            .collect())
    }

    fn create_edge_property_index(&self, edge_type: &str, properties: &[String]) -> Result<()> {
        StorageEngine::create_edge_property_index_composite(self, edge_type, properties)?;
        Ok(())
    }

    fn drop_edge_property_index(&self, edge_type: &str, properties: &[String]) -> Result<()> {
        StorageEngine::drop_edge_property_index_composite(self, edge_type, properties)?;
        Ok(())
    }

    fn list_edge_property_indexes(&self) -> Result<Vec<EdgeIndexSpec>> {
        Ok(StorageEngine::list_edge_property_indexes(self)
            .into_iter()
            .map(|s| (s.edge_type, s.properties))
            .collect())
    }

    fn create_point_index(&self, label: &str, property: &str) -> Result<()> {
        StorageEngine::create_point_index(self, label, property)?;
        Ok(())
    }

    fn drop_point_index(&self, label: &str, property: &str) -> Result<()> {
        StorageEngine::drop_point_index(self, label, property)?;
        Ok(())
    }

    fn list_point_indexes(&self) -> Result<Vec<PointIndexSpec>> {
        Ok(StorageEngine::list_point_indexes(self)
            .into_iter()
            .map(|s| (s.label, s.property))
            .collect())
    }

    fn create_edge_point_index(&self, edge_type: &str, property: &str) -> Result<()> {
        StorageEngine::create_edge_point_index(self, edge_type, property)?;
        Ok(())
    }

    fn drop_edge_point_index(&self, edge_type: &str, property: &str) -> Result<()> {
        StorageEngine::drop_edge_point_index(self, edge_type, property)?;
        Ok(())
    }

    fn list_edge_point_indexes(&self) -> Result<Vec<PointIndexSpec>> {
        Ok(StorageEngine::list_edge_point_indexes(self)
            .into_iter()
            .map(|s| (s.edge_type, s.property))
            .collect())
    }

    fn create_property_constraint(
        &self,
        name: Option<&str>,
        scope: &ConstraintScope,
        properties: &[String],
        kind: PropertyConstraintKind,
        if_not_exists: bool,
    ) -> Result<PropertyConstraintSpec> {
        Ok(StorageEngine::create_property_constraint(
            self,
            name,
            scope,
            properties,
            kind,
            if_not_exists,
        )?)
    }

    fn drop_property_constraint(&self, name: &str, if_exists: bool) -> Result<()> {
        StorageEngine::drop_property_constraint(self, name, if_exists)?;
        Ok(())
    }

    fn list_property_constraints(&self) -> Result<Vec<PropertyConstraintSpec>> {
        Ok(StorageEngine::list_property_constraints(self))
    }

    fn install_trigger(&self, name: &str, spec_blob: &[u8]) -> Result<()> {
        StorageEngine::put_trigger(self, name, spec_blob)?;
        Ok(())
    }

    fn drop_trigger(&self, name: &str) -> Result<()> {
        StorageEngine::delete_trigger(self, name)?;
        Ok(())
    }
}

/// Adapter that lets a `&dyn StorageEngine` act as a `GraphWriter`.
/// See [`crate::reader::StorageReaderAdapter`] for the rationale.
pub struct StorageWriterAdapter<'a>(pub &'a dyn StorageEngine);

impl GraphWriter for StorageWriterAdapter<'_> {
    fn put_node(&self, node: &Node) -> Result<()> {
        self.0.put_node(node)?;
        Ok(())
    }

    fn put_edge(&self, edge: &Edge) -> Result<()> {
        self.0.put_edge(edge)?;
        Ok(())
    }

    fn delete_edge(&self, id: EdgeId) -> Result<()> {
        if self.0.get_edge(id)?.is_some() {
            self.0.delete_edge(id)?;
        }
        Ok(())
    }

    fn detach_delete_node(&self, id: NodeId) -> Result<()> {
        self.0.detach_delete_node(id)?;
        Ok(())
    }

    fn create_property_index(&self, label: &str, properties: &[String]) -> Result<()> {
        self.0.create_property_index_composite(label, properties)?;
        Ok(())
    }

    fn drop_property_index(&self, label: &str, properties: &[String]) -> Result<()> {
        self.0.drop_property_index_composite(label, properties)?;
        Ok(())
    }

    fn list_property_indexes(&self) -> Result<Vec<NodeIndexSpec>> {
        Ok(self
            .0
            .list_property_indexes()
            .into_iter()
            .map(|s| (s.label, s.properties))
            .collect())
    }

    fn create_edge_property_index(&self, edge_type: &str, properties: &[String]) -> Result<()> {
        self.0
            .create_edge_property_index_composite(edge_type, properties)?;
        Ok(())
    }

    fn drop_edge_property_index(&self, edge_type: &str, properties: &[String]) -> Result<()> {
        self.0
            .drop_edge_property_index_composite(edge_type, properties)?;
        Ok(())
    }

    fn list_edge_property_indexes(&self) -> Result<Vec<EdgeIndexSpec>> {
        Ok(self
            .0
            .list_edge_property_indexes()
            .into_iter()
            .map(|s| (s.edge_type, s.properties))
            .collect())
    }

    fn create_point_index(&self, label: &str, property: &str) -> Result<()> {
        self.0.create_point_index(label, property)?;
        Ok(())
    }

    fn drop_point_index(&self, label: &str, property: &str) -> Result<()> {
        self.0.drop_point_index(label, property)?;
        Ok(())
    }

    fn list_point_indexes(&self) -> Result<Vec<PointIndexSpec>> {
        Ok(self
            .0
            .list_point_indexes()
            .into_iter()
            .map(|s| (s.label, s.property))
            .collect())
    }

    fn create_edge_point_index(&self, edge_type: &str, property: &str) -> Result<()> {
        self.0.create_edge_point_index(edge_type, property)?;
        Ok(())
    }

    fn drop_edge_point_index(&self, edge_type: &str, property: &str) -> Result<()> {
        self.0.drop_edge_point_index(edge_type, property)?;
        Ok(())
    }

    fn list_edge_point_indexes(&self) -> Result<Vec<PointIndexSpec>> {
        Ok(self
            .0
            .list_edge_point_indexes()
            .into_iter()
            .map(|s| (s.edge_type, s.property))
            .collect())
    }

    fn create_property_constraint(
        &self,
        name: Option<&str>,
        scope: &ConstraintScope,
        properties: &[String],
        kind: PropertyConstraintKind,
        if_not_exists: bool,
    ) -> Result<PropertyConstraintSpec> {
        Ok(self
            .0
            .create_property_constraint(name, scope, properties, kind, if_not_exists)?)
    }

    fn drop_property_constraint(&self, name: &str, if_exists: bool) -> Result<()> {
        self.0.drop_property_constraint(name, if_exists)?;
        Ok(())
    }

    fn list_property_constraints(&self) -> Result<Vec<PropertyConstraintSpec>> {
        Ok(self.0.list_property_constraints())
    }

    fn install_trigger(&self, name: &str, spec_blob: &[u8]) -> Result<()> {
        self.0.put_trigger(name, spec_blob)?;
        Ok(())
    }

    fn drop_trigger(&self, name: &str) -> Result<()> {
        self.0.delete_trigger(name)?;
        Ok(())
    }
}