Graph Adjacency Index

Three wires answer graph queries, and each keeps its own adjacency structure. The shared GraphAdjacencyIndex in src/index/graph.rs is used by none of them.

Path Adjacency structure Where it lives Persisted as
Neo4j Bolt (Cypher) GraphStore.outgoing_rels / incoming_rels src/protocol/bolt/mod.rs _bolt_nodes, _bolt_relationships tables
Gremlin GremlinGraphStore.outgoing / incoming src/protocol/gremlin/traversal.rs _gremlin_vertices, _gremlin_edges tables
Cypher sent over a SQL wire None: full table scans src/query/engine.rs __graph_nodes__, __graph_edges__ tables
GraphAdjacencyIndex Nested hash maps src/index/graph.rs Nothing. Not wired

(source: src/protocol/bolt/mod.rs, GraphStore; src/protocol/gremlin/traversal.rs, GremlinGraphStore; src/query/engine.rs, execute_graph_relationship_match; src/index/mod.rs, IndexManager::create_graph_index)

The protocol-level behaviour of the first two is on the Neo4j Bolt and Gremlin protocol pages. This page compares the data structures.

flowchart LR B["Bolt session"] --> GS["GraphStore (in memory)"] GS --> BT["_bolt_nodes / _bolt_relationships"] G["Gremlin session"] --> GG["GremlinGraphStore (in memory)"] GG --> GT["_gremlin_vertices / _gremlin_edges"] S["Cypher on a SQL wire"] --> SE["QueryEngine scans"] SE --> ST["__graph_nodes__ / __graph_edges__"] GA["GraphAdjacencyIndex"]

(source: src/protocol/bolt/mod.rs, GraphStore::with_storage; src/protocol/gremlin/traversal.rs, GremlinGraphStore::with_storage; src/query/engine.rs, execute_graph_relationship_match. GraphAdjacencyIndex has no edges because nothing feeds it.)

Neo4j Bolt: GraphStore

Field Type Role
nodes HashMap<i64, GraphNode> Node records
relationships HashMap<i64, GraphRelationship> Relationship records
label_index HashMap<String, HashSet<i64>> Label to node ids
rel_type_index HashMap<String, HashSet<i64>> Type to relationship ids
outgoing_rels HashMap<i64, HashSet<i64>> Node id to outgoing relationship ids
incoming_rels HashMap<i64, HashSet<i64>> Node id to incoming relationship ids

(source: src/protocol/bolt/mod.rs, GraphStore)

Pattern matching expands a bound node through outgoing_rels for ->, incoming_rels for <-, and both (deduplicated) for an undirected hop, so one hop costs the node's degree, not a scan. (source: src/protocol/bolt/pattern.rs, expand) find_outgoing and find_incoming filter those sets by transaction visibility and by relationship type with rel_type_matches. (source: src/protocol/bolt/mod.rs, find_outgoing, find_incoming, rel_type_matches)

The whole store is rebuilt at start by reading _bolt_relationships (and the node table) through the SQL engine and refilling every index. (source: src/protocol/bolt/mod.rs, GraphStore::with_storage) Writes inside an open transaction are tracked per session in uncommitted maps and are hidden from other sessions until commit. (source: src/protocol/bolt/mod.rs, GraphStore, uncommitted node and relationship fields)

Neo4j RANGE, TEXT, POINT and LOOKUP indexes are catalog entries only and do not change how patterns are evaluated; see the protocol page. Wire tests are in tests/protocol_bolt.rs.

Gremlin: GremlinGraphStore

Field Type
vertices RwLock<HashMap<VertexId, GremlinVertex>>
edges RwLock<HashMap<EdgeId, GremlinEdge>>
outgoing, incoming RwLock<HashMap<VertexId, Vec<EdgeId>>>
vertex_label_index, edge_label_index RwLock<HashMap<String, HashSet<...>>>
next_vertex_id, next_edge_id AtomicI64

(source: src/protocol/gremlin/traversal.rs, GremlinGraphStore)

get_outgoing_edges and get_incoming_edges read the vertex's edge list and filter by label. Edge lists are Vecs, so remove_edge is linear in the vertex's degree; remove_vertex removes the vertex's edges too. (source: src/protocol/gremlin/traversal.rs, get_outgoing_edges, get_incoming_edges, remove_edge, remove_vertex)

On start, with_storage loads both tables, rebuilds all four indexes, and sets each id counter to the largest loaded id plus one. Each write is mirrored to the tables as a delete plus insert; errors from that mirror are ignored, so the in-memory graph can run ahead of the tables. (source: src/protocol/gremlin/traversal.rs, with_storage, persist_vertex, persist_edge) Tests are in tests/protocol_gremlin.rs; see the Gremlin protocol page.

