grafeo-engine 0.5.41

Query engine and database management for Grafeo
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
//! Integration tests for temporal (versioned) property storage.
//!
//! These tests validate that when the `temporal` feature is enabled,
//! `get_node_at_epoch()` and `execute_at_epoch()` return correct
//! historical property values, not just current ones.
//!
//! When `temporal` is disabled, these tests are skipped.

#![cfg(feature = "temporal")]

use grafeo_common::types::Value;
use grafeo_engine::GrafeoDB;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn setup_db() -> GrafeoDB {
    GrafeoDB::new_in_memory()
}

// ---------------------------------------------------------------------------
// Property versioning: basic get_node_at_epoch
// ---------------------------------------------------------------------------

#[test]
fn test_temporal_property_at_epoch() {
    let db = setup_db();
    let mut session = db.session();

    // Create node with initial properties
    session.begin_transaction().unwrap();
    session
        .execute("INSERT (:Server {name: 'web-01', status: 'healthy'})")
        .unwrap();
    session.commit().unwrap();
    let epoch_v1 = db.current_epoch();

    // Update status
    session.begin_transaction().unwrap();
    session
        .execute("MATCH (s:Server {name: 'web-01'}) SET s.status = 'degraded'")
        .unwrap();
    session.commit().unwrap();
    let epoch_v2 = db.current_epoch();

    // Update again
    session.begin_transaction().unwrap();
    session
        .execute("MATCH (s:Server {name: 'web-01'}) SET s.status = 'offline'")
        .unwrap();
    session.commit().unwrap();

    // Current state: offline
    let result = session
        .execute("MATCH (s:Server {name: 'web-01'}) RETURN s.status")
        .unwrap();
    assert_eq!(result.rows()[0][0], Value::String("offline".into()));

    // At epoch_v1: healthy
    let result = session
        .execute_at_epoch(
            "MATCH (s:Server {name: 'web-01'}) RETURN s.status",
            epoch_v1,
        )
        .unwrap();
    assert_eq!(result.rows().len(), 1);
    assert_eq!(
        result.rows()[0][0],
        Value::String("healthy".into()),
        "temporal query at epoch_v1 should return 'healthy'"
    );

    // At epoch_v2: degraded
    let result = session
        .execute_at_epoch(
            "MATCH (s:Server {name: 'web-01'}) RETURN s.status",
            epoch_v2,
        )
        .unwrap();
    assert_eq!(result.rows().len(), 1);
    assert_eq!(
        result.rows()[0][0],
        Value::String("degraded".into()),
        "temporal query at epoch_v2 should return 'degraded'"
    );
}

#[test]
fn test_temporal_multiple_properties() {
    let db = setup_db();
    let mut session = db.session();

    // Create with initial properties
    session.begin_transaction().unwrap();
    session
        .execute("INSERT (:Server {name: 'db-01', cpu: 10, memory: 1024})")
        .unwrap();
    session.commit().unwrap();
    let epoch_v1 = db.current_epoch();

    // Update cpu only
    session.begin_transaction().unwrap();
    session
        .execute("MATCH (s:Server {name: 'db-01'}) SET s.cpu = 80")
        .unwrap();
    session.commit().unwrap();
    let epoch_v2 = db.current_epoch();

    // Update memory only
    session.begin_transaction().unwrap();
    session
        .execute("MATCH (s:Server {name: 'db-01'}) SET s.memory = 2048")
        .unwrap();
    session.commit().unwrap();

    // At epoch_v1: cpu=10, memory=1024
    let result = session
        .execute_at_epoch(
            "MATCH (s:Server {name: 'db-01'}) RETURN s.cpu, s.memory",
            epoch_v1,
        )
        .unwrap();
    assert_eq!(result.rows()[0][0], Value::Int64(10));
    assert_eq!(result.rows()[0][1], Value::Int64(1024));

    // At epoch_v2: cpu=80, memory=1024 (only cpu changed)
    let result = session
        .execute_at_epoch(
            "MATCH (s:Server {name: 'db-01'}) RETURN s.cpu, s.memory",
            epoch_v2,
        )
        .unwrap();
    assert_eq!(result.rows()[0][0], Value::Int64(80));
    assert_eq!(result.rows()[0][1], Value::Int64(1024));
}

#[test]
fn test_temporal_where_filter_at_epoch() {
    let db = setup_db();
    let mut session = db.session();

    session.begin_transaction().unwrap();
    session
        .execute("INSERT (:Server {name: 'web-01', status: 'healthy'})")
        .unwrap();
    session
        .execute("INSERT (:Server {name: 'web-02', status: 'healthy'})")
        .unwrap();
    session.commit().unwrap();
    let epoch_all_healthy = db.current_epoch();

    // Make web-01 degraded
    session.begin_transaction().unwrap();
    session
        .execute("MATCH (s:Server {name: 'web-01'}) SET s.status = 'degraded'")
        .unwrap();
    session.commit().unwrap();

    // Current: 1 healthy server
    let result = session
        .execute("MATCH (s:Server) WHERE s.status = 'healthy' RETURN s.name")
        .unwrap();
    assert_eq!(result.rows().len(), 1);

    // At epoch_all_healthy: 2 healthy servers
    let result = session
        .execute_at_epoch(
            "MATCH (s:Server) WHERE s.status = 'healthy' RETURN s.name",
            epoch_all_healthy,
        )
        .unwrap();
    assert_eq!(
        result.rows().len(),
        2,
        "temporal WHERE filter should see both servers as healthy at epoch_all_healthy"
    );
}

