hawdb 0.35.1

An embedded Rust graph database for local data-plane workloads
Documentation
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// Copyright 2026 Nowledge
// SPDX-License-Identifier: Apache-2.0

//! Private parameterized statements used by route qualification fixtures.
//! Hosts own their application queries; these are not embedded database APIs.

use super::{RouteCypherQuery, RouteQuery};
use crate::{HawDBError, Result, Value};
use std::collections::BTreeMap;

// Qualification runs each statement with these independent ceilings. Query LIMIT
// still bounds graph work; exceeding an output ceiling fails the evidence run.
pub(super) const ROUTE_STATEMENT_OUTPUT_LIMITS: crate::QueryStreamOptions =
    crate::QueryStreamOptions {
        max_rows: Some(16_384),
        max_payload_bytes: Some(16 * 1024 * 1024),
    };

const GRAPH_OVERVIEW_ROUTE: &str = "/graph/overview";
const GRAPH_OVERVIEW_MEMORY_RANKING_QUERY: &str = "\
MATCH (m:Memory) \
RETURN m.id AS memory_id, \
id(m) AS node_id, \
COALESCE(m.title, LEFT(m.content, 60)) AS label, \
m.title AS title, \
LEFT(COALESCE(m.content, ''), 200) AS content_preview, \
COALESCE(m.pagerank_score, m.importance, 0.5) AS score, \
m.community_id AS community_id, \
m.space_id AS raw_space_id, \
m.created_at AS created_at, \
m.updated_at AS updated_at, \
m.source AS source, \
m.event_start AS event_start, \
m.event_end AS event_end, \
m.importance AS importance \
ORDER BY COALESCE(m.pagerank_score, m.importance, 0.5) DESC, m.id ASC \
LIMIT $limit";
const GRAPH_SAMPLE_ROUTE: &str = "/graph/sample";
const GRAPH_SAMPLE_MEMORY_QUERY: &str = "\
MATCH (m:Memory) \
RETURN m.id AS memory_id, \
id(m) AS node_id, \
COALESCE(m.title, LEFT(m.content, 60)) AS label, \
m.title AS title, \
LEFT(COALESCE(m.content, ''), 200) AS content_preview, \
COALESCE(m.pagerank_score, m.importance, 0.5) AS score, \
m.community_id AS community_id, \
m.space_id AS raw_space_id, \
m.created_at AS created_at, \
m.updated_at AS updated_at, \
m.source AS source, \
m.event_start AS event_start, \
m.event_end AS event_end, \
m.importance AS importance \
ORDER BY m.id ASC \
LIMIT $limit";
const GRAPH_NODE_DETAILS_ROUTE: &str = "/graph/node-details/{node_id}";
const GRAPH_NODE_DETAILS_MEMORY_QUERY: &str = "\
MATCH (m:Memory) \
WHERE id(m) = $node_id \
RETURN id(m) AS node_id, \
m.id AS memory_id, \
'Memory' AS node_kind, \
COALESCE(m.title, LEFT(m.content, 60), m.id, 'Memory') AS label, \
m.title AS title, \
m.content AS content, \
LEFT(COALESCE(m.content, ''), 500) AS content_preview, \
m.summary AS summary, \
m.source AS source, \
m.space_id AS raw_space_id, \
m.community_id AS community_id, \
m.created_at AS created_at, \
m.updated_at AS updated_at, \
m.event_start AS event_start, \
m.event_end AS event_end, \
m.importance AS importance, \
m.confidence AS confidence, \
m.is_latest AS is_latest, \
m.is_deleted AS is_deleted \
LIMIT 1";
const GRAPH_COMMUNITY_MEMBERS_ROUTE: &str = "/graph/community-members/{community_id}";
const GRAPH_COMMUNITY_MEMBERS_MEMORY_QUERY: &str = "\
MATCH (m:Memory) \
WHERE m.community_id = $community_id \
RETURN m.id AS memory_id, \
id(m) AS node_id, \
COALESCE(m.title, LEFT(m.content, 60)) AS label, \
m.title AS title, \
LEFT(COALESCE(m.content, ''), 200) AS content_preview, \
COALESCE(m.pagerank_score, m.importance, 0.5) AS score, \
m.community_id AS community_id, \
m.space_id AS raw_space_id, \
m.created_at AS created_at, \
m.updated_at AS updated_at, \
m.source AS source, \
m.event_start AS event_start, \
m.event_end AS event_end, \
m.importance AS importance \
ORDER BY COALESCE(m.pagerank_score, m.importance, 0.5) DESC, m.id ASC \
LIMIT $limit";
const GRAPH_COMMUNITY_RECENT_MEMORIES_ROUTE: &str =
    "/library/community/{community_id}/recent-memories";
