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
use super::super::csr_shared::{decode_endpoint_pair, is_reclaimable_cold};
use super::super::{EdgeId, EdgePosition, HotNbr, Nbr, Timestamp, VertexId, INVALID_EDGE_ID};
use super::pack::frozen_key_range;
use super::ImmutableCsr;
impl ImmutableCsr {
pub(crate) fn row_window(&self, src_vid: u32) -> Option<(usize, usize)> {
let idx = src_vid as usize;
if idx >= self.degrees.len() {
return None;
}
let start = self.offsets[idx] as usize;
let degree = self.degrees[idx] as usize;
if start.saturating_add(degree) > self.hot_entries.len() {
return None;
}
Some((start, start + degree))
}
/// Assembled slot copy at a packed index.
#[inline]
pub(crate) fn slot_at(&self, idx: usize) -> Option<Nbr> {
Some(Nbr::from_parts(
*self.hot_entries.get(idx)?,
*self.cold_entries.get(idx)?,
))
}
/// Row length of one vertex. Out-of-range rows report zero.
pub fn row_degree(&self, src_vid: u32) -> usize {
let idx = src_vid as usize;
if idx >= self.degrees.len() {
0
} else {
self.degrees[idx] as usize
}
}
/// Timestamp-filtered read of one row.
///
/// UNFILTERED single-predicate read for tests and offline tools only:
/// filters by row delete stamp without consulting the version authority.
/// Production traversals must use the row iterator or caller-buffer fill
/// paths with the authority gate.
pub fn edges_of(&self, src_vid: u32, ts: Timestamp) -> Vec<Nbr> {
let Some((start, end)) = self.row_window(src_vid) else {
return Vec::new();
};
self.hot_entries[start..end]
.iter()
.zip(&self.cold_entries[start..end])
.filter_map(|(hot, cold)| {
let nbr = Nbr::from_parts(*hot, *cold);
nbr.is_alive_at(ts).then_some(nbr)
})
.collect()
}
/// First timestamp-visible entry matching an endpoint key.
///
/// Bisects the sorted row to the `(endpoint, rank)` key range, then
/// filters inside the range by timestamp. The row sort makes edge-id
/// order the total version order within each key.
pub fn get_edge(&self, src_vid: u32, dst: VertexId, ts: Timestamp) -> Option<Nbr> {
let (decoded_endpoint, decoded_rank) = decode_endpoint_pair(dst)?;
let (start, end) = self.row_window(src_vid)?;
let hot = &self.hot_entries[start..end];
let cold = &self.cold_entries[start..end];
let (lo, hi) = frozen_key_range(hot, decoded_endpoint, decoded_rank);
hot[lo..hi]
.iter()
.zip(&cold[lo..hi])
.find_map(|(hot, cold)| {
let nbr = Nbr::from_parts(*hot, *cold);
nbr.is_alive_at(ts).then_some(nbr)
})
}
/// First live entry matching an endpoint key without consulting snapshots.
///
/// Same key-range bisection as [`Self::get_edge`]; liveness is the raw
/// open-deletion-stamp check instead of a timestamp filter.
pub fn get_edge_physical(&self, src_vid: u32, dst: VertexId) -> Option<Nbr> {
let (decoded_endpoint, decoded_rank) = decode_endpoint_pair(dst)?;
let (start, end) = self.row_window(src_vid)?;
let hot = &self.hot_entries[start..end];
let cold = &self.cold_entries[start..end];
let (lo, hi) = frozen_key_range(hot, decoded_endpoint, decoded_rank);
hot[lo..hi]
.iter()
.zip(&cold[lo..hi])
.find_map(|(hot, cold)| {
if hot.edge_id != INVALID_EDGE_ID && cold.is_live() {
Some(Nbr::from_parts(*hot, *cold))
} else {
None
}
})
}
/// Every physically stored entry of one row without timestamp filtering.
///
/// Test and offline use; production scans use `fill_physical_into` or
/// the row iterator instead of this allocating accessor.
pub fn physical_edges_of(&self, src_vid: u32) -> Vec<Nbr> {
let mut out = Vec::new();
self.fill_physical_into(src_vid, &mut out);
out
}
/// Fill a caller buffer with every physically stored entry of one row.
pub fn fill_physical_into(&self, src_vid: u32, out: &mut Vec<Nbr>) {
out.clear();
let Some((start, end)) = self.row_window(src_vid) else {
return;
};
out.reserve(end - start);
for (hot, cold) in self.hot_entries[start..end]
.iter()
.zip(&self.cold_entries[start..end])
{
out.push(Nbr::from_parts(*hot, *cold));
}
}
/// Visit every physically stored entry of one row without allocating.
pub fn visit_physical<F>(&self, src_vid: u32, mut f: F)
where
F: FnMut(Nbr) -> bool,
{
let Some((start, end)) = self.row_window(src_vid) else {
return;
};
for (hot, cold) in self.hot_entries[start..end]
.iter()
.zip(&self.cold_entries[start..end])
{
if !f(Nbr::from_parts(*hot, *cold)) {
return;
}
}
}
/// Visit every physically stored hot half of one row without allocating
/// and without touching the stamp lines.
///
/// Hot-only counterpart of [`Self::visit_physical`] for traversals that
/// resolve visibility through the version authority by `edge_id`.
pub fn visit_hot<F>(&self, src_vid: u32, mut f: F)
where
F: FnMut(HotNbr) -> bool,
{
let Some((start, end)) = self.row_window(src_vid) else {
return;
};
for hot in &self.hot_entries[start..end] {
if !f(*hot) {
return;
}
}
}
/// Packed rows are always sorted by `(endpoint, rank, edge_id)`.
pub fn is_row_sorted(&self, _src_vid: u32) -> bool {
true
}
/// Visit live entries whose `(endpoint, rank)` key falls in the inclusive
/// `[lower, upper]` range. The packed row is sorted, so the window is
/// bisected and only the window is scanned for liveness.
pub fn visit_threshold<F>(
&self,
src_vid: u32,
lower: Option<(u32, i64)>,
upper: Option<(u32, i64)>,
mut f: F,
) where
F: FnMut(Nbr) -> bool,
{
let Some((start, end)) = self.row_window(src_vid) else {
return;
};
let hot = &self.hot_entries[start..end];
let cold = &self.cold_entries[start..end];
let lo = match lower {
Some((lo_ep, lo_rank)) => {
hot.partition_point(|h| (h.endpoint, h.rank) < (lo_ep, lo_rank))
}
None => 0,
};
let hi = match upper {
Some((hi_ep, hi_rank)) => {
hot.partition_point(|h| (h.endpoint, h.rank) <= (hi_ep, hi_rank))
}
None => hot.len(),
};
for (h, c) in hot[lo..hi].iter().zip(&cold[lo..hi]) {
if c.is_live() && !f(Nbr::from_parts(*h, *c)) {
return;
}
}
}
/// Fill a caller buffer with the same range content as `visit_threshold`.
pub fn fill_threshold_into(
&self,
src_vid: u32,
lower: Option<(u32, i64)>,
upper: Option<(u32, i64)>,
out: &mut Vec<Nbr>,
) {
out.clear();
self.visit_threshold(src_vid, lower, upper, |nbr| {
out.push(nbr);
true
});
}
/// Read-only view of one packed slot without mutating state.
pub fn nbr_at_offset(&self, src_vid: u32, offset: i32) -> Option<Nbr> {
if offset < 0 {
return None;
}
let (start, end) = self.row_window(src_vid)?;
if start.saturating_add(offset as usize) >= end {
return None;
}
self.slot_at(start + offset as usize)
}
/// Whether one row holds any physically stored entry.
pub fn has_physical_entries(&self, vid: u32) -> bool {
self.row_degree(vid) > 0
}
/// Whether one row holds `edge_id`.
pub fn primary_contains(&self, src_vid: u32, edge_id: EdgeId) -> bool {
let Some((start, end)) = self.row_window(src_vid) else {
return false;
};
self.hot_entries[start..end]
.iter()
.any(|hot| hot.edge_id == edge_id)
}
/// Locate the first entry with `edge_id`, returning its packed slot.
pub fn locate_edge(&self, src_vid: u32, edge_id: EdgeId) -> Option<(EdgePosition, Nbr)> {
let (start, end) = self.row_window(src_vid)?;
for (slot, (hot, cold)) in self.hot_entries[start..end]
.iter()
.zip(&self.cold_entries[start..end])
.enumerate()
{
if hot.edge_id == edge_id {
return Some((
EdgePosition::Primary { slot: slot as u32 },
Nbr::from_parts(*hot, *cold),
));
}
}
None
}
/// Whether this frozen group carries a bundled inline value column.
///
/// Only groups packed from a valued bundled group do; every other pack
/// leaves the value columns empty and reads every value as NULL.
pub fn has_valued_entries(&self) -> bool {
!self.values.is_empty()
}
/// Raw value word plus validity at a packed index.
///
/// Out-of-range indexes and groups without value columns read as a zero
/// word with a cleared bit, matching the bundled NULL convention.
pub(crate) fn value_at_index(&self, idx: usize) -> (u64, bool) {
if idx >= self.values.len() {
return (0, false);
}
let raw = self.values[idx];
let valid = self.valid.get(idx).map(|b| *b).unwrap_or(false);
(raw, valid)
}
/// Read the inline value of one edge by id within its source row.
///
/// Paired value half of a frozen topology walk: resolve the edge from
/// the topology walk first, then read its value here.
pub fn value_by_edge_id(&self, src_vid: u32, edge_id: EdgeId) -> Option<(u64, bool)> {
let (start, end) = self.row_window(src_vid)?;
for idx in start..end {
if self.hot_entries[idx].edge_id == edge_id {
return Some(self.value_at_index(idx));
}
}
None
}
/// Read the inline value of the first physical entry for one endpoint.
///
/// First-match walk like the bundled accessor: tombstones are physical
/// entries and may match, mirroring the bundled layout where deletion
/// clears validity but keeps the slot position.
pub fn value_by_endpoint(&self, src_vid: u32, endpoint: u32) -> Option<(u64, bool)> {
let (start, end) = self.row_window(src_vid)?;
for idx in start..end {
let hot = self.hot_entries[idx];
if hot.endpoint == endpoint && hot.edge_id != INVALID_EDGE_ID {
return Some(self.value_at_index(idx));
}
}
None
}
/// Visit every physically stored entry of one row with its inline value
/// (`None` for NULL slots).
///
/// Frozen counterpart of the bundled paired traversal: topology and
/// value stay slot-parallel after the pack reorder, so one index serves
/// both halves.
pub fn visit_physical_with_values<F>(&self, src_vid: u32, mut f: F)
where
F: FnMut(Nbr, Option<u64>) -> bool,
{
let Some((start, end)) = self.row_window(src_vid) else {
return;
};
for idx in start..end {
let nbr = Nbr::from_parts(self.hot_entries[idx], self.cold_entries[idx]);
let (raw, valid) = self.value_at_index(idx);
if !f(nbr, valid.then_some(raw)) {
return;
}
}
}
/// Whether any slot holds a valid inline value.
pub fn any_valid_values(&self) -> bool {
self.valid.count_ones() > 0
}
/// Packed value words for snapshot-file writers.
pub(crate) fn packed_values(&self) -> &[u64] {
&self.values
}
/// Packed validity bytes for snapshot-file writers.
///
/// Exactly covering the packed entries: trailing bits of the last byte
/// are zero padding, never edges.
pub(crate) fn packed_valid_bytes(&self) -> &[u8] {
let want = self.values.len().div_ceil(8);
self.valid.as_raw_slice().get(..want).unwrap_or(&[])
}
/// Physical entry census of one row: `(live, dead, capacity)`.
///
/// Same live/dead predicates as the mutable census; capacity equals the
/// row length because frozen rows carry no reserved gaps.
pub fn vertex_census(&self, vid: u32) -> (usize, usize, usize) {
let Some((start, end)) = self.row_window(vid) else {
return (0, 0, 0);
};
let mut live = 0usize;
let mut dead = 0usize;
for cold in &self.cold_entries[start..end] {
if cold.is_live() {
live += 1;
} else {
dead += 1;
}
}
(live, dead, end - start)
}
/// Count entries of one row reclaimable at `cutoff`.
pub fn reclaimable_count(&self, vid: u32, cutoff: Timestamp) -> usize {
if cutoff == Timestamp::MAX {
return 0;
}
let Some((start, end)) = self.row_window(vid) else {
return 0;
};
self.cold_entries[start..end]
.iter()
.filter(|cold| is_reclaimable_cold(cold, cutoff))
.count()
}
}