> ## Documentation Index
> Fetch the complete documentation index at: https://ladygraph.io.vn/llms.txt
> Use this file to discover all available pages before exploring further.

# Storage

> The store contract, the tables of the embedded graph database, and how a save stays safe.

## The store contract

`GraphStore` is a `Protocol` with these operations: `apply(contribution)` (atomic), `drop_unit`, `versions`, `pinned`, `known_types`, `registry`, `search` and `export`.

`InMemoryStore` is the reference implementation. `LadybugStore` (an embedded graph database) and a networked variant implement the same contract, and the same contract tests run against each. `open_index` opens an index.

| Store | What it is |
| - | - |
| `InMemoryStore` | The reference implementation. |
| `LadybugStore` | The embedded Ladybug graph database. One process owns the file for writing. |
| `LadybugFlightStore` | A Ladybug server reached over Arrow Flight. Other machines read and save through it. |

An index is one `.lbdb` file.

## Tables

| Table | Key | Holds |
| - | - | - |
| `Concept` | `id` | All concept fields. `embedding` is a `DOUBLE[]` of any length. `words`, `verified` and `extra` are text. |
| `Evidence` | `key` | `id`, `resource`, `passage`, `start`, `length`, `fingerprint`, `unit`, `access_labels` and `stale_after`. A shared node: two concepts that cite one quote share it. |
| `Relation` | `key` | `owner`, `seq`, `type`, `target`, `weight`, `evidence_ids` and `unit`. The source of truth for relations. |
| `Context` | `id` | `parent`, `label`, `description`, `metadata_seen`, `embedding`, `status` and `created_at`. |
| `Pin` | `key` | A `ContextChoice`. |
| `Version` | `key` | A unit's `DocVersion`: `file`, `metadata` and `passages`. |
| `Meta` | `key` | `schema_version` and `revision`. |
| `SUPPORTED_BY` | Concept to Evidence | With `seq`. |
| `RELATES` | Concept to Concept | `type`, `weight`, `unit` and `rid`. Derived from the `Relation` rows. |

Depth and tree edges are not stored as tables. Depth is derived from the `parent` chain, and a concept names its context in a column. Timestamps are ISO text so the time zone survives.

## Relations: the rows are the truth, the edges are derived

A relation is kept as a row in `Relation`, and a `RELATES` edge is kept for each row whose target exists. Edges alone would not be enough, for three reasons measured on the embedded database (version 0.15.3):

* A relation may name a concept that does not exist yet. Creating an edge to a missing node is accepted and creates nothing, with no error.
* Rewriting a concept with `DETACH DELETE` removes every edge of the node, including edges that other concepts own and that point at it. In the test, 11 edges became 0 while the rows still held every fact.
* The merge rules work on relation records: their unit, their evidence, and "remove when all evidence goes".

Edges still help. A hop is cheaper, and the graph can be viewed and queried by path. At 50,000 concepts a one-hop lookup took 34 ms from rows and 17 ms from edges, and two hops took 58 ms and 22 ms. At 5,000 concepts they were equal.

The store keeps edges right by rebuilding, in the same transaction as any change, the edges of the changed concept and the edges that point at it, from the rows. A concept that arrives later picks up the relations that were waiting for it. `check_edges` says whether edges equal rows, and `rebuild_edges` repairs them, so edges are never the only copy. Opening a version 1 database, which has no edges, builds them from the rows.

## How a save stays safe

A save is one explicit transaction, the version marker included. A half-written save that moved the marker would never be repaired, because the next update trusts the marker.

Measured on the embedded database:

* A statement sent without `BEGIN` commits alone.
* A failed statement aborts the transaction.
* A process killed before `COMMIT` leaves nothing.
* Only one write transaction runs at a time, and a second is refused at once.
* One process owns the database file.

So the store takes a lock around each save, never carries on after an error, and routes every write through one helper that exists only inside a transaction. Every text parameter passes through an escape rule, because the driver parses text that looks like a list or a map.

The embedded store and the Flight store run the same plan, so they cannot disagree about what a save does.

## Export

The index can be exported as a bundle of markdown files, one per concept, in a folder per context.

```text theme={null}
bundle/
  index.md                      format version
  log.md                        created, deprecated, merged, moved
  ladygraph.manifest.json       format, scope, units to DocVersion, ontology fingerprint
  scope-a/
    index.md                    the top-level contexts
    topic-1/
      index.md
      concept-x.md              id in the front matter
      topic-1a/
        concept-x.md            a different concept: same word, different context
```

Typed relations are written twice: as a `relations` front-matter key and as plain links in the body. A moved concept or context leaves a stub at the old path, with `type: Redirect`.

## Implementation rules

These rules were measured on LadybugDB 0.21.2, embedded, with 768-dimension vectors.

| Rule | Why |
| - | - |
| Write with Arrow tables and `COPY`, never row by row with `CREATE` | 190,507 rows per second against 223 |
| Pass vectors as Arrow `FixedSizeListArray` | Copying from a pandas column of numpy arrays fails |
| Read results with `get_as_arrow()` | 15 ms against 65 ms for 20,000 rows as Python tuples |
| Run each query as one Cypher statement (vector seed plus expansion) | Every extra round trip costs network latency over the Flight server |
| Resolve evidence in batches | One round trip per search, not per hit |
| When a span has moved, hash each window of the passage | Passages are about 2,000 characters, so this is cheap |
| Run extraction with asyncio and a concurrency limit, on changed passages only | Extraction is network-bound and the slowest stage |

> [card] [Flight server](https://ladygraph.io.vn/guides/flight-server.md)
>
> Build and run the server that other machines use to read and save.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.