const GRAPH_COMMUNITY_RECENT_MEMORIES_QUERY: &str = "\
MATCH (m:Memory)-[:MENTIONS]->(e:Entity) \
WHERE e.community_id = $community_id \
WITH m.id AS memory_id, \
id(m) AS node_id, \
COALESCE(m.title, LEFT(m.content, 60)) AS label, \
m.title AS title, \
m.content AS content, \
LEFT(COALESCE(m.content, ''), 200) AS content_preview, \
m.importance AS importance, \
m.created_at AS created_at, \
m.updated_at AS updated_at, \
m.is_crystal AS is_crystal, \
COUNT(DISTINCT e) AS mention_breadth \
ORDER BY created_at DESC \
LIMIT $limit \
RETURN memory_id AS memory_id, \
node_id AS node_id, \
label AS label, \
title AS title, \
content AS content, \
content_preview AS content_preview, \
importance AS importance, \
created_at AS created_at, \
updated_at AS updated_at, \
is_crystal AS is_crystal, \
mention_breadth AS mention_breadth";
const GRAPH_COMMUNITY_SUBGRAPH_ROUTE: &str = "/library/community/{community_id}/subgraph";
const GRAPH_COMMUNITY_SUBGRAPH_ENTITY_QUERY: &str = "\
MATCH (e:Entity) \
WHERE e.community_id = $community_id \
OPTIONAL MATCH (:Memory)-[r:MENTIONS]->(e) \
RETURN e.id AS entity_id, \
id(e) AS node_id, \
COALESCE(e.name, e.id) AS label, \
e.name AS name, \
e.entity_type AS entity_type, \
e.confidence AS confidence, \
COUNT(r) AS mention_count \
ORDER BY mention_count DESC, e.name ASC \
LIMIT $max_entities";
const GRAPH_COMMUNITY_SUBGRAPH_EDGE_QUERY: &str = "\
MATCH (e1:Entity)-[r:RELATES_TO]-(e2:Entity) \
WHERE e1.id IN $entity_ids \
AND e2.id IN $entity_ids \
AND e1.id < e2.id \
RETURN e1.id AS source_entity_id, \
e2.id AS target_entity_id, \
id(r) AS relationship_id, \
r.confidence AS confidence, \
r.relation_type AS relation_type \
LIMIT $max_edges";
const GRAPH_AUGMENTATION_STATE_ROUTE: &str = "/graph/augmentation/state";
const GRAPH_AUGMENTATION_STATE_QUERY: &str = "\
MATCH (m:GraphMeta {meta_id: 'main'}) \
RETURN m.community_detection_applied AS community_detection_applied, \
m.pagerank_applied AS pagerank_applied, \
m.community_algorithm AS community_algorithm, \
m.community_resolution AS community_resolution, \
m.community_count AS community_count, \
m.pagerank_algorithm AS pagerank_algorithm, \
m.pagerank_damping AS pagerank_damping, \
m.pagerank_iterations AS pagerank_iterations, \
m.last_augmentation_at AS last_augmentation_at, \
m.schema_version AS schema_version, \
m.community_detection_computed_at AS community_detection_computed_at, \
m.pagerank_computed_at AS pagerank_computed_at \
LIMIT 1";
const GRAPH_PAGERANK_PLAN_ROUTE: &str = "/graph/augmentation/pagerank/plan";
const GRAPH_PAGERANK_PLAN_GRAPH_META_QUERY: &str = "\
MATCH (m:GraphMeta {meta_id: 'main'}) \
RETURN m.pagerank_applied AS pagerank_applied, \
m.pagerank_computed_at AS pagerank_computed_at \
LIMIT 1";
const GRAPH_PAGERANK_PLAN_MEMORY_COUNT_QUERY: &str = "MATCH (m:Memory) RETURN count(m) AS total";
const GRAPH_PAGERANK_PLAN_ENTITY_COUNT_QUERY: &str = "MATCH (e:Entity) RETURN count(e) AS total";
const GRAPH_PAGERANK_PLAN_ENTITY_RELATION_COUNT_QUERY: &str =
    "MATCH (:Entity)-[r:RELATES_TO]->(:Entity) RETURN count(r) AS total";
