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
//! Checkpoint-facing graph API: restoring checkpoint rows into a graph
//! being loaded, and the payload sizes and dirty ids a checkpoint reads.
use super::id_vec::IdVec;
use super::*;
/// Every checkpoint row is restored as a version born at ts 1.
const RESTORED_XMIN: u64 = 1;
/// Install a checkpoint row as `id`'s only version, inline. Before any
/// write a slot holds at most an earlier row for the same id — overlapping
/// legacy parts (ADR-0183) can hold an id twice — and the later row replaces
/// it rather than stacking a second live version a delete would leave visible.
fn restore<Live>(map: &IdVec<Chain<Live>>, id: i64, live: Live) -> Result<()> {
match map.entry(id) {
super::id_vec::Entry::Vacant(mut slot) => {
slot.insert(Chain::inline(RESTORED_XMIN, live));
}
super::id_vec::Entry::Occupied(mut slot) => match slot.get_mut() {
prev @ Chain::Inline { .. } => *prev = Chain::inline(RESTORED_XMIN, live),
Chain::Spilled(_) => {
return Err(relate_core::Error::storage(
"checkpoint restore into a graph with committed writes",
))
}
},
}
Ok(())
}
/// Checkpoint payload of `id`'s version visible at `ts`, measured by
/// `bytes`; `None` when no version is visible.
fn visible_checkpoint_bytes<Live>(
map: &IdVec<Chain<Live>>,
id: i64,
ts: u64,
bytes: impl FnOnce(&Live) -> Result<u64>,
) -> Result<Option<u64>> {
let Some(vers) = map.get(&id) else {
return Ok(None);
};
vers.visible_at(ts).map(|v| bytes(v.live)).transpose()
}
impl Graph {
/// Node ids changed since the last `clear_checkpoint_dirty` (ADR-0022).
pub fn dirty_node_ids(&self) -> HashSet<NodeId> {
self.dirty_nodes.lock().unwrap().clone()
}
/// Relationship ids changed since the last `clear_checkpoint_dirty`.
pub fn dirty_rel_ids(&self) -> HashSet<RelId> {
self.dirty_rels.lock().unwrap().clone()
}
pub fn clear_checkpoint_dirty(&self) {
self.dirty_nodes.lock().unwrap().clear();
self.dirty_rels.lock().unwrap().clear();
}
/// Variable checkpoint payload of the node visible at `ts`: label bytes
/// plus tagged property JSON. The Parquet codec adds its Int64 column
/// width. Borrows compact properties; does not build the property map.
pub fn node_checkpoint_bytes(&self, id: NodeId, ts: u64) -> Result<Option<u64>> {
visible_checkpoint_bytes(&self.nodes, id, ts, |live| {
live.checkpoint_bytes(&self.interner)
})
}
/// Variable checkpoint payload of the relationship visible at `ts`:
/// type-name bytes plus tagged property JSON. The Parquet codec adds
/// its Int64 column width.
pub fn rel_checkpoint_bytes(&self, id: RelId, ts: u64) -> Result<Option<u64>> {
visible_checkpoint_bytes(&self.rels, id, ts, |live| {
live.checkpoint_bytes(&self.interner)
})
}
/// Restore a node from a checkpoint (visible at commit_ts). Loading an
/// id again replaces its row; the later row wins. Only valid on a graph
/// being restored, before any write: an open write is `Error::Storage`.
/// The label index is built by `Graph::finish_restore` after the load.
pub fn load_checkpoint_node(&self, node: Node) -> Result<()> {
self.check_restoring()?;
let next = checked_persisted_id(node.id, "node")?;
self.next_node_id.fetch_max(next, Ordering::Relaxed);
self.catalog
.lock()
.unwrap()
.record_labels(node.labels.iter().cloned());
let live = LiveNode::from_parts(
&self.interner,
std::sync::Arc::unwrap_or_clone(node.labels),
std::sync::Arc::unwrap_or_clone(node.properties).into(),
);
restore(&self.nodes, node.id, live)
}
/// Restore a relationship from a checkpoint. Loading an id again
/// replaces its row, endpoints and type included; the later row wins.
/// Only valid on a graph being restored, before any write: an open
/// write is `Error::Storage`. Adjacency is built from the restored rows
/// by `Graph::finish_restore` after the load.
pub fn load_checkpoint_rel(&self, rel: Relationship) -> Result<()> {
self.check_restoring()?;
let next = checked_persisted_id(rel.id, "relationship")?;
checked_persisted_id(rel.start, "relationship start")?;
checked_persisted_id(rel.end, "relationship end")?;
self.next_rel_id.fetch_max(next, Ordering::Relaxed);
let live = LiveRel::from_parts(
&self.interner,
rel.start,
rel.end,
&rel.rel_type,
std::sync::Arc::unwrap_or_clone(rel.properties).into(),
);
self.catalog.lock().unwrap().record_rel_type(rel.rel_type);
restore(&self.rels, rel.id, live)
}
/// End a checkpoint load, after its last row: build adjacency — the
/// build itself emits the durable shape, the chunks at or above the
/// seal floor straight into their folded form (ADR-0608) — and clear
/// the dirty ids, which the checkpoint already holds. Called once per
/// load. The index rebuild is deliberately NOT here: the open flow's
/// single post-replay site runs it exactly once per open, after the
/// graph is final (snapshot plus replayed tail) — a snapshot+tail
/// reopen must not pay the whole-graph rebuild twice (LABS-1112).
#[doc(hidden)]
pub fn finish_restore(&self) {
self.rebuild_adj();
self.clear_checkpoint_dirty();
}
fn check_restoring(&self) -> Result<()> {
if self.in_write.load(Ordering::Acquire) {
return Err(relate_core::Error::storage(
"checkpoint restore while a write is open",
));
}
Ok(())
}
}