Chapter 7
Containers: named and nested graphs
The second metagraph power: a vertex that holds a subgraph and is still a vertex. In a statement graph that vertex is a named graph. This chapter covers membership, nesting, scope, and how to make an edge hold a subgraph.
A graph is a node
A named graph groups statements. In RDF the name is an IRI, and membership is the fourth column of a quad. In a statement graph, membership is one more statement:
(s1, :in, :session7) # s1 is in the episode
(:session7, :in, :aliceMemory) # the episode is in Alice's memory
Because the graph's name is a term, the graph can be described like anything else: (:session7, :startedAt, "2026-11-20T09:00Z"). That is Gapanyuk's metavertex: it contains statements, and it is a vertex.
In graphlava a graph has its own ID kind (tag 0x5). Writing into a graph uses standard SPARQL:
PREFIX : <http://example.org/>
INSERT DATA {
GRAPH :session7 {
:alice :worksAt :acme {| :confidence 0.9 ; :source :chat12 |} .
}
}
Membership that costs nothing
Most statements live in exactly one graph. graphlava's spec makes that case free. IDs are allocated in chunks of 220 consecutive IDs, and each chunk is leased to one graph and one kind of item. The home graph of a statement is a function of its ID: look up the chunk. No membership row is stored.
| Membership | Stored as | Cost per item |
|---|---|---|
| Home graph (one per item, fixed at allocation) | the ID's chunk, via gchunk | 0 bytes |
| Extra graph | one key in gmem (g, item) and one in gmem_rev (item, g) | 2 keys |
So "all statements in :session7" is a set of ID ranges, not a scan. A statement learned in the session and also filed under a project belongs to both: home :session7, extra :pricingProject.
The membership triple (x, :in, g) is virtual, like the other virtual statements of chapter 6. It has no statement ID until something annotates it (ADR 0017). Annotate it, "Bob filed this under the project", and the spec allocates an ID and stores it as the gmem value.
One statement, many graphs
A fact keeps one ID however many graphs hold it (ADR 0024). Graph clauses then behave like SPARQL datasets, without a copy per graph:
| Write | Effect (spec) |
|---|---|
INSERT an existing triple into another graph | Adds a membership of that graph. Same statement, same ID, same annotations. |
DELETE a triple inside GRAPH g | Removes only the membership in g. The statement is deleted, with its cascade, when its last membership goes. |
| Move from g to h | Adds h, removes g, in one transaction. The ID and home chunk stay; a moved tombstone in gmem hides the old home membership. |
DROP GRAPH g | Removes every membership in g. Items still in another graph survive; the rest are deleted. |
Two episodes can assert the same fact and each keep its own metadata. Per-episode provenance goes on the promoted membership, not on the fact:
# one fact, two memberships; the spec writes a membership as << X :in g >>
s1 = (:alice, :worksAt, :acme)
m7 = (s1, :in, :session7) # promoted when annotated
m9 = (s1, :in, :session9)
(m7, :confidence, 0.9) # session 7 was sure
(m9, :confidence, 0.6) # session 9 less so
Uniqueness of (s, p, o) holds per store: the global tables, or one partitioned graph's own tables. A triple stored both in a partition and globally therefore has two IDs, and a query over both deduplicates it.
Nesting is a DAG
Graphs nest through (child, :in, parent). graphlava allows several parents and forbids cycles:
:session7 has two parents, and :pricingProject sits directly under :agentMemory. The statement s1 is a direct member of :session7 only, and in the scope of all four graphs.(:session7, :in, :aliceMemory)
(:session7, :in, :pricingProject)
(:aliceMemory, :in, :agentMemory)
(:pricingProject,:in, :agentMemory)
| Rule (spec) | Effect |
|---|---|
| No cycles | Adding (a, :in, b) fails with GraphCycle when a = b or b is already under a. The check is one point read. |
| Depth limit | Default 64 levels, configurable; past it, GraphDepthExceeded. |
| Closure kept current | in_star(descendant, ancestor) holds the transitive closure as a set of pairs, updated in the writer transaction. There are no path counts (ADR 0029): removing a parent link recomputes the ancestors of the graphs below it, so one of two parent links can go without losing a still-reachable ancestor. |
The :in above is the spec's shorthand. The IRI of the system's membership predicate is still an open question.
Scope: what a GRAPH clause sees
The scope of a graph is the graph and every graph nested under it. graphlava's GRAPH matches the scope by default (ADR 0007). A new keyword, GRAPH!, matches direct members only.
| Syntax | Matches | s1 found via |
|---|---|---|
GRAPH :agentMemory { P } | statements in the graph or anywhere under it, each once | yes |
GRAPH! :agentMemory { P } | direct members only | no |
GRAPH ?g { P } | binds ?g to each direct graph of the match, never an ancestor | ?g = :session7 |
FROM :g | the default graph is the scope of :g |
# everything the agent believes about employers, at any depth
SELECT ?who ?org WHERE { GRAPH :agentMemory { ?who :worksAt ?org } }
# which episode did each fact come from, and which memories hold that episode?
SELECT ?who ?ep ?mem WHERE {
GRAPH ?ep { ?who :worksAt :acme }
?ep :in* ?mem .
}
Standard SPARQL means direct membership. A connection flag, graph_scope = direct, restores it, and the W3C test suites run with it.
How a scope is read
The planner picks one of three plans per scoped pattern, from per-graph statistics:
| Plan | When | Cost |
|---|---|---|
| range-driven | the scope is small: read its ID ranges, filter by the pattern | O(items in scope) |
| filter-driven | the pattern is selective and the scope large: scan the pattern, test each ID against the scope bitmap | O(matches) |
| partitioned | the graph was declared partitioned and has its own tables | O(matches in g) |
A query over one small episode costs in proportion to the episode, not the database. DROP GRAPH g removes every membership in g: items that are still in another graph survive, the others are deleted and cascade to their annotations. DROP RECURSIVE GRAPH g also drops descendants that have no other surviving parent.
Edges as containers
Combine both powers and an edge can hold a subgraph: the meeting e1 holds its minutes. graphlava's graph IDs and hyperedge IDs are different kinds, so a hyperedge or statement is not itself a graph name. The pattern is a graph linked to the edge:
# the minutes of meeting e1 are a graph; ?e is the meeting's hyperedge ID
INSERT { ?e :minutes :e1minutes . }
WHERE { ?e a :Meeting ; :agent :alice ; :target :bob ; :topic :pricing }
INSERT DATA { GRAPH :e1minutes { :bob :proposed :discount10 . } }
# open the meeting
SELECT ?s ?p ?o WHERE {
?e a :Meeting ; :agent :alice ; :minutes ?m .
GRAPH ?m { ?s ?p ?o }
}
The link (e1, :minutes, :e1minutes) has e1 as subject, so deleting the meeting deletes the link by cascade. The minutes graph itself stays until dropped. tiramemsu goes one step further: a graph can be named by a statement, GRAPH <urn:tiramemsu:stmt:1> { … }, so the container follows the edge through corrections and goes with a retraction. In graphlava, make the link part of the edge's life explicitly: drop the minutes graph in the same transaction that deletes the meeting.
In graphlava. Graphs are nodes with their own ID kind. Home membership is free, read from the ID's chunk; extra memberships cost two keys. Membership triples are virtual until annotated. One statement can be in several graphs; deleting inside a graph removes only that membership, and per-episode metadata goes on << X :in g >>. Nesting is a DAG with cycle and depth checks and a maintained closure. GRAPH :g sees the whole scope, GRAPH! :g only direct members, and GRAPH ?g binds direct graphs.
Takeaways
- A container is a named graph whose name is also a node.
- Nesting as a DAG lets an episode belong to a person's memory and a project at once.
- A fact in several graphs is one statement with several memberships; put per-episode metadata on the membership.
- Transitive scope makes "everything under this memory" one clause instead of a union.
- For an edge that holds a subgraph, link a graph to the edge and manage their lifetimes together.