traversal.rs imports crate::index::graph::GraphAdjacencyIndex but never uses it. (source: src/protocol/gremlin/traversal.rs, imports)

Cypher over a SQL wire

detect_language routes a query that looks like Cypher (one starting with MATCH or OPTIONAL MATCH , or a CREATE followed by a (n:Label) node pattern) to the Cypher parser in src/query/cypher/. Its nodes and edges live in two ordinary tables created on first use. (source: src/query/engine.rs, detect_language, graph table creation)

Table Columns
__graph_nodes__ _id, _labels (comma separated), _properties
__graph_edges__ _id, _from_id, _to_id, _type, _properties

(source: src/query/engine.rs, graph table creation)

Single hop

execute_graph_relationship_match scans both tables in full, then joins nodes to edges in memory. There is no adjacency index, so a one-hop match costs a scan of every node and every edge. (source: src/query/engine.rs, execute_graph_relationship_match) Tested by test_engine_cypher_create_edge_then_match_traverses, test_engine_cypher_create_then_match_roundtrip, test_engine_cypher_create_emits_one_wellformed_row, test_engine_cypher_two_same_label_nodes_no_collision and test_cross_model_rel_plus_cypher_create_atomic. (source: src/query/engine.rs, those tests)

Variable-length paths

A pattern such as -[*1..3]-> is planned as LogicalPlan::GraphTraverse. (source: src/query/cypher/planner.rs, variable-length relationship planning) execute_graph_traverse runs a breadth-first search from each start node:

  • max_hops defaults to 10 when the pattern gives no upper bound.
  • One visited set is shared by all start nodes, so a node reached from the first start node is never reported for a later one.
  • Each path is returned as a row (path, length) with the node ids joined by ` -> `.

(source: src/query/engine.rs, execute_graph_traverse)

get_graph_neighbors scans every key under __graph_edges__: once per expanded node and decodes each value as a bincode tuple (String, String, String, serde_json::Value). The table's rows are written by the engine's row encoder with five columns, and bincode cannot decode a serde_json::Value, so every decode is expected to fail and the search to find no neighbours. Not verified: no test runs a variable-length Cypher pattern over a SQL wire. (source: src/query/engine.rs, get_graph_neighbors)

Unwired: GraphAdjacencyIndex

Not wired: it is only constructed by IndexManager::create_graph_index, which nothing calls (see Unwired index structures), and by its own tests. (source: src/index/mod.rs, create_graph_index; src/index/graph.rs, tests)

Structure

adjacency: RwLock<HashMap<RecordId, AdjacencyEntry>>
AdjacencyEntry {
    outgoing: HashMap<RecordId /*to*/,   HashMap<String /*type*/, GraphEdge>>,
    incoming: HashMap<RecordId /*from*/, HashMap<String /*type*/, GraphEdge>>,
}
edge_count: RwLock<usize>
GraphEdge { from, to, edge_type, properties: HashMap<String, Value> }

(source: src/index/graph.rs, GraphAdjacencyIndex, AdjacencyEntry, GraphEdge)

At most one edge exists per (from, to, type); adding it again overwrites the stored edge.

Operations

Method Behaviour
add_edge (trait GraphIndex) Insert into both endpoints' entries; overwrite on the same (from, to, type)
remove_edge(from, to, type) type = None removes every type between the two nodes
outgoing, incoming Neighbour lists, optionally filtered by type
shortest_path(from, to, max_depth) Unweighted BFS; from == to returns [from]
neighbors(node, depth) BFS result without the start node
bfs, dfs Traversals with optional type filter and depth limit, returning (node, depth)
has_edge, get_edge, get_all_edges, update_edge, out_degree, in_degree, edge_count, node_count Lookups and counts

(source: src/index/graph.rs, GraphAdjacencyIndex, impl GraphIndex for GraphAdjacencyIndex)

Nothing is persisted; the structure lives only in memory. Tested by test_graph_basic, test_graph_shortest_path, test_graph_neighbors and test_graph_remove_edge. (source: src/index/graph.rs, tests)

The schema enum IndexType::Graph in src/types/schema.rs names this index kind, but no code builds an index from that variant. (source: src/types/schema.rs, IndexType)

Third-party crates

parking_lot for the RwLocks in GraphAdjacencyIndex and GremlinGraphStore, and bincode in the SQL Cypher neighbour decode. (source: src/index/graph.rs, imports; src/query/engine.rs, get_graph_neighbors)