Use the backends directly

A zanzibar.Store is three parts kept in step for you: a tuple log, the set engine (zanzibar.setengine.SetEngine) and the graph index (zanzibar.graphindex.WildcardIndex). Every write goes into the log, and the store then applies it to both backends in the right order. Both backends are public, and you can build either one on its own. This page shows how to use the set engine on its own, and warns against writing to the graph index on its own.

In an application, prefer the Store, with one exception. Open a set engine without a Store for tooling, experiments, and write-heavy loads where the graph index is the wrong trade (see When not to use this library). Never write to the graph index without a Store: driving it yourself means doing by hand what the Store does on every write, and a skipped step fails silently, so the index then gives wrong answers with no error. The two backends explains how they differ.

Decide whether you need this

Use the set engine on its own when you want answers computed from stored tuples and you do not need the graph index, the tuple log or freshness tokens. Examples are a test, a script that evaluates a model against some tuples, a one-off analysis, or a load where writes far outnumber reads, so the graph index’s write-time cost does not pay off. Keep in mind:

  • It never commits. add_tuple and remove_tuple stage rows on your session, and you commit it.

  • Its state lives in memory. Opening one reads every stored tuple of the store, so opening costs time and memory in proportion to the store.

  • Each instance has its own copy of that state. An engine does not see tuples that another engine instance wrote after it was opened, until you call its rebuild().

  • It takes the six-part tuple form only ("...", "user", "alice", "viewer", "doc", "1"). The "user:alice" form is a Store feature.

Do not write to the graph index on its own; see the last section.

Use the set engine on its own

Create the tables, open an engine on a store id with a schema, write, commit, and read:

SQLModel.metadata.create_all creates only the tables of the modules imported so far, so import zanzibar.store first. It brings in every table the library uses, which you will need if you later move to a Store or build a graph index in the same database.

from sqlmodel import SQLModel, Session, create_engine
import zanzibar.store                            # registers every zanzibar_* table
from zanzibar.setengine import SetEngine

SCHEMA = """
model
  schema 1.1

type user

type doc
  relations
    define banned: [user]
    define viewer: [user] but not banned
    define can_view: viewer
"""

engine = create_engine("sqlite://")
SQLModel.metadata.create_all(engine)

session = Session(engine)
se = SetEngine(session, "notes", SCHEMA)
se.add_tuple("...", "user", "alice", "viewer", "doc", "1")
se.add_tuple("...", "user", "bob", "viewer", "doc", "1")
se.add_tuple("...", "user", "bob", "banned", "doc", "1")
session.commit()                                  # the engine never commits for you

print(se.check("...", "user", "alice", "can_view", "doc", "1"))  # -> True
print(se.check("...", "user", "bob", "can_view", "doc", "1"))  # -> False

lookup and lookup_reverse return a zanzibar.setengine.SetLookupResult, which holds ids that only this engine instance can translate. Turn it into (type, name, predicate) keys with result_keys (and excluded_keys for the subjects a wildcard grant leaves out) before you keep it or send it anywhere:

viewers = se.lookup_reverse("can_view", "doc", "1")
users = sorted(k for k in se.result_keys(viewers) if k[0] == "user")
print(users)  # -> [('user', 'alice', '...')]

A key is (type, name, predicate), which is not the order check takes its subject in (subject_predicate, subject_type, subject_name); unpack it by name before passing it back. See Result keys are in a different order.

Translate a result straight away. After a later write that removes a tuple, after a rebuild(), or on another engine instance, result_keys refuses an older result with zanzibar.StaleLookupIds rather than risk naming the wrong entity. Run the lookup again and translate the new result.

A second engine on the same store id, opened earlier, does not see new tuples until it rebuilds:

other = SetEngine(Session(engine), "notes", SCHEMA)

se.add_tuple("...", "user", "carol", "viewer", "doc", "1")
session.commit()

print(other.check("...", "user", "carol", "viewer", "doc", "1"))  # -> False
other.rebuild()
print(other.check("...", "user", "carol", "viewer", "doc", "1"))  # -> True

For Expand, prefer zanzibar.Store.expand(), which returns (type, name, predicate) keys. SetEngine.expand returns a zanzibar.setengine.MemberSet, whose ids are handles that only this engine instance can translate, with the same caveat as a SetLookupResult.

Move to a Store later

A store id whose tuples a standalone set engine wrote has no tuple log, so a Store refuses to open it, with zanzibar.UnloggedTuplesPresent. Adopt it instead: zanzibar.Store.adopt() saves the schema, builds the graph index from the stored tuples and opens the store, in one transaction.

from zanzibar import Store, UnloggedTuplesPresent

refused = False
try:
    Store(session, "notes", schema=SCHEMA)
except UnloggedTuplesPresent:
    refused = True
    session.rollback()
print(refused)  # -> True

store, report = Store.adopt(session, "notes", schema=SCHEMA)

print(store.check("user:carol", "can_view", "doc:1"))  # -> True

From then on the store id belongs to the Store. A SetEngine on it can still read, but its add_tuple and remove_tuple raise zanzibar.UnloggedWriteRefused, because a write that skips the log would leave the graph index behind for good. Write through the Store. Bulk-load or adopt an existing store covers adoption in more detail.

from zanzibar import UnloggedWriteRefused

refused = False
try:
    se.add_tuple("...", "user", "dave", "viewer", "doc", "1")
except UnloggedWriteRefused:
    refused = True
    session.rollback()
print(refused)  # -> True

store.add_tuple("user:dave", "viewer", "doc:1")

print(store.check("user:dave", "can_view", "doc:1"))  # -> True
session.close()

Do not drive the graph index on its own

The graph index (zanzibar.graphindex.WildcardIndex) is public so that you can study or measure it. Do not write to it directly from an application; use a Store.

  • On a schema with and or but not, the index refuses a write on the boolean relation’s own name (here viewer) with a ValueError. A write goes onto the relation’s internal parts, and only zanzibar.graphindex.DeltaProcessor.run_cascade(), in the same transaction, brings the relation in line with it.

  • Committing a transaction that wrote to the index on such a schema and ran no cascade after that write raises zanzibar.CascadeNotRun; the transaction stays open: roll back, or run the cascade and commit again.

  • The check proves a cascade ran, not that it ran with the right watermark: pass the outbox_watermark read before the first write. A refused write (for example zanzibar.ClosureFanoutExceeded) still leaves the transaction owing a cascade or a rollback.

  • A Store does all of this for you, in the write (sync=True) or in zanzibar.Store.catch_up() (sync=False).

Errors only the backends raise

You meet these only when you drive the backends or the offline index tools yourself: a Store’s reads and writes never raise them (Store.adopt, which runs the index build, can raise the last one). All three are zanzibar.ZanzibarErrors; their attributes are in the exceptions reference.