Java Embedded

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

Install

Maven

<dependency>
    <groupId>com.truespar</groupId>
    <artifactId>traverse-java</artifactId>
    <version>0.9.2</version>
</dependency>

Note: the jar carries the native engine for Windows x64, Linux x64 and macOS on Apple silicon (natives/macos-aarch64) and loads it at run time, so there's nothing else to install. Requires Java 25 or newer (run with --enable-native-access=ALL-UNNAMED to avoid the JNI warning). To use a library you built yourself, set TRAVERSE_JAVA_LIB or the traverse.library system property.

Open a Database

import com.truespar.traverse.Database;
import com.truespar.traverse.Transaction;
import com.truespar.traverse.BulkWriter;
import com.truespar.traverse.QueryResult;
import com.truespar.traverse.TraverseException;

// open, or create, a database file; closed (and saved) by try-with-resources
try (Database db = Database.open("mydb.tvdb")) {
    // use db
}

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

Run Queries

execute 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.

try (Database db = Database.open("mydb.tvdb")) {
    db.execute("CREATE (:Person {name: 'Alice', age: 30}), (:Person {name: 'Bob', age: 25})");

    QueryResult result = db.execute(
        "MATCH (n:Person) WHERE n.age > $minAge RETURN n.name AS name", Map.of("minAge", 26));
    for (var row : result) {
        System.out.println(row.get("name")); // Alice
    }
}

GQL

The three-argument execute takes a dialect: pass "gql" to run ISO/IEC 39075 GQL instead of openCypher (on the database or a transaction):

QueryResult result = db.execute(
    "MATCH (n:Person) FILTER n.age > 26 RETURN n.name AS name", null, "gql");

Transactions

// a write transaction; close() without commit() rolls it back
try (Transaction tx = db.begin()) {
    tx.execute("CREATE (:Person {name: 'Carol', age: 41})");
    tx.execute("MATCH (a:Person {name: 'Alice'}), (c:Person {name: 'Carol'}) CREATE (a)-[:KNOWS]->(c)");
    tx.commit();
}

// a read transaction: a snapshot of the database as of now, alongside the writer and other readers
try (Transaction snapshot = db.beginRead()) {
    System.out.println(snapshot.execute("MATCH (p:Person) RETURN count(p) AS n").data()); // [{n=3}]
}

One write transaction is active at a time: begin waits for the active one for up to the busy timeout (5 seconds, setBusyTimeout changes it), then throws a TraverseException. A statement that fails after changing data fails its transaction: later statements 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.

QueryResult

QueryResult result = db.execute("MATCH (n:Person) RETURN n.name, n.age");
System.out.println(result.getColumns());  // [n.name, n.age]
System.out.println(result.getRowCount()); // 3
for (Map row : result) {  // each row is a map
    System.out.println(row.get("n.name"));
}
List> rows = result.data();

System.out.println(db.execute("CREATE (n:Test)").getStats()); // {nodes_created=1, labels_added=1}

Bulk Writer

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 closing without a commit) rolls it back.

try (BulkWriter writer = db.bulkWriter()) {
    long nid1 = writer.createNode(
        new String[]{"Person"},
        Map.of("name", "Alice", "age", 30)
    );
    long nid2 = writer.createNode(
        new String[]{"Person"},
        Map.of("name", "Bob", "age", 25)
    );
    writer.createEdge(nid1, nid2, "KNOWS", Map.of("since", 2024));
    writer.commit();
}

Nodes can be created with labels only:

long nid = writer.createNode(new String[]{"Company", "Active"});

Edges can be created without properties:

writer.createEdge(sourceId, targetId, "WORKS_AT");

Indexes

Create indexes via the bulk writer or Cypher:

// Via BulkWriter
try (BulkWriter writer = db.bulkWriter()) {
    writer.createIndex("idx_person_name", "Person", "name");
    writer.createEdgeIndex("idx_knows_since", "KNOWS", "since");
    writer.createEdgeIndex("idx_knows", "KNOWS");  // type-only index
    writer.commit();
}

// Via Cypher
try (Transaction tx = db.begin()) {
    tx.execute("CREATE INDEX idx_person_age FOR (n:Person) ON (n.age)");
    tx.commit();
}

Drop an edge index:

try (BulkWriter writer = db.bulkWriter()) {
    writer.dropEdgeIndex("idx_knows_since");
    writer.commit();
}

Timeouts, Cancelling and Errors

db.setBusyTimeout(Duration.ofSeconds(10));      // wait for another writer
db.setStatementTimeout(Duration.ofSeconds(30)); // how long a statement may run

try {
    db.execute("MATCH (p:Person) RETURN nosuchfunction(p.name)");
} catch (TraverseException e) {
    System.out.println(e.getGqlStatus()); // 42N48 (unknown function)
}

A statement that runs past the statement timeout, or that another thread stops with Transaction.cancel(), throws QueryInterruptedException. getGqlStatus() returns 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.

Flush and Close

db.flush(); // save now (waits for an active writer like begin)
db.close(); // roll back open transactions, save, release the file (try-with-resources calls it)