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
//! Traversal and iteration methods for the LPG store.
use super::LpgStore;
use crate::graph::Direction;
use crate::graph::lpg::{Edge, Node};
use grafeo_common::types::{EdgeId, NodeId};
impl LpgStore {
// === Traversal ===
/// Iterates over neighbors of a node in the specified direction.
///
/// This is the fast path for graph traversal - goes straight to the
/// adjacency index without loading full node data.
pub fn neighbors(
&self,
node: NodeId,
direction: Direction,
) -> impl Iterator<Item = NodeId> + '_ {
let forward: Box<dyn Iterator<Item = NodeId>> = match direction {
Direction::Outgoing | Direction::Both => {
Box::new(self.forward_adj.neighbors(node).into_iter())
}
Direction::Incoming => Box::new(std::iter::empty()),
};
let backward: Box<dyn Iterator<Item = NodeId>> = match direction {
Direction::Incoming | Direction::Both => {
if let Some(ref adj) = self.backward_adj {
Box::new(adj.neighbors(node).into_iter())
} else {
Box::new(std::iter::empty())
}
}
Direction::Outgoing => Box::new(std::iter::empty()),
};
forward.chain(backward)
}
/// Returns edges from a node with their targets.
///
/// Returns an iterator of (target_node, edge_id) pairs.
pub fn edges_from(
&self,
node: NodeId,
direction: Direction,
) -> impl Iterator<Item = (NodeId, EdgeId)> + '_ {
let forward: Box<dyn Iterator<Item = (NodeId, EdgeId)>> = match direction {
Direction::Outgoing | Direction::Both => {
Box::new(self.forward_adj.edges_from(node).into_iter())
}
Direction::Incoming => Box::new(std::iter::empty()),
};
let backward: Box<dyn Iterator<Item = (NodeId, EdgeId)>> = match direction {
Direction::Incoming | Direction::Both => {
if let Some(ref adj) = self.backward_adj {
Box::new(adj.edges_from(node).into_iter())
} else {
Box::new(std::iter::empty())
}
}
Direction::Outgoing => Box::new(std::iter::empty()),
};
forward.chain(backward)
}
/// Returns edges to a node (where the node is the destination).
///
/// Returns (source_node, edge_id) pairs for all edges pointing TO this node.
/// Uses the backward adjacency index for O(degree) lookup.
///
/// # Example
///
/// ```
/// # use grafeo_core::graph::lpg::LpgStore;
/// # use grafeo_common::types::Value;
/// let store = LpgStore::new().expect("arena allocation");
/// let a = store.create_node(&["Node"]);
/// let b = store.create_node(&["Node"]);
/// let c = store.create_node(&["Node"]);
/// let _e1 = store.create_edge(a, b, "LINKS");
/// let _e2 = store.create_edge(c, b, "LINKS");
///
/// // For edges: A->B, C->B
/// let incoming = store.edges_to(b);
/// assert_eq!(incoming.len(), 2);
/// ```
pub fn edges_to(&self, node: NodeId) -> Vec<(NodeId, EdgeId)> {
if let Some(ref backward) = self.backward_adj {
backward.edges_from(node)
} else {
// Fallback: scan all edges (slow but correct)
self.all_edges()
.filter_map(|edge| {
if edge.dst == node {
Some((edge.src, edge.id))
} else {
None
}
})
.collect()
}
}
/// Returns the out-degree of a node (number of outgoing edges).
///
/// Uses the forward adjacency index for O(1) lookup.
#[must_use]
pub fn out_degree(&self, node: NodeId) -> usize {
self.forward_adj.out_degree(node)
}
/// Returns the in-degree of a node (number of incoming edges).
///
/// Uses the backward adjacency index for O(1) lookup if available,
/// otherwise falls back to scanning edges.
#[must_use]
pub fn in_degree(&self, node: NodeId) -> usize {
if let Some(ref backward) = self.backward_adj {
backward.in_degree(node)
} else {
// Fallback: count edges (slow)
self.all_edges().filter(|edge| edge.dst == node).count()
}
}
// === Admin API: Iteration ===
/// Returns an iterator over all nodes in the database.
///
/// This creates a snapshot of all visible nodes at the current epoch.
/// Useful for dump/export operations.
#[cfg(not(feature = "tiered-storage"))]
pub fn all_nodes(&self) -> impl Iterator<Item = Node> + '_ {
let epoch = self.current_epoch();
let node_ids: Vec<NodeId> = self
.nodes
.read()
.iter()
.filter_map(|(id, chain)| {
chain
.visible_at(epoch)
.and_then(|r| if !r.is_deleted() { Some(*id) } else { None })
})
.collect();
node_ids.into_iter().filter_map(move |id| self.get_node(id))
}
/// Returns an iterator over all nodes in the database.
/// (Tiered storage version)
#[cfg(feature = "tiered-storage")]
pub fn all_nodes(&self) -> impl Iterator<Item = Node> + '_ {
let node_ids = self.node_ids();
node_ids.into_iter().filter_map(move |id| self.get_node(id))
}
/// Returns an iterator over all edges in the database.
///
/// This creates a snapshot of all visible edges at the current epoch.
/// Useful for dump/export operations.
#[cfg(not(feature = "tiered-storage"))]
pub fn all_edges(&self) -> impl Iterator<Item = Edge> + '_ {
let epoch = self.current_epoch();
let edge_ids: Vec<EdgeId> = self
.edges
.read()
.iter()
.filter_map(|(id, chain)| {
chain
.visible_at(epoch)
.and_then(|r| if !r.is_deleted() { Some(*id) } else { None })
})
.collect();
edge_ids.into_iter().filter_map(move |id| self.get_edge(id))
}
/// Returns an iterator over all edges in the database.
/// (Tiered storage version)
#[cfg(feature = "tiered-storage")]
pub fn all_edges(&self) -> impl Iterator<Item = Edge> + '_ {
let epoch = self.current_epoch();
let versions = self.edge_versions.read();
let edge_ids: Vec<EdgeId> = versions
.iter()
.filter_map(|(id, index)| {
index.visible_at(epoch).and_then(|vref| {
self.read_edge_record(&vref)
.and_then(|r| if !r.is_deleted() { Some(*id) } else { None })
})
})
.collect();
edge_ids.into_iter().filter_map(move |id| self.get_edge(id))
}
/// Returns an iterator over nodes with a specific label.
pub fn nodes_with_label<'a>(&'a self, label: &str) -> impl Iterator<Item = Node> + 'a {
let node_ids = self.nodes_by_label(label);
node_ids.into_iter().filter_map(move |id| self.get_node(id))
}
/// Returns an iterator over edges with a specific type.
#[cfg(not(feature = "tiered-storage"))]
pub fn edges_with_type<'a>(&'a self, edge_type: &str) -> impl Iterator<Item = Edge> + 'a {
let epoch = self.current_epoch();
let type_to_id = self.edge_type_to_id.read();
if let Some(&type_id) = type_to_id.get(edge_type) {
let edge_ids: Vec<EdgeId> = self
.edges
.read()
.iter()
.filter_map(|(id, chain)| {
chain.visible_at(epoch).and_then(|r| {
if !r.is_deleted() && r.type_id == type_id {
Some(*id)
} else {
None
}
})
})
.collect();
// Return a boxed iterator for the found edges
Box::new(edge_ids.into_iter().filter_map(move |id| self.get_edge(id)))
as Box<dyn Iterator<Item = Edge> + 'a>
} else {
// Return empty iterator
Box::new(std::iter::empty()) as Box<dyn Iterator<Item = Edge> + 'a>
}
}
/// Returns an iterator over edges with a specific type.
/// (Tiered storage version)
#[cfg(feature = "tiered-storage")]
pub fn edges_with_type<'a>(&'a self, edge_type: &str) -> impl Iterator<Item = Edge> + 'a {
let epoch = self.current_epoch();
let type_to_id = self.edge_type_to_id.read();
if let Some(&type_id) = type_to_id.get(edge_type) {
let versions = self.edge_versions.read();
let edge_ids: Vec<EdgeId> = versions
.iter()
.filter_map(|(id, index)| {
index.visible_at(epoch).and_then(|vref| {
self.read_edge_record(&vref).and_then(|r| {
if !r.is_deleted() && r.type_id == type_id {
Some(*id)
} else {
None
}
})
})
})
.collect();
Box::new(edge_ids.into_iter().filter_map(move |id| self.get_edge(id)))
as Box<dyn Iterator<Item = Edge> + 'a>
} else {
Box::new(std::iter::empty()) as Box<dyn Iterator<Item = Edge> + 'a>
}
}
}