const GRAPH_PAGERANK_PLAN_MENTION_EDGE_COUNT_QUERY: &str =
    "MATCH (:Memory)-[r:MENTIONS]->(:Entity) RETURN count(r) AS total";
const GRAPH_PAGERANK_PLAN_ACTIVE_MEMORY_RELATION_COUNT_QUERY: &str = "MATCH (:Memory)-[r:MEMORY_RELATES_TO]->(:Memory) WHERE r.status = 'active' RETURN count(r) AS total";
const GRAPH_PAGERANK_PLAN_CHANGED_MEMORY_COUNT_QUERY: &str = "MATCH (m:Memory) WHERE m.created_at > $cutoff OR m.updated_at > $cutoff RETURN count(m) AS total";
const GRAPH_PAGERANK_PLAN_CHANGED_ENTITY_COUNT_QUERY: &str = "MATCH (e:Entity) WHERE e.created_at > $cutoff OR e.updated_at > $cutoff RETURN count(e) AS total";
const GRAPH_PAGERANK_PLAN_CHANGED_MENTION_EDGE_COUNT_QUERY: &str = "MATCH (:Memory)-[r:MENTIONS]->(:Entity) WHERE r.created_at > $cutoff OR r.updated_at > $cutoff RETURN count(r) AS total";
const GRAPH_PAGERANK_PLAN_CHANGED_ENTITY_RELATION_COUNT_QUERY: &str = "MATCH (:Entity)-[r:RELATES_TO]->(:Entity) WHERE r.created_at > $cutoff OR r.updated_at > $cutoff RETURN count(r) AS total";
const GRAPH_PAGERANK_PLAN_CHANGED_MEMORY_RELATION_COUNT_QUERY: &str = "MATCH (:Memory)-[r:MEMORY_RELATES_TO]->(:Memory) WHERE r.status = 'active' AND (r.created_at > $cutoff OR r.updated_at > $cutoff) RETURN count(r) AS total";
const GRAPH_ORPHANS_ROUTE: &str = "/graph/orphans";
const GRAPH_ORPHAN_ENTITIES_QUERY: &str = "\
MATCH (e:Entity) \
WHERE NOT (e)<-[:MENTIONS]-(:Memory) \
AND NOT (e)-[:RELATES_TO]-() \
AND NOT (e)-[:HAS_LABEL]-() \
RETURN e.id AS entity_id, \
id(e) AS node_id, \
COALESCE(e.name, e.id) AS label, \
e.name AS name, \
e.entity_type AS entity_type, \
e.description AS description, \
e.community_id AS community_id, \
e.confidence AS confidence, \
e.pagerank_score AS pagerank_score \
ORDER BY e.id ASC \
LIMIT $limit";
pub(crate) fn graph_overview_route_query(limit: usize) -> Result<RouteQuery> {
    let limit = i64::try_from(limit).map_err(|_| {
        HawDBError::Semantic(
            "graph overview route evidence limit exceeds supported range".to_string(),
        )
    })?;
    if limit <= 0 {
        return Err(HawDBError::Semantic(
            "graph overview route evidence limit must be greater than zero".to_string(),
        ));
    }
    Ok(RouteQuery {
        route: GRAPH_OVERVIEW_ROUTE.to_string(),
        shadow_compare_ready: false,
        primary_read_routing_enabled: true,
        primary_ready: false,
        blocker_codes: Vec::new(),
        queries: vec![RouteCypherQuery {
            name: "overview-memory-ranking".to_string(),
            query_family: Some("memory_lookup".to_string()),
            require_scan_pruning: true,
            require_pruned: false,
            cypher: GRAPH_OVERVIEW_MEMORY_RANKING_QUERY.to_string(),
            parameters: BTreeMap::from([("limit".to_string(), Value::Int(limit))]),
        }],
    })
}

