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
//! Graph CLI commands for VelesDB.
//!
//! Provides CLI commands for graph operations using direct core calls.
//! All commands work offline without a running server.
//!
//! Handler implementations live in [`crate::graph_handlers`].
//! Display helpers shared with the REPL live in [`crate::graph_display`].
use clap::{Subcommand, ValueEnum};
use std::path::PathBuf;
use crate::graph_handlers;
/// Traversal algorithm selection.
#[derive(Debug, Clone, Copy, ValueEnum, Default)]
pub enum TraverseAlgo {
#[default]
Bfs,
Dfs,
}
/// Edge direction for neighbor queries.
#[derive(Debug, Clone, Copy, ValueEnum, Default)]
pub enum Direction {
#[default]
Out,
In,
Both,
}
/// Graph subcommands
#[derive(Subcommand)]
pub enum GraphAction {
/// Add an edge to the graph
AddEdge {
/// Path to database directory
path: PathBuf,
/// Collection name
collection: String,
/// Edge ID
id: u64,
/// Source node ID
source: u64,
/// Target node ID
target: u64,
/// Edge label (relationship type)
label: String,
},
/// List edges, optionally filtered by label
GetEdges {
/// Path to database directory
path: PathBuf,
/// Collection name
collection: String,
/// Filter by edge label
#[arg(long)]
label: Option<String>,
/// Output format (table, json)
#[arg(short, long, default_value = "table")]
format: String,
},
/// Get the degree of a node
Degree {
/// Path to database directory
path: PathBuf,
/// Collection name
collection: String,
/// Node ID
node_id: u64,
/// Output format (table, json)
#[arg(short, long, default_value = "table")]
format: String,
},
/// Traverse the graph using BFS or DFS
Traverse {
/// Path to database directory
path: PathBuf,
/// Collection name
collection: String,
/// Source node ID
source: u64,
/// Traversal algorithm (bfs, dfs)
#[arg(long, value_enum, default_value = "bfs")]
algorithm: TraverseAlgo,
/// Maximum depth
#[arg(short = 'd', long, default_value = "3")]
max_depth: u32,
/// Maximum number of results
#[arg(short = 'l', long, default_value = "100")]
limit: usize,
/// Filter by relationship types (comma-separated)
#[arg(short = 'r', long)]
rel_types: Option<String>,
/// Output format (table, json)
#[arg(short, long, default_value = "table")]
format: String,
},
/// Get neighbors of a node (incoming, outgoing, or both)
Neighbors {
/// Path to database directory
path: PathBuf,
/// Collection name
collection: String,
/// Node ID
node_id: u64,
/// Edge direction (in, out, both)
#[arg(long, value_enum, default_value = "out")]
direction: Direction,
/// Output format (table, json)
#[arg(short, long, default_value = "table")]
format: String,
},
/// Store a JSON payload on a graph node
StorePayload {
/// Path to database directory
path: PathBuf,
/// Collection name
collection: String,
/// Node ID
node_id: u64,
/// JSON payload (e.g., '{"name": "Alice"}')
payload: String,
},
/// Retrieve the JSON payload of a graph node
GetPayload {
/// Path to database directory
path: PathBuf,
/// Collection name
collection: String,
/// Node ID
node_id: u64,
},
/// Remove an edge by ID
RemoveEdge {
/// Path to database directory
path: PathBuf,
/// Collection name
collection: String,
/// Edge ID to remove
edge_id: u64,
},
/// Count total edges in the graph
Count {
/// Path to database directory
path: PathBuf,
/// Collection name
collection: String,
/// Output format (table, json)
#[arg(short, long, default_value = "table")]
format: String,
},
/// Search graph nodes by embedding similarity
Search {
/// Path to database directory
path: PathBuf,
/// Collection name
collection: String,
/// Query vector as JSON array (e.g., '[0.1, 0.2, 0.3]')
vector: String,
/// Number of results to return
#[arg(short = 'k', long, default_value = "10")]
top_k: usize,
/// Output format (table, json)
#[arg(short, long, default_value = "table")]
format: String,
},
/// List all nodes with stored payloads (paginated)
Nodes {
/// Path to database directory
path: PathBuf,
/// Graph collection name
collection: String,
/// Page number (1-indexed)
#[arg(short, long, default_value = "1")]
page: usize,
/// Output format (table, json)
#[arg(short, long, default_value = "table")]
format: String,
},
/// Scan for legacy phantom edges (edge store entries whose source or
/// target node has no stored payload) and report or repair them.
///
/// Read-only by default: reports the phantom edge count with no
/// mutation unless `--purge` or `--stub` is passed. These predate
/// #1442's referential-integrity check and can only exist in a
/// database that was created before that fix landed -- WAL replay at
/// `Collection::open` intentionally never re-validates edges, so
/// filtering them there would risk data loss for legitimate
/// edge-only graphs. `doctor` is the explicit, opt-in alternative.
Doctor {
/// Path to database directory
path: PathBuf,
/// Graph collection name
collection: String,
/// Remove phantom edges from the edge store (crash-durable via
/// the existing remove_edge/WAL-tombstone path)
#[arg(long, conflicts_with = "stub")]
purge: bool,
/// Seed a minimal `{}` payload for each missing endpoint instead
/// of removing the edge
#[arg(long, conflicts_with = "purge")]
stub: bool,
/// Output format (table, json)
#[arg(short, long, default_value = "table")]
format: String,
},
}
/// Handle graph subcommands with direct core calls.
///
/// # Errors
///
/// Returns an error if the database cannot be opened or the graph collection
/// is not found.
pub fn handle(action: GraphAction) -> anyhow::Result<()> {
match action {
GraphAction::AddEdge {
path,
collection,
id,
source,
target,
label,
} => graph_handlers::handle_add_edge(&path, &collection, id, source, target, &label),
GraphAction::GetEdges {
path,
collection,
label,
format,
} => graph_handlers::handle_get_edges(&path, &collection, label.as_deref(), &format),
GraphAction::Degree {
path,
collection,
node_id,
format,
} => graph_handlers::handle_degree(&path, &collection, node_id, &format),
GraphAction::Traverse {
path,
collection,
source,
algorithm,
max_depth,
limit,
rel_types,
format,
} => graph_handlers::handle_traverse(
&path,
&collection,
source,
algorithm,
max_depth,
limit,
rel_types.as_deref(),
&format,
),
GraphAction::Neighbors {
path,
collection,
node_id,
direction,
format,
} => graph_handlers::handle_neighbors(&path, &collection, node_id, direction, &format),
GraphAction::StorePayload {
path,
collection,
node_id,
payload,
} => graph_handlers::handle_store_payload(&path, &collection, node_id, &payload),
GraphAction::GetPayload {
path,
collection,
node_id,
} => graph_handlers::handle_get_payload(&path, &collection, node_id),
GraphAction::RemoveEdge {
path,
collection,
edge_id,
} => graph_handlers::handle_remove_edge(&path, &collection, edge_id),
GraphAction::Count {
path,
collection,
format,
} => graph_handlers::handle_count(&path, &collection, &format),
GraphAction::Search {
path,
collection,
vector,
top_k,
format,
} => graph_handlers::handle_graph_search(&path, &collection, &vector, top_k, &format),
GraphAction::Nodes {
path,
collection,
page,
format,
} => graph_handlers::handle_graph_nodes(&path, &collection, page, &format),
GraphAction::Doctor {
path,
collection,
purge,
stub,
format,
} => graph_handlers::handle_graph_doctor(&path, &collection, purge, stub, &format),
}
}
#[cfg(test)]
#[path = "graph_bdd_tests.rs"]
mod graph_bdd_tests;
#[cfg(test)]
#[path = "graph_doctor_tests.rs"]
mod graph_doctor_tests;