#[test]
fn test_temporal_node_history_distinct_properties() {
    let db = setup_db();
    let mut session = db.session();

    // Create and commit
    session.begin_transaction().unwrap();
    session
        .execute("INSERT (:Server {name: 'web-01', status: 'healthy'})")
        .unwrap();
    session.commit().unwrap();

    // Modify status
    session.begin_transaction().unwrap();
    session
        .execute("MATCH (s:Server {name: 'web-01'}) SET s.status = 'degraded'")
        .unwrap();
    session.commit().unwrap();

    // get_node_history should return versions with distinct property snapshots
    let history = db.get_node_history(grafeo_common::types::NodeId::new(0));
    assert!(!history.is_empty(), "node should have history");
}

#[test]
fn test_temporal_edge_property_versioning() {
    let db = setup_db();
    let mut session = db.session();

    session.begin_transaction().unwrap();
    session
        .execute("INSERT (:Server {name: 'web-01'})")
        .unwrap();
    session.execute("INSERT (:Server {name: 'db-01'})").unwrap();
    session
        .execute(
            "MATCH (a:Server {name: 'web-01'}), (b:Server {name: 'db-01'}) \
             INSERT (a)-[:CONNECTS {latency: 5}]->(b)",
        )
        .unwrap();
    session.commit().unwrap();
    let epoch_v1 = db.current_epoch();

    // Update edge property
    session.begin_transaction().unwrap();
    session
        .execute(
            "MATCH (:Server {name: 'web-01'})-[c:CONNECTS]->(:Server {name: 'db-01'}) \
             SET c.latency = 50",
        )
        .unwrap();
    session.commit().unwrap();

    // At epoch_v1: latency=5
    let result = session
        .execute_at_epoch(
            "MATCH (:Server {name: 'web-01'})-[c:CONNECTS]->(:Server {name: 'db-01'}) \
             RETURN c.latency",
            epoch_v1,
        )
        .unwrap();
    assert_eq!(result.rows().len(), 1);
    assert_eq!(
        result.rows()[0][0],
        Value::Int64(5),
        "edge property should be 5 at epoch_v1"
    );
}

#[test]
fn test_temporal_snapshot_roundtrip() {
    let db = setup_db();
    let mut session = db.session();

    // Create data with multiple epochs
    session.begin_transaction().unwrap();
    session
        .execute("INSERT (:Server {name: 'web-01', status: 'healthy'})")
        .unwrap();
    session.commit().unwrap();
    let epoch_v1 = db.current_epoch();

    session.begin_transaction().unwrap();
    session
        .execute("MATCH (s:Server {name: 'web-01'}) SET s.status = 'degraded'")
        .unwrap();
    session.commit().unwrap();

    // Export and import
    let snapshot = db.export_snapshot().unwrap();
    let db2 = GrafeoDB::import_snapshot(&snapshot).unwrap();
    let session2 = db2.session();

    // Current state preserved (via direct API since GQL match-pattern
    // property filtering with temporal snapshots is a known limitation)
    let node = db2.get_node_at_epoch(grafeo_common::types::NodeId::new(0), db2.current_epoch());
    assert!(node.is_some(), "node should exist after snapshot import");
    let status = node.unwrap().get_property("status").cloned();
    assert_eq!(status, Some(Value::String("degraded".into())));

    // Historical state preserved
    let result = session2
        .execute_at_epoch("MATCH (s:Server) RETURN s.status", epoch_v1)
        .unwrap();
    // At epoch_v1, node was imported with its full version history,
    // so status should be 'healthy'
    if !result.rows().is_empty() {
        assert_eq!(
            result.rows()[0][0],
            Value::String("healthy".into()),
            "snapshot roundtrip should preserve temporal property history"
        );
    }
}

