The Store¶
The store keeps each dataset's graph on disk, as a series of numbered generations. Each time you add a file, its graph becomes the dataset's next generation. Old generations stay until you remove them. So you can see what changed between two versions of a file, read the graph as it was at a given time, and load Neo4j with only the changes.
The store needs no database and no optional extra. It lives in a folder,
ddigraph-store by default.
A first look¶
Add a file, then list the store:
$ ddigraph store add questionnaire.xml
Generation 1 of dataset 'questionnaire': 7 nodes, 6 relationships
$ ddigraph store list
questionnaire: HEAD 1, 1 generations
The dataset ID is the file name without its extension, as for load. Pass
--dataset-id to choose another one.
Now edit the file. Here a category gets a new label, and a second category joins the code list. Add the file again:
$ ddigraph store add questionnaire.xml
Generation 2 of dataset 'questionnaire': 8 nodes, 7 relationships
$ ddigraph store diff questionnaire 1 2
Nodes: 1 added, 0 removed, 1 changed
Relationships: 1 added, 0 removed
Generations and HEAD¶
A generation is the whole graph of one file at one time. It gets the next
number, starting from 1. Once written, it never changes. Each dataset has a
HEAD, which names its current generation: the last one written.
The time a generation was written is its created time, in UTC. This is
transaction time: when the store learned about the graph. It is not the
time the metadata describes. DDI keeps that time itself, in its versions
and dates.
The layout¶
The store is a plain folder:
ddigraph-store/
questionnaire/ one folder per dataset
000001/ one folder per generation
MANIFEST.json
nodes.seg
relationships.seg
000002/
MANIFEST.json
nodes.seg
relationships.seg
HEAD the number of the current generation
LOCK the file a writer locks
A dataset's folder name is its ID, with every character but a to z,
0 to 9, - and _ percent-encoded. Upper case is encoded too, so two
IDs never share a folder, even on a file system that ignores case. An ID
that encodes to more than 100 characters gets a shorter name: its first 67
characters, then ~ and 32 hex digits of a hash of the ID. Each manifest
holds the full ID.
On Windows without long paths turned on, a whole path must fit in 260
characters. Keep the store's folder short there, such as C:\ddi-store.
Settings¶
| Setting | Variable | Flag | Default | What it sets |
|---|---|---|---|---|
store_dir |
DDIGRAPH_STORE_DIR |
--store DIR |
ddigraph-store |
The store's folder |
memory_limit_mb |
DDIGRAPH_MEMORY_LIMIT_MB |
--memory-limit MB |
256 |
Memory a load may use for sorting, at least 16 |
use_store |
DDIGRAPH_USE_STORE |
load --store |
false |
Load Neo4j through the store; see below |
Every ddigraph store command takes --store DIR. A relative folder is
read from where you run the command.
store add reads the file and writes the generation at the same time. So
it gives half of the memory limit to the parse and half to the store. The
store sorts the nodes and the relationships. When a sort goes past its
share, it spills to files inside the new generation's folder, on the
store's own disk. The spill files go when the generation is written.
The commands¶
| Command | What it does | Writes? |
|---|---|---|
store add FILE |
Write a DDI file as its dataset's next generation | yes |
store list |
List the datasets, with their HEAD | no |
store show DATASET |
Print a generation's manifest, or with --json its graph |
no |
store diff DATASET OLD NEW |
Print how one generation differs from another | no |
store changes DATASET --since N |
Print the delta from generation N to HEAD, or one JSON row per line | no |
store stubs DATASET |
List the nodes a dataset refers to but does not hold | no |
store gc DATASET --keep N |
Remove all but the newest N generations | yes |
store add¶
ddigraph store add FILE [--dataset-id ID] [--dataset-name NAME]
[--flavor auto|codebook|lifecycle|cdi]
[--strict-parsing] [--resolve exact|latest-declared|fail]
[--memory-limit MB] [--store DIR]
add reads Codebook, DDI-L FragmentInstance and DDI-CDI files, with the
same options as load. A file the parse refuses, such as one with a
reference that --resolve fail cannot resolve, leaves the store as it was.
store list¶
$ ddigraph store list
questionnaire: HEAD 2, 2 generations
Each line gives a dataset's ID, its HEAD and how many generations it has.
It skips a folder with no numbered generation folder, such as .git in a
store kept in git. A dataset whose HEAD or manifest is damaged stops it
with an error, as below.
store show¶
show prints the manifest of the dataset's HEAD:
$ ddigraph store show questionnaire
{
"number": 2,
"created": "2026-11-07T13:01:12.470176Z",
"key_scheme": "b3-128/1",
"dataset_id": "questionnaire",
"dataset_key": "62cce68da898c7f09e5ca0271326e15e",
"nodes": {
"rows": 8,
"sha256": "b5d0f366fde5117268931e474c3a05d386ade1da328077f697573e8e52620500",
"blocks": [
{
"first": "4426a3305dac3d8be13fb0f42dbcd4f5",
"offset": 0
}
]
},
"relationships": {
"rows": 7,
"sha256": "fad94ffdd4228dd75ba9cc1f4a2f5278ac2bf0028e46b67a8afba96516ee3237",
"blocks": [
{
"first": [
"4426a3305dac3d8be13fb0f42dbcd4f5",
"REFERENCES_QUESTION",
"e17630d934098041870a02c603a884e6",
null
],
"offset": 0
}
]
},
"model_hash": "02228f6d9f0e803657a76883368dd813fb7da5320436fa8b59d0638a13c9bd2f",
"root": "b01ddc85d9dfef21243b61355afc9d38b524fdad9548e77f024a6881f6e50c0a"
}
The manifest file also holds the hashes of the generation's 4,096
buckets. show leaves them out, since they fill 4,096 lines,
and keeps their root.
--generation N shows another generation:
$ ddigraph store show questionnaire --generation 1 | head -n 3
{
"number": 1,
"created": "2026-11-07T13:01:07.744001Z",
--json prints the graph itself, in the shape of
export --format json: the
nodes, then the relationships, then the summary. The summary comes
last, so the command never holds the whole graph in memory. Save it to a
file:
$ ddigraph store show questionnaire --json > graph.json
$ python -c "import json; print(list(json.load(open('graph.json'))))"
['nodes', 'relationships', 'summary']
Time travel with --as-of¶
--as-of TIME shows the generation that was HEAD at that time. Here the
time falls between the two writes, so it gives generation 1:
$ ddigraph store show questionnaire --as-of 2026-11-07T13:01:10 | head -n 3
{
"number": 1,
"created": "2026-11-07T13:01:07.744001Z",
The time is an ISO 8601 date and time. A time with no offset is UTC, whatever your computer's time zone. A time before the first generation is refused:
$ ddigraph store show questionnaire --as-of 2026-11-07T08:00:00-05:00
The store has no generation of dataset 'questionnaire' created at or before 2026-11-07T08:00:00-05:00
--as-of works with --json too, to read the graph as it was then.
store diff¶
$ ddigraph store diff questionnaire 1 2
Nodes: 1 added, 0 removed, 1 changed
Relationships: 1 added, 0 removed
With --json, diff prints the rows themselves, in the shape of the JSON
export:
$ ddigraph store diff questionnaire 1 2 --json > delta.json
$ python -c "import json; print(sorted(json.load(open('delta.json'))))"
['added', 'added_relationships', 'changed', 'removed', 'removed_relationships']
Each list is in key order. changed holds each node as it is in the newer
generation.
What counts as a change:
- A node is the same node in both generations when it has the same key. It is changed when its properties, its type label, its stub reason or its provenance differ. Provenance includes the line the node starts on, so a node that only moved in the file counts as changed. In the example above, the relabelled category is changed. So would be any node below the new lines, if there were one.
- A relationship is known by its start, type, end and ordinal. It is only ever added or removed. When its resolution or properties change, the old one is removed and the new one added. An ordered reference that moves to another place has another ordinal, so it is removed and added too.
store changes¶
changes prints the delta from one generation to another, so a program can
follow a dataset. --since N names the generation the program holds, and
--to M the one it wants, HEAD by default. Without --json, it prints the
counts, as diff does:
$ ddigraph store changes questionnaire --since 1
Nodes: 1 added, 0 removed, 1 changed
Relationships: 1 added, 0 removed
With --json, it prints one JSON object per line. Each of the delta's rows
comes first, then a last line with the generation it reached and its
root:
$ ddigraph store changes questionnaire --since 1 --json
{"change":"changed","row":{"key":"4fc41f4fb59acbab4031c11f34883242","label":"Category",...}}
{"change":"added","row":{"key":"9d2e71c4a0f35b8e6c1d7f0a2b4e8c63","label":"Category",...}}
{"change":"added_relationship","row":{"type":"HAS_CATEGORY","start":"ed724a635f0c9de06038268d82ca19eb",...}}
{"generation":2,"root":"b01ddc85d9dfef21243b61355afc9d38b524fdad9548e77f024a6881f6e50c0a"}
Each row is a node or a relationship of the IR, as msgspec writes it
in JSON. The rows come in the order a database applies them: removed
relationships, removed nodes, added and changed nodes, then added
relationships. So a program applies the lines in order, then keeps the
generation as its next --since.
--since 0 gives the whole generation, as if the program held nothing:
every row is added or added_relationship, and the Dataset node comes
first.
store stubs¶
A stub is a node a dataset refers to but does not hold, such as a code
list that another file defines (see
Stubs). stubs lists the open stubs of a
dataset's HEAD. For each, it names the other datasets whose HEAD holds the
real node:
$ ddigraph store stubs survey
CodeList urn:ddi:example.org:codes-1:1 (not_in_file): held by codes
--json prints a list of objects with the key, label, identity,
reason and held_by of each stub. To find the other datasets' nodes,
stubs reads only the blocks that may hold one, not their whole graphs.
When you load both datasets into Neo4j, the real node fills the stub in
place, whatever the order. The stub's edges stay. If you then purge the
dataset that holds the real node, a purge keeps the node for the dataset
that still refers to it. The node stays real, with the values the purged
dataset gave it. A repair or a --replace of the other dataset makes it a
stub again.
store gc¶
$ ddigraph store gc questionnaire --keep 1
Removed generation folders 1 of dataset 'questionnaire'
$ ddigraph store list
questionnaire: HEAD 2, 1 generations
gc keeps the newest N generations, and never removes HEAD. If you load
Neo4j through the store, keep the generation its marker names too, or the
next load writes the whole dataset again (see below).
Safe writes¶
A write is atomic. The store writes the new generation's folder, and
flushes each file and the folder to disk. Only then does it write the new
number to HEAD.tmp and rename it over HEAD. A crash at any point leaves
HEAD on the last complete generation. The store never reads a folder that
HEAD has not named yet.
A crash can leave a folder behind. A folder above HEAD, left by a killed
write, goes on the next write or gc. A folder at or below HEAD with no
manifest, left by a gc that was stopped, goes on the next gc. The store
removes only the folders it made: a numbered folder that holds other files,
or is a link, stops the write or gc with an error before it removes any
folder. The files a file browser leaves, such as .DS_Store, Thumbs.db
and desktop.ini, do not count.
On Windows the store does not flush folders to disk, since Windows cannot
do that. It still flushes every file before HEAD changes.
One writer at a time. A write or gc holds the dataset's LOCK file,
locked through the operating system. A second process that tries to write
or gc the same dataset fails at once:
Another process is writing dataset 'questionnaire' of the store in ddigraph-store: it holds ddigraph-store/questionnaire/LOCK. Try again once it ends.
The operating system lets go of the lock when the process ends, even when it is killed. So a crash never leaves a dataset locked. Reads take no lock. Different datasets can be written at the same time.
Loading Neo4j through the store¶
A plain ddigraph load merges the file into Neo4j. It adds and updates, but
it never removes what the file no longer holds; only --replace does that,
by purging the dataset first. A load through the store sends Neo4j the
delta instead: what was added, changed or removed since the last load.
ddigraph load questionnaire.xml --store # the store in ddigraph-store
ddigraph load questionnaire.xml --store my-store # or in my-store
Put --store after the file when you give it no folder.
DDIGRAPH_USE_STORE=true does the same for every load.
The first load writes the whole generation. Its last two lines say what it wrote:
$ ddigraph load questionnaire.xml --store | tail -n 2
Ingestion complete: dataset_id=questionnaire, nodes_written=7, relationships_written=6, stubs=0, references=5, resolved=5, unresolved=0, conflicts=0
Store: generation=1, delta_from=None, nodes_removed=0, relationships_removed=0
After the edit, the next load writes only the two nodes and the one relationship that changed:
$ ddigraph load questionnaire.xml --store | tail -n 2
Ingestion complete: dataset_id=questionnaire, nodes_written=2, relationships_written=1, stubs=0, references=6, resolved=6, unresolved=0, conflicts=0
Store: generation=2, delta_from=1, nodes_removed=0, relationships_removed=0
delta_from names the generation the delta started from; None means the
load wrote the whole generation. --json adds generation, delta_from,
nodes_removed and relationships_removed to the result.
How it works¶
- The load takes the dataset's lock in the store, so two loads of one dataset never run at once.
- It reads the file and writes it as the dataset's next generation. The
parse and the store each get half of the memory limit. A file that is
refused, by
--strict-parsingor--resolve failsay, is refused here, before anything reaches Neo4j. - It reads the dataset's marker in Neo4j, a node
(:DDILoad {dataset_id, generation, key_scheme, updated}). There is one per dataset. It names the generation the database holds. - It removes the marker. Then, if the store still holds the generation the
marker named, the load applies the delta from it to the new one.
Otherwise (no marker yet, a generation gc removed, or
--replace) it purges the dataset and writes the whole new generation. - Once every batch has committed, it writes the marker again, naming the new generation, in a transaction of its own.
The delta goes in four steps, in this order, each in batches of
chunk_size rows:
- Removed relationships go, unless another dataset holds both of their ends.
- Removed nodes leave the dataset. A node no other dataset holds is deleted, with its relationships.
- Added and changed nodes are written as a plain load writes them.
- Added relationships are merged.
Removals come first because a changed relationship is a removal and an addition (see what diff means).
A crash converges. Each batch gives the same graph when it runs twice.
While a load runs, the dataset has no marker. So after a crash, or Ctrl-C,
the next load of the dataset, of the same file or an edited one, finds no
marker. It purges the dataset and writes its whole new generation, so the
graph ends up as --replace would leave it. The store holds one generation
more, which does no harm.
A first load with --fresh¶
--fresh writes the whole generation with CREATE instead of MERGE, so
it skips the lookup MERGE makes for each node and relationship. It is
meant for a first load into an empty database, and it refuses any other:
$ ddigraph load questionnaire.xml --store --fresh | tail -n 1
A fresh load writes with CREATE, so the database must hold no :DDINode, and it holds some; load without --fresh
--fresh needs --store. If a fresh load stops part way, the database now
holds nodes, so run the load again without --fresh. For a very large
first load, see also bulk import.
Things to know¶
--dry-runreads and checks the file as a plain load does. It writes nothing to the store and nothing to Neo4j. It reads nothing from Neo4j either, so it does not see a node that the load would refuse for its labels.- The store records each node's provenance with the file's absolute path. So the same file reached by another path still gives the same graph.
- Keep the generation the marker names when you run
gc. If it is gone, the next load purges the dataset and writes it whole. The result is the same graph, only slower. - A load without
--storeremoves the dataset's marker, since the graph then no longer matches a generation in the store. A database that a plain load filled has no marker either. Either way, the next load through the store purges the dataset and writes it whole. - A node that two datasets share keeps the values of the last load that
wrote it. A delta writes only the nodes its own file changed. So after a
load of the other dataset, use
--replaceto write this dataset's values back. - A relationship between two nodes that another dataset also holds stays
when this dataset's file drops it, as a purge would keep it. It stays
even once the other dataset no longer holds those nodes, since no later
delta lists it again.
--replaceremoves it.
Repairing Neo4j¶
Someone may edit the graph in Neo4j by hand, or a script may delete a node.
The store still holds what the dataset should be. ddigraph repair finds
what differs and puts it back:
$ ddigraph repair questionnaire
Dataset 'questionnaire' against generation 2: 3 buckets differed; wrote nodes: 1 added, 0 removed, 1 changed; relationships: 1 added, 1 removed
It works in four steps:
- It hashes what Neo4j holds of the dataset in the same 4,096 buckets as the store. It reads a few buckets per query, through the index on the key.
- It compares those hashes with HEAD's, and skips the buckets that match.
- In each bucket that differs, it reads the rows on both sides and writes the difference, as a delta load does.
- It moves the dataset's marker to HEAD.
So it puts back a deleted node or edge, a changed value, even one of the
wrong type such as a date, a lost type label, an :Unresolved label lost or
added, or a lost membership in the dataset. It removes an edge that HEAD
does not have, even one whose ordinal is wrong for its type or is not a
number. It removes the extra copy of an edge or of
a membership, even of an edge that another dataset holds. It also removes
a node whose key falls in no bucket, since no store holds such a key. And
it brings a database that holds an older generation of the dataset up to
HEAD.
--dry-run finds and counts what differs, and writes nothing. --json
prints the result as JSON. repair takes the Neo4j options of load, and
--store DIR.
repair reads every bucket that differs before it writes anything, so a
dry run counts what the repair writes. It holds those rows in memory: a few
rows after a few edits, but the whole dataset if Neo4j lost all of it.
Run it after someone edits the graph by hand, or after a tool other than
ddigraph writes to it. With --dry-run, which only reads, it is also a
cheap check to run on a schedule.
What it never touches:
- A property the spec does not own, such as one you added. It stays.
- A label or a relationship type the spec does not have.
- The other datasets' nodes. A node that this dataset shares with another gets this dataset's values, as a load would give it.
- A stub that another dataset's node filled stays filled, and an edge between two nodes that another dataset holds stays. A load leaves both as they are. Neo4j holds the generation of each dataset that its marker names. So the stub counts as filled when another dataset that holds the node has it as a real node in that generation. It also counts as filled when that dataset is not in the store, or has no marker that names one of its generations in the store. If each dataset that holds the node has it as a stub, the repair makes it a stub again. Drift on a filled stub's node, such as a changed value, is for the repair of the dataset that has the node real.
While it writes, repair holds the dataset's lock in the store, so it
never runs during a load of the same dataset.
The file format¶
A segment, nodes.seg or relationships.seg, is a series of blocks. Each
block is an 8-byte little-endian length, then a
MessagePack array of up to 1,000 rows. A row is an IR
node or relationship, as msgspec encodes it, with its property names
sorted. Nodes are sorted by key. Relationships are sorted by start, type,
end and ordinal, with an unordered edge before its ordered ones. So the
same graph always gives the same bytes.
MANIFEST.json describes the generation:
| Field | What it holds |
|---|---|
number |
The generation's number |
created |
When it was written, in UTC |
key_scheme |
The key scheme, b3-128/1 (see Node identity) |
dataset_id |
The dataset's ID |
dataset_key |
The key of the dataset's Dataset node |
nodes, relationships |
Each segment's rows, its sha256, and its blocks: each block's first key and byte offset |
model_hash |
Which metamodel the ddigraph that wrote it was built from: the SHA-256 of its model files (see Contributing). An older manifest has none, and it reads as empty |
buckets |
The hash of each of the 4,096 buckets, in order (see below). An older manifest has none |
root |
The SHA-256 of the bucket hashes |
The block list lets a reader jump to a key without reading the whole file.
Buckets¶
The rows of a generation fall into 4,096 buckets by the first three hex
digits of a key, from 000 to fff. A node goes in the bucket of its key,
and a relationship in the bucket of its start. Both segments are sorted by
key, so each bucket is one run of rows in each, which the block list finds.
A bucket's hash is the SHA-256 of its rows as the segments hold them: the
nodes, then the relationships, each row's bytes after its 8-byte length. An
empty bucket's hash is the SHA-256 of nothing. The root is the SHA-256 of
the 4,096 hashes, 32 bytes each, in order. The store works them out as it
writes the segments.
Two generations with the same hash for a bucket hold the same rows in it.
So diff, changes and a load through the store read only the buckets
whose hashes differ. They check each bucket against its hash before they
use it. On a 100,000-node dataset where 10 nodes change, the diff takes a
tenth of the time of a diff that reads the whole segments. A manifest that
an older ddigraph wrote has no buckets. The store then works them out from
the segments when it needs them, and never writes to that generation. A
diff with such a generation reads the whole segments.
A damaged segment stops the read with an error that names the file and says the store is damaged. A file cut short gives:
ddigraph-store/questionnaire/000002/nodes.seg is cut short: the store is damaged
A block that does not decode, or a file whose rows or SHA-256 differ from
the manifest, says instead that the file does not match its manifest. So
does a bucket that does not match its hash, which names the bucket. A
HEAD that holds no generation number, or a MANIFEST.json that does not
decode, gives the same kind of error.
In Python¶
from ddigraph.store import Store
store = Store("ddigraph-store")
print(store.datasets()) # ['questionnaire']
print(store.head("questionnaire")) # 2
for generation in store.generations("questionnaire"):
print(generation.number, generation.created)
delta = store.diff("questionnaire", 1, 2)
print(len(delta.added), len(delta.changed)) # 1 1
for change, row in store.changes("questionnaire", since=1):
print(change, type(row).__name__) # e.g. changed Node
print(store.manifest("questionnaire").root)
print(store.stubs("questionnaire")) # []
Store.write(dataset_id, chunks, memory_limit=...) writes an IR stream as a
generation, and Store.read(dataset_id, n) streams one back, the Dataset
node first. Store.as_of(dataset_id, time) and Store.gc(dataset_id,
keep=n) match the commands above, as do Store.changes(dataset_id, since,
to) and Store.stubs(dataset_id). Store.diff_rows(dataset_id, old,
new) streams a delta, and Store.buckets(dataset_id, n) gives a
generation's bucket hashes, which ddigraph.store.merkle describes. To
search other generations than the other datasets' HEADs, give
Store.stubs a dict of dataset IDs and generation numbers, as a repair
does with the generations Neo4j holds.