Skip to content

Imperative API

Every PersistenceManager exposes indexes and constraints managers. Operations always run in auto-commit mode (schema DDL cannot run inside a data transaction):

// Idempotent — call on every startup
val result = persistenceManager.indexes.ensure(
VectorIndexSpec(label = "Proposition", property = "embedding", dimensions = 1536)
)
persistenceManager.indexes.ensure(RangeIndexSpec("Proposition", "contextId"))
persistenceManager.indexes.ensure(RangeIndexSpec("Message", listOf("sessionId", "createdAt"))) // composite
persistenceManager.indexes.ensure(FullTextIndexSpec("Chunk", "text")) // full-text (single property)
persistenceManager.indexes.ensure(FullTextIndexSpec("Entity", listOf("name", "description"))) // full-text (multi-property)
persistenceManager.indexes.ensure(TextIndexSpec("Chunk", "title")) // Neo4j TEXT index
persistenceManager.constraints.ensure(UniquenessConstraintSpec("ChatSession", "sessionId"))
persistenceManager.constraints.ensure(UniquenessConstraintSpec("Membership", listOf("tenantId", "userId")))

ensure returns an EnsureResult:

Result Meaning
Created Nothing existed and the item was created, or it existed without part of what the engine makes for it (on Oracle, a range index without its index over the label’s nodes) and that part was made
AlreadyMatching A matching item exists; nothing changed. On Oracle, Postgres and SQLite, so too one made in another form, by an earlier or a newer version or another library, which is kept and used as it is, with a WARN (forms)
Drift An item exists with a different shape (e.g. vector dimensions changed), or on Oracle, Postgres and SQLite another object stands under the name Drivine gives the item (forms). Nothing changed — call recreate(spec) to replace it (destructive)
Violation Constraint only: existing data violates it — a key the constraint covers repeats. Includes a bounded sample of the conflicting values, each with the nodes that hold it: their engine id (elementId), and their @NodeId when the constraint is declared on a fragment, as many nodes per value as violationSampleSize. A refusal the data does not bear out (no key repeats) is thrown, not reported as a Violation
BlockedByIndex Constraint only: a plain range index over the same label and properties is in the way of it, and is named. Neo4j alone refuses a constraint for one, since its constraint brings its own index. Nothing changed — drop the index, or ensure with replaceBlockingIndex = true (drivine.schema.replace-blocking-index=true), which drops it and makes the constraint
Recreated From an explicit recreate(spec) — old item dropped, new one created. On Oracle, Postgres and SQLite a uniqueness constraint is built anew beside its old index, which is dropped only once the new one is built (below)

An item that ignores case (ignoreCase = true) is made over the lowercased copy a @CaseInsensitive field keeps, and ensure fills the copy in first: see @CaseInsensitive. The object managers keep the copy in step, but Cypher you send yourself does not: set the copy in the same statement, or recreate the item afterwards to fill it in again (how).

ensure waits for its indexes to be built on Neo4j and FalkorDB. Both build an index in the background, so ensure and recreate return only once the index they made, or found there, is built: on Neo4j they wait with db.awaitIndex, and on FalkorDB they poll db.indexes() until the index is OPERATIONAL. The wait is at most 60 seconds (drivine.schema.index-build-timeout for the SchemaManager; Neo4j’s procedure counts whole seconds, so a timeout is rounded up to them, and only a zero timeout checks once). Called on persistenceManager.indexes, a build that failed, or one still running when the wait runs out, is thrown as a DrivineException that names the index and gives the engine’s reason. Applied by the SchemaManager (enforce, recreateAll, ensure), it is a problem of its owner, as a drift is: under FAIL_FAST it fails the owner, after the owner’s other items are applied, and under WARN it is logged at WARN as BUILD FAILED or STILL BUILDING, with the index’s name and the engine’s reason, and the rest goes on; the item is reported as what was done (Created, AlreadyMatching or Recreated) and counted among those needing attention, and the index is left as it is, to finish building or to stay failed, and waited for again on the next apply. On FalkorDB an index that is still being built answers queries with only part of the rows. Waiting fixes that for the indexes Drivine’s own ensure makes, but an index made some other way, by another process or by Cypher you send yourself, can still answer with part of the rows until FalkorDB lists it as OPERATIONAL.

A TEXT index is Neo4j’s. TextIndexSpec (or @TextIndex on a property) makes CREATE TEXT INDEX … FOR (n:Label) ON (n.prop). That index is over one string property and serves CONTAINS, STARTS WITH and ENDS WITH on it. It is not a full-text index, and Neo4j lists, drift-checks and drops it as a kind of its own (SchemaItemKind.TEXT_INDEX), so the two can stand on the same property. No other engine has one. There, ensure, recreate, find and drop throw a DrivineException before anything runs, rather than make another index in its place, and applying a catalog that declares one fails for its owner. Mind the names: what Memgraph calls a TEXT INDEX is a full-text index, declared with FullTextIndexSpec / @FullTextIndex.