pub(crate) fn graph_sample_route_query(limit: usize) -> Result<RouteQuery> {
    let limit = i64::try_from(limit).map_err(|_| {
        HawDBError::Semantic(
            "graph sample route evidence limit exceeds supported range".to_string(),
        )
    })?;
    if limit <= 0 {
        return Err(HawDBError::Semantic(
            "graph sample route evidence limit must be greater than zero".to_string(),
        ));
    }
    Ok(RouteQuery {
        route: GRAPH_SAMPLE_ROUTE.to_string(),
        shadow_compare_ready: false,
        primary_read_routing_enabled: true,
        primary_ready: false,
        blocker_codes: Vec::new(),
        queries: vec![RouteCypherQuery {
            name: "sample-memory-list".to_string(),
            query_family: Some("memory_lookup".to_string()),
            require_scan_pruning: false,
            require_pruned: false,
            cypher: GRAPH_SAMPLE_MEMORY_QUERY.to_string(),
            parameters: BTreeMap::from([("limit".to_string(), Value::Int(limit))]),
        }],
    })
}

pub(crate) fn graph_node_details_route_query(node_id: u64) -> Result<RouteQuery> {
    let node_id = i64::try_from(node_id).map_err(|_| {
        HawDBError::Semantic(
            "graph node details route evidence node_id exceeds supported range".to_string(),
        )
    })?;
    Ok(RouteQuery {
        route: GRAPH_NODE_DETAILS_ROUTE.to_string(),
        shadow_compare_ready: false,
        primary_read_routing_enabled: true,
        primary_ready: false,
        blocker_codes: Vec::new(),
        queries: vec![RouteCypherQuery {
            name: "node-details-memory-lookup".to_string(),
            query_family: Some("memory_lookup".to_string()),
            require_scan_pruning: true,
            require_pruned: false,
            cypher: GRAPH_NODE_DETAILS_MEMORY_QUERY.to_string(),
            parameters: BTreeMap::from([("node_id".to_string(), Value::Int(node_id))]),
        }],
    })
}

pub(crate) fn graph_community_members_route_query(
    community_id: i64,
    limit: usize,
) -> Result<RouteQuery> {
    let limit = i64::try_from(limit).map_err(|_| {
        HawDBError::Semantic(
            "graph community members route evidence limit exceeds supported range".to_string(),
        )
    })?;
    if limit <= 0 {
        return Err(HawDBError::Semantic(
            "graph community members route evidence limit must be greater than zero".to_string(),
        ));
    }
    Ok(RouteQuery {
        route: GRAPH_COMMUNITY_MEMBERS_ROUTE.to_string(),
        shadow_compare_ready: false,
        primary_read_routing_enabled: true,
        primary_ready: false,
        blocker_codes: Vec::new(),
        queries: vec![RouteCypherQuery {
            name: "community-members-memory-lookup".to_string(),
            query_family: Some("memory_lookup".to_string()),
            require_scan_pruning: true,
            require_pruned: false,
            cypher: GRAPH_COMMUNITY_MEMBERS_MEMORY_QUERY.to_string(),
            parameters: BTreeMap::from([
                ("community_id".to_string(), Value::Int(community_id)),
                ("limit".to_string(), Value::Int(limit)),
            ]),
        }],
    })
}

pub(crate) fn graph_community_recent_memories_route_query(
    community_id: i64,
    limit: usize,
) -> Result<RouteQuery> {
    let limit = i64::try_from(limit).map_err(|_| {
        HawDBError::Semantic(
            "graph community recent memories route evidence limit exceeds supported range"
                .to_string(),
        )
    })?;
    if limit <= 0 {
        return Err(HawDBError::Semantic(
            "graph community recent memories route evidence limit must be greater than zero"
                .to_string(),
        ));
    }
    Ok(RouteQuery {
        route: GRAPH_COMMUNITY_RECENT_MEMORIES_ROUTE.to_string(),
        shadow_compare_ready: false,
        primary_read_routing_enabled: true,
        primary_ready: false,
        blocker_codes: Vec::new(),
        queries: vec![RouteCypherQuery {
            name: "community-recent-memories-lookup".to_string(),
            query_family: Some("memory_lookup".to_string()),
            require_scan_pruning: true,
            require_pruned: false,
            cypher: GRAPH_COMMUNITY_RECENT_MEMORIES_QUERY.to_string(),
            parameters: BTreeMap::from([
                ("community_id".to_string(), Value::Int(community_id)),
                ("limit".to_string(), Value::Int(limit)),
            ]),
        }],
    })
}

