Documentation

Multiple databases

A DatabaseManager holds several named databases, each with its own storage. Clients use it through a Session, which has a current database and runs the database commands:

CREATE DATABASE movies;                                   // default storage of the manager
CREATE DATABASE scratch IF NOT EXISTS OPTIONS {storage: 'memory'};
CREATE OR REPLACE DATABASE events
  OPTIONS {storage: 'hybrid', sync: 'os', checkpointAfterBytes: 16777216};
SHOW DATABASES;            // name, storage, default, current, nodes, relationships
SHOW DATABASE movies;
SHOW DEFAULT DATABASE;
USE movies;                               // switch the session's current database
USE scratch MATCH (n) RETURN count(n);    // run one query elsewhere, without switching
DROP DATABASE scratch IF EXISTS;
use graphbuff::DatabaseManager;

let mgr = DatabaseManager::open("data")?;   // or DatabaseManager::in_memory()
let mut s = mgr.session();                  // starts on the default database, `graphbuff`
s.execute("CREATE OR REPLACE DATABASE movies")?;
s.execute("USE movies")?;
s.execute("CREATE (:Movie {title: 'Heat'})")?;
  • CREATE OR REPLACE DATABASE drops an existing database with all its data and creates it again empty, with the given options; if it doesn't exist it is just created. It cannot be combined with IF NOT EXISTS. Plain CREATE DATABASE fails if the name is taken; IF NOT EXISTS leaves the existing database alone.
  • Options: storage is 'memory', 'hybrid' or 'persistent'. sync ('commit' or 'os') and checkpointAfterBytes apply to hybrid and persistent storage, cacheBytes and maxTransactionBytes (the transaction size cap) to persistent storage. Without OPTIONS, a manager opened on a directory creates hybrid databases (persistent ones with --persistent in the shell, or DatabaseManager::open_with(dir, StorageKind::persistent())) and an in-memory manager creates memory databases. OPTIONS $map works too, as does $name for the database name. WAIT/NOWAIT and DESTROY DATA are accepted and ignored.
  • Names are case-insensitive, 1–63 characters (letters, digits, ., -, _), start with a letter, and need backticks if they contain -. Windows device names such as con are rejected because names are also folder names.
  • The default database (graphbuff) cannot be dropped, but it can be replaced. Dropping a session's current database moves that session back to the default one.
  • On disk, data/catalog lists the databases and their storage settings, and each hybrid or persistent database lives in data/databases/<name>/. Memory databases stay in the catalog across restarts but come back empty. A LOCK file keeps a second process out.
  • A plain Database has no other databases, so it rejects USE and database commands with an Unsupported error.

In the shell, :use name and :dbs are shortcuts, and the prompt shows the current database.

Try it on your machine: one command starts the server with the movies graph. Needs Docker.

$ docker run --rm -p 127.0.0.1:7475:7475 -p 127.0.0.1:7687:7687 ghcr.io/janvencl/graphbuff --demo movies

Then follow Getting started.