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
// SPDX-License-Identifier: BUSL-1.1
use nodedb_types::{DatabaseId, TenantId};
use super::store::{Direction, Edge, EdgeStore, REVERSE_EDGES, redb_err};
use super::temporal::{
EdgeRef, EdgeValuePayload, is_sentinel, parse_versioned_edge_key, versioned_edge_key,
};
impl EdgeStore {
/// Outbound neighbors of a node within the caller's database, tenant and
/// collection, current-state only.
pub fn neighbors_out(
&self,
db: u64,
tid: TenantId,
collection: &str,
src: &str,
label_filter: Option<&str>,
) -> crate::Result<Vec<Edge>> {
let prefix = match label_filter {
Some(label) => format!("{collection}\x00{src}\x00{label}\x00"),
None => format!("{collection}\x00{src}\x00"),
};
self.scan_edges_with_prefix(
db,
tid,
&prefix,
|fwd_collection, fwd_src, fwd_label, fwd_dst| Edge {
collection: fwd_collection.to_string(),
src_id: fwd_src.to_string(),
label: fwd_label.to_string(),
dst_id: fwd_dst.to_string(),
properties: Vec::new(),
},
)
}
/// Inbound neighbors of a node within the caller's tenant and
/// collection, current-state only.
///
/// Scans the reverse index (dst-first versioned keys), dedupes by base,
/// and drops bases whose latest reverse entry is a sentinel.
pub fn neighbors_in(
&self,
db: u64,
tid: TenantId,
collection: &str,
dst: &str,
label_filter: Option<&str>,
) -> crate::Result<Vec<Edge>> {
use std::collections::HashMap;
let prefix = match label_filter {
Some(label) => format!("{collection}\x00{dst}\x00{label}\x00"),
None => format!("{collection}\x00{dst}\x00"),
};
let t = tid.as_u64();
let read_txn = self
.db
.begin_read()
.map_err(|e| redb_err("begin_read", e))?;
let table = read_txn
.open_table(REVERSE_EDGES)
.map_err(|e| redb_err("open reverse", e))?;
// In the reverse index the key is `{coll}\x00{dst}\x00{label}\x00{src}\x00{sys}`.
// So the parsed tuple's "src" position IS the original dst, and the
// "dst" position IS the original src. We track versions by the
// logical base `(coll, src, label, dst)` i.e. after the swap.
let mut latest: HashMap<(String, String, String), (i64, bool)> = HashMap::new();
let range = table
.range((db, t, prefix.as_str())..)
.map_err(|e| redb_err("range", e))?;
for entry in range {
let (key, val) = entry.map_err(|e| redb_err("iter", e))?;
let (kd, kt, composite) = key.value();
if kd != db || kt != t || !composite.starts_with(&prefix) {
break;
}
let Some((_coll, _rev_dst, rev_label, rev_src, sys)) =
parse_versioned_edge_key(composite)
else {
continue;
};
// Logical src (the neighbor we want) and label form the base.
let base = (rev_label.to_string(), rev_src.to_string(), dst.to_string());
let is_sent = is_sentinel(val.value());
latest
.entry(base)
.and_modify(|(cur, cur_sent)| {
if sys > *cur {
*cur = sys;
*cur_sent = is_sent;
}
})
.or_insert((sys, is_sent));
}
// Live bases only — load properties from forward table's Ceiling.
let mut edges = Vec::with_capacity(latest.len());
for ((label, src_id, dst_id), (_sys, is_sent)) in latest {
if is_sent {
continue;
}
let Some(props) = self.ceiling_resolve_edge(
EdgeRef::new(
DatabaseId::new(db),
tid,
collection,
&src_id,
&label,
&dst_id,
),
i64::MAX,
None,
)?
else {
continue;
};
edges.push(Edge {
collection: collection.to_string(),
src_id,
label,
dst_id,
properties: props,
});
}
Ok(edges)
}
/// All neighbors (both directions) within the caller's tenant and collection.
pub fn neighbors(
&self,
db: u64,
tid: TenantId,
collection: &str,
node: &str,
label_filter: Option<&str>,
direction: Direction,
) -> crate::Result<Vec<Edge>> {
match direction {
Direction::Out => self.neighbors_out(db, tid, collection, node, label_filter),
Direction::In => self.neighbors_in(db, tid, collection, node, label_filter),
Direction::Both => {
let mut out = self.neighbors_out(db, tid, collection, node, label_filter)?;
let inbound = self.neighbors_in(db, tid, collection, node, label_filter)?;
out.extend(inbound);
Ok(out)
}
}
}
/// Count outbound edges from a source node (current-state).
pub fn out_degree(
&self,
db: u64,
tid: TenantId,
collection: &str,
src: &str,
label_filter: Option<&str>,
) -> crate::Result<usize> {
Ok(self
.neighbors_out(db, tid, collection, src, label_filter)?
.len())
}
/// Count inbound edges to a destination node (current-state).
pub fn in_degree(
&self,
db: u64,
tid: TenantId,
collection: &str,
dst: &str,
label_filter: Option<&str>,
) -> crate::Result<usize> {
Ok(self
.neighbors_in(db, tid, collection, dst, label_filter)?
.len())
}
}
// Keep `versioned_edge_key` / `EdgeValuePayload` referenced in case a future
// tier inlines them here instead of routing through `scan_edges_with_prefix`.
#[allow(dead_code)]
fn _keep_temporal_helpers_referenced() {
let _ = versioned_edge_key;
let _: fn(_, _, _) -> _ = EdgeValuePayload::new;
}