Connecting
Describe a database in the environment, with the same variables as TypeScript’s Drivine and drivine4j, so one .env
serves an application in any of them:
DATABASE_TYPE=NEO4JDATABASE_HOST=localhostDATABASE_PORT=7687DATABASE_USER=neo4jDATABASE_PASSWORD=…A second database takes a prefix, ANALYTICS_DATABASE_TYPE and so on. Then register them:
from drivine import DatabaseRegistry, PersistenceManagerFactory
async with DatabaseRegistry() as registry: # closes the drivers and pools on the way out registry.build_or_resolve_from_env() # "default" registry.build_or_resolve_from_env("analytics") # ANALYTICS_DATABASE_*
pm = PersistenceManagerFactory(registry).get() # the default database ...DrivineAssembly registers the default database alone; to register others, subclass it and override
register_databases(registry) (see With pi-pilgrim).
On FalkorDB, DATABASE_NAME names the graph, graph by default.
A time with an offset is saved and bound on each graph engine
(#565). Neo4j holds it as a Time of its offset, and
gives it back as a time of that offset. Memgraph, which has no Time, holds it as text of one width, as drivine4j
writes it, 10:11:12.000000000+02:00, so the text sorts as the times do, and FalkorDB holds its ISO text,
10:11:12+02:00. A field declared time loads either back as the time, and a statement that returns it gives the text. A time without
an offset, or of a ZoneInfo, which gives none for a time alone, is a local time on each.
A UUID is saved and bound on each graph engine as its text, in lists and maps too, as drivine4j and Drivine for
TypeScript hold one (#583): before, a save of one raised
on each. A field declared UUID loads the text back as the UUID, and a statement that returns it gives the text.
On Oracle, DATABASE_NAME is the service (FREEPDB1, say), and DATABASE_DEFAULT_GRAPH_PATH names the graph,
drivine by default. A graph is drivine4j’s tables, <graph>_node, <graph>_edge and <graph>_schema, and a SQL
property graph over them, made on the first connection when they aren’t there, so one store serves the JVM, Node and
Python. <graph>_schema records the indexes and constraints Drivine made as drivine4j records them, so each library
sees and drops the indexes the others made, and makes none beside them. A range index is two indexes, as drivine4j
makes it: one over the property whatever the label, and one over the label’s nodes alone, which a filter is read
through. A uniqueness constraint comes with a range index over its properties, which stays when the constraint is
dropped. The object manager writes SQL for it, and the query DSL, grouped reads, search and schema management work as on
the graph engines. A raw where is SQL over the node’s row, n: its labels, props (a JSON object keyed by stored
name) and stamp, say JSON_VALUE(n.props, '$.name') = $name, read over the row a statement calls it by, n_ (the row
the shared module’s fragments read). A property of the node may be written as Cypher writes it, n.name = $name (or
n.`a name` ), and is read as that property, one whose stored name begins with $, n.`$score` , by the name the
store holds it by; a property compared with true or false, or with a parameter bound to True or False, is
compared with the text a JSON boolean is read as, and to compare one with a number, read it as a number in SQL:
CAST(n.`$score` AS NUMERIC) > $least. Your own statements are SQL too, unless written as Cypher (see Cypher on
Postgres, Oracle and SQLite), and a $name in quoted text or a
comment, such as the JSON path '$."$t"', is no parameter. The query DSL’s conditions, orders, seeks and grouped reads
are the shared module’s SQL for the Cypher the DSL writes for Neo4j
(drivine-cypher-spec#235), as in compiled Cypher: a
date is compared by its moment, text is compared and ordered by its code points (B, a, b), and an equality of a
property with a range index or a uniqueness constraint is read through layout 2’s index of the label. Each statement
carries the optimizer hints that SQL asks for. matches takes a regular expression as compiled Cypher’s =~ does,
matched against the whole value, in Oracle’s POSIX dialect. A leading (?i) ignores case. A backreference is read as
Java reads it only where it is \1 to \8, to a simple group; the shared module refuses any other, with why. A vector
index is a table of embeddings beside the nodes, holding those of the index’s size: a node written with another size is
left out, not refused. load_nearest is approximate through it. With no index made, or one of another size than the
vector’s, it measures every node of the label whose embedding is of the vector’s size, and is exact.
Each session of Drivine’s Oracle pool makes no adaptive plan (ALTER SESSION SET OPTIMIZER_ADAPTIVE_PLANS = FALSE, run
as the pool opens it; a session it fails in is dropped from the pool, not given out). Once a graph’s tables had grown
and shrunk without statistics, Oracle’s adaptive join plan gave no rows for a read that filters a node by a label and a
property, where the same plan, fixed, gave them all (#146).
A statement of yours that relies on an adaptive plan can ask for one with the hint
/*+ OPT_PARAM('optimizer_adaptive_plans' 'true') */. To have the sessions left as Oracle opens them, register a
provider made in code with sets_no_adaptive_plans=False: registry.register(OracleConnectionProvider("default", "localhost:1521/FREEPDB1", user, password, sets_no_adaptive_plans=False)), its class from
drivine.connection.oracle.oracle_connection_provider (drivine.sync.connection.oracle.oracle_connection_provider for
the sync API). It is on by default because, without it, a read can come back with no rows and no
error; if you turn it off, run that statement in each session yourself.
Before that, each session runs the statement the shared module states for it, ALTER SESSION SET NLS_COMP = BINARY NLS_SORT = BINARY, so compiled Cypher compares, orders, groups and finds text by its code points whatever NLS_COMP and
NLS_SORT the database or a logon trigger gave the session
(#566). Under a linguistic sort such as BINARY_CI,
'B' < 'a' would be false and 'a' = 'A' true, with no error. sets_no_adaptive_plans=False leaves this one in place.
Each session of the pool keeps the statements it runs prepared, 20 of them (python-oracledb’s stmtcachesize), so a
save, the same block each time, is neither sent nor parsed again. A Cypher statement compiled to SQL is not kept: Oracle
holds a kept statement’s memory for as long as the driver keeps it, from about 4 MB for a compiled statement to over
20 MB for one that reads a long text, and a few dozen of them ran the instance out of memory (ORA-04036,
#218). So it is prepared again each time it runs: about
0.3 ms more for a short statement (Oracle reuses its plan; it doesn’t parse it again from scratch). The object manager’s
statements are kept, so where the object manager does what you need, it is the faster way to Oracle.
Beside the tables an Oracle graph is made with functions: <graph>_nulls_equal, by which a compiled Cypher statement
compares two lists or maps that may hold a null (see
Cypher on Postgres, Oracle and SQLite), <graph>_era and
<graph>_era_date, by which it renumbers a year before 1 between Cypher’s numbering and Oracle’s as it reads and writes
a date or a moment, and <graph>_temporal_arith, which works out +, -, * and / of a temporal value read from
the store. drivine4j and Drivine for TypeScript make them byte for byte alike. Making them takes
CREATE PROCEDURE, which the RESOURCE role grants, as well as the CREATE TABLE the tables take. A store an earlier
version made gains those it lacks the next time a pool first connects to the graph. Where the user that makes the graph
may not make a function, the graph is made without them, and Drivine logs a warning once for the graph when a statement
would call one, and looks for it again a minute later. Until then, without <graph>_nulls_equal, a statement that
writes, or the step of a reduce, compares two lists or maps that hold a null and differ as JSON as false, where Cypher
may give null (a = b, of [null] AS a, [1] AS b), and a read declares the function itself. Without the era
functions, a statement gives the same rows, renumbering a year inline, but Oracle takes longer to plan one of temporal
arithmetic: seconds, and over a minute for a moment two CALLs deep. A statement of the query DSL that would call one
the session may not call (a comparison of a list or a map that holds a null with another, or an order by arithmetic of
a property, which may hold a date) is refused with a DrivineError that names it, where compiled Cypher is compiled
again without a comparison’s function.
On Postgres, DATABASE_NAME is the database, and DATABASE_DEFAULT_GRAPH_PATH names the graph, drivine by default:
see Postgres. On either, Cypher you write is compiled to SQL: see
Cypher on a relational database.
Store layout. Oracle, Postgres and SQLite hold a graph in store layout 2, as drivine4j and Drivine for TypeScript
do: a temporal value is kept as a tagged map, a map key that begins with $ gains one more, and bytes are kept as the
list of their signed values. A float is held as a Cypher CREATE holds it: on Oracle as a binary double, NaN and the
infinities too, on Postgres a whole one with its fraction (2.0), and on SQLite an infinity as 9e999; Postgres’s NaN
and infinities, and SQLite’s NaN, which their JSON holds as no number, are refused with a DrivineError before anything
is written. A NodeId that is a float is found by a load, a delete, edges.relate and edges.load_related as the store
holds it, as a save finds it. The store’s stamp records the layout. On a provider’s first connection, before anything is
made, the shared module judges the graph: a new one is made in this layout, and one in an earlier layout, or none, that
holds none of your nodes is made this layout’s (what the earlier layout left is dropped; nothing is migrated). A graph
in an earlier layout that holds nodes, or one a later Drivine wrote, is refused on that connection and every one after
it with a StoreLayoutError, from drivine and drivine.sync, whose message says what to do (reload it with this
version, or upgrade) and whose graph names the graph.
Migrating a graph from layout 1 to layout 2. StoreLayoutMigration, in drivine.migration
(and drivine.sync.migration), moves a graph on Oracle, Postgres or SQLite from layout 1, which
0.8.0 and earlier wrote, to layout 2 in place, by the shared compiler’s migrator, which drivine4j and Drivine for
TypeScript run alike: Migrating a graph from store layout 1 to layout 2 gives the
steps. Run it while nothing else uses the graph, with a backup taken first; it runs on connections of its own from the provider’s
pool, never one of an application’s transaction. Layout 1 can’t tell which texts are dates or moments, which whole
numbers are floats and which texts are bytes, so it is told by declarations, a label’s property and its kind each:
declarations_of(Person) derives them from a node fragment’s fields of type date, datetime (naive or aware) or
AwareDatetime, timedelta or float, or a list, a tuple or a set of one (under each of its labels, inherited ones
too), by the name each is stored under (pass registry= the ModelRegistry the application’s managers are given,
where it names properties otherwise than in camel case), and declarations_of(Knows, "KNOWS") from a relationship
fragment’s; with_declared(derived, by_hand) replaces or adds declarations by hand. A field 0.8.0 saved in another
form than its kind’s is not declared, as the migration would read it as no value of its kind and refuse the graph, and
is kept as it is: a NaiveDatetime, saved as a UTC moment with a Z, no local datetime; a time, saved with its
offset where it had one, which its type can’t tell; bytes, saved as their UTF-8 text; and a JsonPacked field, saved
as a JSON text.
migrate(declarations, dry_run=True) writes nothing, and reports what would change, the values that are
no value of their declared kind, the properties whose values all read as one kind (candidates to declare), and the
uniqueness constraints’ keys that may collide. A migration gives back its report, whose status says how it ended:
migrated, current or empty (nothing to do), dryRun (a dry run’s), inProgress (a dry run of a graph part way
through a migration, which only resuming it goes on with), invalid (a value not of its kind: declare it otherwise, or
pass keep_invalid=True), collisions (keys a uniqueness constraint holds once that layout 2 reads as one: correct them
with plain SQL and run it again), refused (a graph of another layout, or one changed since its migration began, or
resumed, before every row is written, with other declarations or keep_invalid), or failed (the store’s error). One
stopped at any moment, or by collisions, is resumed by running it again with the same declarations. It makes no index
or constraint, which the application’s schema plan makes at its next start, and leaves the count store unseeded.
from drivine.migration import StoreLayoutMigration, declarations_of
declarations = [*declarations_of(Person), *declarations_of(Knows, "KNOWS")]migration = StoreLayoutMigration(registry.connection_provider("graph"))report = await migration.migrate(declarations, dry_run=True)if report["status"] == "dryRun": report = await migration.migrate(declarations)Or in code:
registry.builder().database_type(DatabaseType.MEMGRAPH).host("localhost").register("graph")
