SqliteReplicator class final

Ships an SQLite database in WAL mode to a Disk, as Litestream does (Blueprint #30): a page-exact snapshot, then the committed frames of the write-ahead log as they appear, so a restore loses at most what was committed since the last sync.

Two rules keep a replica correct whatever the application and SQLite do meanwhile:

  • Only frames SQLite itself would trust are shipped: the salts of the current log, an unbroken cumulative checksum, and whole committed transactions.
  • When the log was restarted or truncated and the replicator cannot prove it read every frame of the old one, it starts a generation: a new snapshot, through SQLite's backup API (page-exact, so the log's frames still apply), taken between two reads of the log header that find the same log.

Between syncs it holds a read transaction, which stops SQLite from restarting the log under it; when the log grows past checkpointPages, it checkpoints (RESTART) right after catching up, and the next sync reads the few frames written in between from the old log's tail. A database that has no log yet gets one write into the _daravel_replication table, as Litestream does with its own table.

Constructors

SqliteReplicator(String databasePath, Disk disk, {String prefix = '', int checkpointPages = 1000, Duration snapshotInterval = const Duration(hours: 24), int retain = 2, DateTime clock()?})
Replicates the SQLite database at databasePath to disk.

Properties

checkpointPages → int
Log size (in frames) after which the replicator checkpoints.
final
databasePath → String
The database file.
final
disk → Disk
The disk holding the replica.
final
generation → String?
The current generation, once the first sync started one.
no setter
hashCode → int
The hash code for this object.
no setterinherited
prefix → String
Where on disk the replica lives.
final
retain → int
Generations kept on the disk, the current one included.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
snapshotInterval → Duration
A generation older than this is replaced by a fresh snapshot, which bounds how many segments a restore applies.
final

Methods

close() → void
Releases the database.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
start({Duration interval = const Duration(seconds: 1), void onSync(ReplicationSync sync)?, void onError(Object error, StackTrace stack)?}) → Timer
Syncs every interval until stop. Errors go to onError and the next tick tries again (frames not shipped stay in the log).
sync() → Future<ReplicationSync>
Ships what was committed since the last call; starts a generation when needed. Not reentrant: run syncs one after the other.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited