zanzibar.setengine

The set engine stores only the raw tuples and computes every answer when it is asked, with bitmap algebra. A zanzibar.Store keeps one beside the graph index; use this package directly only to run the set engine on its own.

The engine

class zanzibar.setengine.SetEngine(session, store_id, schema=None, *, object_wildcard_shapes=frozenset({}), ops=SetOps('roaring'), ruleset=None)

Raw-tuple, bitmap-backed reachability engine (storage, writes and reads).

State is held in memory: opening the engine replays every stored tuple of the store into bitmaps, so opening costs time and memory in proportion to the store.

The caller owns the transaction: this class never commits. add_tuple / remove_tuple stage their RelationTuple rows on the session; commit it yourself (a Store does, for the engine it drives). That is the opposite of Store, which commits every write.

SetEngine(session, store_id, schema=None, *, object_wildcard_shapes=frozenset(), ops=RoaringSets, ruleset=None) opens the set engine on store_id and replays its stored tuples. schema is OpenFGA DSL text. It may be left out for a store a Store created: the engine then reads the saved schema and its object-wildcard shapes. Leaving it out for a store with no saved schema raises TypeError. ops selects the set representation (RoaringSets or PySets); ruleset is an already-compiled RuleSet for the same schema, to skip compiling it again.

Parameters:
add_tuple(subject_predicate, subject_type, subject_name, relation, object_type, object_name) → bool

Add one raw tuple. Returns True if it was added, False if it was already stored (nothing changes). subject_predicate is "..." for a bare subject. Does not commit. A tuple the schema does not admit raises AdmissionRejected (a ValueError) before anything changes. On a store that a Store or a TupleSource governs, it raises UnloggedWriteRefused.

remove_tuple(subject_predicate, subject_type, subject_name, relation, object_type, object_name) → None

Remove one raw tuple. Does not commit. A tuple that is not stored, or one the schema does not admit, raises AdmissionRejected (a ValueError) before anything changes. On a store that a Store or a TupleSource governs, it raises UnloggedWriteRefused.

lookup_reverse(relation, object_type, object_name) → SetLookupResult

Every subject that has relation on the object. Concrete subjects are in node_ids; a subject wildcard grant is a (type, predicate) marker, never listed subject by subject; subjects a marker covers but that are refused are in excluded_node_ids. Read the ids with result_keys() and excluded_keys().

expand(relation, o_type, o_name) → MemberSet

The member set of the relation on the object, as a MemberSet. Its ids are the engine’s internal handles and are not checked for staleness; to get keys, use lookup_reverse() with result_keys() and excluded_keys(), or zanzibar.Store.expand().

rebuild()

Replay the RelationTuple table into fresh in-memory state.

Return type:

None

check(subject_predicate, subject_type, subject_name, relation, object_type, object_name)

Is the subject related to the object? check("...", "user", "alice", "viewer", "doc", "d1"), where subject_predicate is "..." for a bare subject or a relation for a userset ("member", "group", "eng").

Evaluated on demand over the stored tuples, and / but not included. It reads the engine’s in-memory state only and issues no database query.

Parameters:
  • subject_type (str)

  • subject_name (str)

  • relation (str)

  • object_type (str)

  • object_name (str)

Return type:

bool

filter_result(res, entity_type=None, predicate=None)

A copy of a lookup / lookup_reverse result keeping only the entries of one type and/or predicate: every id, marker, excluded id and implicit key whose (type, predicate) matches every filter given. From lookup the entries are objects (predicate is the relation); from lookup_reverse they are subjects (predicate is "..." for a bare subject such as a user, or the relation of a userset). Both None returns res itself. An exclusion always has its marker’s shape, so the filtered result still reads as “keys, plus every subject of a marker’s shape, minus the excluded keys”. The ids are classified through this instance’s id map, so the same staleness rule as result_keys applies (StaleLookupIds); the copy keeps the result’s id_stamp.

Parameters:
Return type:

SetLookupResult

result_keys(res)

Translate res.node_ids to their stable (type, name, predicate) surrogate keys. Interner ids are recycled instance-local handles – valid against THIS instance until an id is freed (a remove, or a catch-up applying another instance’s remove) or the engine is rebuilt; after that, or on another instance, this raises StaleLookupIds.