pub(crate) fn graph_community_subgraph_route_query<I, S>(
    community_id: i64,
    max_entities: usize,
    edge_entity_ids: I,
    max_edges: usize,
) -> Result<RouteQuery>
where
    I: IntoIterator<Item = S>,
    S: Into<String>,
{
    let max_entities = i64::try_from(max_entities).map_err(|_| {
        HawDBError::Semantic(
            "graph community subgraph route evidence max_entities exceeds supported range"
                .to_string(),
        )
    })?;
    let max_edges = i64::try_from(max_edges).map_err(|_| {
        HawDBError::Semantic(
            "graph community subgraph route evidence max_edges exceeds supported range".to_string(),
        )
    })?;
    if max_entities <= 0 {
        return Err(HawDBError::Semantic(
            "graph community subgraph route evidence max_entities must be greater than zero"
                .to_string(),
        ));
    }
    if max_edges < 0 {
        return Err(HawDBError::Semantic(
            "graph community subgraph route evidence max_edges must not be negative".to_string(),
        ));
    }
    let entity_ids = edge_entity_ids
        .into_iter()
        .map(|entity_id| Value::String(entity_id.into()))
        .collect::<Vec<_>>();
    Ok(RouteQuery {
        route: GRAPH_COMMUNITY_SUBGRAPH_ROUTE.to_string(),
        shadow_compare_ready: false,
        primary_read_routing_enabled: true,
        primary_ready: false,
        blocker_codes: Vec::new(),
        queries: vec![
            RouteCypherQuery {
                name: "community-subgraph-entity-ranking".to_string(),
                query_family: Some("graph_traversal".to_string()),
                require_scan_pruning: true,
                require_pruned: false,
                cypher: GRAPH_COMMUNITY_SUBGRAPH_ENTITY_QUERY.to_string(),
                parameters: BTreeMap::from([
                    ("community_id".to_string(), Value::Int(community_id)),
                    ("max_entities".to_string(), Value::Int(max_entities)),
                ]),
            },
            RouteCypherQuery {
                name: "community-subgraph-relation-edges".to_string(),
                query_family: Some("graph_traversal".to_string()),
                require_scan_pruning: false,
                require_pruned: false,
                cypher: GRAPH_COMMUNITY_SUBGRAPH_EDGE_QUERY.to_string(),
                parameters: BTreeMap::from([
                    ("entity_ids".to_string(), Value::List(entity_ids)),
                    ("max_edges".to_string(), Value::Int(max_edges)),
                ]),
            },
        ],
    })
}

pub(crate) fn graph_augmentation_state_route_query() -> RouteQuery {
    RouteQuery {
        route: GRAPH_AUGMENTATION_STATE_ROUTE.to_string(),
        shadow_compare_ready: false,
        primary_read_routing_enabled: true,
        primary_ready: false,
        blocker_codes: Vec::new(),
        queries: vec![RouteCypherQuery {
            name: "augmentation-state-graph-meta".to_string(),
            query_family: Some("projected_graph".to_string()),
            require_scan_pruning: true,
            require_pruned: false,
            cypher: GRAPH_AUGMENTATION_STATE_QUERY.to_string(),
            parameters: BTreeMap::new(),
        }],
    }
}

