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
//! Copy-on-write for the schema-scale maps, and the accessors that write them.
//!
//! ## Why these six maps are `Arc`
//!
//! Statement rollback takes a `schema_shell` — a `DirGraph::clone()` with the
//! ten O(V+E) fields parked — before **every** mutating statement, and that
//! clone used to deep-copy the whole property catalogue. Measured on a
//! 200-type × 50-column schema (release, 2026-08-14): 337 µs per shell, of
//! which `node_type_metadata` was 326 µs, `type_schemas` 10.0 µs and
//! `title_field_aliases` 4.6 µs — paid by every statement, including one that
//! matched nothing and wrote nothing. Node count does not enter it; schema
//! width does.
//!
//! Behind an `Arc` the shell's copy is six refcount bumps. The maps then
//! follow the same discipline the columnar cell journal follows one level
//! down:
//!
//! - The shell holds the pristine handle, so the statement's **first** write to
//! a given map forks it once — O(that map), and only on a statement that
//! actually changes the schema, which after warmup is rare.
//! - Every later write in the same statement sees a uniquely-owned map and
//! mutates it in place.
//! - **Rollback** swaps the pristine handle back (`restore_schema_shell`);
//! **commit** drops the shell and uniqueness returns.
//!
//! The correctness argument is the same one that lets the shell restore work
//! at all: these maps are restored *verbatim*, so a pointer to the
//! pre-statement value is exactly as good as a copy of it.
//!
//! ## The rule for writers
//!
//! Every mutation goes through the `*_mut` accessor below — never through
//! `Arc::make_mut` at the call site — because that is where the fork counter
//! lives, and the counter is what keeps the cost model testable.
//!
//! And a writer that can determine it changes nothing must **not** call the
//! accessor: taking `&mut` forks the map whether or not anything is written,
//! so a metadata upsert that re-declares keys the catalogue already holds pays
//! the whole copy for a no-op. `upsert_node_type_metadata`,
//! `upsert_connection_type_metadata` and `ensure_type_schema_keys` each check
//! first for exactly that reason — the Cypher `SET` path calls all three once
//! per written row.
use HashMap;
use Arc;
use FxHashMap;
use DirGraph;
use cratecow_mut;
use crate;