Architecture¶
GoGraph is a single-file embedded graph database written in pure Go, designed for minimalist, zero-dependency, high-performance runtime scenarios. The system design decouples through a clear layered model, ensuring single responsibility and high cohesion between modules.
1. System Module Division¶
GoGraph's core architecture is divided into 5 layers:
- API Layer (
pkg/api): Provides user-friendly interfaces, similar toOpen,Query,Execfromdatabase/sql. - Cypher Parsing and Execution Layer (
pkg/cypher): - Parser: Lexical analysis and syntax parsing, parsing Cypher statements into Abstract Syntax Trees (AST).
- AST: Declares execution nodes, structures, and data blocks.
- Executor: Coordinates Matcher, Creator, and Modifier to execute specific statement intents.
- Graph Model Layer (
pkg/graph): Contains data structure encapsulation for nodes (Node), edges (Relationship), and property types (PropertyValue), along with core logic handling such as adjacency list management (AdjacencyList) and indexes (Index). - Transaction Layer (
pkg/tx): Encapsulates Pebble DB's transaction mechanism, controls multi-version concurrency (MVCC), and handles safe read/write operations. - Storage Layer (
pkg/storage): Based on CockroachDB's Pebble as a single-file storage engine, and handlesencoding/gobserialization and deserialization.
2. System Architecture Diagram¶
The following Mermaid diagram shows GoGraph's architecture and data flow:
graph TD
User([Application/User]) --> API[API Layer
pkg/api]
subgraph GoGraph Core Engine
API --> |Cypher String| Parser[Cypher Parser
pkg/cypher/parser.go]
Parser --> |AST| Executor[Executor
pkg/cypher/executor.go]
subgraph Action Handlers
Executor --> |CREATE| Creator[Creator
pkg/cypher/creators]
Executor --> |MATCH| Matcher[Matcher
pkg/cypher/matchers]
Executor --> |SET/DELETE| Modifier[Modifier
pkg/cypher/modifiers]
end
Creator --> GraphModel[Graph Model / Data Encapsulation
pkg/graph]
Matcher --> GraphModel
Modifier --> GraphModel
GraphModel --> |KV Mapping| TxMgr[Transaction Manager
pkg/tx]
end
TxMgr --> |Batch / Iterator| Storage[Storage Layer Pebble KV
pkg/storage]
Storage --> |WAL & SSTables| Disk[(Local Single-File DB)]
3. Data Flow Description (CREATE Example)¶
- User calls
api.Exec("CREATE (n:User {name: 'Alice'})"). api.DBcallscypher.Parserto parse the statement into anAST(withCreateClause).Executorobtains a transaction objectTxand passes it to theCreatorengine.Creatorparses theASTto generate the correspondinggraph.Nodeobject, assigning a globally unique auto-incrementingID.- The
graphlayer usesstorage.Marshalto serialize it into binary (gob). Creatordelegates to the domain layer'sgraph.Indexandgraph.AdjacencyList(implicitly convertingTxtograph.Mutatorfor passing), synchronously building label indexes, property indexes, and adjacency list KV keys.- Finally,
Storagewrites to disk through Pebble'sCommit, making changes visible to other queries.
4. Core Execution Engine Optimizations (Query Optimization)¶
The refactored GoGraph implements a more advanced underlying query execution path:
- Index Scan: When a MATCH statement specifies a node's Label (e.g., MATCH (n:User)), the engine no longer performs O(N) full table scans, but directly hits matching Node IDs via the Index tree with O(K) complexity.
- Graph Traversal: For queries involving relationships (e.g., MATCH (n)-[r]->(m)), the system fully utilizes the adjacency list AdjacencyList, obtaining all associated edge IDs from the starting node in O(1) and precisely loading them, completely eliminating full-table brute-force traversal of edge relationships.
- Rich Domain Model: All underlying KV assembly logic is consolidated in the pkg/graph layer, and the Cypher executor is only responsible for scheduling, ensuring atomic transaction modifications and high code cohesion.
5. Core Technology Stack¶
- Base Language: Go 1.18+ (broad cross-platform compatibility)
- Persistence Underlay:
github.com/cockroachdb/pebble(replacement for LevelDB, high-performance KV library) - Object Serialization: Go's native
encoding/gob(zero external dependencies, extremely fast and native encoding/decoding solution) - Observability: Logger/Tracer design based on the Option pattern for injection (defined in
pkg/cypher/observability.go).