storage_engine library

For authors of persistence engines: the synchronous PersistenceStorageEngine interface, its record types, and databasePersistence, which turns a PersistenceStorageEngineOpener into the DatabasePersistence a FirebaseDatabase takes. The client calls the opener once at start with the instance's PersistenceKey and its logger (null when the instance has none), through which the engine reports lease, degraded-mode and storage failures.

Path, ChildKey, Node, CompoundWrite, QuerySpec and PruneForest are the ports of Android's core types that the interface needs; they are exported only here, never from the main library. Index, KeyIndex and NamedNode are exported because Node and QuerySpec name them. nodeFromJson, keyIndex, ImmutableTree and estimateSerializedNodeSize are the helpers an engine needs to read, walk and size stored data. An engine stores a query as its path and QuerySpec.toWireObject(), and rebuilds it with QuerySpec.fromPathAndWireObject. InMemoryPersistenceStorageEngine is the reference engine; the SQLite engine is checked against it. persistenceDatabaseUrl turns a database URL into the key the client puts in PersistenceKey.databaseUrl (a key gives itself), for an engine function that deletes a cache by URL.

final FirebaseDatabase db = FirebaseDatabase(
  app,
  persistence: databasePersistence(
    opener: (PersistenceKey key, {DatabaseLogger? logger}) async => MyEngine.open(key, logger),
    cacheSizeBytes: 20 * 1024 * 1024,
  ),
);

Classes

ChildKey
The name of a child; integer-like names (within int32) carry intValue and sort numerically before every other name.
CompoundWrite
An immutable tree of complete writes (nodes) at paths; a write shadows writes below it.
ImmutableTree<T>
An immutable tree with an optional value at every node and children by ChildKey.
Index
An ordering of children: by priority (default), key, value, or a child path.
InMemoryPersistenceStorageEngine
Keeps everything in maps; a transaction that ends without setTransactionSuccessful is rolled back. Mutations outside a transaction throw a StateError unless disableTransactionCheck.
KeyIndex
Orders children by key.
NamedNode
A Node together with its name; the element of index sorted maps and of query bounds ("posts").
Node
An immutable database value: a leaf or a set of children, each with a priority.
Path
An immutable path of ChildKey segments, such as /users/ada.
PersistenceKey
Identifies the cache of one database of one app: the engine derives its file or store name from it.
PersistenceStorageEngine
A synchronous store of user writes, the server cache tree, tracked queries and their keys.
PruneForest
A tree of prune (true) and keep (false) marks; a mark applies to its whole subtree unless a deeper mark overrides it.
QuerySpec
The identity of a query in the sync tree and in persistence.
TrackedQuery
A persisted record of a query: id, querySpec, lastUse (ms), complete, active.
UserWriteRecord
A user overwrite (with overwrite) or merge (with merge) at path, as kept in the write tree and in persistence.

Constants

keyIndex → const KeyIndex
The single instance.

Functions

databasePersistence({required PersistenceStorageEngineOpener opener, int? cacheSizeBytes, bool cacheServerData = true}) → DatabasePersistence
Makes the persistence option of an engine package: the client opens the engine with opener and prunes the server cache above cacheSizeBytes (null: 10 MiB).
estimateSerializedNodeSize(Node node) → int
The estimated size in bytes of node serialised as JSON (decides whether a listen sends a compound hash).
nodeFromJson(Object? json, [Object? priority]) → Node
The node of json (export form accepted: .priority, .value; other keys starting with . ignored), with priority when given.
persistenceDatabaseUrl(Uri url) → Uri
The URL under which a FirebaseDatabase keys the persisted cache of the database at url (PersistenceKey.databaseUrl, FirebaseDatabase.databaseUrl). A key the client produced gives itself.

Typedefs

PersistenceStorageEngineOpener = Future<PersistenceStorageEngine> Function(PersistenceKey key, {DatabaseLogger? logger})
Opens the engine for key; called by the client once at Repo start, before the Repo's first operation runs (taking a lease may wait, hence the future). An engine that cannot be opened durably still completes, with isDurable false; the future fails only on programming errors. The lease, degraded mode and storage failures are reported through logger (null: dropped).