#[test]
fn test_temporal_snapshot_query_roundtrip() {
    let db = setup_db();
    let mut session = db.session();

    session.begin_transaction().unwrap();
    session
        .execute("INSERT (:Server {name: 'web-01', status: 'healthy'})")
        .unwrap();
    session.commit().unwrap();
    let epoch_v1 = db.current_epoch();

    session.begin_transaction().unwrap();
    session
        .execute("MATCH (s:Server {name: 'web-01'}) SET s.status = 'degraded'")
        .unwrap();
    session.commit().unwrap();

    let snapshot = db.export_snapshot().unwrap();
    let db2 = GrafeoDB::import_snapshot(&snapshot).unwrap();
    let session2 = db2.session();

    // This requires the planner's filter pushdown to use epoch-aware
    // property reads when pattern-matching like {name: 'web-01'}.
    let result = session2
        .execute("MATCH (s:Server {name: 'web-01'}) RETURN s.status")
        .unwrap();
    assert_eq!(result.rows()[0][0], Value::String("degraded".into()));

    // Historical state preserved
    let result = session2
        .execute_at_epoch(
            "MATCH (s:Server {name: 'web-01'}) RETURN s.status",
            epoch_v1,
        )
        .unwrap();
    assert_eq!(
        result.rows()[0][0],
        Value::String("healthy".into()),
        "snapshot roundtrip should preserve temporal property history"
    );
}

#[test]
fn test_temporal_transaction_rollback() {
    let db = setup_db();
    let mut session = db.session();

    session.begin_transaction().unwrap();
    session
        .execute("INSERT (:Server {name: 'web-01', status: 'healthy'})")
        .unwrap();
    session.commit().unwrap();

    // Start transaction, modify, then rollback
    session.begin_transaction().unwrap();
    session
        .execute("MATCH (s:Server {name: 'web-01'}) SET s.status = 'offline'")
        .unwrap();
    session.rollback().unwrap();

    // Status should still be healthy
    let result = session
        .execute("MATCH (s:Server {name: 'web-01'}) RETURN s.status")
        .unwrap();
    assert_eq!(result.rows()[0][0], Value::String("healthy".into()));
}

#[test]
fn test_temporal_savepoint_rollback() {
    let db = setup_db();
    let mut session = db.session();

    // Create node and commit
    session.begin_transaction().unwrap();
    session
        .execute("INSERT (:Server {name: 'web-01', status: 'healthy', cpu: 10})")
        .unwrap();
    session.commit().unwrap();
    let epoch_v1 = db.current_epoch();

    // Start transaction, modify, set savepoint, modify again, rollback to savepoint
    session.begin_transaction().unwrap();
    session
        .execute("MATCH (s:Server {name: 'web-01'}) SET s.status = 'degraded'")
        .unwrap();
    session.savepoint("sp1").unwrap();

    // Changes after savepoint
    session
        .execute("MATCH (s:Server {name: 'web-01'}) SET s.status = 'offline'")
        .unwrap();
    session
        .execute("MATCH (s:Server {name: 'web-01'}) SET s.cpu = 99")
        .unwrap();

    // Rollback to savepoint: status should revert to 'degraded', cpu to 10
    session.rollback_to_savepoint("sp1").unwrap();

    // Commit the pre-savepoint changes
    session.commit().unwrap();
    let epoch_v2 = db.current_epoch();

    // Current: status should be 'degraded' (pre-savepoint), cpu should be 10 (unchanged by commit)
    let result = session
        .execute("MATCH (s:Server {name: 'web-01'}) RETURN s.status, s.cpu")
        .unwrap();
    assert_eq!(
        result.rows()[0][0],
        Value::String("degraded".into()),
        "post-savepoint status change should be rolled back"
    );
    assert_eq!(
        result.rows()[0][1],
        Value::Int64(10),
        "post-savepoint cpu change should be rolled back"
    );

    // Historical: epoch_v1 should still show 'healthy'
    let result = session
        .execute_at_epoch(
            "MATCH (s:Server {name: 'web-01'}) RETURN s.status",
            epoch_v1,
        )
        .unwrap();
    assert_eq!(
        result.rows()[0][0],
        Value::String("healthy".into()),
        "temporal query at epoch_v1 should still return 'healthy' after savepoint rollback"
    );

    // epoch_v2 should show 'degraded'
    let result = session
        .execute_at_epoch(
            "MATCH (s:Server {name: 'web-01'}) RETURN s.status",
            epoch_v2,
        )
        .unwrap();
    assert_eq!(
        result.rows()[0][0],
        Value::String("degraded".into()),
        "temporal query at epoch_v2 should return 'degraded'"
    );
}

#[test]
fn test_temporal_gc_does_not_break_current_state() {
    let db = setup_db();
    let mut session = db.session();

    session.begin_transaction().unwrap();
    session
        .execute("INSERT (:Server {name: 'web-01', status: 'v1'})")
        .unwrap();
    session.commit().unwrap();

    session.begin_transaction().unwrap();
    session
        .execute("MATCH (s:Server {name: 'web-01'}) SET s.status = 'v2'")
        .unwrap();
    session.commit().unwrap();

    session.begin_transaction().unwrap();
    session
        .execute("MATCH (s:Server {name: 'web-01'}) SET s.status = 'v3'")
        .unwrap();
    session.commit().unwrap();

    // Run GC
    db.gc();

    // Current state should still work
    let result = session
        .execute("MATCH (s:Server {name: 'web-01'}) RETURN s.status")
        .unwrap();
    assert_eq!(result.rows()[0][0], Value::String("v3".into()));
}