.NET Embedded

Run Traverse inside your .NET application, with no server: open or create a .tvdb file, query it with openCypher or GQL, and load data with the bulk writer.

dotnet add package Traverse.Embedded

Targets net10.0. The package carries the native engine for Windows x64, Linux x64 and macOS on Apple silicon (runtimes/osx-arm64), so the database runs in your process with no network in between and a Mac needs nothing installed. To use a library you built yourself, point TRAVERSE_FFI_LIB at it.

Open a Database

using Traverse;

// open, or create, a database file
using var db = Database.Open("mydata.tvdb");

Disposing the database rolls back transactions still open on it, saves it, and leaves the .tvdb as a single file. Another process can't open the file while it's open.

File Size Limit

Traverse holds the whole database in memory, so Database.Open refuses a file larger than 80% of physical RAM by default:

Database.MaxFileSizeBytes = 16L * 1024 * 1024 * 1024; // 16 GB
Database.MaxFileSizeBytes = 0;                       // no check

Run Queries

Query on the database runs a query as a transaction of its own: a read runs on a snapshot and never waits, a write commits when it succeeds and rolls back when it fails. QueryGql runs ISO/IEC 39075 GQL instead of openCypher.

db.Query("CREATE (:Person {name: 'Alice', age: 30}), (:Person {name: 'Bob', age: 25})").Dispose();

using (var result = db.Query("MATCH (p:Person) RETURN p.name AS name ORDER BY name"))
    foreach (var row in result)
        Console.WriteLine(row.GetString("name")); // Alice, Bob

using (var result = db.QueryGql("MATCH (p:Person) FILTER p.age > 26 RETURN p.name AS name"))
    foreach (var row in result)
        Console.WriteLine(row.GetString("name")); // Alice

Transactions

// a write transaction; Dispose without Commit rolls it back
using (var tx = db.BeginTransaction())
{
    tx.Query("CREATE (:Person {name: 'Carol', age: 41})").Dispose();
    tx.Query("MATCH (a:Person {name: 'Alice'}), (c:Person {name: 'Carol'}) CREATE (a)-[:KNOWS]->(c)").Dispose();
    tx.Commit();
}

// a read transaction: a snapshot of the database as of now, alongside the writer and other readers
using (var snapshot = db.BeginReadTransaction())
using (var result = snapshot.Query("MATCH (p:Person) RETURN count(p) AS n"))
    foreach (var row in result)
        Console.WriteLine(row.GetInt64("n")); // 3

One write transaction is active at a time: BeginTransaction waits for the active one for up to db.BusyTimeout (5 seconds), then throws with TvErrorCode.Busy. A statement that fails after changing data fails its transaction: later queries and Commit throw, and only a rollback ends it.

A commit is durable when it returns: it's written to the database's redo log first. The database also saves itself in the background (once the log has grown by 64 MiB, or when a commit has gone five minutes unsaved), so reopening after a crash replays little. Flush() saves at once.

Query Results

QueryResult is a forward-only cursor:

using var result = db.Query("MATCH (n:Person) RETURN n.name, n.age");

// column metadata
foreach (var col in result.Columns)
    Console.WriteLine($"{col.Index}: {col.Name}");

// rows
foreach (var row in result)
{
    if (!row.IsNull(0))
    {
        string? name = row.GetString("n.name");
        long age = row.GetInt64("n.age");
    }
}

Row Access Methods

MethodReturn TypeDescription
GetString(int col)string?String value by column index
GetString(string name)string?String value by column name
GetInt64(int col)longInteger value by column index
GetInt64(string name)longInteger value by column name
GetDouble(int col)doubleFloat value by column index
GetDouble(string name)doubleFloat value by column name
GetBool(int col)boolBoolean value by column index
GetBool(string name)boolBoolean value by column name
GetValue(int col)object?Boxed typed value
IsNull(int col)boolCheck if value is null
GetTypeName(int col)stringType name (INTEGER, FLOAT, STRING, NODE, EDGE, PATH, etc.)
Snapshot()RowCreate a managed copy valid beyond cursor lifetime

Bulk Loading

A bulk load is a write transaction that skips the redo log: Commit saves the database with the load in it, so the data is written once and is durable when Commit returns. Readers don't see the load until then, and Abort (or disposing without a commit) rolls it back. It waits for an active writer like BeginTransaction.

using var bulk = db.BeginBulkWrite();

// Create nodes (returns node ID)
long alice = bulk.CreateNode(
    new[] { "Person" },
    new[] { "name", "age" },
    new[] { "Alice", "30" });  // values are auto-type-inferred

long bob = bulk.CreateNode(
    new[] { "Person" },
    new[] { "name", "age" },
    new[] { "Bob", "25" });

// Create edges
bulk.CreateEdge(alice, bob, "KNOWS",
    new[] { "since" },
    new[] { "2024" });

// Create indexes
bulk.CreateIndex("idx_person_name", "Person", "name");
bulk.CreateEdgeIndex("idx_knows", "KNOWS");

bulk.Commit();

BulkWriter Methods

MethodDescription
CreateNode(labels, propKeys, propValues)Create a node, returns node ID
CreateEdge(source, target, type, propKeys, propValues)Create a relationship, returns edge ID
CreateIndex(name, label, property)Create a node property index
CreateEdgeIndex(name, edgeType, property)Create an edge property index
CreateEdgeIndex(name, edgeType)Create a type-only edge index
DropEdgeIndex(name)Drop an edge index
Commit()Save the database with the load in it and make it visible
Abort()Roll the load back

Graph JSON for Sigma.js

GraphJsonSerializer turns a result into graphology JSON for Sigma.js:

using var result = db.Query("MATCH (a)-[e]->(b) RETURN a, e, b");
string json = GraphJsonSerializer.Serialize(result); // {"nodes":[...],"edges":[...]}

Error Handling

db.BusyTimeout = TimeSpan.FromSeconds(10);      // wait for another writer
db.StatementTimeout = TimeSpan.FromSeconds(30); // how long a statement may run

try
{
    db.Query("MATCH (p:Person) RETURN nosuchfunction(p.name)").Dispose();
}
catch (TraverseException ex)
{
    Console.WriteLine(ex.ErrorCode); // TvErrorCode.Syntax
    Console.WriteLine(ex.GqlStatus); // 42N48 (unknown function)
}

A statement that runs past StatementTimeout, whose CancellationToken (an overload of every Query) is cancelled, or that another thread stops with Transaction.Cancel(), throws with TvErrorCode.Interrupted. GqlStatus holds the error's GQLSTATUS code (the ISO/IEC 39075 code where the standard has one). Embedded transactions show in SHOW TRANSACTIONS, and TERMINATE TRANSACTIONS stops them.

Error Codes

CodeDescription
IoFile I/O error
CorruptDatabase file corruption detected
LockedDatabase file is locked by another process
SyntaxCypher syntax error
TypeType mismatch
SemanticSemantic query error
GraphNode or relationship not found
TransactionTransaction conflict or abort
InvalidArgInvalid argument
InternalInternal engine error
ResourceExhaustedA resource limit was exceeded (memory budget, file size)
BusyThe writer lock stayed taken for the whole busy timeout; retry
InterruptedThe statement was stopped: its time limit passed, it was cancelled, or its transaction was terminated