Supported environments¶
Python¶
Python 3.13 or later (requires-python = ">=3.13"). 3.13 is the version the library is
tested on. The package is pure Python and declares no operating-system restriction.
The distribution is zanzibar-index; the import name is zanzibar.
Dependencies¶
package |
required version |
why |
|---|---|---|
|
|
the table models and sessions; brings SQLAlchemy |
|
|
the compressed bitmaps behind the default set representation |
Extras¶
extra |
adds |
for |
|---|---|---|
|
|
connecting to PostgreSQL |
|
|
running the library’s test suite, in a source checkout of the repository (the tests are not in the published package) |
|
|
building this documentation, in a source checkout of the repository. New in 0.1.0; 0.0.5 has no |
pip install zanzibar-index # SQLite
pip install "zanzibar-index[postgres]" # adds the PostgreSQL driver
Databases¶
SQLite and PostgreSQL only. No other database is supported, MySQL included.
PostgreSQL¶
The session’s isolation level must be
READ COMMITTED, PostgreSQL’s default. Any other level,SERIALIZABLEandREPEATABLE READincluded, is refused when the store opens, withzanzibar.UnsafeIsolationLevel: under a level that pins a read snapshot, a committed write can stay invisible to the index’s catch-up, and a freshness token would then certify a stale answer. If you set a level, set this one:create_engine(url, isolation_level="READ COMMITTED").Writers to one store are serialised by a row lock (
SELECT ... FOR UPDATE), so several processes or machines may open the same store id.The
postgresextra installspsycopg2, so the URL startspostgresql+psycopg2://(orpostgresql://, SQLAlchemy’s default for that driver).
See Run on PostgreSQL.
SQLite¶
There is no isolation level to check; SQLite serialises writers itself. A concurrent writer can get
database is lockedand must retry. The store raises the connection’s busy timeout to at least 10 seconds before it takes its write lock, so a waiting writer first waits that long.The connection must not be in autocommit mode, because the store’s write lock would be released the moment it was taken. Opening a store on such a connection raises
zanzibar.WriteLockUnsafe, whose message names the fix. The Pythonsqlite3default, which is whatcreate_engine("sqlite://...")gives you, is fine.A file whose tables were created by a 0.0.x release is refused with
zanzibar.TablesOutdated; see Database tables.
from sqlmodel import SQLModel, Session, create_engine
import zanzibar
import zanzibar.store # registers the zanzibar_* tables before create_all
SCHEMA = "type user\ntype doc\n relations\n define viewer: [user]\n"
ok = create_engine("sqlite://") # the default: fine
SQLModel.metadata.create_all(ok)
with Session(ok) as session:
store = zanzibar.Store(session, "env-demo", schema=SCHEMA)
store.add_tuple("user:alice", "viewer", "doc:1")
print(store.check("user:alice", "viewer", "doc:1")) # -> True
autocommit = create_engine("sqlite://", connect_args={"isolation_level": None})
SQLModel.metadata.create_all(autocommit)
with Session(autocommit) as session:
try:
zanzibar.Store(session, "env-demo", schema=SCHEMA)
except zanzibar.WriteLockUnsafe:
refused = True
print(refused) # -> True
Threads and processes¶
Give each
Storeits ownSession, and use oneSessionper thread. Never share aStorebetween threads.Each
Storekeeps the set engine’s state in memory, rebuilt from the stored tuples when it opens. Several instances, in threads, processes or on other machines, may serve the same store id.The API is synchronous: there are no
asyncmethods. From an asyncio application, run the calls in a worker thread that creates and owns its ownSessionandStore, not on the event loop.
Status and license¶
development status |
|
license |
Apache-2.0 ( |