Repository navigation
Expand file tree
/
Copy path21_graph_analytical_view_sql.py
More file actions
937 lines (823 loc) · 30.3 KB
/
Copy path21_graph_analytical_view_sql.py
File metadata and controls
937 lines (823 loc) · 30.3 KB
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
#!/usr/bin/env python3
"""Example 21: Graph Analytical View SQL Workflow.
This example mirrors the Java-side Graph Analytical View (GAV) lifecycle, but it
does so at a more realistic scale and still uses only SQL from Python.
Workflow covered:
- generate a synthetic transport graph with tens of thousands of City vertices and
a denser ROAD topology spread across multiple regions
- create a GAV with richer vertex and edge property filters
- poll schema:graphAnalyticalViews until the initial async build reaches READY
- inspect live GAV metadata such as status, nodeCount, edgeCount, memory usage,
and build duration
- run normal SQL MATCH traversals and aggregate queries without introducing a
Python-native GAV wrapper
- add thousands more vertices and edges while update mode is OFF and observe the
view become STALE
- rebuild the view explicitly through SQL and verify refreshed counts and query
results against the expanded graph
- alter update mode and compaction threshold through SQL
- add another growth wave under SYNCHRONOUS mode and verify live counts
- reopen the database and confirm persisted GAV restoration (the restore is lazy: one
query through the view first, then poll the metadata row)
Requirements:
- A packaged ArcadeDB runtime with Graph Analytical View SQL support
Notes:
- The example intentionally uses only db.command(..., "sql", ...) and
db.query(..., "sql", ...).
- CREATE GRAPH ANALYTICAL VIEW performs the first build asynchronously, so polling
schema metadata is part of the normal lifecycle.
- The default scale is intentionally substantial. Use the CLI flags to run a
smaller version if you only want a quick smoke test.
"""
from __future__ import annotations
import argparse
import re
import shutil
import time
from pathlib import Path
import arcadedb
GAV_NAME = "roadNet"
CITY_CODE_WIDTH = 6
LANE_CLASSES = {
"ring": "arterial",
"local": "collector",
"express": "express",
"hub": "regional",
"bridge": "interchange",
}
SAFE_GRAPH_LITERAL_RE = re.compile(r"^[A-Za-z0-9_-]+$")
def _sql_string_literal(value: str) -> str:
if not SAFE_GRAPH_LITERAL_RE.fullmatch(value):
raise ValueError(f"Unsafe SQL literal: {value!r}")
return f"'{value}'"
def _validated_limit(value: int) -> int:
limit = int(value)
if limit <= 0:
raise ValueError(f"SQL limit must be positive: {value!r}")
return limit
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Example 21: graph analytical view SQL workflow at larger scale"
)
parser.add_argument(
"--db-path",
default="./my_test_databases/graph_analytical_view_sql_db",
help=(
"Database path (default: "
"./my_test_databases/graph_analytical_view_sql_db)"
),
)
parser.add_argument(
"--base-cities",
type=int,
default=100000,
help="Number of base City vertices to generate (default: 100000)",
)
parser.add_argument(
"--regions",
type=int,
default=40,
help="Number of synthetic regions and hub cities (default: 40)",
)
parser.add_argument(
"--stale-growth-cities",
type=int,
default=25000,
help="Number of cities to add while UPDATE MODE is OFF (default: 25000)",
)
parser.add_argument(
"--sync-growth-cities",
type=int,
default=10000,
help=(
"Number of cities to add after switching to SYNCHRONOUS mode "
"(default: 10000)"
),
)
parser.add_argument(
"--batch-size",
type=int,
default=1000,
help="Transaction batch size for inserts (default: 1000)",
)
parser.add_argument(
"--local-jump",
type=int,
default=7,
help="Deterministic local fan-out jump for ROAD edges (default: 7)",
)
parser.add_argument(
"--express-jump",
type=int,
default=101,
help="Deterministic express fan-out jump for ROAD edges (default: 101)",
)
parser.add_argument(
"--sample-limit",
type=int,
default=8,
help="How many sample query rows to print (default: 8)",
)
args = parser.parse_args()
if args.base_cities < 10:
raise ValueError("--base-cities must be at least 10")
if args.regions < 2:
raise ValueError("--regions must be at least 2")
if args.regions > args.base_cities:
raise ValueError("--regions cannot exceed --base-cities")
if args.stale_growth_cities < 0 or args.sync_growth_cities < 0:
raise ValueError("growth counts must be non-negative")
if args.batch_size < 1:
raise ValueError("--batch-size must be at least 1")
if args.local_jump < 1 or args.express_jump < 1:
raise ValueError("edge jump values must be at least 1")
if args.sample_limit < 1:
raise ValueError("--sample-limit must be at least 1")
return args
def reset_db(db_path: Path) -> None:
if db_path.exists():
shutil.rmtree(db_path)
db_path.parent.mkdir(parents=True, exist_ok=True)
def require(condition: bool, message: str) -> None:
if not condition:
raise RuntimeError(message)
def city_code(index: int) -> str:
return f"CITY-{index:0{CITY_CODE_WIDTH}d}"
def city_name(index: int) -> str:
return f"Synthetic City {index:0{CITY_CODE_WIDTH}d}"
def region_index(index: int, regions: int) -> int:
return index % regions
def region_name(region: int) -> str:
return f"region_{region:02d}"
def zone_name(index: int, regions: int) -> str:
return f"zone_{(index // regions) % 8:02d}"
def hub_index_for_region(region: int) -> int:
return region
def city_row(index: int, regions: int) -> tuple:
region = region_index(index, regions)
tier = 1 if index < regions else 2 + ((index // regions) % 3)
demand_index = 50 + ((index * 17 + region * 11) % 450)
return (
city_code(index),
city_name(index),
region_name(region),
tier,
zone_name(index, regions),
demand_index,
)
def road_targets(
index: int,
total_cities: int,
regions: int,
local_jump: int,
express_jump: int,
) -> list[tuple[int, str]]:
region = region_index(index, regions)
hub = hub_index_for_region(region)
next_hub = hub_index_for_region((region + 1) % regions)
candidates = [
((index + 1) % total_cities, "ring"),
((index + local_jump + region) % total_cities, "local"),
((index + express_jump + region * 3) % total_cities, "express"),
]
if index != hub:
candidates.append((hub, "hub"))
elif next_hub != index:
candidates.append((next_hub, "bridge"))
deduped: list[tuple[int, str]] = []
seen_targets: set[int] = set()
for target, edge_kind in candidates:
if target == index or target in seen_targets:
continue
seen_targets.add(target)
deduped.append((target, edge_kind))
return deduped
def road_properties(
source_index: int,
target_index: int,
edge_kind: str,
regions: int,
) -> tuple:
kind_factor = {
"ring": 3,
"local": 7,
"express": 11,
"hub": 17,
"bridge": 23,
}[edge_kind]
distance = 5 + ((source_index * 17 + target_index * 13 + kind_factor) % 95)
duration = distance + 3 + ((source_index + target_index + kind_factor) % 21)
toll = (source_index + target_index + kind_factor) % 6
capacity = 80 + ((source_index * 23 + target_index * 11 + kind_factor) % 220)
corridor = f"{region_name(region_index(source_index, regions))}:{edge_kind}"
return (
distance,
duration,
toll,
capacity,
LANE_CLASSES[edge_kind],
corridor,
)
def chunked(iterable, chunk_size: int):
chunk = []
for item in iterable:
chunk.append(item)
if len(chunk) >= chunk_size:
yield chunk
chunk = []
if chunk:
yield chunk
def iter_city_rows(start_index: int, count: int, regions: int):
for index in range(start_index, start_index + count):
yield city_row(index, regions)
def iter_road_rows(
start_index: int,
end_index: int,
total_cities: int,
regions: int,
local_jump: int,
express_jump: int,
):
for source_index in range(start_index, end_index):
source_code = city_code(source_index)
for target_index, edge_kind in road_targets(
source_index,
total_cities,
regions,
local_jump,
express_jump,
):
yield (
source_code,
city_code(target_index),
*road_properties(source_index, target_index, edge_kind, regions),
)
def format_int(value: int) -> str:
return f"{value:,}"
def rows_to_dicts(result_set, fields: list[str]) -> list[dict]:
rows = []
for row in result_set:
rows.append({field: row.get(field) for field in fields})
return rows
def print_rows(title: str, rows: list[dict]) -> None:
print(title)
if not rows:
print(" (no rows)")
print()
return
for row in rows:
print(f" - {row}")
print(f" Rows returned: {len(rows)}")
print()
def print_metric(title: str, value: str) -> None:
print(f" - {title}: {value}")
def print_note(message: str) -> None:
print(f" note: {message}")
def print_gav_metadata(title: str, metadata: dict) -> None:
print(title)
print(f" name: {metadata['name']}")
print(f" status: {metadata['status']}")
print(f" updateMode: {metadata['updateMode']}")
print(f" vertexTypes: {metadata['vertexTypes']}")
print(f" edgeTypes: {metadata['edgeTypes']}")
print(f" propertyFilter: {metadata['propertyFilter']}")
print(f" edgePropertyFilter: {metadata['edgePropertyFilter']}")
print(f" nodeCount: {metadata['nodeCount']}")
print(f" edgeCount: {metadata['edgeCount']}")
print(f" compactionThreshold: {metadata['compactionThreshold']}")
print(f" memoryUsageBytes: {metadata['memoryUsageBytes']}")
print(f" buildDurationMs: {metadata['buildDurationMs']}")
print()
def create_schema(db) -> None:
db.command("sql", "CREATE VERTEX TYPE City")
db.command("sql", "CREATE PROPERTY City.code STRING")
db.command("sql", "CREATE PROPERTY City.name STRING")
db.command("sql", "CREATE PROPERTY City.region STRING")
db.command("sql", "CREATE PROPERTY City.tier INTEGER")
db.command("sql", "CREATE PROPERTY City.zone STRING")
db.command("sql", "CREATE PROPERTY City.demand_index INTEGER")
# code is only looked up by equality, so a hash index (ArcadeData/arcadedb#9169)
db.command("sql", "CREATE INDEX ON City (code) UNIQUE_HASH")
db.command("sql", "CREATE EDGE TYPE ROAD")
db.command("sql", "CREATE PROPERTY ROAD.distance INTEGER")
db.command("sql", "CREATE PROPERTY ROAD.duration INTEGER")
db.command("sql", "CREATE PROPERTY ROAD.toll INTEGER")
db.command("sql", "CREATE PROPERTY ROAD.capacity INTEGER")
db.command("sql", "CREATE PROPERTY ROAD.lane_class STRING")
db.command("sql", "CREATE PROPERTY ROAD.corridor STRING")
def insert_cities(
db,
start_index: int,
count: int,
regions: int,
batch_size: int,
) -> int:
inserted = 0
for batch in chunked(iter_city_rows(start_index, count, regions), batch_size):
with db.transaction():
for code, name, region, tier, zone, demand_index in batch:
db.command(
"sql",
"INSERT INTO City SET code = ?, name = ?, region = ?, tier = ?, "
"zone = ?, demand_index = ?",
code,
name,
region,
tier,
zone,
demand_index,
)
inserted += 1
return inserted
def insert_roads(
db,
start_index: int,
end_index: int,
total_cities: int,
regions: int,
batch_size: int,
local_jump: int,
express_jump: int,
) -> int:
inserted = 0
for batch in chunked(
iter_road_rows(
start_index,
end_index,
total_cities,
regions,
local_jump,
express_jump,
),
batch_size,
):
with db.transaction():
for road in batch:
(
source_code,
target_code,
distance,
duration,
toll,
capacity,
lane_class,
corridor,
) = road
db.command(
"sql",
"CREATE EDGE ROAD FROM (SELECT FROM City WHERE code = ?) "
"TO (SELECT FROM City WHERE code = ?) "
"SET distance = ?, duration = ?, toll = ?, capacity = ?, "
"lane_class = ?, corridor = ?",
source_code,
target_code,
distance,
duration,
toll,
capacity,
lane_class,
corridor,
)
inserted += 1
return inserted
def create_gav(db) -> bool:
try:
db.command(
"sql",
"CREATE GRAPH ANALYTICAL VIEW roadNet "
"VERTEX TYPES (City) "
"EDGE TYPES (ROAD) "
"PROPERTIES (code, name, region, tier, zone, demand_index) "
"EDGE PROPERTIES "
"(distance, duration, toll, capacity, lane_class, corridor) "
"UPDATE MODE OFF",
)
except arcadedb.ArcadeDBError as exc:
message = str(exc)
if any(
token in message
for token in (
"Graph Analytical View",
"GRAPH ANALYTICAL VIEW",
"graph analytical view",
"extraneous input",
"no viable alternative",
)
):
print(
"Graph Analytical View SQL support is not available in this "
"packaged runtime."
)
print(
"This example depends on CREATE/ALTER/REBUILD GRAPH "
"ANALYTICAL VIEW support."
)
print(f"Original error: {exc}")
return False
raise
return True
def fetch_gav_metadata(db, name: str) -> dict | None:
result = db.query(
"sql",
"SELECT FROM schema:graphAnalyticalViews WHERE name = ?",
name,
)
row = result.first()
if row is None:
return None
return {
"name": row.get("name"),
"status": row.get("status"),
"updateMode": row.get("updateMode"),
"vertexTypes": row.get("vertexTypes"),
"edgeTypes": row.get("edgeTypes"),
"propertyFilter": row.get("propertyFilter"),
"edgePropertyFilter": row.get("edgePropertyFilter"),
"nodeCount": row.get("nodeCount"),
"edgeCount": row.get("edgeCount"),
"compactionThreshold": row.get("compactionThreshold"),
"memoryUsageBytes": row.get("memoryUsageBytes"),
"buildDurationMs": row.get("buildDurationMs"),
}
def wait_for_gav_status(
db,
name: str,
expected_statuses: set[str],
timeout_sec: float = 180.0,
) -> dict:
start = time.perf_counter()
last_metadata = None
while True:
metadata = fetch_gav_metadata(db, name)
if metadata is not None:
last_metadata = metadata
if metadata["status"] in expected_statuses:
return metadata
if time.perf_counter() - start > timeout_sec:
raise RuntimeError(
f"Timed out waiting for GAV {name} to reach "
f"{sorted(expected_statuses)}. "
f"Last metadata: {last_metadata}"
)
time.sleep(0.25)
def wait_for_gav_counts(
db,
name: str,
expected_nodes: int,
expected_edges: int,
timeout_sec: float = 180.0,
) -> dict:
"""Poll schema:graphAnalyticalViews until the view is READY with real counts.
A view restored when the database opens is restored lazily: the row answers
READY with nodeCount 0 and edgeCount 0 until the first query that uses the
view. Run one such query before calling this, or the 0 stays.
"""
start = time.perf_counter()
last_metadata = None
while True:
metadata = fetch_gav_metadata(db, name)
if metadata is not None:
last_metadata = metadata
if (
metadata["status"] == "READY"
and metadata["nodeCount"] == expected_nodes
and metadata["edgeCount"] == expected_edges
):
return metadata
if time.perf_counter() - start > timeout_sec:
raise RuntimeError(
f"Timed out waiting for GAV {name} to report "
f"{expected_nodes} nodes and {expected_edges} edges. "
f"Last metadata: {last_metadata}"
)
time.sleep(0.25)
def query_direct_neighbor_sample(
db,
origin_code: str,
sample_limit: int,
) -> list[dict]:
result = db.query(
"sql",
f"""
MATCH {{type: City, as: src, where: (code = '{origin_code}')}}
-ROAD->
{{type: City, as: dst}}
RETURN dst.code AS code, dst.region AS region,
dst.zone AS zone, dst.tier AS tier
ORDER BY dst.code
LIMIT {sample_limit}
""",
)
return rows_to_dicts(result, ["code", "region", "zone", "tier"])
def query_two_hop_summary(db, origin_code: str) -> dict:
safe_origin_code = _sql_string_literal(origin_code)
result = db.query(
"sql",
f"""
SELECT count(*) AS destination_count FROM (
MATCH {{type: City, as: src, where: (code = {safe_origin_code})}}
-ROAD->
{{type: City, as: mid}}
-ROAD->
{{type: City, as: dst}}
RETURN DISTINCT dst.code AS code
)
""", # nosec B608 - value validated via _sql_string_literal
)
row = result.first()
require(row is not None, "Expected a two-hop summary row")
return {"destination_count": row.get("destination_count")}
def query_hub_inbound_count(db, hub_code: str) -> int:
safe_hub_code = _sql_string_literal(hub_code)
result = db.query(
"sql",
f"""
SELECT count(*) AS inbound_count FROM (
MATCH {{type: City, as: src}}
-ROAD->
{{type: City, as: hub, where: (code = {safe_hub_code})}}
RETURN src.code AS code
)
""", # nosec B608 - value validated via _sql_string_literal
)
row = result.first()
require(row is not None, "Expected an inbound count row")
return row.get("inbound_count")
def query_region_sample(db, sample_limit: int) -> list[dict]:
safe_sample_limit = _validated_limit(sample_limit)
result = db.query(
"sql",
f"""
SELECT region, count(*) AS city_count, avg(demand_index) AS avg_demand
FROM City
GROUP BY region
ORDER BY region
LIMIT {safe_sample_limit}
""", # nosec B608 - value validated via _validated_limit
)
return rows_to_dicts(result, ["region", "city_count", "avg_demand"])
def print_scale_summary(args: argparse.Namespace) -> None:
print("Scale summary:")
print_metric("base cities", format_int(args.base_cities))
print_metric("regions", format_int(args.regions))
print_metric("stale growth cities", format_int(args.stale_growth_cities))
print_metric("sync growth cities", format_int(args.sync_growth_cities))
print_metric("batch size", format_int(args.batch_size))
print_metric("local jump", format_int(args.local_jump))
print_metric("express jump", format_int(args.express_jump))
print()
def main() -> int:
args = parse_args()
db_path = Path(args.db_path)
reset_db(db_path)
total_cities_after_stale = args.base_cities + args.stale_growth_cities
total_cities_after_sync = total_cities_after_stale + args.sync_growth_cities
print("=" * 72)
print("ArcadeDB Python - Example 21: Graph Analytical View SQL Workflow")
print("=" * 72)
print()
print(
"Positioning: use SQL for GAV lifecycle management instead of a new "
"Python API"
)
print(f"Database path: {db_path}")
print_scale_summary(args)
anchor_code = city_code(0)
hub_code = city_code(0)
with arcadedb.create_database(str(db_path)) as db:
print("Phase 1: Create schema and generate the base transport graph")
create_schema(db)
start = time.perf_counter()
base_city_count = insert_cities(
db, 0, args.base_cities, args.regions, args.batch_size
)
base_edge_count = insert_roads(
db,
0,
args.base_cities,
args.base_cities,
args.regions,
args.batch_size,
args.local_jump,
args.express_jump,
)
phase_duration = time.perf_counter() - start
print_metric("base city inserts", format_int(base_city_count))
print_metric("base road inserts", format_int(base_edge_count))
print_metric("phase duration", f"{phase_duration:.2f}s")
print()
print_rows(
"Region sample after base load:",
query_region_sample(db, min(args.sample_limit, args.regions)),
)
if not create_gav(db):
return 0
ready_metadata = wait_for_gav_status(db, GAV_NAME, {"READY"})
require(
ready_metadata["nodeCount"] == base_city_count,
f"Expected initial GAV nodeCount = {base_city_count}",
)
require(
ready_metadata["edgeCount"] == base_edge_count,
f"Expected initial GAV edgeCount = {base_edge_count}",
)
print_gav_metadata("Initial GAV metadata after async build:", ready_metadata)
direct_neighbors = query_direct_neighbor_sample(
db, anchor_code, args.sample_limit
)
require(bool(direct_neighbors), "Expected direct neighbors for the anchor city")
print_rows("Sample direct neighbors from the anchor city:", direct_neighbors)
two_hop_before = query_two_hop_summary(db, anchor_code)
print_rows("Two-hop summary from the anchor city:", [two_hop_before])
inbound_before = query_hub_inbound_count(db, hub_code)
print_rows(
"Inbound ROAD count into the first region hub:",
[{"hub": hub_code, "inbound_count": inbound_before}],
)
print("Phase 2: Alter compaction threshold while staying SQL-first")
db.command(
"sql",
"ALTER GRAPH ANALYTICAL VIEW roadNet COMPACTION THRESHOLD 2000",
)
altered_threshold = fetch_gav_metadata(db, GAV_NAME)
require(altered_threshold is not None, "Expected GAV metadata to exist")
require(
altered_threshold["compactionThreshold"] == 2000,
"Expected compaction threshold to update to 2000",
)
print_gav_metadata(
"Metadata after ALTER ... COMPACTION THRESHOLD:",
altered_threshold,
)
print(
"Phase 3: Add a large growth wave with UPDATE MODE OFF and "
"observe STALE status"
)
start = time.perf_counter()
stale_city_count = insert_cities(
db,
args.base_cities,
args.stale_growth_cities,
args.regions,
args.batch_size,
)
stale_edge_count = insert_roads(
db,
args.base_cities,
total_cities_after_stale,
total_cities_after_stale,
args.regions,
args.batch_size,
args.local_jump,
args.express_jump,
)
phase_duration = time.perf_counter() - start
print_metric("stale growth city inserts", format_int(stale_city_count))
print_metric("stale growth road inserts", format_int(stale_edge_count))
print_metric("phase duration", f"{phase_duration:.2f}s")
print()
stale_metadata = wait_for_gav_status(db, GAV_NAME, {"STALE"})
require(
stale_metadata["nodeCount"] == base_city_count,
"Stale GAV should still show old nodeCount",
)
require(
stale_metadata["edgeCount"] == base_edge_count,
"Stale GAV should still show old edgeCount",
)
print_gav_metadata("Metadata after writes in OFF mode:", stale_metadata)
print("Phase 4: Rebuild explicitly through SQL and verify refreshed counts")
rebuild_result = rows_to_dicts(
db.command("sql", "REBUILD GRAPH ANALYTICAL VIEW roadNet"),
["operation", "name", "status"],
)
print_rows("REBUILD command result:", rebuild_result)
rebuilt_metadata = wait_for_gav_status(db, GAV_NAME, {"READY"})
expected_rebuilt_nodes = base_city_count + stale_city_count
expected_rebuilt_edges = base_edge_count + stale_edge_count
require(
rebuilt_metadata["nodeCount"] == expected_rebuilt_nodes,
f"Expected rebuilt GAV nodeCount = {expected_rebuilt_nodes}",
)
require(
rebuilt_metadata["edgeCount"] == expected_rebuilt_edges,
f"Expected rebuilt GAV edgeCount = {expected_rebuilt_edges}",
)
print_gav_metadata("Metadata after explicit rebuild:", rebuilt_metadata)
two_hop_after_rebuild = query_two_hop_summary(db, anchor_code)
require(
two_hop_after_rebuild["destination_count"]
>= two_hop_before["destination_count"],
"Expected two-hop reachability to stay flat or increase after rebuild",
)
print_rows(
"Two-hop summary after rebuild:",
[two_hop_after_rebuild],
)
inbound_after_rebuild = query_hub_inbound_count(db, hub_code)
require(
inbound_after_rebuild > inbound_before,
"Expected hub inbound count to increase after stale growth rebuild",
)
print_rows(
"Inbound ROAD count after rebuild:",
[{"hub": hub_code, "inbound_count": inbound_after_rebuild}],
)
print("Phase 5: Switch to SYNCHRONOUS update mode and verify live counts")
db.command(
"sql",
"ALTER GRAPH ANALYTICAL VIEW roadNet UPDATE MODE SYNCHRONOUS",
)
sync_mode_metadata = fetch_gav_metadata(db, GAV_NAME)
require(
sync_mode_metadata is not None,
"Expected metadata after sync-mode ALTER",
)
require(
sync_mode_metadata["updateMode"] == "SYNCHRONOUS",
"Expected update mode to be SYNCHRONOUS",
)
print_gav_metadata(
"Metadata after ALTER ... UPDATE MODE SYNCHRONOUS:",
sync_mode_metadata,
)
start = time.perf_counter()
sync_city_count = insert_cities(
db,
total_cities_after_stale,
args.sync_growth_cities,
args.regions,
args.batch_size,
)
sync_edge_count = insert_roads(
db,
total_cities_after_stale,
total_cities_after_sync,
total_cities_after_sync,
args.regions,
args.batch_size,
args.local_jump,
args.express_jump,
)
phase_duration = time.perf_counter() - start
print_metric("sync growth city inserts", format_int(sync_city_count))
print_metric("sync growth road inserts", format_int(sync_edge_count))
print_metric("phase duration", f"{phase_duration:.2f}s")
print()
sync_metadata = wait_for_gav_status(db, GAV_NAME, {"READY"})
expected_sync_nodes = expected_rebuilt_nodes + sync_city_count
expected_sync_edges = expected_rebuilt_edges + sync_edge_count
print_gav_metadata("Metadata after writes in SYNCHRONOUS mode:", sync_metadata)
if (
sync_metadata["nodeCount"] != expected_sync_nodes
or sync_metadata["edgeCount"] != expected_sync_edges
):
print_note(
"Some runtimes keep schema metadata counts at the last rebuilt "
"snapshot immediately after switching from OFF to SYNCHRONOUS. "
"The live verification below relies on query results, and the "
"reopen step still checks the persisted counts strictly."
)
print()
inbound_after_sync = query_hub_inbound_count(db, hub_code)
require(
inbound_after_sync > inbound_after_rebuild,
"Expected hub inbound count to increase again under SYNCHRONOUS mode",
)
print_rows(
"Inbound ROAD count after synchronous growth:",
[{"hub": hub_code, "inbound_count": inbound_after_sync}],
)
print("Phase 6: Reopen the database and confirm persisted GAV restoration")
with arcadedb.open_database(str(db_path)) as reopened_db:
# The restore on open is lazy: until a query uses the view, the schema row
# can say READY with nodeCount 0 and edgeCount 0. Run one query first, then
# poll the row for the real counts.
direct_neighbors = query_direct_neighbor_sample(
reopened_db, anchor_code, args.sample_limit
)
restored_metadata = wait_for_gav_counts(
reopened_db, GAV_NAME, expected_sync_nodes, expected_sync_edges
)
require(
restored_metadata["nodeCount"] == expected_sync_nodes,
f"Expected restored GAV nodeCount = {expected_sync_nodes}",
)
require(
restored_metadata["edgeCount"] == expected_sync_edges,
f"Expected restored GAV edgeCount = {expected_sync_edges}",
)
print_gav_metadata("Metadata after reopen:", restored_metadata)
print_rows(
"Sample direct neighbors after reopen:",
direct_neighbors,
)
print_rows(
"Region sample after reopen:",
query_region_sample(reopened_db, min(args.sample_limit, args.regions)),
)
print(f"Database remains on disk for inspection: {db_path}")
print("Example 21 completed.")
return 0
if __name__ == "__main__":
raise SystemExit(main())