Skip to content

SQLite

SQLite 3.53.2 or later, over JDBC with xerial’s driver: a graph in a file, with no server to run. Drivine keeps it in the same two tables as on Oracle and Postgres, so models, the query DSL and StatelessGraphObjectManager are the same as on the other stores. Add the driver yourself: org.xerial:sqlite-jdbc, 3.53.2.0 or later.

Compiled Cypher needs SQLite 3.53.2. Before it, a compiled statement writes a float to 15 digits, so 0.1 + 0.2 comes back as 0.3 (sqlite-jdbc 3.53.0.0 and 3.53.1.0 still do); SQLite 3.49 refuses a CALL subquery that groups by a variable it imports; and a SQLite before 3.47 refuses the -> -1 that last() and a negative list index compile to (#565). So on an older sqlite-jdbc a Cypher statement is refused, saying which SQLite it found and that sqlite-jdbc is to be upgraded. Plain SQL and the schema manager are not refused there, and neither are the object managers, but their conditions and orders are the compiler’s SQL too, and a least or greatest value of a grouped read, and a path’s order, are an ordered aggregate (json_group_array(… ORDER BY …)), which SQLite before 3.44 refuses: run them on 3.53.2 or later.

database:
datasources:
graph:
type: SQLITE
database-name: /var/data/app.db # the file, made if it is not there
default-graph-path: drivine # names the graph's tables; this is the default

database-name is required, and names a file: an in-memory database is refused, as each pooled connection would open one of its own. A host, a protocol or a cypher-dialect means nothing here and is ignored with a warning; a port, a user and a password are not read. The pool is HikariCP with pool-max. Each connection is opened with a write-ahead log, foreign keys enforced, a wait of 30 seconds for a locked file, and the functions Drivine’s statements call registered on it, so make the provider with JdbcConnectionProvider.sqlite, not over a DataSource of your own:

val provider = JdbcConnectionProvider.sqlite("graph", "/var/data/app.db", graphName = "drivine")

How a graph is stored. <graph>_node (nid, labels, props, stamp) and <graph>_edge (eid, src, dst, type, props), made, where they are not there yet, before the datasource’s first connection, with labels and props as JSON text (not SQLite’s binary jsonb, so Drivine for TypeScript and for Python read the same file), and <graph>_schema for what schema management made. A save is the plan of steps the other stores carry out, carried out by the function drivine_write that Drivine registers on each connection: applied whole or not at all, and stamped and checked as elsewhere. A save and a CREATE hold a ByteArray alike, as the list of its signed bytes, [1, 2, -1] (a release before this one saved it as base64 text; such a node loads as before). Deleting a node’s row deletes its relationships with it.

nid and eid are INTEGER PRIMARY KEY AUTOINCREMENT, so the id of a node or relationship that is deleted is never given to another, within a statement, a transaction or after it, as on Oracle and Postgres: an element id an application holds names the element it was read from or nothing. SQLite keeps the greatest id each table has given in its own table sqlite_sequence; deleting every row, as a test’s cleanup does, leaves it, so ids go on from there. A file whose graph tables an earlier Drivine made, with INTEGER PRIMARY KEY alone, which gives a new row the id of the last one deleted, is refused on the provider’s first connection and every one after it, with a ReusedIdsException that names the graph and the file:

The SQLite graph 'drivine' in /data/app.db was made by an earlier Drivine, whose node and relationship ids could be reused after a delete. Make a new file and save the graph into it again.

There is no migration, as SQLite cannot change a table’s key in place.

Notes:

  • A statement written by hand is Cypher, compiled as below, unless it is marked SQL with sqlStatement(...) or the datasource defaults to SQL, as on Oracle. SQLite has no JSON type, so JSON a SQL statement of your own returns is text; one that opens with /* drivine:json */ has each of its text values read as the JSON it is.
  • A CypherStatement is compiled to SQL over the graph’s tables, as on Oracle and Postgres (see Cypher on a relational database), and gives the rows a graph engine gives. A read is a query; a write is a procedure of steps, as on the other stores, which the function drivine_cypher carries out on the statement’s connection under a savepoint, so it is applied whole or not at all. A value is JSON text: two compare as Cypher compares them (1 equal to 1.0, a map to a map of the same entries in any order) by functions Drivine registers, which also add lists and texts and work out temporal values with java.time. A query that reads the row it is in (OPTIONAL MATCH, CALL { … }, a variable-length walk, a shortest path) is gathered as JSON and joined with json_each, as SQLite joins no lateral query. Text compares and sorts by SQLite’s BINARY collation; toLower and toUpper change the case of any letter, where SQLite’s own lower and upper change ASCII alone.
  • sum() and avg() add floats as doubles, as Neo4j does, so 0.1 + 0.2 sums to 0.30000000000000004 here too. SQLite also corrects for rounding as it adds (Kahan–Babuška summation), so a long sum of floats, or one that mixes an integer past 2^53 with a float, can be more accurate than Neo4j’s in its last digit: 9007199254740993 and 0.5 sum to 9.007199254740994E15, where Neo4j gives 9.007199254740992E15. A result still comes back as a float. A sum() of integers that passes a long’s range fails with integer overflow, where Neo4j gives a float.
  • An infinity is the number SQLite’s JSON writes for one, and is stored, by a statement and a save alike. Its JSON holds no NaN: a statement given one as a parameter writes it as {"$numberDouble": "NaN"}, works with it as the float it is, as Neo4j does, and gives it back as NaN. A statement that writes and is given NaN is refused, rather than store that map, as is a save of NaN, and one that writes NaN it works out from an infinity ($i - $i, n.inf + n.negative) fails. A statement that may work NaN or an infinity out, as on Postgres (dividing or taking % by anything but a number other than 0, toFloat of a text), is compiled as one given one: a number divided by a float zero is NaN or an infinity, as on Neo4j, and so is a float by an integer zero where both are written in the statement (1.0 / 0); an infinity it works out is stored. A number divided or taken % by any other integer zero fails, as on Neo4j, where SQLite’s own / and % give null. NaN worked out from a stored infinity by any other statement (n.inf - n.inf, n.inf * 0, n.inf % 2, sum() of both infinities) is NaN too, as on Neo4j.
  • A raw whereClause is a SQL predicate, as on Postgres: n.note = 'unverified', or SQL over n.props, n.labels and n.stamp. SQLite’s ->> gives a JSON value as the SQL value it is (true as 1), where Postgres gives its text.
  • A temporal value is held as a tagged map of its ISO 8601 text and the key it orders by, as on Postgres, and a moment is compared and ordered by its instant, whatever offset it was written with; one instant written at two offsets is still two values, which a grouped read’s key keeps apart. A year before 1 is numbered as Neo4j numbers it: 0000 is 1 BC and -0044 is 45 BC.
  • SQLite has one writer at a time, and transactions are serializable:
    • A transaction takes the file’s write lock as it begins (BEGIN IMMEDIATE), so no write in it is turned away part of the way through. Another writer, and another transaction even if it only reads, waits for it to end: a Drivine connection for up to busyTimeoutMillis (30 seconds unless you set it), and then fails as busy. Keep transactions short: one that holds the lock for more than 5 seconds is logged as a warning when it ends.
    • A read outside a transaction does not wait for a writer. The file is in WAL mode: a read sees what was committed when it started, and nothing of a transaction still open.
    • Outside a transaction, each statement is a transaction of its own. A save, a patch, a delete or a relationship write runs in one that takes the write lock as it begins, as a transaction does, so it waits for another writer rather than being turned away for what that writer committed. One the file stays locked against for longer than the wait fails, as a DrivineException, and is not run again: it has waited. One turned away without the wait is run again, as a contended save is on the other stores. A statement of your own that reads and then writes, run with query or execute, takes the lock only as it writes: run it in a transaction, or with executeBatch, if other connections write at the same time.
    • Each statement Drivine writes applies whole or not at all, in a transaction or outside one: a statement that fails part of the way undoes what it wrote.
    • A DataSource of your own is opened with none of this: JdbcConnectionProvider.sqlite(…) makes the connections, with their write lock, WAL mode and the functions Drivine’s statements call.
  • Index the id of each node type you save, with a uniqueness constraint or a range index: each is a partial index over the property for the nodes of one label.
  • matches takes a regular expression in Java’s dialect, matched against the whole value. A leading (?i) ignores case.
  • A vector index and a full-text index are recorded and nothing is made for them: loadNearest and loadMatching measure every node of the label, and are exact. loadMatching scores by BM25, as Postgres does.
  • The test kit’s Cypher fixtures (SqlCypherFixtures) work here as on Oracle and Postgres, and so does TestCleanup.beforeEach. SQLite’s tests need no container.