This is the CONCRETE third of a lookup result only. A result that crosses a service boundary is three parts: these keys, res.markers (already portable (type, predicate) shapes), and excluded_keys(res) – the subjects a marker covers but check refuses. Sending a marker without its exclusions over-grants: under define viewer: [user:*] but not banned with a user:* grant and mallory banned, lookup_reverse is the ('user', '...') marker with mallory excluded, and result_keys alone is set().

Parameters:

res (SetLookupResult)

Return type:

set[tuple[str, str, str]]

excluded_keys(res)

Translate res.excluded_node_ids to stable (type, name, predicate) keys: the portable form of “every subject of a marker’s shape EXCEPT these”. The companion of result_keys; a caller expanding res.markers must subtract these.

Parameters:

res (SetLookupResult)

Return type:

set[tuple[str, str, str]]

lookup(subject_predicate, subject_type, subject_name)

Everything the subject can reach, found by a reverse walk from the subject.

Returns a SetLookupResult: the concrete objects the subject has a relation to, plus intensional (type, relation) markers meaning “every object of that type”, each with the objects it excludes (under but not / and). The cost is proportional to what the subject can reach, not to the store size; every result is confirmed by check, so the answer is exact.

Parameters:
  • subject_type (str)

  • subject_name (str)

Return type:

SetLookupResult

class zanzibar.setengine.SetLookupResult(node_ids=set(), markers=set(), excluded_node_ids=set(), implicit_keys=set(), implicit_excluded_keys=set())

What SetEngine.lookup() and SetEngine.lookup_reverse() return. A dataclass: build one by keyword and compare with ==.

node_ids and excluded_node_ids are ids local to the engine instance that produced the result. Translate them with SetEngine.result_keys() and SetEngine.excluded_keys() on that same instance, before the next write: once any id has been freed (by a remove, or a catch-up that applies one), the engine has been rebuilt, or the result came from another instance, both raise StaleLookupIds.

node_ids: set[int]

The concrete results.

markers: set[tuple[str, str]]

Wildcard grants as (type, predicate) pairs. Unlike zanzibar.LookupResult.markers, these have no third field.

excluded_node_ids: set[int]

Ids a marker would cover but that are refused (by a but not or an and).

implicit_keys: set[tuple[str, str, str]]

Results that have no id, as (type, name, predicate) keys: for example doc:1#viewer is a viewer of doc:1 even on an empty store. SetEngine.result_keys() includes them.

implicit_excluded_keys: set[tuple[str, str, str]]

Exclusions that have no id, as keys. SetEngine.excluded_keys() includes them.

class zanzibar.setengine.MemberSet

What SetEngine.expand() returns: a set of members that can include “every subject of a shape”. A frozen dataclass. The ids in it are the engine’s internal handles.

pos

Ids of the members listed one by one.

stars: frozenset[tuple[str, str]]

(type, predicate) shapes of which every subject is a member (predicate is "..." for a bare subject).

neg

Ids a shape in stars covers that are not members.

ipos: frozenset[tuple[str, str, str]]

Members that have no id, as (type, name, predicate) keys.

ineg: frozenset[tuple[str, str, str]]

Keys a shape in stars covers that are not members.

contains_star(shape) → bool

Is shape in stars?

contains_entity(uid, utype) → bool

Is the bare subject with id uid and type utype a member?

contains_userset(uid, shape) → bool

Is the userset with id uid and shape shape a member?

contains_key(key) → bool

Is the (type, name, predicate) key, which has no id, a member: in ipos, or covered by a shape in stars and not in ineg?

Stored tuples

class zanzibar.setengine.RelationTuple

A row of the zanzibar_relation_tuple table: one currently stored tuple. subject_predicate is '...' for a bare subject. Columns: id, store_id, subject_predicate, subject_type, subject_name, relation, object_type, object_name, created_at. See the Database tables page.

Set implementations

class zanzibar.setengine.SetOps

The set representation the engine uses for its id sets. Pass one of the two instances below as ops; answers are the same with either.

name: str

'roaring' or 'py'.

zanzibar.setengine.RoaringSets: SetOps

Compressed bitmaps (pyroaring). The default.

zanzibar.setengine.PySets: SetOps

Python’s built-in set and frozenset.

Exceptions

zanzibar.setengine exports no exceptions. Import them from zanzibar; they are documented on the Exceptions page. The set engine raises, among others, AdmissionRejected, SchemaMismatch, PersistedSchemaRefused, UnloggedWriteRefused and StaleLookupIds.