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_tuplestage theirRelationTuplerows on the session; commit it yourself (aStoredoes, for the engine it drives). That is the opposite ofStore, which commits every write.SetEngine(session, store_id, schema=None, *, object_wildcard_shapes=frozenset(), ops=RoaringSets, ruleset=None)opens the set engine onstore_idand replays its stored tuples.schemais OpenFGA DSL text. It may be left out for a store aStorecreated: the engine then reads the saved schema and its object-wildcard shapes. Leaving it out for a store with no saved schema raisesTypeError.opsselects the set representation (RoaringSetsorPySets);rulesetis an already-compiledRuleSetfor 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
Trueif it was added,Falseif it was already stored (nothing changes).subject_predicateis"..."for a bare subject. Does not commit. A tuple the schema does not admit raisesAdmissionRejected(aValueError) before anything changes. On a store that aStoreor aTupleSourcegoverns, it raisesUnloggedWriteRefused.
- 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(aValueError) before anything changes. On a store that aStoreor aTupleSourcegoverns, it raisesUnloggedWriteRefused.
- lookup_reverse(relation, object_type, object_name) SetLookupResult¶
Every subject that has
relationon the object. Concrete subjects are innode_ids; a subject wildcard grant is a(type, predicate)marker, never listed subject by subject; subjects a marker covers but that are refused are inexcluded_node_ids. Read the ids withresult_keys()andexcluded_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, uselookup_reverse()withresult_keys()andexcluded_keys(), orzanzibar.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"), wheresubject_predicateis"..."for a bare subject or a relation for a userset ("member", "group", "eng").Evaluated on demand over the stored tuples,
and/but notincluded. It reads the engine’s in-memory state only and issues no database query.
- filter_result(res, entity_type=None, predicate=None)¶
A copy of a
lookup/lookup_reverseresult 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. Fromlookupthe entries are objects (predicateis the relation); fromlookup_reversethey are subjects (predicateis"..."for a bare subject such as a user, or the relation of a userset). BothNonereturnsresitself. 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 asresult_keysapplies (StaleLookupIds); the copy keeps the result’sid_stamp.- Parameters:
res (SetLookupResult)
entity_type (str | None)
predicate (str | None)
- Return type:
- result_keys(res)¶
Translate
res.node_idsto 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 raisesStaleLookupIds.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), andexcluded_keys(res)– the subjects a marker covers butcheckrefuses. Sending a marker without its exclusions over-grants: underdefine viewer: [user:*] but not bannedwith auser:*grant andmallorybanned,lookup_reverseis the('user', '...')marker with mallory excluded, andresult_keysalone isset().
- excluded_keys(res)¶
Translate
res.excluded_node_idsto stable(type, name, predicate)keys: the portable form of “every subject of a marker’s shape EXCEPT these”. The companion ofresult_keys; a caller expandingres.markersmust subtract these.
- 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 (underbut not/and). The cost is proportional to what the subject can reach, not to the store size; every result is confirmed bycheck, so the answer is exact.- Parameters:
- Return type:
- class zanzibar.setengine.SetLookupResult(node_ids=set(), markers=set(), excluded_node_ids=set(), implicit_keys=set(), implicit_excluded_keys=set())¶
What
SetEngine.lookup()andSetEngine.lookup_reverse()return. A dataclass: build one by keyword and compare with==.node_idsandexcluded_node_idsare ids local to the engine instance that produced the result. Translate them withSetEngine.result_keys()andSetEngine.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 raiseStaleLookupIds.- markers: set[tuple[str, str]]¶
Wildcard grants as
(type, predicate)pairs. Unlikezanzibar.LookupResult.markers, these have no third field.
- excluded_node_ids: set[int]¶
Ids a marker would cover but that are refused (by a
but notor anand).
- 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 (predicateis"..."for a bare subject).
- neg¶
Ids a shape in
starscovers that are not members.
Stored tuples¶
- class zanzibar.setengine.RelationTuple¶
A row of the
zanzibar_relation_tupletable: one currently stored tuple.subject_predicateis'...'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.
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.