pub(crate) fn graph_pagerank_plan_route_query(
    changed_since_epoch_nanos: Option<i64>,
) -> RouteQuery {
    let mut queries = vec![
        pagerank_plan_route_query(
            "pagerank-plan-graph-meta",
            GRAPH_PAGERANK_PLAN_GRAPH_META_QUERY,
            BTreeMap::new(),
            true,
        ),
        pagerank_plan_route_query(
            "pagerank-plan-memory-count",
            GRAPH_PAGERANK_PLAN_MEMORY_COUNT_QUERY,
            BTreeMap::new(),
            false,
        ),
        pagerank_plan_route_query(
            "pagerank-plan-entity-count",
            GRAPH_PAGERANK_PLAN_ENTITY_COUNT_QUERY,
            BTreeMap::new(),
            false,
        ),
        pagerank_plan_route_query(
            "pagerank-plan-entity-relation-count",
            GRAPH_PAGERANK_PLAN_ENTITY_RELATION_COUNT_QUERY,
            BTreeMap::new(),
            false,
        ),
        pagerank_plan_route_query(
            "pagerank-plan-mention-edge-count",
            GRAPH_PAGERANK_PLAN_MENTION_EDGE_COUNT_QUERY,
            BTreeMap::new(),
            false,
        ),
        pagerank_plan_route_query(
            "pagerank-plan-active-memory-relation-count",
            GRAPH_PAGERANK_PLAN_ACTIVE_MEMORY_RELATION_COUNT_QUERY,
            BTreeMap::new(),
            false,
        ),
    ];
    if let Some(cutoff) = changed_since_epoch_nanos {
        let parameters = BTreeMap::from([("cutoff".to_string(), Value::Int(cutoff))]);
        queries.extend([
            pagerank_plan_route_query(
                "pagerank-plan-changed-memory-count",
                GRAPH_PAGERANK_PLAN_CHANGED_MEMORY_COUNT_QUERY,
                parameters.clone(),
                true,
            ),
            pagerank_plan_route_query(
                "pagerank-plan-changed-entity-count",
                GRAPH_PAGERANK_PLAN_CHANGED_ENTITY_COUNT_QUERY,
                parameters.clone(),
                true,
            ),
            pagerank_plan_route_query(
                "pagerank-plan-changed-mention-edge-count",
                GRAPH_PAGERANK_PLAN_CHANGED_MENTION_EDGE_COUNT_QUERY,
                parameters.clone(),
                false,
            ),
            pagerank_plan_route_query(
                "pagerank-plan-changed-entity-relation-count",
                GRAPH_PAGERANK_PLAN_CHANGED_ENTITY_RELATION_COUNT_QUERY,
                parameters.clone(),
                false,
            ),
            pagerank_plan_route_query(
                "pagerank-plan-changed-memory-relation-count",
                GRAPH_PAGERANK_PLAN_CHANGED_MEMORY_RELATION_COUNT_QUERY,
                parameters,
                false,
            ),
        ]);
    }
    RouteQuery {
        route: GRAPH_PAGERANK_PLAN_ROUTE.to_string(),
        shadow_compare_ready: false,
        primary_read_routing_enabled: true,
        primary_ready: false,
        blocker_codes: Vec::new(),
        queries,
    }
}

fn pagerank_plan_route_query(
    name: impl Into<String>,
    cypher: impl Into<String>,
    parameters: BTreeMap<String, Value>,
    require_scan_pruning: bool,
) -> RouteCypherQuery {
    RouteCypherQuery {
        name: name.into(),
        query_family: Some("projected_graph".to_string()),
        require_scan_pruning,
        require_pruned: false,
        cypher: cypher.into(),
        parameters,
    }
}

pub(crate) fn graph_orphans_route_query(limit: usize) -> Result<RouteQuery> {
    let limit = i64::try_from(limit).map_err(|_| {
        HawDBError::Semantic(
            "graph orphans route evidence limit exceeds supported range".to_string(),
        )
    })?;
    if limit <= 0 {
        return Err(HawDBError::Semantic(
            "graph orphans route evidence limit must be greater than zero".to_string(),
        ));
    }
    Ok(RouteQuery {
        route: GRAPH_ORPHANS_ROUTE.to_string(),
        shadow_compare_ready: false,
        primary_read_routing_enabled: true,
        primary_ready: false,
        blocker_codes: Vec::new(),
        queries: vec![RouteCypherQuery {
            name: "orphan-entity-relationship-exclusion".to_string(),
            query_family: Some("graph_traversal".to_string()),
            require_scan_pruning: false,
            require_pruned: false,
            cypher: GRAPH_ORPHAN_ENTITIES_QUERY.to_string(),
            parameters: BTreeMap::from([("limit".to_string(), Value::Int(limit))]),
        }],
    })
}