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
//! Temporal Diff Engine.
//!
//! Computes the structural and semantic difference between two points in time.
use crate::AletheiaDB;
use crate::core::error::Result;
use crate::core::id::{EdgeId, EntityId, NodeId, VersionId};
use crate::core::interning::GLOBAL_INTERNER;
use crate::core::property::PropertyMap;
use crate::core::temporal::Timestamp;
use std::collections::HashMap;
/// A report of changes between two timestamps.
#[derive(Debug, Clone)]
pub struct DiffReport {
/// The first timestamp (baseline).
pub t1: Timestamp,
/// The second timestamp (comparison).
pub t2: Timestamp,
/// The list of changes detected.
pub changes: Vec<EntityChange>,
}
impl Default for DiffReport {
fn default() -> Self {
// Use a safe default (e.g. epoch 0) since Timestamp doesn't impl Default
let ts = Timestamp::from(0);
Self {
t1: ts,
t2: ts,
changes: Vec::new(),
}
}
}
/// A change to a single entity.
#[derive(Debug, Clone)]
pub enum EntityChange {
/// A node changed.
Node {
/// The ID of the node.
id: NodeId,
/// The type of change.
change: ChangeType,
},
/// An edge changed.
Edge {
/// The ID of the edge.
id: EdgeId,
/// The type of change.
change: ChangeType,
},
}
/// The type of change.
#[derive(Debug, Clone)]
pub enum ChangeType {
/// Entity exists at T2 but not at T1.
Added,
/// Entity exists at T1 but not at T2.
Removed,
/// Entity exists at both but properties differ.
Modified {
/// The difference in properties.
diff: PropertyDiff,
},
}
/// Difference in properties.
#[derive(Debug, Clone, Default)]
pub struct PropertyDiff {
/// Keys added in the new version.
pub added: Vec<String>,
/// Keys removed in the new version.
pub removed: Vec<String>,
/// Keys changed. Maps key -> (old_value, new_value).
pub changed: HashMap<String, (String, String)>,
}
/// The Temporal Diff Engine.
pub struct TemporalDiff<'a> {
db: &'a AletheiaDB,
}
impl<'a> TemporalDiff<'a> {
/// Create a new TemporalDiff engine.
pub fn new(db: &'a AletheiaDB) -> Self {
Self { db }
}
/// Compute the difference between the graph state at `t1` and `t2`.
///
/// This compares the set of valid entities and their properties.
/// It uses `valid_time` = T and `transaction_time` = T for both points,
/// effectively comparing "What we knew at T1 about T1" vs "What we knew at T2 about T2".
/// This is the standard "Snapshot Diff".
///
/// # Arguments
/// * `t1`: The first timestamp (baseline).
/// * `t2`: The second timestamp (comparison).
/// * `limit`: Optional maximum number of entities to process. If None, processes all entities.
/// **Warning**: Without a limit, this operation is O(N) and may consume significant memory.
pub fn compute_diff(
&self,
t1: Timestamp,
t2: Timestamp,
limit: Option<usize>,
) -> Result<DiffReport> {
let iterator = self.db.temporal_indexes.entity_ids();
let mut changes = Vec::new();
// Apply limit if provided, otherwise iterate all
let entity_iter: Box<dyn Iterator<Item = EntityId>> = if let Some(limit) = limit {
Box::new(iterator.take(limit))
} else {
Box::new(iterator)
};
for entity_id in entity_iter {
// Find version visible at t1
let v1 = self.find_version(entity_id, t1);
// Find version visible at t2
let v2 = self.find_version(entity_id, t2);
let change = match (v1, v2) {
(None, None) => continue, // Not visible at either time
(None, Some(_)) => Some(ChangeType::Added),
(Some(_), None) => Some(ChangeType::Removed),
(Some(ver1), Some(ver2)) => {
if ver1 == ver2 {
// Same version ID means no change in properties
None
} else {
// Different versions, check properties
let props1 = self.get_properties(entity_id, ver1)?;
let props2 = self.get_properties(entity_id, ver2)?;
let diff = self.diff_properties(&props1, &props2);
if diff.is_empty() {
None
} else {
Some(ChangeType::Modified { diff })
}
}
}
};
if let Some(change_type) = change {
match entity_id {
EntityId::Node(id) => changes.push(EntityChange::Node {
id,
change: change_type,
}),
EntityId::Edge(id) => changes.push(EntityChange::Edge {
id,
change: change_type,
}),
}
}
}
Ok(DiffReport { t1, t2, changes })
}
fn find_version(&self, entity_id: EntityId, time: Timestamp) -> Option<VersionId> {
match entity_id {
EntityId::Node(id) => {
let versions = self
.db
.temporal_indexes
.find_node_version_at_point(id, time, time);
versions.first().copied()
}
EntityId::Edge(id) => {
let versions = self
.db
.temporal_indexes
.find_edge_version_at_point(id, time, time);
versions.first().copied()
}
}
}
fn get_properties(&self, entity_id: EntityId, version_id: VersionId) -> Result<PropertyMap> {
let historical = self.db.historical.read();
match entity_id {
EntityId::Node(_) => {
// Reconstruct properties (handles deltas).
// This will return an error if the version is not found.
historical.reconstruct_node_properties(version_id)
}
EntityId::Edge(_) => {
// Reconstruct properties (handles deltas).
// This will return an error if the version is not found.
historical.reconstruct_edge_properties(version_id)
}
}
}
fn diff_properties(&self, p1: &PropertyMap, p2: &PropertyMap) -> PropertyDiff {
let mut added = Vec::new();
let mut removed = Vec::new();
let mut changed = HashMap::new();
// Check for removed or changed
for (key, val1) in p1.iter() {
match p2.get_by_interned_key(key) {
None => {
if let Some(s) = GLOBAL_INTERNER.resolve_with(*key, |s| s.to_string()) {
removed.push(s);
}
}
Some(val2) => {
// Simple comparison using PartialEq
if val1 != val2
&& let Some(s) = GLOBAL_INTERNER.resolve_with(*key, |s| s.to_string())
{
changed.insert(s, (format!("{:?}", val1), format!("{:?}", val2)));
}
}
}
}
// Check for added
for (key, _) in p2.iter() {
if !p1.contains_interned_key(key)
&& let Some(s) = GLOBAL_INTERNER.resolve_with(*key, |s| s.to_string())
{
added.push(s);
}
}
PropertyDiff {
added,
removed,
changed,
}
}
}
impl PropertyDiff {
/// Check if the difference is empty (no changes).
pub fn is_empty(&self) -> bool {
self.added.is_empty() && self.removed.is_empty() && self.changed.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::api::transaction::WriteOps;
use crate::core::property::PropertyMapBuilder;
use crate::core::temporal::time;
#[test]
fn test_temporal_diff_lifecycle() {
let db = AletheiaDB::new().unwrap();
let t0 = time::now();
// Ensure t1 > t0
std::thread::sleep(std::time::Duration::from_millis(10));
// T1: Create Node
let props1 = PropertyMapBuilder::new()
.insert("name", "Nova")
.insert("level", 1)
.build();
let node_id = db.create_node("Agent", props1).unwrap();
std::thread::sleep(std::time::Duration::from_millis(10));
let t1 = time::now();
std::thread::sleep(std::time::Duration::from_millis(10));
// T2: Update Node
let props2 = PropertyMapBuilder::new()
.insert("level", 2)
.insert("status", "Active")
.build();
// Update usually merges/replaces properties.
db.write(|tx| tx.update_node(node_id, props2)).unwrap();
std::thread::sleep(std::time::Duration::from_millis(10));
let t2 = time::now();
std::thread::sleep(std::time::Duration::from_millis(10));
// T3: Delete Node
db.write(|tx| tx.delete_node(node_id)).unwrap();
std::thread::sleep(std::time::Duration::from_millis(10));
let t3 = time::now();
let diff_engine = TemporalDiff::new(&db);
// Diff T0 -> T1 (Added)
let report_0_1 = diff_engine.compute_diff(t0, t1, None).unwrap();
assert_eq!(report_0_1.changes.len(), 1, "Should report 1 addition");
if let EntityChange::Node { id, change } = &report_0_1.changes[0] {
assert_eq!(*id, node_id);
assert!(matches!(change, ChangeType::Added));
} else {
panic!("Expected Node change");
}
// Diff T1 -> T2 (Modified)
let report_1_2 = diff_engine.compute_diff(t1, t2, None).unwrap();
assert_eq!(report_1_2.changes.len(), 1, "Should report 1 modification");
if let EntityChange::Node { id, change } = &report_1_2.changes[0] {
assert_eq!(*id, node_id);
match change {
ChangeType::Modified { diff } => {
// "level" changed 1 -> 2.
assert!(diff.changed.contains_key("level"));
// "status" added.
assert!(diff.added.contains(&"status".to_string()));
// "name" unchanged, should NOT be in diff.
assert!(
!diff.changed.contains_key("name"),
"Unchanged property 'name' should not be in diff"
);
assert!(
!diff.removed.contains(&"name".to_string()),
"Unchanged property 'name' should not be removed"
);
// Explicitly verify is_empty logic by asserting this diff is NOT empty
assert!(!diff.is_empty(), "PropertyDiff should not be empty");
}
_ => panic!("Expected Modified, got {:?}", change),
}
} else {
panic!("Expected Node change");
}
// Diff T2 -> T3 (Removed)
let report_2_3 = diff_engine.compute_diff(t2, t3, None).unwrap();
assert_eq!(report_2_3.changes.len(), 1, "Should report 1 removal");
if let EntityChange::Node { id, change } = &report_2_3.changes[0] {
assert_eq!(*id, node_id);
assert!(matches!(change, ChangeType::Removed));
} else {
panic!("Expected Node change");
}
// No-Op Diff: T1 -> T1
let report_same = diff_engine.compute_diff(t1, t1, None).unwrap();
assert_eq!(
report_same.changes.len(),
0,
"No changes expected for same timestamp"
);
}
#[test]
fn test_temporal_diff_limit() {
let db = AletheiaDB::new().unwrap();
// Create 10 nodes
for i in 0..10 {
let props = PropertyMapBuilder::new().insert("idx", i as i64).build();
db.create_node("Node", props).unwrap();
}
let t1 = time::now();
let t0 = Timestamp::from(0); // Before creation
let diff_engine = TemporalDiff::new(&db);
// Limit = 5
let report = diff_engine.compute_diff(t0, t1, Some(5)).unwrap();
assert_eq!(
report.changes.len(),
5,
"Should only report 5 changes due to limit"
);
// Limit = None (All)
let report_all = diff_engine.compute_diff(t0, t1, None).unwrap();
assert_eq!(
report_all.changes.len(),
10,
"Should report all 10 changes without limit"
);
}
#[test]
fn test_property_diff_is_empty() {
let diff = PropertyDiff::default();
assert!(diff.is_empty(), "Default diff should be empty");
let diff_added = PropertyDiff {
added: vec!["key".to_string()],
..Default::default()
};
assert!(
!diff_added.is_empty(),
"Diff with added should not be empty"
);
let diff_removed = PropertyDiff {
removed: vec!["key".to_string()],
..Default::default()
};
assert!(
!diff_removed.is_empty(),
"Diff with removed should not be empty"
);
let mut changed = HashMap::new();
changed.insert("key".to_string(), ("old".to_string(), "new".to_string()));
let diff_changed = PropertyDiff {
changed,
..Default::default()
};
assert!(
!diff_changed.is_empty(),
"Diff with changed should not be empty"
);
}
}