Skip to main content

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. An index is one .lbdb file.

Tables

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.
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.

Flight server

Build and run the server that other machines use to read and save.