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
//! The graph's logical-byte accounting (ADR-0042, ADR-0202): the
//! live-size oracle, the redo-batch admission estimates, and the charge
//! helpers every mutation seam rides. Split out of `graph/mod.rs` (LABS-677).
use std::sync::Arc;
use relate_core::{Node, NodeId, RelId};
use super::*;
pub(super) mod adjacency;
impl Graph {
/// Live graph + index logical bytes (ADR-0042, ADR-0202).
pub fn used_bytes(&self) -> u64 {
self.used_bytes
.load(Ordering::Relaxed)
.saturating_add(self.interner.bytes())
}
/// Full walk of retained versions, adjacency, and the interner
/// (test / `rebuild_indexes` oracle).
pub fn accounted_bytes(&self) -> u64 {
self.structure_bytes().saturating_add(self.interner.bytes())
}
pub(super) fn structure_bytes(&self) -> u64 {
let charge = self.index_charge();
let mut n = 0u64;
n = n.saturating_add(self.nodes.array_bytes());
self.nodes.for_each(|_id, vers| {
n = n.saturating_add(crate::memory::chain_bytes(
vers.shape(),
node_version_stride(),
));
for v in vers.iter() {
n = n.saturating_add(crate::memory::live_node_account_live(
v.live,
&self.interner,
&charge,
));
}
});
n = n.saturating_add(self.rels.array_bytes());
self.rels.for_each(|_id, vers| {
n = n.saturating_add(crate::memory::chain_bytes(
vers.shape(),
rel_version_stride(),
));
for v in vers.iter() {
n = n.saturating_add(crate::memory::live_rel_account_live(v.live));
}
});
// Each side's slot arrays and retained delta entries, and the sealed
// arenas (ADR-0551 / ADR-0599) — the arena bytes, the 12-byte
// entries, the tombstones, and the dense span tables that index
// them — the term the heap census reads as `slot_bytes`.
for side in [self.out_side(), self.in_side()] {
n = n.saturating_add(side.delta().array_bytes());
side.delta()
.for_each(|_id, adj| n = n.saturating_add(adj.floor_bytes()));
}
n = n.saturating_add(self.sealed_floor());
// LABS-351 / LABS-370: the degree histograms and the count
// bundle's charged maps are retained structure — the recount
// oracle must include them or the incremental entry charges
// drift from it (the #398 lesson). Each family's term lives
// with the family (graph/degree_stats.rs,
// graph/count_state.rs) and composes here, at the oracle's
// one site, beside the constraint-key term.
n = n.saturating_add(self.degree_stats_structure_bytes());
n = n.saturating_add(self.count_state_structure_bytes());
// ADR-0439: the committed constraint-key map is retained structure
// too; it carries the bytes each entry was charged, so the recount
// and the incremental charge cannot drift.
n = n.saturating_add(self.constraint_keys_structure_bytes());
n
}
pub fn recompute_used(&self) {
self.used_bytes
.store(self.structure_bytes(), Ordering::Relaxed);
}
/// The committed dense id spans — a group admission walk's seed
/// ([`DenseSpans`](crate::memory::DenseSpans)): its first slot
/// estimates against these and the walk carries the values each
/// admitted slot advanced them to. Every dense span the admission
/// estimate prices rides here: the node and relationship slot arrays,
/// the adjacency tier's two directions, and the sealed span table's
/// two.
#[doc(hidden)]
pub fn dense_spans(&self) -> crate::memory::DenseSpans {
crate::memory::DenseSpans {
node: self.nodes.span(),
rel: self.rels.span(),
adj_out: self.out_side().delta().span(),
adj_in: self.in_side().delta().span(),
sealed_out: self.out_side().sealed().span(),
sealed_in: self.in_side().sealed().span(),
}
}
/// Live-size delta of `ops`, its spill reported separately
/// ([`RedoDelta::spill`](crate::memory::RedoDelta::spill)). Nodes are
/// accounted under the charge in force after `ops`; when that differs
/// from the current charge (DDL in the batch), every retained version —
/// including ones `ops` tombstone — is moved from the old charge to the
/// new one. A delete keeps its record until GC, so it adds nothing. A
/// copy-on-write set adds the new version.
///
/// The one single-record entry point: it seeds from the committed spans
/// and derives the ADR-0439 term itself, so no caller re-spells that
/// order — a caller holding a seed (the group admission walk) instead
/// passes its own term to [`Graph::estimate_redo_delta_with_constraint_growth`].
#[doc(hidden)]
pub fn estimate_redo_delta(&self, ops: &[RedoOp]) -> crate::memory::RedoDelta {
let growth = self.estimate_constraint_key_entry_growth(ops);
self.estimate_redo_delta_with_constraint_growth(ops, growth, self.dense_spans())
.0
}
/// [`Graph::estimate_redo_delta`] with the constraint-key entry-growth
/// term and the dense-span seed supplied by the caller. The group-commit
/// admission loop classifies each slot once and passes the delta its walk
/// produced, so the slot's ops are not re-classified here (ADR-0439).
/// `spans` is the seed — the walk's advanced dense spans for a group (its
/// slots price against what the group already reserved), the committed
/// ones for a single-record caller — and the advanced spans come back OUT
/// beside the delta, never written through the seed: the caller keeps them
/// only for an accepted slot, so a refused estimate leaves no phantom rung
/// behind.
#[doc(hidden)]
pub fn estimate_redo_delta_with_constraint_growth(
&self,
ops: &[RedoOp],
constraint_growth: i64,
spans: crate::memory::DenseSpans,
) -> (crate::memory::RedoDelta, crate::memory::DenseSpans) {
let before = self.index_charge();
let after = self.charge_after_ops(ops);
let (
crate::memory::RedoDelta {
total: mut delta,
mut spill,
},
chain,
) = crate::memory::estimate_redo_delta(
&self.interner,
ops,
|id| self.node(id),
|id| self.rel_form(id),
|id| self.node_chain_shape(id),
|id| self.rel_chain_shape(id),
|s| crate::memory::chain_bytes(s, node_version_stride()),
|s| crate::memory::chain_bytes(s, rel_version_stride()),
spans,
&after,
);
let (adj, adj_spans) = self.estimate_adj_delta(ops, &spans);
delta += adj.refusable + adj.spill;
spill += adj.spill;
delta += self.estimate_intern_delta(ops);
// LABS-370 (security follow-up): the count family's entry growth
// is committed structure too — an oversized-label write must be
// rejected at admission, not after its entries land past the cap
// (graph/degree_stats.rs owns the term).
delta += self.estimate_stats_entry_growth(ops);
// ADR-0439: a constrained write's new entries are committed
// structure too — refused at admission, never after they land. The
// caller supplies the term: derived by
// [`Graph::estimate_constraint_key_entry_growth`] for a single-record
// caller,
// or the delta the group admission walk already computed.
delta += constraint_growth;
if before.as_ref() != &after {
self.nodes.for_each(|id, vers| {
for v in vers.iter() {
let n = v.live.to_node(id, &self.interner);
delta += crate::memory::live_node_account(&n, &after) as i64
- crate::memory::live_node_account(&n, &before) as i64;
}
});
}
(
crate::memory::RedoDelta {
total: delta,
spill,
},
// The one merge of the two advances: the chain spans from the
// memory walk, the adjacency and sealed spans from this one — a
// seed advanced by every dense-span family the estimate prices,
// each walk owning exactly its fields.
crate::memory::DenseSpans {
node: chain.node,
rel: chain.rel,
adj_out: adj_spans.adj_out,
adj_in: adj_spans.adj_in,
sealed_out: adj_spans.sealed_out,
sealed_in: adj_spans.sealed_in,
},
)
}
fn estimate_intern_delta(&self, ops: &[RedoOp]) -> i64 {
let mut seen = HashSet::new();
let mut extra = 0i64;
let mut consider = |s: &str| {
if !seen.insert(s.to_string()) {
return;
}
if self.interner.get_id(s).is_none() {
extra += crate::memory::interned_string_bytes(s.len()) as i64;
}
};
for op in ops {
match op {
RedoOp::CreateNode {
labels, properties, ..
} => {
for label in labels.iter() {
consider(label);
}
for key in properties.keys() {
consider(key);
}
}
RedoOp::CreateRel {
rel_type,
properties,
..
} => {
consider(rel_type);
for key in properties.keys() {
consider(key);
}
}
RedoOp::SetNodeProp { key, .. }
| RedoOp::RemoveNodeProp { key, .. }
| RedoOp::SetRelProp { key, .. }
| RedoOp::RemoveRelProp { key, .. } => consider(key),
RedoOp::AddLabels { labels, .. } | RedoOp::RemoveLabels { labels, .. } => {
for label in labels {
consider(label);
}
}
_ => {}
}
}
extra
}
pub(super) fn index_charge(&self) -> Arc<crate::memory::IndexCharge> {
Arc::clone(&self.indexes.charge_cache.lock().unwrap())
}
pub(super) fn refresh_index_charge(&self) {
let charge = {
let theater = self.theater.lock().unwrap();
Arc::new(crate::memory::IndexCharge::from_details(
theater.index_details.values(),
))
};
*self.indexes.charge_cache.lock().unwrap() = charge;
}
fn charge_after_ops(&self, ops: &[RedoOp]) -> crate::memory::IndexCharge {
let theater = self.theater.lock().unwrap();
let mut details = theater.index_details.clone();
drop(theater);
crate::redo::project_index_details(&mut details, ops);
crate::memory::IndexCharge::from_details(details.values())
}
pub(super) fn charge(&self, delta: i64) {
// One policy in three stated cases: nothing to apply (routine —
// `charge(0)` is the no-constrained-tuple-moved call), a plain
// growth, and a release that is the saturation itself
// (`saturating_add_signed` floors at zero and takes the delta's
// sign, so no negation that overflows at `i64::MIN`).
if delta == 0 {
return;
}
if delta > 0 {
self.used_bytes.fetch_add(delta as u64, Ordering::Relaxed);
return;
}
self.used_bytes
.try_update(Ordering::Relaxed, Ordering::Relaxed, |u| {
Some(u.saturating_add_signed(delta))
})
.expect("the saturating-add closure never rejects");
}
pub(super) fn charge_node_swap(&self, before: Option<&Node>, after: Option<&Node>) {
let charge = self.index_charge();
let b = before
.map(|n| crate::memory::live_node_account(n, &charge))
.unwrap_or(0);
let a = after
.map(|n| crate::memory::live_node_account(n, &charge))
.unwrap_or(0);
self.charge(a as i64 - b as i64);
}
pub(super) fn charge_rel_after_edit(
&self,
id: RelId,
before_bytes: u64,
before_shape: ChainShape,
) {
let after_shape = self.rel_chain_shape(id);
let after_bytes = self.visible_rel_account(id);
if after_shape != before_shape {
let growth = crate::memory::chain_bytes(after_shape, rel_version_stride()) as i64
- crate::memory::chain_bytes(before_shape, rel_version_stride()) as i64;
self.charge(after_bytes as i64 + growth);
} else {
self.charge(after_bytes as i64 - before_bytes as i64);
}
}
#[cfg(test)]
pub(super) fn node_chain_len(&self, id: NodeId) -> usize {
self.nodes.get(&id).map_or(0, |v| v.len())
}
pub(super) fn node_chain_shape(&self, id: NodeId) -> ChainShape {
self.nodes
.get(&id)
.map_or(ChainShape::Absent, |v| v.shape())
}
pub(super) fn rel_chain_shape(&self, id: RelId) -> ChainShape {
self.rels.get(&id).map_or(ChainShape::Absent, |v| v.shape())
}
/// Copy-on-write keeps the previous version. An in-place edit replaces it.
pub(super) fn charge_node_after_edit(
&self,
id: NodeId,
before: Option<&Node>,
before_shape: ChainShape,
) {
let after = self.node(id);
let after_shape = self.node_chain_shape(id);
if after_shape != before_shape {
let charge = self.index_charge();
let added = after
.as_ref()
.map(|n| crate::memory::live_node_account(n, &charge))
.unwrap_or(0);
let growth = crate::memory::chain_bytes(after_shape, node_version_stride()) as i64
- crate::memory::chain_bytes(before_shape, node_version_stride()) as i64;
self.charge(added as i64 + growth);
} else {
self.charge_node_swap(before, after.as_ref());
}
}
}