Skip to content
JohnathonNowPublic

Repository files navigation

yagdb (Yet Another Graph Database)

yagdb is an experimental, in-memory graph database written in Rust. It implements a subset of the Cypher query language, features disk persistence via Write-Ahead Logging (WAL) and snapshots, and provides an HTTP API for query execution. It also supports compiling to WebAssembly (WASM) for use in browsers. The project was largely built as an exercise in using AI-tooling, and at this time should not be trusted for anything beyond goofing off.

Features

  • Cypher Query Support: Implements a subset of Cypher using the nom parser combinator library.
    • CREATE: Creates new nodes and relationships.
      • Nodes: CREATE (n:Person {name: 'Alice', age: 30})
      • Relationships: CREATE (a:User)-[:KNOWS]->(b:User)
      • Mixed: CREATE (a:Person {name: 'Alice'})-[:KNOWS]->(b:Person {name: 'Bob'})
    • MATCH: Matches patterns in the graph.
      • Nodes: MATCH (n:Person)
      • Relationships: MATCH (a)-[r:KNOWS]->(b)
      • Variable Length Paths: MATCH (a)-[*1..3]->(b)
      • Bound Variables to Paths: MATCH p = (a)-[*1..3]->(b)
    • WHERE: Filters results based on conditions. Supports AND, OR, NOT, and comparison operators (=, !=, <, >, <=, >=, =~).
      • Property comparison: MATCH (n:Person) WHERE n.age > 25
      • Regex match: MATCH (n:Person) WHERE n.name =~ '(?i)^a.*'
      • Boolean logic: MATCH (n:Person) WHERE n.age > 25 AND NOT n.name = 'Bob'
    • RETURN: Specifies what to include in the query result. Supports aliasing.
      • Variables: RETURN n, r
      • Properties: RETURN n.name, n.age
      • Aliasing: RETURN n.name AS personName
      • All variables: RETURN *
      • Aggregations: RETURN COUNT(n), COLLECT(n.name), UNIQUE(n.age)
    • WITH: Chains query parts together, allowing you to manipulate the output before it is passed to the following clauses. Similar syntax to RETURN.
      • MATCH (n:Person) WITH n, n.age AS age WHERE age > 25 RETURN n.name
    • ORDER BY: Sorts the result set. Supports ASC and DESC.
      • MATCH (n:Person) RETURN n ORDER BY n.age DESC
    • LIMIT: Restricts the number of rows in the result set.
      • MATCH (n:Person) RETURN n LIMIT 10
    • CREATE INDEX ON: Creates an index on a specific node label and property for faster lookups.
      • CREATE INDEX ON :Person(name)
    • SET: Updates properties on existing nodes or relationships.
      • MATCH (n:Person {name: 'Alice'}) SET n.age = 31
    • MERGE: Matches an existing pattern or creates it if it doesn't exist.
      • MERGE (n:Person {name: 'Charlie'})
    • DELETE: Deletes nodes or relationships.
      • MATCH (n:Person {name: 'Alice'})-[r]-() DELETE n, r
    • UNWIND: Expands a list into a sequence of rows.
      • UNWIND [1, 2, 3] AS x RETURN x
    • CALL: Executes a subquery.
      • MATCH (n:Person) CALL { WITH n MATCH (n)-[:KNOWS]->(m) RETURN m } RETURN n, m
    • Expressions:
      • Property Access: n.name
      • Literals: Strings ('Alice'), Numbers (42, 3.14), Booleans (true, false)
      • Variables: n, r, p
      • Functions: rand() is supported.
    • PROFILE: Prefixing a query with PROFILE outputs the execution plan instead of executing the query.
      • PROFILE MATCH (n:Person) RETURN n
  • Graph Engine: Native Rust graph execution engine utilizing Depth-First Search (DFS) for path finding.
  • Persistence: Fast binary serialization of the database state and Write-Ahead Log (WAL) entries using bincode and serde. Automatically recovers state upon startup.
  • HTTP Server: An asynchronous API server using axum and tokio. Exposes a POST endpoint to execute queries.
  • Observability: Exposes Prometheus metrics at the GET /metrics endpoint for production monitoring, tracking query counts, duration, and graph sizes.
  • Clustering (Optional): Can be compiled with the cluster feature to enable distributed consensus using openraft. Extra experimental.
  • Disk Storage: Experimental support for managing large graphs by caching nodes and serving from disk.
  • WASM Support: Can be compiled to the wasm32-unknown-unknown target for browser embedding.

Getting Started

Prerequisites

Running the Server

To start the HTTP server, simply run:

cargo run

The server will start listening on 127.0.0.1:3000 by default. It will automatically create graph.bin and wal.bin in the current directory for persistence.

Running with Clustering

To compile and run with Raft cluster support:

cargo run --features cluster

Using the Database

You can interact with yagdb using HTTP POST requests to the /query endpoint. The body of the request should be the raw Cypher query string.

Examples

Create nodes and a relationship:

curl -X POST -H "Content-Type: text/plain" -d "CREATE (a:User {name: 'Alice'})-[r:KNOWS]->(b:User {name: 'Bob'})" http://127.0.0.1:3000/query

Match and return nodes:

curl -X POST -H "Content-Type: text/plain" -d "MATCH (a:User)-[r:KNOWS]->(b:User) RETURN a, b" http://127.0.0.1:3000/query

Create an index:

curl -X POST -H "Content-Type: text/plain" -d "CREATE INDEX ON :User(name)" http://127.0.0.1:3000/query

Profile a query:

curl -X POST -H "Content-Type: text/plain" -d "PROFILE MATCH (a:User {name: 'Alice'}) RETURN a" http://127.0.0.1:3000/query

Library Usage

You can also use yagdb as an embedded library in your own Rust applications.

use yagdb::graph::Graph;

fn main() {
    // Start an in-memory database without persistence for testing
    let mut g = Graph::new();

    // Execute queries
    g.execute("CREATE (n:Person {name: 'Alice'})").unwrap();
    let result = g.execute("MATCH (n:Person) RETURN n").unwrap();

    println!("{}", result);
}

Testing

Run the test suite using standard Cargo commands:

cargo test

To test cluster features specifically:

cargo test --features cluster

Benchmarks

yagdb includes a benchmark suite using the criterion crate.

cargo bench

WebAssembly Build

To build the project for WASM:

wasm-pack build --target web

In WASM mode, disk persistence (WAL, snapshots) and the HTTP server are conditionally disabled. The library exposes a wasm_bindgen function execute_query(query: &str) -> String that operates on a globally shared in-memory graph.

Roadmap to Production

To transition yagdb into a production-ready system, the following features would have to be implemented:

  • Strongly Typed Properties: Transitioning from string-only properties to native support for integers, floats, booleans, and dates to improve performance and data integrity.
  • Authentication & Authorization: Implementing user roles, credentials, and access control for the API.
  • Transactions: Adding support for multi-statement ACID transactions with commit and rollback capabilities.
  • Expanded Cypher Support: Adding advanced Cypher clauses like OPTIONAL MATCH, REMOVE, and string/math functions.
  • TLS/SSL Encryption: Securing the HTTP server and Raft node-to-node communication.
  • Graph Algorithms: Built-in procedures for PageRank, Shortest Path, and Community Detection.
  • Backup & Restore Tooling: Dedicated utilities for point-in-time recovery, snapshot management, version migration, and hot backups.