@rocicorp/zero 1.12.0-canary.7 → 1.12.0-canary.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/out/replicache/src/btree/node.d.ts +0 -2
- package/out/replicache/src/btree/node.d.ts.map +1 -1
- package/out/replicache/src/btree/node.js.map +1 -1
- package/out/replicache/src/connection-loop.d.ts.map +1 -1
- package/out/replicache/src/connection-loop.js +1 -1
- package/out/replicache/src/connection-loop.js.map +1 -1
- package/out/replicache/src/db/commit.d.ts +0 -1
- package/out/replicache/src/db/commit.d.ts.map +1 -1
- package/out/replicache/src/db/commit.js +1 -1
- package/out/replicache/src/db/commit.js.map +1 -1
- package/out/replicache/src/db/write.d.ts +0 -2
- package/out/replicache/src/db/write.d.ts.map +1 -1
- package/out/replicache/src/db/write.js +1 -1
- package/out/replicache/src/db/write.js.map +1 -1
- package/out/replicache/src/deleted-clients.d.ts +0 -2
- package/out/replicache/src/deleted-clients.d.ts.map +1 -1
- package/out/replicache/src/deleted-clients.js +1 -1
- package/out/replicache/src/deleted-clients.js.map +1 -1
- package/out/replicache/src/frozen-json.d.ts +0 -1
- package/out/replicache/src/frozen-json.d.ts.map +1 -1
- package/out/replicache/src/frozen-json.js.map +1 -1
- package/out/replicache/src/index-defs.d.ts +0 -2
- package/out/replicache/src/index-defs.d.ts.map +1 -1
- package/out/replicache/src/index-defs.js +1 -1
- package/out/replicache/src/index-defs.js.map +1 -1
- package/out/replicache/src/kv/expo-sqlite/store.d.ts +0 -1
- package/out/replicache/src/kv/expo-sqlite/store.d.ts.map +1 -1
- package/out/replicache/src/kv/expo-sqlite/store.js +1 -1
- package/out/replicache/src/kv/expo-sqlite/store.js.map +1 -1
- package/out/replicache/src/kv/idb-store.js +1 -1
- package/out/replicache/src/kv/idb-store.js.map +1 -1
- package/out/replicache/src/kv/throw-if-closed.d.ts +0 -3
- package/out/replicache/src/kv/throw-if-closed.d.ts.map +1 -1
- package/out/replicache/src/kv/throw-if-closed.js.map +1 -1
- package/out/replicache/src/persist/client-group-gc.d.ts +0 -7
- package/out/replicache/src/persist/client-group-gc.d.ts.map +1 -1
- package/out/replicache/src/persist/client-group-gc.js +1 -1
- package/out/replicache/src/persist/client-group-gc.js.map +1 -1
- package/out/replicache/src/process-scheduler.js +1 -1
- package/out/replicache/src/process-scheduler.js.map +1 -1
- package/out/replicache/src/replicache-impl.d.ts +2 -7
- package/out/replicache/src/replicache-impl.d.ts.map +1 -1
- package/out/replicache/src/replicache-impl.js +1 -1
- package/out/replicache/src/replicache-impl.js.map +1 -1
- package/out/replicache/src/sync/patch.d.ts +0 -5
- package/out/replicache/src/sync/patch.d.ts.map +1 -1
- package/out/replicache/src/sync/patch.js +1 -1
- package/out/replicache/src/sync/patch.js.map +1 -1
- package/out/replicache/src/sync/pull.d.ts +0 -1
- package/out/replicache/src/sync/pull.d.ts.map +1 -1
- package/out/replicache/src/sync/pull.js.map +1 -1
- package/out/replicache/src/sync/push.d.ts +2 -1
- package/out/replicache/src/sync/push.d.ts.map +1 -1
- package/out/replicache/src/sync/push.js.map +1 -1
- package/out/replicache/src/types.d.ts +2 -1
- package/out/replicache/src/types.d.ts.map +1 -1
- package/out/shared/src/asserts.d.ts +0 -5
- package/out/shared/src/asserts.d.ts.map +1 -1
- package/out/shared/src/asserts.js +1 -1
- package/out/shared/src/asserts.js.map +1 -1
- package/out/shared/src/browser-env.d.ts +0 -17
- package/out/shared/src/browser-env.d.ts.map +1 -1
- package/out/shared/src/browser-env.js.map +1 -1
- package/out/shared/src/deep-clone.d.ts +0 -1
- package/out/shared/src/deep-clone.d.ts.map +1 -1
- package/out/shared/src/deep-clone.js +1 -1
- package/out/shared/src/deep-clone.js.map +1 -1
- package/out/shared/src/document-visible.js +1 -1
- package/out/shared/src/document-visible.js.map +1 -1
- package/out/shared/src/expand.d.ts +0 -3
- package/out/shared/src/expand.d.ts.map +1 -1
- package/out/shared/src/immutable.d.ts +1 -1
- package/out/shared/src/immutable.d.ts.map +1 -1
- package/out/shared/src/iterables.d.ts +0 -1
- package/out/shared/src/iterables.d.ts.map +1 -1
- package/out/shared/src/iterables.js.map +1 -1
- package/out/shared/src/logging-test-utils.js +1 -1
- package/out/shared/src/logging-test-utils.js.map +1 -1
- package/out/shared/src/options-types.d.ts +1 -1
- package/out/shared/src/options-types.d.ts.map +1 -1
- package/out/shared/src/options.d.ts +2 -3
- package/out/shared/src/options.d.ts.map +1 -1
- package/out/shared/src/options.js +1 -1
- package/out/shared/src/options.js.map +1 -1
- package/out/shared/src/promise-race.js +1 -1
- package/out/shared/src/promise-race.js.map +1 -1
- package/out/shared/src/queue.js +1 -1
- package/out/shared/src/queue.js.map +1 -1
- package/out/shared/src/resolved-promises.d.ts +0 -4
- package/out/shared/src/resolved-promises.d.ts.map +1 -1
- package/out/shared/src/resolved-promises.js +0 -1
- package/out/shared/src/resolved-promises.js.map +1 -1
- package/out/shared/src/resolver.d.ts +9 -0
- package/out/shared/src/resolver.d.ts.map +1 -0
- package/out/shared/src/resolver.js +6 -0
- package/out/shared/src/resolver.js.map +1 -0
- package/out/shared/src/sleep.js +1 -1
- package/out/shared/src/sleep.js.map +1 -1
- package/out/shared/src/tdigest.d.ts +0 -1
- package/out/shared/src/tdigest.d.ts.map +1 -1
- package/out/shared/src/tdigest.js.map +1 -1
- package/out/z2s/src/sql.d.ts +1 -5
- package/out/z2s/src/sql.d.ts.map +1 -1
- package/out/z2s/src/sql.js.map +1 -1
- package/out/zero/package.js +2 -3
- package/out/zero/package.js.map +1 -1
- package/out/zero/src/zero-cache-dev.js +1 -1
- package/out/zero/src/zero-cache-dev.js.map +1 -1
- package/out/zero-cache/src/config/zero-config.d.ts +14 -11
- package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
- package/out/zero-cache/src/config/zero-config.js +23 -0
- package/out/zero-cache/src/config/zero-config.js.map +1 -1
- package/out/zero-cache/src/db/migration-lite.js.map +1 -1
- package/out/zero-cache/src/db/migration.js +1 -1
- package/out/zero-cache/src/db/migration.js.map +1 -1
- package/out/zero-cache/src/db/pg-copy-binary.js +1 -1
- package/out/zero-cache/src/db/pg-copy-binary.js.map +1 -1
- package/out/zero-cache/src/db/pg-type-parser.js.map +1 -1
- package/out/zero-cache/src/db/specs.d.ts +3 -4
- package/out/zero-cache/src/db/specs.d.ts.map +1 -1
- package/out/zero-cache/src/db/specs.js +5 -7
- package/out/zero-cache/src/db/specs.js.map +1 -1
- package/out/zero-cache/src/db/transaction-pool.js +2 -2
- package/out/zero-cache/src/db/transaction-pool.js.map +1 -1
- package/out/zero-cache/src/observability/events.js +1 -1
- package/out/zero-cache/src/observability/events.js.map +1 -1
- package/out/zero-cache/src/server/change-streamer.js +1 -1
- package/out/zero-cache/src/server/change-streamer.js.map +1 -1
- package/out/zero-cache/src/server/main.js +1 -1
- package/out/zero-cache/src/server/main.js.map +1 -1
- package/out/zero-cache/src/server/runner/run-worker.js +1 -1
- package/out/zero-cache/src/server/runner/run-worker.js.map +1 -1
- package/out/zero-cache/src/services/change-source/common/backfill-manager.js +1 -1
- package/out/zero-cache/src/services/change-source/common/backfill-manager.js.map +1 -1
- package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js +1 -1
- package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js.map +1 -1
- package/out/zero-cache/src/services/change-source/common/replica-restore.js.map +1 -1
- package/out/zero-cache/src/services/change-source/custom/change-source.js +1 -1
- package/out/zero-cache/src/services/change-source/custom/change-source.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/backfill-stream.js +1 -1
- package/out/zero-cache/src/services/change-source/pg/backfill-stream.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/change-source.js +1 -1
- package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/initial-sync.js +1 -1
- package/out/zero-cache/src/services/change-source/pg/initial-sync.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/lsn.d.ts +0 -2
- package/out/zero-cache/src/services/change-source/pg/lsn.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/lsn.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/replication-slots.js +1 -1
- package/out/zero-cache/src/services/change-source/pg/replication-slots.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/ddl.d.ts +0 -12
- package/out/zero-cache/src/services/change-source/pg/schema/ddl.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/ddl.js +5 -3
- package/out/zero-cache/src/services/change-source/pg/schema/ddl.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.d.ts +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/validation.d.ts +0 -1
- package/out/zero-cache/src/services/change-source/pg/schema/validation.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/validation.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/backup-monitor.js +1 -1
- package/out/zero-cache/src/services/change-streamer/backup-monitor.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/broadcast.js +1 -1
- package/out/zero-cache/src/services/change-streamer/broadcast.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer-http.js +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer-http.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer.js +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/drain-tracker.js +1 -1
- package/out/zero-cache/src/services/change-streamer/drain-tracker.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/litestream3-prometheus-poller.js +2 -2
- package/out/zero-cache/src/services/change-streamer/litestream3-prometheus-poller.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/restore-admission.js +2 -2
- package/out/zero-cache/src/services/change-streamer/restore-admission.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/schema/tables.js +1 -1
- package/out/zero-cache/src/services/change-streamer/schema/tables.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/snapshot-message.js +2 -3
- package/out/zero-cache/src/services/change-streamer/snapshot-message.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/sqlite-change-log-catchup.js +2 -2
- package/out/zero-cache/src/services/change-streamer/sqlite-change-log-catchup.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/sqlite-change-log-read-router.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/storer.js +1 -1
- package/out/zero-cache/src/services/change-streamer/storer.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/subscribe.js +1 -1
- package/out/zero-cache/src/services/change-streamer/subscribe.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/subscriber.js +1 -1
- package/out/zero-cache/src/services/change-streamer/subscriber.js.map +1 -1
- package/out/zero-cache/src/services/life-cycle.js +1 -1
- package/out/zero-cache/src/services/life-cycle.js.map +1 -1
- package/out/zero-cache/src/services/litestream/commands.js +1 -1
- package/out/zero-cache/src/services/litestream/commands.js.map +1 -1
- package/out/zero-cache/src/services/litestream/metrics.js.map +1 -1
- package/out/zero-cache/src/services/mutagen/mutagen.js +2 -2
- package/out/zero-cache/src/services/mutagen/mutagen.js.map +1 -1
- package/out/zero-cache/src/services/mutagen/pusher.js +57 -14
- package/out/zero-cache/src/services/mutagen/pusher.js.map +1 -1
- package/out/zero-cache/src/services/plannerz.d.ts +4 -12
- package/out/zero-cache/src/services/plannerz.d.ts.map +1 -1
- package/out/zero-cache/src/services/plannerz.js +1 -3
- package/out/zero-cache/src/services/plannerz.js.map +1 -1
- package/out/zero-cache/src/services/replicator/change-log-db.js +1 -1
- package/out/zero-cache/src/services/replicator/change-log-db.js.map +1 -1
- package/out/zero-cache/src/services/replicator/change-log-stream-writer.js +1 -1
- package/out/zero-cache/src/services/replicator/change-log-stream-writer.js.map +1 -1
- package/out/zero-cache/src/services/replicator/drain-handler.js +1 -1
- package/out/zero-cache/src/services/replicator/drain-handler.js.map +1 -1
- package/out/zero-cache/src/services/replicator/reporter/recorder.js +1 -1
- package/out/zero-cache/src/services/replicator/reporter/recorder.js.map +1 -1
- package/out/zero-cache/src/services/replicator/reporter/report-schema.d.ts +2 -11
- package/out/zero-cache/src/services/replicator/reporter/report-schema.d.ts.map +1 -1
- package/out/zero-cache/src/services/replicator/reporter/report-schema.js +1 -1
- package/out/zero-cache/src/services/replicator/reporter/report-schema.js.map +1 -1
- package/out/zero-cache/src/services/replicator/schema/change-log.d.ts +0 -10
- package/out/zero-cache/src/services/replicator/schema/change-log.d.ts.map +1 -1
- package/out/zero-cache/src/services/replicator/schema/change-log.js.map +1 -1
- package/out/zero-cache/src/services/replicator/schema/column-metadata.d.ts +0 -7
- package/out/zero-cache/src/services/replicator/schema/column-metadata.d.ts.map +1 -1
- package/out/zero-cache/src/services/replicator/schema/column-metadata.js +1 -1
- package/out/zero-cache/src/services/replicator/schema/column-metadata.js.map +1 -1
- package/out/zero-cache/src/services/replicator/write-worker-client.js +1 -1
- package/out/zero-cache/src/services/replicator/write-worker-client.js.map +1 -1
- package/out/zero-cache/src/services/running-state.js +1 -1
- package/out/zero-cache/src/services/running-state.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/client-handler.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/connection-context-manager.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/cvr-store.js +1 -1
- package/out/zero-cache/src/services/view-syncer/cvr-store.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/cvr.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/drain-coordinator.js +2 -2
- package/out/zero-cache/src/services/view-syncer/drain-coordinator.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/query-stats.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/row-record-cache.js +1 -1
- package/out/zero-cache/src/services/view-syncer/row-record-cache.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/schema/cvr.js +1 -1
- package/out/zero-cache/src/services/view-syncer/schema/cvr.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/schema/types.js +3 -17
- package/out/zero-cache/src/services/view-syncer/schema/types.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/snapshotter.js +1 -1
- package/out/zero-cache/src/services/view-syncer/snapshotter.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.js +13 -10
- package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
- package/out/zero-cache/src/types/lite.d.ts +0 -1
- package/out/zero-cache/src/types/lite.d.ts.map +1 -1
- package/out/zero-cache/src/types/lite.js +2 -2
- package/out/zero-cache/src/types/lite.js.map +1 -1
- package/out/zero-cache/src/types/pg-types.d.ts +0 -40
- package/out/zero-cache/src/types/pg-types.d.ts.map +1 -1
- package/out/zero-cache/src/types/pg-types.js.map +1 -1
- package/out/zero-cache/src/types/poke-chunk.js +1 -1
- package/out/zero-cache/src/types/poke-chunk.js.map +1 -1
- package/out/zero-cache/src/types/row-key.d.ts +0 -5
- package/out/zero-cache/src/types/row-key.d.ts.map +1 -1
- package/out/zero-cache/src/types/row-key.js.map +1 -1
- package/out/zero-cache/src/types/streams.js +1 -1
- package/out/zero-cache/src/types/streams.js.map +1 -1
- package/out/zero-cache/src/types/subscription.js +1 -1
- package/out/zero-cache/src/types/subscription.js.map +1 -1
- package/out/zero-cache/src/types/websocket-handoff.d.ts +0 -6
- package/out/zero-cache/src/types/websocket-handoff.d.ts.map +1 -1
- package/out/zero-cache/src/types/websocket-handoff.js +8 -2
- package/out/zero-cache/src/types/websocket-handoff.js.map +1 -1
- package/out/zero-cache/src/types/ws.d.ts +2 -1
- package/out/zero-cache/src/types/ws.d.ts.map +1 -1
- package/out/zero-cache/src/types/ws.js +1 -1
- package/out/zero-cache/src/types/ws.js.map +1 -1
- package/out/zero-cache/src/workers/connection.js +4 -1
- package/out/zero-cache/src/workers/connection.js.map +1 -1
- package/out/zero-cache/src/workers/mutator.js +1 -1
- package/out/zero-cache/src/workers/mutator.js.map +1 -1
- package/out/zero-cache/src/workers/syncer.js +45 -3
- package/out/zero-cache/src/workers/syncer.js.map +1 -1
- package/out/zero-client/src/client/active-clients-manager.d.ts.map +1 -1
- package/out/zero-client/src/client/active-clients-manager.js +1 -1
- package/out/zero-client/src/client/active-clients-manager.js.map +1 -1
- package/out/zero-client/src/client/connection-manager.js +1 -1
- package/out/zero-client/src/client/connection-manager.js.map +1 -1
- package/out/zero-client/src/client/crud.d.ts +2 -8
- package/out/zero-client/src/client/crud.d.ts.map +1 -1
- package/out/zero-client/src/client/crud.js +1 -1
- package/out/zero-client/src/client/crud.js.map +1 -1
- package/out/zero-client/src/client/custom.d.ts +0 -2
- package/out/zero-client/src/client/custom.d.ts.map +1 -1
- package/out/zero-client/src/client/custom.js +1 -1
- package/out/zero-client/src/client/custom.js.map +1 -1
- package/out/zero-client/src/client/enable-analytics.d.ts +0 -1
- package/out/zero-client/src/client/enable-analytics.d.ts.map +1 -1
- package/out/zero-client/src/client/enable-analytics.js +2 -1
- package/out/zero-client/src/client/enable-analytics.js.map +1 -1
- package/out/zero-client/src/client/error.d.ts +4 -5
- package/out/zero-client/src/client/error.d.ts.map +1 -1
- package/out/zero-client/src/client/error.js.map +1 -1
- package/out/zero-client/src/client/keys.d.ts +0 -1
- package/out/zero-client/src/client/keys.d.ts.map +1 -1
- package/out/zero-client/src/client/keys.js +4 -2
- package/out/zero-client/src/client/keys.js.map +1 -1
- package/out/zero-client/src/client/mutation-tracker.d.ts.map +1 -1
- package/out/zero-client/src/client/mutation-tracker.js +1 -1
- package/out/zero-client/src/client/mutation-tracker.js.map +1 -1
- package/out/zero-client/src/client/options.d.ts +0 -4
- package/out/zero-client/src/client/options.d.ts.map +1 -1
- package/out/zero-client/src/client/options.js.map +1 -1
- package/out/zero-client/src/client/replicache-types.d.ts +3 -2
- package/out/zero-client/src/client/replicache-types.d.ts.map +1 -1
- package/out/zero-client/src/client/version.js +1 -1
- package/out/zero-client/src/client/zero-poke-handler.d.ts +1 -2
- package/out/zero-client/src/client/zero-poke-handler.d.ts.map +1 -1
- package/out/zero-client/src/client/zero-poke-handler.js +1 -1
- package/out/zero-client/src/client/zero-poke-handler.js.map +1 -1
- package/out/zero-client/src/client/zero.d.ts +1 -5
- package/out/zero-client/src/client/zero.d.ts.map +1 -1
- package/out/zero-client/src/client/zero.js +1 -1
- package/out/zero-client/src/client/zero.js.map +1 -1
- package/out/zero-client/src/types/client-state.d.ts +0 -6
- package/out/zero-client/src/types/client-state.d.ts.map +1 -1
- package/out/zero-permissions/src/permissions.d.ts +1 -12
- package/out/zero-permissions/src/permissions.d.ts.map +1 -1
- package/out/zero-permissions/src/permissions.js +1 -2
- package/out/zero-permissions/src/permissions.js.map +1 -1
- package/out/zero-protocol/src/ast.d.ts +0 -4
- package/out/zero-protocol/src/ast.d.ts.map +1 -1
- package/out/zero-protocol/src/ast.js +1 -2
- package/out/zero-protocol/src/ast.js.map +1 -1
- package/out/zero-protocol/src/change-desired-queries.d.ts +0 -20
- package/out/zero-protocol/src/change-desired-queries.d.ts.map +1 -1
- package/out/zero-protocol/src/change-desired-queries.js.map +1 -1
- package/out/zero-protocol/src/client-schema.d.ts +2 -5
- package/out/zero-protocol/src/client-schema.d.ts.map +1 -1
- package/out/zero-protocol/src/client-schema.js +1 -1
- package/out/zero-protocol/src/client-schema.js.map +1 -1
- package/out/zero-protocol/src/close-connection.d.ts +0 -8
- package/out/zero-protocol/src/close-connection.d.ts.map +1 -1
- package/out/zero-protocol/src/close-connection.js +1 -1
- package/out/zero-protocol/src/close-connection.js.map +1 -1
- package/out/zero-protocol/src/connect.d.ts +0 -12
- package/out/zero-protocol/src/connect.d.ts.map +1 -1
- package/out/zero-protocol/src/connect.js +1 -1
- package/out/zero-protocol/src/connect.js.map +1 -1
- package/out/zero-protocol/src/custom-queries.d.ts +3 -17
- package/out/zero-protocol/src/custom-queries.d.ts.map +1 -1
- package/out/zero-protocol/src/custom-queries.js +1 -1
- package/out/zero-protocol/src/custom-queries.js.map +1 -1
- package/out/zero-protocol/src/data.d.ts +0 -1
- package/out/zero-protocol/src/data.d.ts.map +1 -1
- package/out/zero-protocol/src/data.js +1 -1
- package/out/zero-protocol/src/data.js.map +1 -1
- package/out/zero-protocol/src/down.d.ts +27 -27
- package/out/zero-protocol/src/inspect-down.d.ts +82 -84
- package/out/zero-protocol/src/inspect-down.d.ts.map +1 -1
- package/out/zero-protocol/src/inspect-down.js +1 -1
- package/out/zero-protocol/src/inspect-down.js.map +1 -1
- package/out/zero-protocol/src/inspect-up.d.ts +0 -28
- package/out/zero-protocol/src/inspect-up.d.ts.map +1 -1
- package/out/zero-protocol/src/inspect-up.js +1 -1
- package/out/zero-protocol/src/inspect-up.js.map +1 -1
- package/out/zero-protocol/src/mutate-server.d.ts +0 -21
- package/out/zero-protocol/src/mutate-server.d.ts.map +1 -1
- package/out/zero-protocol/src/mutate-server.js +1 -1
- package/out/zero-protocol/src/mutate-server.js.map +1 -1
- package/out/zero-protocol/src/mutation-type-enum.d.ts +0 -1
- package/out/zero-protocol/src/mutation-type-enum.d.ts.map +1 -1
- package/out/zero-protocol/src/mutation-type-enum.js.map +1 -1
- package/out/zero-protocol/src/mutation.d.ts +2 -4
- package/out/zero-protocol/src/mutation.d.ts.map +1 -1
- package/out/zero-protocol/src/mutation.js +1 -1
- package/out/zero-protocol/src/mutation.js.map +1 -1
- package/out/zero-protocol/src/mutations-patch.d.ts +0 -33
- package/out/zero-protocol/src/mutations-patch.d.ts.map +1 -1
- package/out/zero-protocol/src/mutations-patch.js +1 -1
- package/out/zero-protocol/src/mutations-patch.js.map +1 -1
- package/out/zero-protocol/src/ping.d.ts +0 -2
- package/out/zero-protocol/src/ping.d.ts.map +1 -1
- package/out/zero-protocol/src/ping.js +1 -1
- package/out/zero-protocol/src/ping.js.map +1 -1
- package/out/zero-protocol/src/poke.d.ts +3 -2
- package/out/zero-protocol/src/poke.d.ts.map +1 -1
- package/out/zero-protocol/src/poke.js +1 -1
- package/out/zero-protocol/src/poke.js.map +1 -1
- package/out/zero-protocol/src/pong.d.ts +0 -2
- package/out/zero-protocol/src/pong.d.ts.map +1 -1
- package/out/zero-protocol/src/pong.js +1 -1
- package/out/zero-protocol/src/pong.js.map +1 -1
- package/out/zero-protocol/src/pull.d.ts +2 -7
- package/out/zero-protocol/src/pull.d.ts.map +1 -1
- package/out/zero-protocol/src/pull.js +1 -1
- package/out/zero-protocol/src/pull.js.map +1 -1
- package/out/zero-protocol/src/push.d.ts +1 -3
- package/out/zero-protocol/src/push.d.ts.map +1 -1
- package/out/zero-protocol/src/push.js +1 -1
- package/out/zero-protocol/src/push.js.map +1 -1
- package/out/zero-protocol/src/queries-patch.d.ts +0 -16
- package/out/zero-protocol/src/queries-patch.d.ts.map +1 -1
- package/out/zero-protocol/src/queries-patch.js +1 -1
- package/out/zero-protocol/src/queries-patch.js.map +1 -1
- package/out/zero-protocol/src/query-server.d.ts +2 -41
- package/out/zero-protocol/src/query-server.d.ts.map +1 -1
- package/out/zero-protocol/src/query-server.js +1 -1
- package/out/zero-protocol/src/query-server.js.map +1 -1
- package/out/zero-protocol/src/up.d.ts +42 -42
- package/out/zero-protocol/src/version.d.ts +0 -1
- package/out/zero-protocol/src/version.d.ts.map +1 -1
- package/out/zero-protocol/src/version.js.map +1 -1
- package/out/zero-react/src/use-query.js +1 -1
- package/out/zero-react/src/use-query.js.map +1 -1
- package/out/zero-schema/src/builder/relationship-builder.d.ts +0 -2
- package/out/zero-schema/src/builder/relationship-builder.d.ts.map +1 -1
- package/out/zero-schema/src/builder/relationship-builder.js.map +1 -1
- package/out/zero-schema/src/builder/table-builder.d.ts +1 -1
- package/out/zero-schema/src/builder/table-builder.d.ts.map +1 -1
- package/out/zero-schema/src/builder/table-builder.js +1 -1
- package/out/zero-schema/src/builder/table-builder.js.map +1 -1
- package/out/zero-schema/src/schema-config.d.ts +0 -1
- package/out/zero-schema/src/schema-config.d.ts.map +1 -1
- package/out/zero-schema/src/schema-config.js +1 -1
- package/out/zero-schema/src/schema-config.js.map +1 -1
- package/out/zero-schema/src/table-schema.d.ts +3 -11
- package/out/zero-schema/src/table-schema.d.ts.map +1 -1
- package/out/zero-server/src/queries/process-queries.d.ts +2 -4
- package/out/zero-server/src/queries/process-queries.d.ts.map +1 -1
- package/out/zero-server/src/queries/process-queries.js.map +1 -1
- package/out/zero-solid/src/solid-view.d.ts +0 -2
- package/out/zero-solid/src/solid-view.d.ts.map +1 -1
- package/out/zero-solid/src/solid-view.js +1 -1
- package/out/zero-solid/src/solid-view.js.map +1 -1
- package/out/zero-types/src/name-mapper.d.ts +2 -1
- package/out/zero-types/src/name-mapper.d.ts.map +1 -1
- package/out/zero-types/src/name-mapper.js.map +1 -1
- package/out/zero-types/src/schema-value.d.ts +3 -2
- package/out/zero-types/src/schema-value.d.ts.map +1 -1
- package/out/zero-types/src/schema.d.ts +1 -1
- package/out/zero-types/src/schema.d.ts.map +1 -1
- package/out/zql/src/builder/builder.d.ts +1 -11
- package/out/zql/src/builder/builder.d.ts.map +1 -1
- package/out/zql/src/builder/builder.js +1 -1
- package/out/zql/src/builder/builder.js.map +1 -1
- package/out/zql/src/builder/filter.d.ts +0 -1
- package/out/zql/src/builder/filter.d.ts.map +1 -1
- package/out/zql/src/builder/filter.js.map +1 -1
- package/out/zql/src/error.d.ts +0 -5
- package/out/zql/src/error.d.ts.map +1 -1
- package/out/zql/src/error.js.map +1 -1
- package/out/zql/src/ivm/constraint.d.ts +0 -7
- package/out/zql/src/ivm/constraint.d.ts.map +1 -1
- package/out/zql/src/ivm/constraint.js +1 -1
- package/out/zql/src/ivm/constraint.js.map +1 -1
- package/out/zql/src/ivm/data.d.ts +0 -1
- package/out/zql/src/ivm/data.d.ts.map +1 -1
- package/out/zql/src/ivm/data.js.map +1 -1
- package/out/zql/src/ivm/memory-source.d.ts +1 -2
- package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
- package/out/zql/src/ivm/memory-source.js +1 -1
- package/out/zql/src/ivm/memory-source.js.map +1 -1
- package/out/zql/src/ivm/stream.d.ts +0 -1
- package/out/zql/src/ivm/stream.d.ts.map +1 -1
- package/out/zql/src/ivm/stream.js.map +1 -1
- package/out/zql/src/ivm/union-fan-in.d.ts +0 -1
- package/out/zql/src/ivm/union-fan-in.d.ts.map +1 -1
- package/out/zql/src/ivm/union-fan-in.js +1 -1
- package/out/zql/src/ivm/union-fan-in.js.map +1 -1
- package/out/zql/src/ivm/view-apply-change.d.ts +5 -35
- package/out/zql/src/ivm/view-apply-change.d.ts.map +1 -1
- package/out/zql/src/ivm/view-apply-change.js +1 -1
- package/out/zql/src/ivm/view-apply-change.js.map +1 -1
- package/out/zql/src/mutate/crud.d.ts +1 -3
- package/out/zql/src/mutate/crud.d.ts.map +1 -1
- package/out/zql/src/mutate/crud.js +1 -1
- package/out/zql/src/mutate/crud.js.map +1 -1
- package/out/zql/src/mutate/mutator-registry.d.ts +1 -1
- package/out/zql/src/mutate/mutator-registry.d.ts.map +1 -1
- package/out/zql/src/mutate/mutator-registry.js.map +1 -1
- package/out/zql/src/mutate/mutator.d.ts +1 -1
- package/out/zql/src/mutate/mutator.d.ts.map +1 -1
- package/out/zql/src/mutate/mutator.js.map +1 -1
- package/out/zql/src/planner/planner-debug.d.ts +10 -9
- package/out/zql/src/planner/planner-debug.d.ts.map +1 -1
- package/out/zql/src/planner/planner-debug.js.map +1 -1
- package/out/zql/src/planner/planner-node.d.ts +0 -1
- package/out/zql/src/planner/planner-node.d.ts.map +1 -1
- package/out/zql/src/planner/planner-node.js.map +1 -1
- package/out/zql/src/planner/planner-warnings.d.ts +3 -3
- package/out/zql/src/planner/planner-warnings.d.ts.map +1 -1
- package/out/zql/src/planner/planner-warnings.js.map +1 -1
- package/out/zql/src/query/expression.d.ts +0 -22
- package/out/zql/src/query/expression.d.ts.map +1 -1
- package/out/zql/src/query/expression.js +1 -1
- package/out/zql/src/query/expression.js.map +1 -1
- package/out/zql/src/query/query-delegate-base.d.ts +0 -7
- package/out/zql/src/query/query-delegate-base.d.ts.map +1 -1
- package/out/zql/src/query/query-delegate-base.js +2 -2
- package/out/zql/src/query/query-delegate-base.js.map +1 -1
- package/out/zql/src/query/query-delegate.d.ts +1 -4
- package/out/zql/src/query/query-delegate.d.ts.map +1 -1
- package/out/zql/src/query/query-internals.d.ts +0 -3
- package/out/zql/src/query/query-internals.d.ts.map +1 -1
- package/out/zql/src/query/query-internals.js.map +1 -1
- package/out/zql/src/query/query-registry.d.ts +5 -5
- package/out/zql/src/query/query-registry.d.ts.map +1 -1
- package/out/zql/src/query/query-registry.js.map +1 -1
- package/out/zql/src/query/query.d.ts +2 -13
- package/out/zql/src/query/query.d.ts.map +1 -1
- package/out/zql/src/query/runnable-query-impl.d.ts +1 -19
- package/out/zql/src/query/runnable-query-impl.d.ts.map +1 -1
- package/out/zql/src/query/runnable-query-impl.js +1 -1
- package/out/zql/src/query/runnable-query-impl.js.map +1 -1
- package/out/zql/src/query/ttl.d.ts +2 -3
- package/out/zql/src/query/ttl.d.ts.map +1 -1
- package/out/zql/src/query/ttl.js.map +1 -1
- package/out/zqlite/src/query-builder.d.ts +0 -2
- package/out/zqlite/src/query-builder.d.ts.map +1 -1
- package/out/zqlite/src/query-builder.js +1 -1
- package/out/zqlite/src/query-builder.js.map +1 -1
- package/out/zqlite/src/table-source.d.ts +0 -1
- package/out/zqlite/src/table-source.d.ts.map +1 -1
- package/out/zqlite/src/table-source.js +1 -1
- package/out/zqlite/src/table-source.js.map +1 -1
- package/package.json +2 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snapshotter.js","names":[],"sources":["../../../../../../zero-cache/src/services/view-syncer/snapshotter.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {assert} from '../../../../shared/src/asserts.ts';\nimport {stringify, type JSONValue} from '../../../../shared/src/bigint-json.ts';\nimport {getOrInsertComputed} from '../../../../shared/src/map.ts';\nimport type {Row} from '../../../../zero-protocol/src/data.ts';\nimport type {PrimaryKey} from '../../../../zero-types/src/schema.ts';\nimport {Database} from '../../../../zqlite/src/db.ts';\nimport {recordIndexUsage} from '../../../../zqlite/src/index-usage.ts';\nimport {\n fromSQLiteTypes,\n type SQLiteRow,\n} from '../../../../zqlite/src/table-source.ts';\nimport type {\n LiteAndZqlSpec,\n LiteTableSpecWithKeysAndVersion,\n} from '../../db/specs.ts';\nimport {StatementRunner} from '../../db/statements.ts';\nimport {normalizedKeyOrder, type RowKey} from '../../types/row-key.ts';\nimport type {AppID} from '../../types/shards.ts';\nimport {id} from '../../types/sql.ts';\nimport {\n RESET_OP,\n parseChangeLogEntry,\n SET_OP,\n TRUNCATE_OP,\n} from '../replicator/schema/change-log.ts';\nimport {\n getReplicationState,\n ZERO_VERSION_COLUMN_NAME as ROW_VERSION,\n} from '../replicator/schema/replication-state.ts';\nimport type {SnapshotRowCache} from './snapshot-row-cache.ts';\n\n/**\n * A `Snapshotter` manages the progression of database snapshots for a\n * ViewSyncer.\n *\n * The Replicator and ViewSyncers operate on the same SQLite file, with the\n * Replicator being the sole writer to the database. The IVM logic in\n * ViewSyncers, however, rely on incrementally applying changes to the DB to\n * update the state of its pipelines.\n *\n * To avoid coupling the progress of the Replicator and all IVM pipelines on\n * each other, ViewSyncers operate on ephemeral forks of the database by holding\n * [concurrent](https://sqlite.org/src/doc/begin-concurrent/doc/begin_concurrent.md)\n * snapshots of the database and simulating (but ultimately rolling back)\n * mutations on these snapshots.\n *\n * Example:\n * 1. ViewSyncer takes `snapshot_a` at version `t1` of the database and\n * hydrates its pipeline(s).\n * 2. Replicator applies a new transaction to the database and notifies\n * subscribers.\n * 3. ViewSyncer takes `snapshot_b` at `t2`, and queries the `ChangeLog` at\n * that snapshot for changes since `t1`.\n * 4. ViewSyncer applies those changes to `snapshot_a` for IVM, but does not\n * commit them. (Recall that the Replicator is the sole writer to the db, so\n * the ViewSyncer never commits any writes.)\n * 5. Replicator applies the next transaction and advances the database to `t3`.\n * 6. ViewSyncer rolls back `snapshot_a` and opens `snapshot_c` at `t3`, using\n * `snapshot_b` to simulate changes from `t2` to `t3`.\n *\n * ```\n * Replicator: t1 --------------> t2 --------------> t3 --------------->\n * ViewSyncer: [snapshot_a] ----> [snapshot_b] ----> [snapshot_c]\n * ```\n *\n * Note that the Replicator (and ViewSyncers) do not wait on the progress of\n * other ViewSyncers. If a ViewSyncer is busy hydrating at `t1`, the Replicator\n * and other ViewSyncers can progress through `t2`, `t3`, etc. independently,\n * as the busy ViewSyncer simply takes its own snapshot when it is ready.\n *\n * ```\n * Replicator: t1 --------------> t2 --------------> t3 --------------->\n * ViewSyncer1: [snapshot_a] ----> [snapshot_b] ----> [snapshot_c]\n * ViewSyncer2: [.......... snapshot_a ..........] ----> [snapshot_b]\n * ```\n *\n * To minimize Database connections (and statement preparation, etc.), the\n * Snapshotter reuses the connection from the previous (rolled back)\n * snapshot when opening the new one.\n *\n * ```\n * Replicator: t1 --------------> t2 --------------> t3 --------------->\n * ViewSyncer: [snapshot_a] ----> [snapshot_b] ----> [snapshot_c]\n * (conn_1) (conn_2) (conn_1)\n * ```\n *\n * In this manner, each ViewSyncer uses two connections that continually\n * \"leapfrog\" each other to replay the timeline of changes in isolation from\n * the Replicator and other ViewSyncers.\n */\nexport class Snapshotter {\n readonly #lc: LogContext;\n readonly #dbFile: string;\n readonly #appID: string;\n readonly #pageCacheSizeKib: number | undefined;\n readonly #rowCache: SnapshotRowCache | undefined;\n #curr: Snapshot | undefined;\n #prev: Snapshot | undefined;\n\n /**\n * @param rowCache An optional worker-wide {@link SnapshotRowCache} through\n * which the row reads performed when iterating over a\n * {@link SnapshotDiff} are shared with the other Snapshotters\n * (i.e. client groups) on the worker.\n */\n constructor(\n lc: LogContext,\n dbFile: string,\n {appID}: AppID,\n pageCacheSizeKib?: number,\n rowCache?: SnapshotRowCache,\n ) {\n this.#lc = lc;\n this.#dbFile = dbFile;\n this.#appID = appID;\n this.#pageCacheSizeKib = pageCacheSizeKib;\n this.#rowCache = rowCache;\n }\n\n /**\n * Initializes the snapshot to the current head of the database. This must be\n * only be called once. The state of whether a Snapshotter has been initialized\n * can be determined by calling {@link initialized()}.\n */\n init(): this {\n assert(this.#curr === undefined, 'Already initialized');\n this.#curr = Snapshot.create(\n this.#lc,\n this.#dbFile,\n this.#appID,\n this.#pageCacheSizeKib,\n );\n this.#lc.debug?.(`Initial snapshot at version ${this.#curr.version}`);\n return this;\n }\n\n initialized(): boolean {\n return this.#curr !== undefined;\n }\n\n /** Returns the current snapshot. Asserts if {@link initialized()} is false. */\n current(): Snapshot {\n assert(this.#curr !== undefined, 'Snapshotter has not been initialized');\n return this.#curr;\n }\n\n /**\n * Advances to the head of the Database, returning a diff between the\n * previously current Snapshot and a new Snapshot at head. This is called\n * in response to a notification from a Replicator subscription. Subsequent\n * calls to {@link current()} return the new Snapshot. Note that the Snapshotter\n * must be initialized before advancing.\n *\n * The returned {@link SnapshotDiff} contains snapshots at the endpoints\n * of the database timeline. Iterating over the diff generates a sequence\n * of {@link Change}s between the two snapshots.\n *\n * Note that this sequence is not chronological; rather, the sequence is\n * ordered by `<table, row-key>`, such that a row can appear at most once\n * in the common case, or twice if its table is `TRUNCATE`'d and a new value\n * is subsequently `INSERT`'ed. This results in dropping most intermediate\n * changes to a row and bounds the amount of work needed to catch up;\n * however, as a consequence, a consistent database state is only guaranteed\n * when the sequence has been fully consumed.\n *\n * Note that Change generation relies on the state of the underlying\n * database connections, and because the connection for the previous snapshot\n * is reused to produce the next snapshot, the diff object is only valid\n * until the next call to `advance()`.\n *\n * It is okay for the caller to apply `Change`s to the `prev` snapshot\n * during the iteration (e.g. this is necessary for IVM); the remainder\n * of the iteration is not affected because a given row can appear at most\n * once in the sequence (with the exception being TRUNCATE, after which the\n * deleted rows can be re-inserted, but this will also behave correctly if\n * the changes are applied).\n *\n * Once the changes have been applied, however, a _subsequent_ iteration\n * will not produce the correct results. In order to perform multiple\n * change-applying iterations, the caller must (1) create a save point\n * on `prev` before each iteration, and (2) rollback to the save point after\n * the iteration.\n *\n * @param prevWrites How the caller writes to `prev` during the iteration,\n * which determines which `prev` reads can be shared through the\n * {@link SnapshotRowCache}. See {@link PrevWrites}.\n */\n advance(\n syncableTables: Map<string, LiteAndZqlSpec>,\n allTableNames: Set<string>,\n observedTables?: TableFilter | undefined,\n prevWrites: PrevWrites = 'uniform',\n ): SnapshotDiff {\n this.advanceWithoutDiff();\n return this.diff(syncableTables, allTableNames, observedTables, prevWrites);\n }\n\n /**\n * The diff between the two snapshots of the last\n * {@link advanceWithoutDiff()}, as {@link advance()} returns it.\n *\n * @param counts The counts of the change log entries between the two\n * snapshots, if the caller has counted them already (see\n * `Snapshot.countChangesSince()`). The diff then does not count\n * them again.\n * @param readTables If set, only the change log entries of these tables\n * (and of the permissions table) are read, along with the\n * table-wide ops of every table, where `observedTables` reads the\n * entries of every table and skips those it does not have. For a\n * caller that does not need the changes of the other tables, this\n * saves reading and parsing their entries. This requires `counts`.\n * If they show that there is no entry of these tables and no\n * table-wide op, the change log is not read at all, and the\n * diff's `changes` is 0.\n */\n diff(\n syncableTables: Map<string, LiteAndZqlSpec>,\n allTableNames: Set<string>,\n observedTables?: TableFilter | undefined,\n prevWrites: PrevWrites = 'uniform',\n counts?: ChangeLogCounts | undefined,\n readTables?: Iterable<string> | undefined,\n ): SnapshotDiff {\n assert(\n this.#prev !== undefined && this.#curr !== undefined,\n 'Snapshotter has not been advanced',\n );\n return new Diff(\n this.#appID,\n syncableTables,\n allTableNames,\n this.#prev,\n this.#curr,\n observedTables,\n this.#rowCache,\n prevWrites,\n counts,\n readTables,\n );\n }\n\n advanceWithoutDiff() {\n assert(this.#curr !== undefined, 'Snapshotter has not been initialized');\n const next = this.#prev\n ? this.#prev.resetToHead()\n : Snapshot.create(\n this.#lc,\n this.#curr.db.db.name,\n this.#appID,\n this.#pageCacheSizeKib,\n );\n this.#prev = this.#curr;\n this.#curr = next;\n return {prev: this.#prev, curr: this.#curr};\n }\n\n /**\n * The reset of the pipelines that iterating over the changes from the\n * previous snapshot to the current one ends in, if it does: the first of\n * a schema change, a truncation, and a change of the permissions. It is\n * found without iterating, so that a caller that would reset the pipelines\n * anyway does not first process the changes that come before it.\n *\n * If there is none, iterating over a {@link diff()} of the two snapshots\n * does not throw a {@link ResetPipelinesSignal}, and can be stopped at any\n * point without missing one.\n *\n * @param counts The changes from the previous snapshot to the current\n * one, from which it is known whether there is anything to look\n * for.\n */\n pendingReset(\n syncableTables: Map<string, LiteAndZqlSpec>,\n counts: ChangeLogCounts,\n ): ResetPipelinesSignal | undefined {\n assert(\n this.#prev !== undefined && this.#curr !== undefined,\n 'Snapshotter has not been advanced',\n );\n const prev = this.#prev;\n const curr = this.#curr;\n let first: (ChangeLogPosition & {signal: ResetPipelinesSignal}) | undefined;\n\n if (counts.tableWideOps > 0) {\n // Iterating aborts at a table-wide op whatever its table is.\n const {stateVersion, pos, table, op} = curr.firstTableWideOpSince(\n prev.version,\n );\n first = {\n stateVersion,\n pos,\n signal:\n op === TRUNCATE_OP\n ? new ResetPipelinesSignal(\n `table ${table} has been truncated`,\n 'truncation',\n )\n : new ResetPipelinesSignal(\n `schema for table ${table} has changed`,\n 'schema-change',\n ),\n };\n }\n\n // An entry of the permissions table only aborts the iteration if the\n // permissions are different, which takes reading the row to know.\n const permissionsTable = `${this.#appID}.permissions`;\n const specs = syncableTables.get(permissionsTable);\n if (\n specs &&\n (counts.sampled || (counts.byTable.get(permissionsTable) ?? 0) > 0)\n ) {\n const {tableSpec} = specs;\n for (const entry of curr.entriesSince(permissionsTable, prev.version)) {\n if (first && !isBefore(entry, first)) {\n break;\n }\n const rowKey: JSONValue = entry.rowKey && JSON.parse(entry.rowKey);\n if (rowKey === null) {\n continue;\n }\n const nextValue =\n entry.op === SET_OP ? curr.getRow(tableSpec, rowKey) : null;\n let prevValues;\n if (nextValue) {\n prevValues = prev.getRows(tableSpec, tableSpec.uniqueKeys, nextValue);\n } else {\n const prevValue = prev.getRow(tableSpec, rowKey);\n prevValues = prevValue ? [prevValue] : [];\n }\n const changed = prevValues.find(\n prevValue => prevValue.permissions !== nextValue?.permissions,\n );\n if (changed) {\n first = {\n ...entry,\n signal: new ResetPipelinesSignal(\n `Permissions have changed ${changed.hash} => ${nextValue?.hash}`,\n 'permissions-change',\n ),\n };\n break;\n }\n }\n }\n return first?.signal;\n }\n\n /**\n * Releases the previous snapshot, i.e. the `prev` of the last\n * {@link advance()} or {@link advanceWithoutDiff()}. It is only read\n * while the diff that it is the start of is iterated over, but its read\n * transaction otherwise stays open until the next advance, which keeps\n * the replica from checkpointing its write-ahead log past it. A diff that\n * starts at it can no longer be iterated over.\n */\n releasePrevious(): void {\n this.#prev?.release();\n }\n\n /**\n * Call this to close the database connections when the Snapshotter is\n * no longer needed.\n */\n destroy() {\n // The replica's statistics are maintained by the replicator. The\n // `PRAGMA optimize` that closing a writable connection otherwise runs\n // analyzes the tables that the connection read, which blocks the worker\n // for as long as that takes, and what it finds is discarded with the\n // snapshot's transaction.\n this.#curr?.db.db.close({optimize: false});\n this.#prev?.db.db.close({optimize: false});\n this.#lc.debug?.('closed database connections');\n }\n}\n\nexport type Change = {\n readonly table: string;\n /**\n * If this change represents a remove the row to remove,\n * if nextValue is not null then all rows that have a unique constraint\n * violation with nextValue.\n * In both cases these rows should be removed.\n */\n readonly prevValues: Readonly<Row>[];\n readonly nextValue: Readonly<Row> | null;\n readonly rowKey: RowKey;\n};\n\n/** See `Snapshot.countChangesSince()`. */\n/** An entry of {@link Snapshot.changesSince}, in the order of its SELECT. */\ntype RawChangeLogEntry = readonly [\n stateVersion: string,\n table: string,\n rowKey: string,\n op: string,\n];\n\n/** Where an entry is in the order that the change log is read in. */\ntype ChangeLogPosition = {\n readonly stateVersion: string;\n readonly pos: number;\n};\n\nfunction isBefore(a: ChangeLogPosition, b: ChangeLogPosition): boolean {\n return a.stateVersion === b.stateVersion\n ? a.pos < b.pos\n : a.stateVersion < b.stateVersion;\n}\n\nexport type ChangeLogCounts = {\n /** The number of entries. */\n readonly total: number;\n /** The number of them that are table-wide ops (a RESET or TRUNCATE). */\n readonly tableWideOps: number;\n /**\n * The number of entries of each table. If {@link sampled}, these are\n * estimates, and a table with few entries may be missing.\n */\n readonly byTable: ReadonlyMap<string, number>;\n /** Whether {@link byTable} was estimated from a sample of the entries. */\n readonly sampled: boolean;\n};\n\n/**\n * The most change log entries that are read to count a batch's changes by\n * table. Reading an entry costs microseconds, and nothing else runs on the\n * sync worker in the meantime, so the cost of counting must not grow with\n * the number of changes that a client group has to catch up on.\n */\nexport const CHANGE_LOG_SAMPLE_SIZE = 1000;\n\nexport interface TableFilter {\n has(table: string): boolean;\n}\n\n/**\n * How the caller of {@link Snapshotter.advance()} writes the changes of a\n * {@link SnapshotDiff} to its `prev` snapshot while iterating over it.\n *\n * A `prev` read for a table with a single unique key (its primary key)\n * cannot observe earlier changes of the iteration, since a row key appears\n * once in the diff. A read for a table with other unique keys can, because\n * the rows it returns may have been displaced or edited by earlier changes.\n *\n * * `uniform`: every caller applies every change, so such a read depends on\n * the `<prev, curr>` versions of the diff, and is shared on that basis.\n * * `divergent`: callers apply different subsets of the changes (e.g. the\n * pipeline driver skips changes that no pipeline can observe), so such a\n * read depends on the caller and is not shared.\n * * `none`: the caller does not write to `prev` (e.g. IVM derivation is\n * held in memory), so every read depends solely on `prev.version`.\n */\nexport type PrevWrites = 'uniform' | 'divergent' | 'none';\n\n/**\n * Represents the difference between two database Snapshots.\n * Iterating over the object will produce a sequence of {@link Change}s\n * between the two snapshots.\n *\n * See {@link Snapshotter.advance()} for semantics and usage.\n */\nexport type DiffTimingStats = {\n changeLogReadMs: number;\n rowReadMs: number;\n parseMs: number;\n totalDiffMs: number;\n};\n\nexport interface SnapshotDiff extends Iterable<Change> {\n readonly prev: {\n readonly db: StatementRunner;\n readonly version: string;\n };\n readonly curr: {\n readonly db: StatementRunner;\n readonly version: string;\n };\n\n /**\n * The raw number of ChangeLog entries between the snapshots, before filtering\n * out unobserved/non-syncable tables or no-op changes. Progress against this\n * total must use {@link changesScanned}, rather than the number of yielded\n * changes. Table-wide operations abort iteration and reset the pipelines.\n */\n readonly changes: number;\n\n /** The number of raw ChangeLog entries read so far, including skipped entries. */\n readonly changesScanned: number;\n\n /**\n * The raw ChangeLog entries read so far, by table. Like\n * {@link changesScanned}, this includes the entries that are not yielded,\n * so it counts the same entries as {@link ChangeLogCounts.byTable} does.\n */\n readonly changesScannedByTable: ReadonlyMap<string, number>;\n\n /** The scanned entries omitted because their table is unobserved/non-syncable or the change is a no-op. */\n readonly changesSkipped: number;\n\n /**\n * The scanned entries whose rows were looked up in the snapshots. Unlike\n * the entries that are yielded, this includes the no-op changes, which are\n * only known to be that from their rows.\n */\n readonly changesLookedUp: number;\n\n /** Timing metrics recorded during diff iteration. */\n readonly stats?: DiffTimingStats | undefined;\n\n /**\n * Overrides the `prevWrites` passed to {@link Snapshotter.advance()}, for a\n * caller that decides how to write to `prev` once it knows the number of\n * {@link changes}. Must be called before the diff is iterated, except to\n * switch to `divergent`, which can be done between any two changes: it\n * shares only the reads that no write to `prev` can affect.\n */\n setPrevWrites(prevWrites: PrevWrites): void;\n}\n\n/**\n * Thrown during an iteration of a {@link SnapshotDiff} when a schema\n * change or truncate is encountered, which result in aborting the\n * advancement and resetting / rehydrating the pipelines.\n */\nexport type ResetPipelinesReason =\n | 'advancement-timeout'\n | 'scalar-subquery'\n | 'schema-change'\n | 'truncation'\n | 'permissions-change';\n\nexport class ResetPipelinesSignal extends Error {\n readonly name = 'ResetPipelinesSignal';\n readonly reason: ResetPipelinesReason;\n readonly breakdown?: string | undefined;\n\n constructor(msg: string, reason: ResetPipelinesReason, breakdown?: string) {\n super(msg);\n this.reason = reason;\n this.breakdown = breakdown;\n }\n}\n\n// Memoizes, per table spec, the SQL of the row lookups performed for each\n// change, which would otherwise be rebuilt from the table spec for every\n// read. Handing out the same string for every read of a given shape also\n// lets the SnapshotRowCache and the statement cache look it up without\n// rehashing it. Specs are recreated whenever they are recomputed, so stale\n// SQL is never served.\ntype TableReads = {\n /** `SELECT <columns> FROM <table> WHERE ` */\n readonly prefix: string;\n /** {@link Snapshot.getRow} SQL, by the NUL-joined key columns. */\n readonly byKeyColumns: Map<string, string>;\n /**\n * {@link Snapshot.getRows} SQL, by the bitmask of the table's `uniqueKeys`\n * that are queried.\n */\n readonly byUniqueKeys: Map<number, string>;\n};\n\nconst tableReads = new WeakMap<LiteTableSpecWithKeysAndVersion, TableReads>();\n\nfunction readsFor(table: LiteTableSpecWithKeysAndVersion): TableReads {\n return getOrInsertComputed(tableReads, table, () => ({\n prefix: `SELECT ${Object.keys(table.columns)\n .map(c => id(c))\n .join(',')} FROM ${id(table.name)} WHERE `,\n byKeyColumns: new Map(),\n byUniqueKeys: new Map(),\n }));\n}\n\n// The byUniqueKeys bitmask must fit in a (positive) 31-bit integer.\nconst MAX_MEMOIZED_UNIQUE_KEYS = 30;\n\n/**\n * The index that SQLite creates for the `PRIMARY KEY(\"stateVersion\", \"pos\")`\n * of `_zero.changeLog2` (the first constraint of its CREATE TABLE statement,\n * hence the `_1` suffix).\n *\n * For a `stateVersion > ?` query that reads a column outside of this index\n * (e.g. `op`) and has no ORDER BY, the query planner chooses between a range\n * search of the index and a full scan of the table based on `sqlite_stat1`.\n * Statistics recorded while the change log held only a few rows make the full\n * scan look cheaper, and they persist as the table grows to millions of rows:\n * the change log of a replication-manager's replica only receives entries\n * during column backfills, and view-syncers inherit its statistics when they\n * restore its backup. Such queries therefore pin this index with INDEXED BY,\n * which also makes them fail to prepare (rather than silently scan) if the\n * index is ever renamed.\n */\nconst CHANGE_LOG_STATE_VERSION_INDEX = 'sqlite_autoindex__zero.changeLog2_1';\n\n/**\n * The index that SQLite creates for the `UNIQUE(\"table\", \"rowKey\")` of\n * `_zero.changeLog2` (the second constraint of its CREATE TABLE statement).\n */\nconst CHANGE_LOG_TABLE_INDEX = 'sqlite_autoindex__zero.changeLog2_2';\n\n/**\n * Selects the change log entries of the tables in a JSON array of names,\n * and the table-wide ops (RESET and TRUNCATE) of every table, which abort\n * the diff whatever table they are for. Binds the two ops, then the array.\n * The array is a single parameter so that the statement is the same, and is\n * prepared once, for any set of tables.\n */\nconst CHANGE_LOG_TABLES_FILTER = `(\"op\" IN (?, ?) OR \"table\" IN (SELECT \"value\" FROM json_each(?)))`;\n\nclass Snapshot {\n static create(\n lc: LogContext,\n dbFile: string,\n appID: string,\n pageCacheSizeKib: number | undefined,\n ) {\n const conn = new Database(lc, dbFile);\n conn.pragma('synchronous = OFF'); // Applied changes are ephemeral; COMMIT is never called.\n if (pageCacheSizeKib !== undefined) {\n conn.pragma(`cache_size = -${pageCacheSizeKib}`); // Negative = size in KiB\n }\n const [{journal_mode: mode}] = conn.pragma('journal_mode') as [\n {journal_mode: string},\n ];\n // BEGIN CONCURRENT lets the Snapshotter simulate changes on a historic\n // snapshot without taking the WAL writer lock. These private changes are\n // always rolled back; the replicator is the only committer and can use\n // BEGIN IMMEDIATE on the same WAL2 database.\n assert(\n mode === 'wal2',\n `replica db must be in wal2 mode (current: ${mode})`,\n );\n\n const db = new StatementRunner(conn);\n return new Snapshot(db, appID);\n }\n\n readonly db: StatementRunner;\n readonly #appID: string;\n readonly version: string;\n #reset = false;\n\n /**\n * @param begun Whether `db` is already in a `BEGIN CONCURRENT` transaction\n * that has not read anything.\n */\n constructor(db: StatementRunner, appID: string, begun = false) {\n if (!begun) {\n db.beginConcurrent();\n }\n // Note: The subsequent read is necessary to acquire the read lock\n // (which results in the logical creation of the snapshot). Calling\n // `BEGIN CONCURRENT` alone does not result in acquiring the read lock.\n const {stateVersion} = getReplicationState(db);\n\n this.db = db;\n this.#appID = appID;\n this.version = stateVersion;\n }\n\n numChangesSince(prevVersion: string) {\n const {count} = this.db.get(\n 'SELECT COUNT(*) AS count FROM \"_zero.changeLog2\" WHERE stateVersion > ?',\n prevVersion,\n );\n return count;\n }\n\n /**\n * Counts the change log entries after `prevVersion`. The change log keeps\n * the latest entry of a row, so these are counts of the rows that changed,\n * which is what a diff from `prevVersion` to this snapshot reads.\n *\n * The total, and the number of table-wide ops (a RESET or TRUNCATE), are\n * exact, and are counted from the index alone. The counts by table are\n * exact if there are at most `sampleSize` entries. Otherwise they are\n * estimated from the newest `sampleSize` entries, so that at most that\n * many entries are read however many there are.\n */\n countChangesSince(\n prevVersion: string,\n sampleSize = CHANGE_LOG_SAMPLE_SIZE,\n ): ChangeLogCounts {\n // The index has the stateVersion and pos of every entry, and a\n // table-wide op is logged at pos -1 (see ChangeLog), so no entry is read.\n const {total, tableWideOps} = this.db.get(\n `SELECT COUNT(*) AS total, COALESCE(SUM(pos < 0), 0) AS tableWideOps\n FROM \"_zero.changeLog2\" INDEXED BY \"${CHANGE_LOG_STATE_VERSION_INDEX}\"\n WHERE stateVersion > ?`,\n prevVersion,\n );\n const byTable = new Map<string, number>();\n if (total === 0) {\n return {total, tableWideOps, byTable, sampled: false};\n }\n const rows: {table: string; count: number}[] = this.db.all(\n `SELECT \"table\", COUNT(*) AS count FROM (\n SELECT \"table\"\n FROM \"_zero.changeLog2\" INDEXED BY \"${CHANGE_LOG_STATE_VERSION_INDEX}\"\n WHERE stateVersion > ?\n ORDER BY stateVersion DESC, pos DESC LIMIT ?\n ) GROUP BY \"table\"`,\n prevVersion,\n sampleSize,\n );\n const sampled = total > sampleSize;\n const scale = sampled ? total / sampleSize : 1;\n for (const {table, count} of rows) {\n byTable.set(table, count * scale);\n }\n return {total, tableWideOps, byTable, sampled};\n }\n\n /**\n * Whether an entry of `table` was logged after `prevVersion`. This reads\n * the table's entries in the change log, however old, so it is for tables\n * with few rows.\n */\n tableChangedSince(table: string, prevVersion: string): boolean {\n const row = this.db.get(\n `SELECT 1 FROM \"_zero.changeLog2\" INDEXED BY \"${CHANGE_LOG_TABLE_INDEX}\"\n WHERE \"table\" = ? AND stateVersion > ? LIMIT 1`,\n table,\n prevVersion,\n );\n return row !== undefined;\n }\n\n /**\n * Whether a table-wide RESET op (i.e. a schema change) was logged after\n * `prevVersion`. Table specs computed at or before `prevVersion` are\n * stale with respect to this snapshot if so.\n */\n schemaChangedSince(prevVersion: string): boolean {\n const row = this.db.get(\n `SELECT 1 FROM \"_zero.changeLog2\" INDEXED BY \"${CHANGE_LOG_STATE_VERSION_INDEX}\"\n WHERE stateVersion > ? AND op = ? LIMIT 1`,\n prevVersion,\n RESET_OP,\n );\n return row !== undefined;\n }\n\n /**\n * Whether any table-wide op (i.e. a RESET or TRUNCATE) was logged after\n * `prevVersion`. Iterating over a diff ending at this snapshot will\n * abort with a {@link ResetPipelinesSignal} if so.\n */\n hasTableWideOpSince(prevVersion: string): boolean {\n const row = this.db.get(\n `SELECT 1 FROM \"_zero.changeLog2\" INDEXED BY \"${CHANGE_LOG_STATE_VERSION_INDEX}\"\n WHERE stateVersion > ? AND op IN (?, ?) LIMIT 1`,\n prevVersion,\n RESET_OP,\n TRUNCATE_OP,\n );\n return row !== undefined;\n }\n\n /**\n * The first table-wide op (a schema change or a truncation) logged after\n * `prevVersion`, in the order that {@link changesSince} reads the entries.\n */\n firstTableWideOpSince(\n prevVersion: string,\n ): ChangeLogPosition & {table: string; op: string} {\n return this.db.get(\n `SELECT \"stateVersion\", \"pos\", \"table\", \"op\" FROM \"_zero.changeLog2\" INDEXED BY \"${CHANGE_LOG_STATE_VERSION_INDEX}\"\n WHERE \"stateVersion\" > ? AND \"pos\" < 0\n ORDER BY \"stateVersion\" ASC, \"pos\" ASC LIMIT 1`,\n prevVersion,\n );\n }\n\n /**\n * The entries of `table` logged after `prevVersion`, in the order that\n * {@link changesSince} reads them. For a table with few rows.\n */\n entriesSince(\n table: string,\n prevVersion: string,\n ): (ChangeLogPosition & {rowKey: string | null; op: string})[] {\n return this.db.all(\n `SELECT \"stateVersion\", \"pos\", \"rowKey\", \"op\" FROM \"_zero.changeLog2\" INDEXED BY \"${CHANGE_LOG_TABLE_INDEX}\"\n WHERE \"table\" = ? AND \"stateVersion\" > ?\n ORDER BY \"stateVersion\" ASC, \"pos\" ASC`,\n table,\n prevVersion,\n );\n }\n\n /**\n * @param tables If set, only the entries of these tables, and the\n * table-wide ops of every table, are read.\n */\n changesSince(prevVersion: string, tables?: string | undefined) {\n // Note: The queried fields are constrained to only those that are relevant\n // to the snapshot diff, i.e. those of a ChangeLogEntry.\n const cached = this.db.statementCache.get(\n tables === undefined\n ? `SELECT \"stateVersion\", \"table\", \"rowKey\", \"op\" FROM \"_zero.changeLog2\"\n WHERE \"stateVersion\" > ? ORDER BY \"stateVersion\" ASC, \"pos\" ASC`\n : `SELECT \"stateVersion\", \"table\", \"rowKey\", \"op\" FROM \"_zero.changeLog2\"\n WHERE \"stateVersion\" > ? AND ${CHANGE_LOG_TABLES_FILTER}\n ORDER BY \"stateVersion\" ASC, \"pos\" ASC`,\n );\n // Raw rows skip building a row object per entry. See RawChangeLogEntry.\n cached.statement.raw(true);\n return {\n changes:\n tables === undefined\n ? cached.statement.iterate<RawChangeLogEntry>(prevVersion)\n : cached.statement.iterate<RawChangeLogEntry>(\n prevVersion,\n RESET_OP,\n TRUNCATE_OP,\n tables,\n ),\n cleanup: () => this.db.statementCache.return(cached),\n };\n }\n\n /**\n * Reads the row with the given `rowKey`, or `undefined` if it does not\n * exist.\n *\n * @param cache An optional {@link SnapshotRowCache} through which to\n * perform the read, along with the `tag` that identifies the\n * version(s) of the replica that the result depends on.\n */\n getRow(\n table: LiteTableSpecWithKeysAndVersion,\n rowKey: JSONValue,\n cache?: SnapshotRowCache,\n tag?: string,\n ) {\n const key = normalizedKeyOrder(rowKey as RowKey);\n const cols = Object.keys(key);\n const reads = readsFor(table);\n const sql = getOrInsertComputed(\n reads.byKeyColumns,\n cols.join('\\0'),\n () => reads.prefix + cols.map(c => `${id(c)}=?`).join(' AND '),\n );\n const args = Object.values(key);\n const read = () =>\n this.db.statementCache.use(sql, cached => {\n cached.statement.safeIntegers(true);\n const row = cached.statement.get<SQLiteRow>(args);\n recordIndexUsage(cached.statement, table.name);\n return row;\n });\n return cache\n ? cache.getOrRead(tag ?? '', sql, 'get', args, () => {\n const row = read();\n if (row !== undefined) {\n this.#assertNotAdvanced(row);\n }\n return row;\n })\n : read();\n }\n\n /**\n * Reads the rows that conflict with `row` on any of the given unique\n * `keys` (which includes the row with the same primary key, if any).\n *\n * @param cache An optional {@link SnapshotRowCache} through which to\n * perform the read, along with the `tag` that identifies the\n * version(s) of the replica that the result depends on.\n */\n getRows(\n table: LiteTableSpecWithKeysAndVersion,\n keys: PrimaryKey[],\n row: SQLiteRow,\n cache?: SnapshotRowCache,\n tag?: string,\n ) {\n // Filter out keys where any column is NULL. This is both correct and\n // critical for performance:\n // 1. Correctness: NULL values can't violate uniqueness (NULL != NULL in SQL)\n // 2. Performance: SQLite's MULTI-INDEX OR optimization completely fails when\n // any branch involves NULL, falling back to a full table scan. This was\n // causing slowdowns of hundreds of times on tables with nullable unique columns.\n let validKeys: PrimaryKey[] = keys;\n let mask = (1 << keys.length) - 1;\n\n for (let i = 0; i < keys.length; i++) {\n const key = keys[i];\n for (let j = 0; j < key.length; j++) {\n const val = row[key[j]];\n if (val === null || val === undefined) {\n validKeys = [];\n mask = 0;\n for (let k = 0; k < keys.length; k++) {\n const candidate = keys[k];\n if (\n candidate.every(\n col => row[col] !== null && row[col] !== undefined,\n )\n ) {\n validKeys.push(candidate);\n mask |= 1 << k;\n }\n }\n break;\n }\n }\n if (validKeys !== keys) {\n break;\n }\n }\n\n if (validKeys.length === 0) {\n return [];\n }\n const reads = readsFor(table);\n const buildSQL = () =>\n reads.prefix +\n validKeys\n .map(key => key.map(c => `${id(c)}=?`).join(' AND '))\n .join(' OR ');\n const sql =\n keys === table.uniqueKeys && keys.length <= MAX_MEMOIZED_UNIQUE_KEYS\n ? getOrInsertComputed(reads.byUniqueKeys, mask, buildSQL)\n : buildSQL();\n const args: unknown[] = [];\n for (let i = 0; i < validKeys.length; i++) {\n const key = validKeys[i];\n for (let j = 0; j < key.length; j++) {\n args.push(row[key[j]]);\n }\n }\n const read = () =>\n this.db.statementCache.use(sql, cached => {\n cached.statement.safeIntegers(true);\n const rows = cached.statement.all<SQLiteRow>(args);\n recordIndexUsage(cached.statement, table.name);\n return rows;\n });\n return cache\n ? cache.getOrRead(tag ?? '', sql, 'all', args, () => {\n const rows = read();\n for (const row of rows) {\n this.#assertNotAdvanced(row);\n }\n return rows;\n })\n : read();\n }\n\n /**\n * Rows are shared with other client groups via the SnapshotRowCache, so\n * one read from a connection that has advanced past this snapshot (i.e. a\n * {@link SnapshotDiff} used after its Snapshotter advanced) must fail\n * before it is cached, rather than be served to the valid Diffs of other\n * client groups (whose own validity checks would then fail).\n */\n #assertNotAdvanced(row: SQLiteRow) {\n if ((row[ROW_VERSION] ?? '~') > this.version) {\n throw new InvalidDiffError(\n `Diff is no longer valid. db has advanced past ${this.version}.`,\n );\n }\n }\n\n /**\n * Whether the snapshot has been released, or its connection reset to a\n * newer snapshot (i.e. via {@link release()} or {@link resetToHead()}),\n * after which reads no longer reflect {@link version}.\n */\n get reset(): boolean {\n return this.#reset;\n }\n\n /**\n * Ends the snapshot's read transaction, after which reads no longer\n * reflect {@link version}. An open read transaction keeps SQLite from\n * checkpointing the write-ahead log past the snapshot, so a snapshot that\n * is no longer read should not be left open.\n */\n release(): void {\n if (!this.#reset) {\n this.#reset = true;\n this.db.rollback();\n // The connection is put back into a transaction, which holds nothing\n // until it reads. A snapshot's connection is writable, and outside of\n // a transaction anything that is run on it would be committed to the\n // replica under the replica's write lock, which only the replicator\n // should take.\n this.db.beginConcurrent();\n }\n }\n\n resetToHead(): Snapshot {\n this.release();\n return new Snapshot(this.db, this.#appID, true);\n }\n}\n\nclass Diff implements SnapshotDiff {\n readonly #permissionsTable: string;\n readonly #syncableTables: Map<string, LiteAndZqlSpec>;\n readonly #allTableNames: Set<string>;\n readonly #observedTables: TableFilter | undefined;\n readonly #rowCache: SnapshotRowCache | undefined;\n /** The JSON array of the names of the tables whose entries are read. */\n readonly #readTables: string | undefined;\n #permissionsChanged: boolean | undefined;\n #nothingToRead = false;\n #prevWrites: PrevWrites;\n readonly #prevTagUniform: string;\n readonly #prevTagDivergent: string;\n #iterated = false;\n readonly prev: Snapshot;\n readonly curr: Snapshot;\n readonly changes: number;\n changesScanned = 0;\n readonly changesScannedByTable = new Map<string, number>();\n changesSkipped = 0;\n changesLookedUp = 0;\n readonly stats: DiffTimingStats = {\n changeLogReadMs: 0,\n rowReadMs: 0,\n parseMs: 0,\n totalDiffMs: 0,\n };\n\n constructor(\n appID: string,\n syncableTables: Map<string, LiteAndZqlSpec>,\n allTableNames: Set<string>,\n prev: Snapshot,\n curr: Snapshot,\n observedTables?: TableFilter | undefined,\n rowCache?: SnapshotRowCache | undefined,\n prevWrites: PrevWrites = 'uniform',\n counts?: ChangeLogCounts | undefined,\n readTables?: Iterable<string> | undefined,\n ) {\n this.#prevWrites = prevWrites;\n this.#permissionsTable = `${appID}.permissions`;\n // A change of the permissions is always looked for.\n const read = readTables && new Set(readTables).add(this.#permissionsTable);\n this.#readTables = read && JSON.stringify([...read]);\n this.#syncableTables = syncableTables;\n this.#allTableNames = allTableNames;\n this.#observedTables = observedTables;\n this.prev = prev;\n this.curr = curr;\n this.#prevTagUniform = `p:${prev.version}`;\n this.#prevTagDivergent = `p:${prev.version}:${curr.version}`;\n if (counts === undefined) {\n assert(read === undefined, 'readTables requires counts');\n this.changes = curr.numChangesSince(prev.version);\n } else {\n // Without an entry of a table that is read, or a table-wide op (which\n // aborts the diff whatever table it is for), there is nothing to read.\n const hasEntries = (table: string) => this.#hasEntries(counts, table);\n this.#nothingToRead =\n counts.total === 0 ||\n (read !== undefined &&\n counts.tableWideOps === 0 &&\n ![...read].some(hasEntries));\n // As without counts, this is the number of entries of every table.\n this.changes = this.#nothingToRead ? 0 : counts.total;\n }\n // Row values are only shared (via the cache) between Diffs that do not\n // straddle a table-wide op. This guarantees that any two Diffs that\n // encounter the same change log entry read the row from tables with\n // the same schema (and contents), since a RESET or TRUNCATE between\n // their `curr` versions would necessarily fall within the range of the\n // later Diff. (Such a Diff aborts with a ResetPipelinesSignal anyway.)\n const hasTableWideOp = counts\n ? counts.tableWideOps > 0\n : curr.hasTableWideOpSince(prev.version);\n this.#rowCache = rowCache && !hasTableWideOp ? rowCache : undefined;\n }\n\n /**\n * Whether the diff has an entry of `table`. Sampled counts cannot show\n * that a table has none, so the answer is then `true`, except for the\n * permissions table: it has few rows, so whether it has an entry is\n * looked up instead.\n */\n #hasEntries(counts: ChangeLogCounts, table: string): boolean {\n if (!counts.sampled) {\n return (counts.byTable.get(table) ?? 0) > 0;\n }\n if (table !== this.#permissionsTable) {\n return true;\n }\n return (this.#permissionsChanged ??= this.curr.tableChangedSince(\n table,\n this.prev.version,\n ));\n }\n\n setPrevWrites(prevWrites: PrevWrites): void {\n assert(\n !this.#iterated || prevWrites === 'divergent',\n 'prevWrites must be set before iterating the diff',\n );\n this.#prevWrites = prevWrites;\n }\n\n [Symbol.iterator](): Iterator<Change> {\n this.#iterated = true;\n if (this.#nothingToRead) {\n return [][Symbol.iterator]();\n }\n const {changes, cleanup: done} = this.curr.changesSince(\n this.prev.version,\n this.#readTables,\n );\n\n const cleanup = () => {\n try {\n // Allow open iterators to clean up their state.\n changes.return?.(undefined);\n } finally {\n done();\n }\n };\n\n return {\n next: () => {\n const nextStart = performance.now();\n try {\n for (;;) {\n const clStart = performance.now();\n const {value, done} = changes.next();\n this.stats.changeLogReadMs += performance.now() - clStart;\n if (done) {\n cleanup();\n return {value, done: true};\n }\n\n this.changesScanned++;\n const parseStart = performance.now();\n const {table, rowKey, op, stateVersion} = parseChangeLogEntry(\n ...value,\n );\n this.stats.parseMs += performance.now() - parseStart;\n this.changesScannedByTable.set(\n table,\n (this.changesScannedByTable.get(table) ?? 0) + 1,\n );\n if (op === RESET_OP) {\n // The current map of `TableSpec`s may not have the correct or complete information.\n throw new ResetPipelinesSignal(\n `schema for table ${table} has changed`,\n 'schema-change',\n );\n }\n if (op === TRUNCATE_OP) {\n // Truncates are also processed by rehydrating pipelines at current.\n throw new ResetPipelinesSignal(\n `table ${table} has been truncated`,\n 'truncation',\n );\n }\n if (\n this.#observedTables &&\n !this.#observedTables.has(table) &&\n table !== this.#permissionsTable\n ) {\n if (this.#allTableNames.has(table)) {\n this.changesSkipped++;\n continue; // skip change log entries for unobserved tables.\n }\n throw new Error(`change for unknown table ${table}`);\n }\n const specs = this.#syncableTables.get(table);\n if (!specs) {\n if (this.#allTableNames.has(table)) {\n this.changesSkipped++;\n continue; // skip change log entries for non-syncable tables.\n }\n throw new Error(`change for unknown table ${table}`);\n }\n const {tableSpec, zqlSpec} = specs;\n\n // Sanity check: All change log ops should have a stateVersion\n // greater than minRowVersion in the table metadata. This is a\n // mini-proof that the overlay does not need to be applied to\n // rows produced in incremental catchup, based on the invariant in\n // change-processor's #bumpVersions(), whereby the setting of the\n // minRowVersion is always followed by a RESET OP, meaning that\n // subsequent change-log traversal happens at a later version.\n assert(\n (tableSpec.minRowVersion ?? '') < stateVersion,\n () =>\n `unexpected change @${stateVersion} for table ${table} with ` +\n `minRowVersion ${tableSpec.minRowVersion}: ${op}(${rowKey})`,\n );\n\n assert(rowKey !== null, 'rowKey must be present for row changes');\n\n // Row reads are shared with the other client groups on the\n // worker via the SnapshotRowCache (if configured):\n //\n // * The new value of the row is read from `curr`, which is\n // never written to during the iteration, and is the version\n // of the row at `stateVersion` (as verified by\n // checkThatDiffIsValid()). It is thus identical for every\n // Diff that encounters the change log entry.\n //\n // * Previous values are read from `prev`, at `prev.version`,\n // which the caller (i.e. IVM) writes to as the iteration\n // progresses. For a table whose only unique key is its\n // primary key, no change applied before this one can affect\n // this row, so the result depends solely on `prev.version`.\n // Otherwise, the result can include rows that conflict on\n // other unique keys, which may have been added or removed by\n // preceding changes; see PrevWrites for when that can be\n // shared.\n // The first advance() after this Diff was created resets the\n // `prev` connection to head. Reads would then no longer reflect\n // `prev.version`, and cache hits would not reveal it (the cached\n // rows were read by valid Diffs), so this must be checked\n // explicitly rather than inferred from the rows' versions.\n if (this.prev.reset) {\n throw new InvalidDiffError(\n `Diff is no longer valid. prev db has advanced past ${this.prev.version}.`,\n );\n }\n\n const cache = this.#rowCache;\n const prevDependsOnWrites =\n tableSpec.uniqueKeys.length > 1 && this.#prevWrites !== 'none';\n const prevCache =\n prevDependsOnWrites && this.#prevWrites === 'divergent'\n ? undefined\n : cache;\n const prevTag = prevDependsOnWrites\n ? this.#prevTagDivergent\n : this.#prevTagUniform;\n this.changesLookedUp++;\n const rowStart = performance.now();\n const nextValue =\n op === SET_OP\n ? this.curr.getRow(\n tableSpec,\n rowKey,\n cache,\n `n:${stateVersion}`,\n )\n : null;\n let prevValues;\n if (nextValue) {\n prevValues = this.prev.getRows(\n tableSpec,\n tableSpec.uniqueKeys,\n nextValue,\n prevCache,\n prevTag,\n );\n } else {\n const prevValue = this.prev.getRow(\n tableSpec,\n rowKey,\n prevCache,\n prevTag,\n );\n prevValues = prevValue ? [prevValue] : [];\n }\n this.stats.rowReadMs += performance.now() - rowStart;\n if (nextValue === undefined) {\n throw new Error(\n `Missing value for ${table} ${stringify(rowKey)}`,\n );\n }\n // Sanity check detects if the diff is being accessed after the Snapshots have advanced.\n this.checkThatDiffIsValid(stateVersion, op, prevValues, nextValue);\n\n if (prevValues.length === 0 && nextValue === null) {\n // Filter out no-op changes (e.g. a delete of a row that does not exist in prev).\n // TODO: Consider doing this for deep-equal values.\n this.changesSkipped++;\n continue;\n }\n\n if (table === this.#permissionsTable) {\n const changed = prevValues.find(\n prevValue => prevValue.permissions !== nextValue?.permissions,\n );\n if (changed) {\n throw new ResetPipelinesSignal(\n `Permissions have changed ${changed.hash} => ${nextValue?.hash}`,\n 'permissions-change',\n );\n }\n }\n\n // Note: The raw SQLite rows may be shared with other client\n // groups via the SnapshotRowCache, so they must not be modified.\n // fromSQLiteTypes() produces new row objects.\n return {\n value: {\n table,\n prevValues: prevValues.map(prevValue =>\n fromSQLiteTypes(zqlSpec, prevValue, table),\n ),\n nextValue: nextValue\n ? fromSQLiteTypes(zqlSpec, nextValue, table)\n : null,\n rowKey,\n } satisfies Change,\n };\n }\n } catch (e) {\n // This control flow path is not covered by the return() method (i.e. `break`).\n cleanup();\n throw e;\n } finally {\n this.stats.totalDiffMs += performance.now() - nextStart;\n }\n },\n\n return: (value: unknown) => {\n cleanup();\n return {value, done: true};\n },\n };\n }\n\n checkThatDiffIsValid(\n stateVersion: string,\n op: string,\n prevValues: readonly SQLiteRow[],\n nextValue: SQLiteRow | null,\n ) {\n // Sanity checks to detect that the diff is not being accessed after\n // the Snapshots have advanced.\n if (stateVersion > this.curr.version) {\n throw new InvalidDiffError(\n `Diff is no longer valid. curr db has advanced past ${this.curr.version}`,\n );\n }\n if (\n prevValues.findIndex(\n prevValue => (prevValue[ROW_VERSION] ?? '~') > this.prev.version,\n ) !== -1\n ) {\n throw new InvalidDiffError(\n `Diff is no longer valid. prev db has advanced past ${this.prev.version}.`,\n );\n }\n if (op === SET_OP && nextValue?.[ROW_VERSION] !== stateVersion) {\n throw new InvalidDiffError(\n 'Diff is no longer valid. curr db has advanced.',\n );\n }\n }\n}\n\nexport class InvalidDiffError extends Error {\n constructor(msg: string) {\n super(msg);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2FA,IAAa,cAAb,MAAyB;CACvB;CACA;CACA;CACA;CACA;CACA;CACA;;;;;;;CAQA,YACE,IACA,QACA,EAAC,SACD,kBACA,UACA;EACA,KAAK,MAAM;EACX,KAAK,UAAU;EACf,KAAK,SAAS;EACd,KAAK,oBAAoB;EACzB,KAAK,YAAY;CACnB;;;;;;CAOA,OAAa;EACX,OAAO,KAAK,UAAU,KAAA,GAAW,qBAAqB;EACtD,KAAK,QAAQ,SAAS,OACpB,KAAK,KACL,KAAK,SACL,KAAK,QACL,KAAK,iBACP;EACA,KAAK,IAAI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,GAAG;CACX;AACF"}
|
|
1
|
+
{"version":3,"file":"snapshotter.js","names":[],"sources":["../../../../../../zero-cache/src/services/view-syncer/snapshotter.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {assert} from '../../../../shared/src/asserts.ts';\nimport {stringify, type JSONValue} from '../../../../shared/src/bigint-json.ts';\nimport {getOrInsertComputed} from '../../../../shared/src/map.ts';\nimport type {Row} from '../../../../zero-protocol/src/data.ts';\nimport type {PrimaryKey} from '../../../../zero-types/src/schema.ts';\nimport {Database} from '../../../../zqlite/src/db.ts';\nimport {recordIndexUsage} from '../../../../zqlite/src/index-usage.ts';\nimport {\n fromSQLiteTypes,\n type SQLiteRow,\n} from '../../../../zqlite/src/table-source.ts';\nimport type {\n LiteAndZqlSpec,\n LiteTableSpecWithKeysAndVersion,\n} from '../../db/specs.ts';\nimport {StatementRunner} from '../../db/statements.ts';\nimport {normalizedKeyOrder, type RowKey} from '../../types/row-key.ts';\nimport type {AppID} from '../../types/shards.ts';\nimport {id} from '../../types/sql.ts';\nimport {\n RESET_OP,\n parseChangeLogEntry,\n SET_OP,\n TRUNCATE_OP,\n} from '../replicator/schema/change-log.ts';\nimport {\n getReplicationState,\n ZERO_VERSION_COLUMN_NAME as ROW_VERSION,\n} from '../replicator/schema/replication-state.ts';\nimport type {SnapshotRowCache} from './snapshot-row-cache.ts';\n\n/**\n * A `Snapshotter` manages the progression of database snapshots for a\n * ViewSyncer.\n *\n * The Replicator and ViewSyncers operate on the same SQLite file, with the\n * Replicator being the sole writer to the database. The IVM logic in\n * ViewSyncers, however, rely on incrementally applying changes to the DB to\n * update the state of its pipelines.\n *\n * To avoid coupling the progress of the Replicator and all IVM pipelines on\n * each other, ViewSyncers operate on ephemeral forks of the database by holding\n * [concurrent](https://sqlite.org/src/doc/begin-concurrent/doc/begin_concurrent.md)\n * snapshots of the database and simulating (but ultimately rolling back)\n * mutations on these snapshots.\n *\n * Example:\n * 1. ViewSyncer takes `snapshot_a` at version `t1` of the database and\n * hydrates its pipeline(s).\n * 2. Replicator applies a new transaction to the database and notifies\n * subscribers.\n * 3. ViewSyncer takes `snapshot_b` at `t2`, and queries the `ChangeLog` at\n * that snapshot for changes since `t1`.\n * 4. ViewSyncer applies those changes to `snapshot_a` for IVM, but does not\n * commit them. (Recall that the Replicator is the sole writer to the db, so\n * the ViewSyncer never commits any writes.)\n * 5. Replicator applies the next transaction and advances the database to `t3`.\n * 6. ViewSyncer rolls back `snapshot_a` and opens `snapshot_c` at `t3`, using\n * `snapshot_b` to simulate changes from `t2` to `t3`.\n *\n * ```\n * Replicator: t1 --------------> t2 --------------> t3 --------------->\n * ViewSyncer: [snapshot_a] ----> [snapshot_b] ----> [snapshot_c]\n * ```\n *\n * Note that the Replicator (and ViewSyncers) do not wait on the progress of\n * other ViewSyncers. If a ViewSyncer is busy hydrating at `t1`, the Replicator\n * and other ViewSyncers can progress through `t2`, `t3`, etc. independently,\n * as the busy ViewSyncer simply takes its own snapshot when it is ready.\n *\n * ```\n * Replicator: t1 --------------> t2 --------------> t3 --------------->\n * ViewSyncer1: [snapshot_a] ----> [snapshot_b] ----> [snapshot_c]\n * ViewSyncer2: [.......... snapshot_a ..........] ----> [snapshot_b]\n * ```\n *\n * To minimize Database connections (and statement preparation, etc.), the\n * Snapshotter reuses the connection from the previous (rolled back)\n * snapshot when opening the new one.\n *\n * ```\n * Replicator: t1 --------------> t2 --------------> t3 --------------->\n * ViewSyncer: [snapshot_a] ----> [snapshot_b] ----> [snapshot_c]\n * (conn_1) (conn_2) (conn_1)\n * ```\n *\n * In this manner, each ViewSyncer uses two connections that continually\n * \"leapfrog\" each other to replay the timeline of changes in isolation from\n * the Replicator and other ViewSyncers.\n */\nexport class Snapshotter {\n readonly #lc: LogContext;\n readonly #dbFile: string;\n readonly #appID: string;\n readonly #pageCacheSizeKib: number | undefined;\n readonly #rowCache: SnapshotRowCache | undefined;\n #curr: Snapshot | undefined;\n #prev: Snapshot | undefined;\n\n /**\n * @param rowCache An optional worker-wide {@link SnapshotRowCache} through\n * which the row reads performed when iterating over a\n * {@link SnapshotDiff} are shared with the other Snapshotters\n * (i.e. client groups) on the worker.\n */\n constructor(\n lc: LogContext,\n dbFile: string,\n {appID}: AppID,\n pageCacheSizeKib?: number,\n rowCache?: SnapshotRowCache,\n ) {\n this.#lc = lc;\n this.#dbFile = dbFile;\n this.#appID = appID;\n this.#pageCacheSizeKib = pageCacheSizeKib;\n this.#rowCache = rowCache;\n }\n\n /**\n * Initializes the snapshot to the current head of the database. This must be\n * only be called once. The state of whether a Snapshotter has been initialized\n * can be determined by calling {@link initialized()}.\n */\n init(): this {\n assert(this.#curr === undefined, 'Already initialized');\n this.#curr = Snapshot.create(\n this.#lc,\n this.#dbFile,\n this.#appID,\n this.#pageCacheSizeKib,\n );\n this.#lc.debug?.(`Initial snapshot at version ${this.#curr.version}`);\n return this;\n }\n\n initialized(): boolean {\n return this.#curr !== undefined;\n }\n\n /** Returns the current snapshot. Asserts if {@link initialized()} is false. */\n current(): Snapshot {\n assert(this.#curr !== undefined, 'Snapshotter has not been initialized');\n return this.#curr;\n }\n\n /**\n * Advances to the head of the Database, returning a diff between the\n * previously current Snapshot and a new Snapshot at head. This is called\n * in response to a notification from a Replicator subscription. Subsequent\n * calls to {@link current()} return the new Snapshot. Note that the Snapshotter\n * must be initialized before advancing.\n *\n * The returned {@link SnapshotDiff} contains snapshots at the endpoints\n * of the database timeline. Iterating over the diff generates a sequence\n * of {@link Change}s between the two snapshots.\n *\n * Note that this sequence is not chronological; rather, the sequence is\n * ordered by `<table, row-key>`, such that a row can appear at most once\n * in the common case, or twice if its table is `TRUNCATE`'d and a new value\n * is subsequently `INSERT`'ed. This results in dropping most intermediate\n * changes to a row and bounds the amount of work needed to catch up;\n * however, as a consequence, a consistent database state is only guaranteed\n * when the sequence has been fully consumed.\n *\n * Note that Change generation relies on the state of the underlying\n * database connections, and because the connection for the previous snapshot\n * is reused to produce the next snapshot, the diff object is only valid\n * until the next call to `advance()`.\n *\n * It is okay for the caller to apply `Change`s to the `prev` snapshot\n * during the iteration (e.g. this is necessary for IVM); the remainder\n * of the iteration is not affected because a given row can appear at most\n * once in the sequence (with the exception being TRUNCATE, after which the\n * deleted rows can be re-inserted, but this will also behave correctly if\n * the changes are applied).\n *\n * Once the changes have been applied, however, a _subsequent_ iteration\n * will not produce the correct results. In order to perform multiple\n * change-applying iterations, the caller must (1) create a save point\n * on `prev` before each iteration, and (2) rollback to the save point after\n * the iteration.\n *\n * @param prevWrites How the caller writes to `prev` during the iteration,\n * which determines which `prev` reads can be shared through the\n * {@link SnapshotRowCache}. See {@link PrevWrites}.\n */\n advance(\n syncableTables: Map<string, LiteAndZqlSpec>,\n allTableNames: Set<string>,\n observedTables?: TableFilter | undefined,\n prevWrites: PrevWrites = 'uniform',\n ): SnapshotDiff {\n this.advanceWithoutDiff();\n return this.diff(syncableTables, allTableNames, observedTables, prevWrites);\n }\n\n /**\n * The diff between the two snapshots of the last\n * {@link advanceWithoutDiff()}, as {@link advance()} returns it.\n *\n * @param counts The counts of the change log entries between the two\n * snapshots, if the caller has counted them already (see\n * `Snapshot.countChangesSince()`). The diff then does not count\n * them again.\n * @param readTables If set, only the change log entries of these tables\n * (and of the permissions table) are read, along with the\n * table-wide ops of every table, where `observedTables` reads the\n * entries of every table and skips those it does not have. For a\n * caller that does not need the changes of the other tables, this\n * saves reading and parsing their entries. This requires `counts`.\n * If they show that there is no entry of these tables and no\n * table-wide op, the change log is not read at all, and the\n * diff's `changes` is 0.\n */\n diff(\n syncableTables: Map<string, LiteAndZqlSpec>,\n allTableNames: Set<string>,\n observedTables?: TableFilter | undefined,\n prevWrites: PrevWrites = 'uniform',\n counts?: ChangeLogCounts | undefined,\n readTables?: Iterable<string> | undefined,\n ): SnapshotDiff {\n assert(\n this.#prev !== undefined && this.#curr !== undefined,\n 'Snapshotter has not been advanced',\n );\n return new Diff(\n this.#appID,\n syncableTables,\n allTableNames,\n this.#prev,\n this.#curr,\n observedTables,\n this.#rowCache,\n prevWrites,\n counts,\n readTables,\n );\n }\n\n advanceWithoutDiff() {\n assert(this.#curr !== undefined, 'Snapshotter has not been initialized');\n const next = this.#prev\n ? this.#prev.resetToHead()\n : Snapshot.create(\n this.#lc,\n this.#curr.db.db.name,\n this.#appID,\n this.#pageCacheSizeKib,\n );\n this.#prev = this.#curr;\n this.#curr = next;\n return {prev: this.#prev, curr: this.#curr};\n }\n\n /**\n * The reset of the pipelines that iterating over the changes from the\n * previous snapshot to the current one ends in, if it does: the first of\n * a schema change, a truncation, and a change of the permissions. It is\n * found without iterating, so that a caller that would reset the pipelines\n * anyway does not first process the changes that come before it.\n *\n * If there is none, iterating over a {@link diff()} of the two snapshots\n * does not throw a {@link ResetPipelinesSignal}, and can be stopped at any\n * point without missing one.\n *\n * @param counts The changes from the previous snapshot to the current\n * one, from which it is known whether there is anything to look\n * for.\n */\n pendingReset(\n syncableTables: Map<string, LiteAndZqlSpec>,\n counts: ChangeLogCounts,\n ): ResetPipelinesSignal | undefined {\n assert(\n this.#prev !== undefined && this.#curr !== undefined,\n 'Snapshotter has not been advanced',\n );\n const prev = this.#prev;\n const curr = this.#curr;\n let first: (ChangeLogPosition & {signal: ResetPipelinesSignal}) | undefined;\n\n if (counts.tableWideOps > 0) {\n // Iterating aborts at a table-wide op whatever its table is.\n const {stateVersion, pos, table, op} = curr.firstTableWideOpSince(\n prev.version,\n );\n first = {\n stateVersion,\n pos,\n signal:\n op === TRUNCATE_OP\n ? new ResetPipelinesSignal(\n `table ${table} has been truncated`,\n 'truncation',\n )\n : new ResetPipelinesSignal(\n `schema for table ${table} has changed`,\n 'schema-change',\n ),\n };\n }\n\n // An entry of the permissions table only aborts the iteration if the\n // permissions are different, which takes reading the row to know.\n const permissionsTable = `${this.#appID}.permissions`;\n const specs = syncableTables.get(permissionsTable);\n if (\n specs &&\n (counts.sampled || (counts.byTable.get(permissionsTable) ?? 0) > 0)\n ) {\n const {tableSpec} = specs;\n for (const entry of curr.entriesSince(permissionsTable, prev.version)) {\n if (first && !isBefore(entry, first)) {\n break;\n }\n const rowKey: JSONValue = entry.rowKey && JSON.parse(entry.rowKey);\n if (rowKey === null) {\n continue;\n }\n const nextValue =\n entry.op === SET_OP ? curr.getRow(tableSpec, rowKey) : null;\n let prevValues;\n if (nextValue) {\n prevValues = prev.getRows(tableSpec, tableSpec.uniqueKeys, nextValue);\n } else {\n const prevValue = prev.getRow(tableSpec, rowKey);\n prevValues = prevValue ? [prevValue] : [];\n }\n const changed = prevValues.find(\n prevValue => prevValue.permissions !== nextValue?.permissions,\n );\n if (changed) {\n first = {\n ...entry,\n signal: new ResetPipelinesSignal(\n `Permissions have changed ${changed.hash} => ${nextValue?.hash}`,\n 'permissions-change',\n ),\n };\n break;\n }\n }\n }\n return first?.signal;\n }\n\n /**\n * Releases the previous snapshot, i.e. the `prev` of the last\n * {@link advance()} or {@link advanceWithoutDiff()}. It is only read\n * while the diff that it is the start of is iterated over, but its read\n * transaction otherwise stays open until the next advance, which keeps\n * the replica from checkpointing its write-ahead log past it. A diff that\n * starts at it can no longer be iterated over.\n */\n releasePrevious(): void {\n this.#prev?.release();\n }\n\n /**\n * Call this to close the database connections when the Snapshotter is\n * no longer needed.\n */\n destroy() {\n // The replica's statistics are maintained by the replicator. The\n // `PRAGMA optimize` that closing a writable connection otherwise runs\n // analyzes the tables that the connection read, which blocks the worker\n // for as long as that takes, and what it finds is discarded with the\n // snapshot's transaction.\n this.#curr?.db.db.close({optimize: false});\n this.#prev?.db.db.close({optimize: false});\n this.#lc.debug?.('closed database connections');\n }\n}\n\nexport type Change = {\n readonly table: string;\n /**\n * If this change represents a remove the row to remove,\n * if nextValue is not null then all rows that have a unique constraint\n * violation with nextValue.\n * In both cases these rows should be removed.\n */\n readonly prevValues: Readonly<Row>[];\n readonly nextValue: Readonly<Row> | null;\n readonly rowKey: RowKey;\n};\n\n/** See `Snapshot.countChangesSince()`. */\n/** An entry of {@link Snapshot.changesSince}, in the order of its SELECT. */\ntype RawChangeLogEntry = readonly [\n stateVersion: string,\n table: string,\n rowKey: string,\n op: string,\n];\n\n/** Where an entry is in the order that the change log is read in. */\ntype ChangeLogPosition = {\n readonly stateVersion: string;\n readonly pos: number;\n};\n\nfunction isBefore(a: ChangeLogPosition, b: ChangeLogPosition): boolean {\n return a.stateVersion === b.stateVersion\n ? a.pos < b.pos\n : a.stateVersion < b.stateVersion;\n}\n\nexport type ChangeLogCounts = {\n /** The number of entries. */\n readonly total: number;\n /** The number of them that are table-wide ops (a RESET or TRUNCATE). */\n readonly tableWideOps: number;\n /**\n * The number of entries of each table. If {@link sampled}, these are\n * estimates, and a table with few entries may be missing.\n */\n readonly byTable: ReadonlyMap<string, number>;\n /** Whether {@link byTable} was estimated from a sample of the entries. */\n readonly sampled: boolean;\n};\n\n/**\n * The most change log entries that are read to count a batch's changes by\n * table. Reading an entry costs microseconds, and nothing else runs on the\n * sync worker in the meantime, so the cost of counting must not grow with\n * the number of changes that a client group has to catch up on.\n */\nconst CHANGE_LOG_SAMPLE_SIZE = 1000;\n\nexport interface TableFilter {\n has(table: string): boolean;\n}\n\n/**\n * How the caller of {@link Snapshotter.advance()} writes the changes of a\n * {@link SnapshotDiff} to its `prev` snapshot while iterating over it.\n *\n * A `prev` read for a table with a single unique key (its primary key)\n * cannot observe earlier changes of the iteration, since a row key appears\n * once in the diff. A read for a table with other unique keys can, because\n * the rows it returns may have been displaced or edited by earlier changes.\n *\n * * `uniform`: every caller applies every change, so such a read depends on\n * the `<prev, curr>` versions of the diff, and is shared on that basis.\n * * `divergent`: callers apply different subsets of the changes (e.g. the\n * pipeline driver skips changes that no pipeline can observe), so such a\n * read depends on the caller and is not shared.\n * * `none`: the caller does not write to `prev` (e.g. IVM derivation is\n * held in memory), so every read depends solely on `prev.version`.\n */\nexport type PrevWrites = 'uniform' | 'divergent' | 'none';\n\n/**\n * Represents the difference between two database Snapshots.\n * Iterating over the object will produce a sequence of {@link Change}s\n * between the two snapshots.\n *\n * See {@link Snapshotter.advance()} for semantics and usage.\n */\ntype DiffTimingStats = {\n changeLogReadMs: number;\n rowReadMs: number;\n parseMs: number;\n totalDiffMs: number;\n};\n\nexport interface SnapshotDiff extends Iterable<Change> {\n readonly prev: {\n readonly db: StatementRunner;\n readonly version: string;\n };\n readonly curr: {\n readonly db: StatementRunner;\n readonly version: string;\n };\n\n /**\n * The raw number of ChangeLog entries between the snapshots, before filtering\n * out unobserved/non-syncable tables or no-op changes. Progress against this\n * total must use {@link changesScanned}, rather than the number of yielded\n * changes. Table-wide operations abort iteration and reset the pipelines.\n */\n readonly changes: number;\n\n /** The number of raw ChangeLog entries read so far, including skipped entries. */\n readonly changesScanned: number;\n\n /**\n * The raw ChangeLog entries read so far, by table. Like\n * {@link changesScanned}, this includes the entries that are not yielded,\n * so it counts the same entries as {@link ChangeLogCounts.byTable} does.\n */\n readonly changesScannedByTable: ReadonlyMap<string, number>;\n\n /** The scanned entries omitted because their table is unobserved/non-syncable or the change is a no-op. */\n readonly changesSkipped: number;\n\n /**\n * The scanned entries whose rows were looked up in the snapshots. Unlike\n * the entries that are yielded, this includes the no-op changes, which are\n * only known to be that from their rows.\n */\n readonly changesLookedUp: number;\n\n /** Timing metrics recorded during diff iteration. */\n readonly stats?: DiffTimingStats | undefined;\n\n /**\n * Overrides the `prevWrites` passed to {@link Snapshotter.advance()}, for a\n * caller that decides how to write to `prev` once it knows the number of\n * {@link changes}. Must be called before the diff is iterated, except to\n * switch to `divergent`, which can be done between any two changes: it\n * shares only the reads that no write to `prev` can affect.\n */\n setPrevWrites(prevWrites: PrevWrites): void;\n}\n\n/**\n * Thrown during an iteration of a {@link SnapshotDiff} when a schema\n * change or truncate is encountered, which result in aborting the\n * advancement and resetting / rehydrating the pipelines.\n */\nexport type ResetPipelinesReason =\n | 'advancement-timeout'\n | 'scalar-subquery'\n | 'schema-change'\n | 'truncation'\n | 'permissions-change';\n\nexport class ResetPipelinesSignal extends Error {\n readonly name = 'ResetPipelinesSignal';\n readonly reason: ResetPipelinesReason;\n readonly breakdown?: string | undefined;\n\n constructor(msg: string, reason: ResetPipelinesReason, breakdown?: string) {\n super(msg);\n this.reason = reason;\n this.breakdown = breakdown;\n }\n}\n\n// Memoizes, per table spec, the SQL of the row lookups performed for each\n// change, which would otherwise be rebuilt from the table spec for every\n// read. Handing out the same string for every read of a given shape also\n// lets the SnapshotRowCache and the statement cache look it up without\n// rehashing it. Specs are recreated whenever they are recomputed, so stale\n// SQL is never served.\ntype TableReads = {\n /** `SELECT <columns> FROM <table> WHERE ` */\n readonly prefix: string;\n /** {@link Snapshot.getRow} SQL, by the NUL-joined key columns. */\n readonly byKeyColumns: Map<string, string>;\n /**\n * {@link Snapshot.getRows} SQL, by the bitmask of the table's `uniqueKeys`\n * that are queried.\n */\n readonly byUniqueKeys: Map<number, string>;\n};\n\nconst tableReads = new WeakMap<LiteTableSpecWithKeysAndVersion, TableReads>();\n\nfunction readsFor(table: LiteTableSpecWithKeysAndVersion): TableReads {\n return getOrInsertComputed(tableReads, table, () => ({\n prefix: `SELECT ${Object.keys(table.columns)\n .map(c => id(c))\n .join(',')} FROM ${id(table.name)} WHERE `,\n byKeyColumns: new Map(),\n byUniqueKeys: new Map(),\n }));\n}\n\n// The byUniqueKeys bitmask must fit in a (positive) 31-bit integer.\nconst MAX_MEMOIZED_UNIQUE_KEYS = 30;\n\n/**\n * The index that SQLite creates for the `PRIMARY KEY(\"stateVersion\", \"pos\")`\n * of `_zero.changeLog2` (the first constraint of its CREATE TABLE statement,\n * hence the `_1` suffix).\n *\n * For a `stateVersion > ?` query that reads a column outside of this index\n * (e.g. `op`) and has no ORDER BY, the query planner chooses between a range\n * search of the index and a full scan of the table based on `sqlite_stat1`.\n * Statistics recorded while the change log held only a few rows make the full\n * scan look cheaper, and they persist as the table grows to millions of rows:\n * the change log of a replication-manager's replica only receives entries\n * during column backfills, and view-syncers inherit its statistics when they\n * restore its backup. Such queries therefore pin this index with INDEXED BY,\n * which also makes them fail to prepare (rather than silently scan) if the\n * index is ever renamed.\n */\nconst CHANGE_LOG_STATE_VERSION_INDEX = 'sqlite_autoindex__zero.changeLog2_1';\n\n/**\n * The index that SQLite creates for the `UNIQUE(\"table\", \"rowKey\")` of\n * `_zero.changeLog2` (the second constraint of its CREATE TABLE statement).\n */\nconst CHANGE_LOG_TABLE_INDEX = 'sqlite_autoindex__zero.changeLog2_2';\n\n/**\n * Selects the change log entries of the tables in a JSON array of names,\n * and the table-wide ops (RESET and TRUNCATE) of every table, which abort\n * the diff whatever table they are for. Binds the two ops, then the array.\n * The array is a single parameter so that the statement is the same, and is\n * prepared once, for any set of tables.\n */\nconst CHANGE_LOG_TABLES_FILTER = `(\"op\" IN (?, ?) OR \"table\" IN (SELECT \"value\" FROM json_each(?)))`;\n\nclass Snapshot {\n static create(\n lc: LogContext,\n dbFile: string,\n appID: string,\n pageCacheSizeKib: number | undefined,\n ) {\n const conn = new Database(lc, dbFile);\n conn.pragma('synchronous = OFF'); // Applied changes are ephemeral; COMMIT is never called.\n if (pageCacheSizeKib !== undefined) {\n conn.pragma(`cache_size = -${pageCacheSizeKib}`); // Negative = size in KiB\n }\n const [{journal_mode: mode}] = conn.pragma('journal_mode') as [\n {journal_mode: string},\n ];\n // BEGIN CONCURRENT lets the Snapshotter simulate changes on a historic\n // snapshot without taking the WAL writer lock. These private changes are\n // always rolled back; the replicator is the only committer and can use\n // BEGIN IMMEDIATE on the same WAL2 database.\n assert(\n mode === 'wal2',\n `replica db must be in wal2 mode (current: ${mode})`,\n );\n\n const db = new StatementRunner(conn);\n return new Snapshot(db, appID);\n }\n\n readonly db: StatementRunner;\n readonly #appID: string;\n readonly version: string;\n #reset = false;\n\n /**\n * @param begun Whether `db` is already in a `BEGIN CONCURRENT` transaction\n * that has not read anything.\n */\n constructor(db: StatementRunner, appID: string, begun = false) {\n if (!begun) {\n db.beginConcurrent();\n }\n // Note: The subsequent read is necessary to acquire the read lock\n // (which results in the logical creation of the snapshot). Calling\n // `BEGIN CONCURRENT` alone does not result in acquiring the read lock.\n const {stateVersion} = getReplicationState(db);\n\n this.db = db;\n this.#appID = appID;\n this.version = stateVersion;\n }\n\n numChangesSince(prevVersion: string) {\n const {count} = this.db.get(\n 'SELECT COUNT(*) AS count FROM \"_zero.changeLog2\" WHERE stateVersion > ?',\n prevVersion,\n );\n return count;\n }\n\n /**\n * Counts the change log entries after `prevVersion`. The change log keeps\n * the latest entry of a row, so these are counts of the rows that changed,\n * which is what a diff from `prevVersion` to this snapshot reads.\n *\n * The total, and the number of table-wide ops (a RESET or TRUNCATE), are\n * exact, and are counted from the index alone. The counts by table are\n * exact if there are at most `sampleSize` entries. Otherwise they are\n * estimated from the newest `sampleSize` entries, so that at most that\n * many entries are read however many there are.\n */\n countChangesSince(\n prevVersion: string,\n sampleSize = CHANGE_LOG_SAMPLE_SIZE,\n ): ChangeLogCounts {\n // The index has the stateVersion and pos of every entry, and a\n // table-wide op is logged at pos -1 (see ChangeLog), so no entry is read.\n const {total, tableWideOps} = this.db.get(\n `SELECT COUNT(*) AS total, COALESCE(SUM(pos < 0), 0) AS tableWideOps\n FROM \"_zero.changeLog2\" INDEXED BY \"${CHANGE_LOG_STATE_VERSION_INDEX}\"\n WHERE stateVersion > ?`,\n prevVersion,\n );\n const byTable = new Map<string, number>();\n if (total === 0) {\n return {total, tableWideOps, byTable, sampled: false};\n }\n const rows: {table: string; count: number}[] = this.db.all(\n `SELECT \"table\", COUNT(*) AS count FROM (\n SELECT \"table\"\n FROM \"_zero.changeLog2\" INDEXED BY \"${CHANGE_LOG_STATE_VERSION_INDEX}\"\n WHERE stateVersion > ?\n ORDER BY stateVersion DESC, pos DESC LIMIT ?\n ) GROUP BY \"table\"`,\n prevVersion,\n sampleSize,\n );\n const sampled = total > sampleSize;\n const scale = sampled ? total / sampleSize : 1;\n for (const {table, count} of rows) {\n byTable.set(table, count * scale);\n }\n return {total, tableWideOps, byTable, sampled};\n }\n\n /**\n * Whether an entry of `table` was logged after `prevVersion`. This reads\n * the table's entries in the change log, however old, so it is for tables\n * with few rows.\n */\n tableChangedSince(table: string, prevVersion: string): boolean {\n const row = this.db.get(\n `SELECT 1 FROM \"_zero.changeLog2\" INDEXED BY \"${CHANGE_LOG_TABLE_INDEX}\"\n WHERE \"table\" = ? AND stateVersion > ? LIMIT 1`,\n table,\n prevVersion,\n );\n return row !== undefined;\n }\n\n /**\n * Whether a table-wide RESET op (i.e. a schema change) was logged after\n * `prevVersion`. Table specs computed at or before `prevVersion` are\n * stale with respect to this snapshot if so.\n */\n schemaChangedSince(prevVersion: string): boolean {\n const row = this.db.get(\n `SELECT 1 FROM \"_zero.changeLog2\" INDEXED BY \"${CHANGE_LOG_STATE_VERSION_INDEX}\"\n WHERE stateVersion > ? AND op = ? LIMIT 1`,\n prevVersion,\n RESET_OP,\n );\n return row !== undefined;\n }\n\n /**\n * Whether any table-wide op (i.e. a RESET or TRUNCATE) was logged after\n * `prevVersion`. Iterating over a diff ending at this snapshot will\n * abort with a {@link ResetPipelinesSignal} if so.\n */\n hasTableWideOpSince(prevVersion: string): boolean {\n const row = this.db.get(\n `SELECT 1 FROM \"_zero.changeLog2\" INDEXED BY \"${CHANGE_LOG_STATE_VERSION_INDEX}\"\n WHERE stateVersion > ? AND op IN (?, ?) LIMIT 1`,\n prevVersion,\n RESET_OP,\n TRUNCATE_OP,\n );\n return row !== undefined;\n }\n\n /**\n * The first table-wide op (a schema change or a truncation) logged after\n * `prevVersion`, in the order that {@link changesSince} reads the entries.\n */\n firstTableWideOpSince(\n prevVersion: string,\n ): ChangeLogPosition & {table: string; op: string} {\n return this.db.get(\n `SELECT \"stateVersion\", \"pos\", \"table\", \"op\" FROM \"_zero.changeLog2\" INDEXED BY \"${CHANGE_LOG_STATE_VERSION_INDEX}\"\n WHERE \"stateVersion\" > ? AND \"pos\" < 0\n ORDER BY \"stateVersion\" ASC, \"pos\" ASC LIMIT 1`,\n prevVersion,\n );\n }\n\n /**\n * The entries of `table` logged after `prevVersion`, in the order that\n * {@link changesSince} reads them. For a table with few rows.\n */\n entriesSince(\n table: string,\n prevVersion: string,\n ): (ChangeLogPosition & {rowKey: string | null; op: string})[] {\n return this.db.all(\n `SELECT \"stateVersion\", \"pos\", \"rowKey\", \"op\" FROM \"_zero.changeLog2\" INDEXED BY \"${CHANGE_LOG_TABLE_INDEX}\"\n WHERE \"table\" = ? AND \"stateVersion\" > ?\n ORDER BY \"stateVersion\" ASC, \"pos\" ASC`,\n table,\n prevVersion,\n );\n }\n\n /**\n * @param tables If set, only the entries of these tables, and the\n * table-wide ops of every table, are read.\n */\n changesSince(prevVersion: string, tables?: string | undefined) {\n // Note: The queried fields are constrained to only those that are relevant\n // to the snapshot diff, i.e. those of a ChangeLogEntry.\n const cached = this.db.statementCache.get(\n tables === undefined\n ? `SELECT \"stateVersion\", \"table\", \"rowKey\", \"op\" FROM \"_zero.changeLog2\"\n WHERE \"stateVersion\" > ? ORDER BY \"stateVersion\" ASC, \"pos\" ASC`\n : `SELECT \"stateVersion\", \"table\", \"rowKey\", \"op\" FROM \"_zero.changeLog2\"\n WHERE \"stateVersion\" > ? AND ${CHANGE_LOG_TABLES_FILTER}\n ORDER BY \"stateVersion\" ASC, \"pos\" ASC`,\n );\n // Raw rows skip building a row object per entry. See RawChangeLogEntry.\n cached.statement.raw(true);\n return {\n changes:\n tables === undefined\n ? cached.statement.iterate<RawChangeLogEntry>(prevVersion)\n : cached.statement.iterate<RawChangeLogEntry>(\n prevVersion,\n RESET_OP,\n TRUNCATE_OP,\n tables,\n ),\n cleanup: () => this.db.statementCache.return(cached),\n };\n }\n\n /**\n * Reads the row with the given `rowKey`, or `undefined` if it does not\n * exist.\n *\n * @param cache An optional {@link SnapshotRowCache} through which to\n * perform the read, along with the `tag` that identifies the\n * version(s) of the replica that the result depends on.\n */\n getRow(\n table: LiteTableSpecWithKeysAndVersion,\n rowKey: JSONValue,\n cache?: SnapshotRowCache,\n tag?: string,\n ) {\n const key = normalizedKeyOrder(rowKey as RowKey);\n const cols = Object.keys(key);\n const reads = readsFor(table);\n const sql = getOrInsertComputed(\n reads.byKeyColumns,\n cols.join('\\0'),\n () => reads.prefix + cols.map(c => `${id(c)}=?`).join(' AND '),\n );\n const args = Object.values(key);\n const read = () =>\n this.db.statementCache.use(sql, cached => {\n cached.statement.safeIntegers(true);\n const row = cached.statement.get<SQLiteRow>(args);\n recordIndexUsage(cached.statement, table.name);\n return row;\n });\n return cache\n ? cache.getOrRead(tag ?? '', sql, 'get', args, () => {\n const row = read();\n if (row !== undefined) {\n this.#assertNotAdvanced(row);\n }\n return row;\n })\n : read();\n }\n\n /**\n * Reads the rows that conflict with `row` on any of the given unique\n * `keys` (which includes the row with the same primary key, if any).\n *\n * @param cache An optional {@link SnapshotRowCache} through which to\n * perform the read, along with the `tag` that identifies the\n * version(s) of the replica that the result depends on.\n */\n getRows(\n table: LiteTableSpecWithKeysAndVersion,\n keys: PrimaryKey[],\n row: SQLiteRow,\n cache?: SnapshotRowCache,\n tag?: string,\n ) {\n // Filter out keys where any column is NULL. This is both correct and\n // critical for performance:\n // 1. Correctness: NULL values can't violate uniqueness (NULL != NULL in SQL)\n // 2. Performance: SQLite's MULTI-INDEX OR optimization completely fails when\n // any branch involves NULL, falling back to a full table scan. This was\n // causing slowdowns of hundreds of times on tables with nullable unique columns.\n let validKeys: PrimaryKey[] = keys;\n let mask = (1 << keys.length) - 1;\n\n for (let i = 0; i < keys.length; i++) {\n const key = keys[i];\n for (let j = 0; j < key.length; j++) {\n const val = row[key[j]];\n if (val === null || val === undefined) {\n validKeys = [];\n mask = 0;\n for (let k = 0; k < keys.length; k++) {\n const candidate = keys[k];\n if (\n candidate.every(\n col => row[col] !== null && row[col] !== undefined,\n )\n ) {\n validKeys.push(candidate);\n mask |= 1 << k;\n }\n }\n break;\n }\n }\n if (validKeys !== keys) {\n break;\n }\n }\n\n if (validKeys.length === 0) {\n return [];\n }\n const reads = readsFor(table);\n const buildSQL = () =>\n reads.prefix +\n validKeys\n .map(key => key.map(c => `${id(c)}=?`).join(' AND '))\n .join(' OR ');\n const sql =\n keys === table.uniqueKeys && keys.length <= MAX_MEMOIZED_UNIQUE_KEYS\n ? getOrInsertComputed(reads.byUniqueKeys, mask, buildSQL)\n : buildSQL();\n const args: unknown[] = [];\n for (let i = 0; i < validKeys.length; i++) {\n const key = validKeys[i];\n for (let j = 0; j < key.length; j++) {\n args.push(row[key[j]]);\n }\n }\n const read = () =>\n this.db.statementCache.use(sql, cached => {\n cached.statement.safeIntegers(true);\n const rows = cached.statement.all<SQLiteRow>(args);\n recordIndexUsage(cached.statement, table.name);\n return rows;\n });\n return cache\n ? cache.getOrRead(tag ?? '', sql, 'all', args, () => {\n const rows = read();\n for (const row of rows) {\n this.#assertNotAdvanced(row);\n }\n return rows;\n })\n : read();\n }\n\n /**\n * Rows are shared with other client groups via the SnapshotRowCache, so\n * one read from a connection that has advanced past this snapshot (i.e. a\n * {@link SnapshotDiff} used after its Snapshotter advanced) must fail\n * before it is cached, rather than be served to the valid Diffs of other\n * client groups (whose own validity checks would then fail).\n */\n #assertNotAdvanced(row: SQLiteRow) {\n if ((row[ROW_VERSION] ?? '~') > this.version) {\n throw new InvalidDiffError(\n `Diff is no longer valid. db has advanced past ${this.version}.`,\n );\n }\n }\n\n /**\n * Whether the snapshot has been released, or its connection reset to a\n * newer snapshot (i.e. via {@link release()} or {@link resetToHead()}),\n * after which reads no longer reflect {@link version}.\n */\n get reset(): boolean {\n return this.#reset;\n }\n\n /**\n * Ends the snapshot's read transaction, after which reads no longer\n * reflect {@link version}. An open read transaction keeps SQLite from\n * checkpointing the write-ahead log past the snapshot, so a snapshot that\n * is no longer read should not be left open.\n */\n release(): void {\n if (!this.#reset) {\n this.#reset = true;\n this.db.rollback();\n // The connection is put back into a transaction, which holds nothing\n // until it reads. A snapshot's connection is writable, and outside of\n // a transaction anything that is run on it would be committed to the\n // replica under the replica's write lock, which only the replicator\n // should take.\n this.db.beginConcurrent();\n }\n }\n\n resetToHead(): Snapshot {\n this.release();\n return new Snapshot(this.db, this.#appID, true);\n }\n}\n\nclass Diff implements SnapshotDiff {\n readonly #permissionsTable: string;\n readonly #syncableTables: Map<string, LiteAndZqlSpec>;\n readonly #allTableNames: Set<string>;\n readonly #observedTables: TableFilter | undefined;\n readonly #rowCache: SnapshotRowCache | undefined;\n /** The JSON array of the names of the tables whose entries are read. */\n readonly #readTables: string | undefined;\n #permissionsChanged: boolean | undefined;\n #nothingToRead = false;\n #prevWrites: PrevWrites;\n readonly #prevTagUniform: string;\n readonly #prevTagDivergent: string;\n #iterated = false;\n readonly prev: Snapshot;\n readonly curr: Snapshot;\n readonly changes: number;\n changesScanned = 0;\n readonly changesScannedByTable = new Map<string, number>();\n changesSkipped = 0;\n changesLookedUp = 0;\n readonly stats: DiffTimingStats = {\n changeLogReadMs: 0,\n rowReadMs: 0,\n parseMs: 0,\n totalDiffMs: 0,\n };\n\n constructor(\n appID: string,\n syncableTables: Map<string, LiteAndZqlSpec>,\n allTableNames: Set<string>,\n prev: Snapshot,\n curr: Snapshot,\n observedTables?: TableFilter | undefined,\n rowCache?: SnapshotRowCache | undefined,\n prevWrites: PrevWrites = 'uniform',\n counts?: ChangeLogCounts | undefined,\n readTables?: Iterable<string> | undefined,\n ) {\n this.#prevWrites = prevWrites;\n this.#permissionsTable = `${appID}.permissions`;\n // A change of the permissions is always looked for.\n const read = readTables && new Set(readTables).add(this.#permissionsTable);\n this.#readTables = read && JSON.stringify([...read]);\n this.#syncableTables = syncableTables;\n this.#allTableNames = allTableNames;\n this.#observedTables = observedTables;\n this.prev = prev;\n this.curr = curr;\n this.#prevTagUniform = `p:${prev.version}`;\n this.#prevTagDivergent = `p:${prev.version}:${curr.version}`;\n if (counts === undefined) {\n assert(read === undefined, 'readTables requires counts');\n this.changes = curr.numChangesSince(prev.version);\n } else {\n // Without an entry of a table that is read, or a table-wide op (which\n // aborts the diff whatever table it is for), there is nothing to read.\n const hasEntries = (table: string) => this.#hasEntries(counts, table);\n this.#nothingToRead =\n counts.total === 0 ||\n (read !== undefined &&\n counts.tableWideOps === 0 &&\n ![...read].some(hasEntries));\n // As without counts, this is the number of entries of every table.\n this.changes = this.#nothingToRead ? 0 : counts.total;\n }\n // Row values are only shared (via the cache) between Diffs that do not\n // straddle a table-wide op. This guarantees that any two Diffs that\n // encounter the same change log entry read the row from tables with\n // the same schema (and contents), since a RESET or TRUNCATE between\n // their `curr` versions would necessarily fall within the range of the\n // later Diff. (Such a Diff aborts with a ResetPipelinesSignal anyway.)\n const hasTableWideOp = counts\n ? counts.tableWideOps > 0\n : curr.hasTableWideOpSince(prev.version);\n this.#rowCache = rowCache && !hasTableWideOp ? rowCache : undefined;\n }\n\n /**\n * Whether the diff has an entry of `table`. Sampled counts cannot show\n * that a table has none, so the answer is then `true`, except for the\n * permissions table: it has few rows, so whether it has an entry is\n * looked up instead.\n */\n #hasEntries(counts: ChangeLogCounts, table: string): boolean {\n if (!counts.sampled) {\n return (counts.byTable.get(table) ?? 0) > 0;\n }\n if (table !== this.#permissionsTable) {\n return true;\n }\n return (this.#permissionsChanged ??= this.curr.tableChangedSince(\n table,\n this.prev.version,\n ));\n }\n\n setPrevWrites(prevWrites: PrevWrites): void {\n assert(\n !this.#iterated || prevWrites === 'divergent',\n 'prevWrites must be set before iterating the diff',\n );\n this.#prevWrites = prevWrites;\n }\n\n [Symbol.iterator](): Iterator<Change> {\n this.#iterated = true;\n if (this.#nothingToRead) {\n return [][Symbol.iterator]();\n }\n const {changes, cleanup: done} = this.curr.changesSince(\n this.prev.version,\n this.#readTables,\n );\n\n const cleanup = () => {\n try {\n // Allow open iterators to clean up their state.\n changes.return?.(undefined);\n } finally {\n done();\n }\n };\n\n return {\n next: () => {\n const nextStart = performance.now();\n try {\n for (;;) {\n const clStart = performance.now();\n const {value, done} = changes.next();\n this.stats.changeLogReadMs += performance.now() - clStart;\n if (done) {\n cleanup();\n return {value, done: true};\n }\n\n this.changesScanned++;\n const parseStart = performance.now();\n const {table, rowKey, op, stateVersion} = parseChangeLogEntry(\n ...value,\n );\n this.stats.parseMs += performance.now() - parseStart;\n this.changesScannedByTable.set(\n table,\n (this.changesScannedByTable.get(table) ?? 0) + 1,\n );\n if (op === RESET_OP) {\n // The current map of `TableSpec`s may not have the correct or complete information.\n throw new ResetPipelinesSignal(\n `schema for table ${table} has changed`,\n 'schema-change',\n );\n }\n if (op === TRUNCATE_OP) {\n // Truncates are also processed by rehydrating pipelines at current.\n throw new ResetPipelinesSignal(\n `table ${table} has been truncated`,\n 'truncation',\n );\n }\n if (\n this.#observedTables &&\n !this.#observedTables.has(table) &&\n table !== this.#permissionsTable\n ) {\n if (this.#allTableNames.has(table)) {\n this.changesSkipped++;\n continue; // skip change log entries for unobserved tables.\n }\n throw new Error(`change for unknown table ${table}`);\n }\n const specs = this.#syncableTables.get(table);\n if (!specs) {\n if (this.#allTableNames.has(table)) {\n this.changesSkipped++;\n continue; // skip change log entries for non-syncable tables.\n }\n throw new Error(`change for unknown table ${table}`);\n }\n const {tableSpec, zqlSpec} = specs;\n\n // Sanity check: All change log ops should have a stateVersion\n // greater than minRowVersion in the table metadata. This is a\n // mini-proof that the overlay does not need to be applied to\n // rows produced in incremental catchup, based on the invariant in\n // change-processor's #bumpVersions(), whereby the setting of the\n // minRowVersion is always followed by a RESET OP, meaning that\n // subsequent change-log traversal happens at a later version.\n assert(\n (tableSpec.minRowVersion ?? '') < stateVersion,\n () =>\n `unexpected change @${stateVersion} for table ${table} with ` +\n `minRowVersion ${tableSpec.minRowVersion}: ${op}(${rowKey})`,\n );\n\n assert(rowKey !== null, 'rowKey must be present for row changes');\n\n // Row reads are shared with the other client groups on the\n // worker via the SnapshotRowCache (if configured):\n //\n // * The new value of the row is read from `curr`, which is\n // never written to during the iteration, and is the version\n // of the row at `stateVersion` (as verified by\n // checkThatDiffIsValid()). It is thus identical for every\n // Diff that encounters the change log entry.\n //\n // * Previous values are read from `prev`, at `prev.version`,\n // which the caller (i.e. IVM) writes to as the iteration\n // progresses. For a table whose only unique key is its\n // primary key, no change applied before this one can affect\n // this row, so the result depends solely on `prev.version`.\n // Otherwise, the result can include rows that conflict on\n // other unique keys, which may have been added or removed by\n // preceding changes; see PrevWrites for when that can be\n // shared.\n // The first advance() after this Diff was created resets the\n // `prev` connection to head. Reads would then no longer reflect\n // `prev.version`, and cache hits would not reveal it (the cached\n // rows were read by valid Diffs), so this must be checked\n // explicitly rather than inferred from the rows' versions.\n if (this.prev.reset) {\n throw new InvalidDiffError(\n `Diff is no longer valid. prev db has advanced past ${this.prev.version}.`,\n );\n }\n\n const cache = this.#rowCache;\n const prevDependsOnWrites =\n tableSpec.uniqueKeys.length > 1 && this.#prevWrites !== 'none';\n const prevCache =\n prevDependsOnWrites && this.#prevWrites === 'divergent'\n ? undefined\n : cache;\n const prevTag = prevDependsOnWrites\n ? this.#prevTagDivergent\n : this.#prevTagUniform;\n this.changesLookedUp++;\n const rowStart = performance.now();\n const nextValue =\n op === SET_OP\n ? this.curr.getRow(\n tableSpec,\n rowKey,\n cache,\n `n:${stateVersion}`,\n )\n : null;\n let prevValues;\n if (nextValue) {\n prevValues = this.prev.getRows(\n tableSpec,\n tableSpec.uniqueKeys,\n nextValue,\n prevCache,\n prevTag,\n );\n } else {\n const prevValue = this.prev.getRow(\n tableSpec,\n rowKey,\n prevCache,\n prevTag,\n );\n prevValues = prevValue ? [prevValue] : [];\n }\n this.stats.rowReadMs += performance.now() - rowStart;\n if (nextValue === undefined) {\n throw new Error(\n `Missing value for ${table} ${stringify(rowKey)}`,\n );\n }\n // Sanity check detects if the diff is being accessed after the Snapshots have advanced.\n this.checkThatDiffIsValid(stateVersion, op, prevValues, nextValue);\n\n if (prevValues.length === 0 && nextValue === null) {\n // Filter out no-op changes (e.g. a delete of a row that does not exist in prev).\n // TODO: Consider doing this for deep-equal values.\n this.changesSkipped++;\n continue;\n }\n\n if (table === this.#permissionsTable) {\n const changed = prevValues.find(\n prevValue => prevValue.permissions !== nextValue?.permissions,\n );\n if (changed) {\n throw new ResetPipelinesSignal(\n `Permissions have changed ${changed.hash} => ${nextValue?.hash}`,\n 'permissions-change',\n );\n }\n }\n\n // Note: The raw SQLite rows may be shared with other client\n // groups via the SnapshotRowCache, so they must not be modified.\n // fromSQLiteTypes() produces new row objects.\n return {\n value: {\n table,\n prevValues: prevValues.map(prevValue =>\n fromSQLiteTypes(zqlSpec, prevValue, table),\n ),\n nextValue: nextValue\n ? fromSQLiteTypes(zqlSpec, nextValue, table)\n : null,\n rowKey,\n } satisfies Change,\n };\n }\n } catch (e) {\n // This control flow path is not covered by the return() method (i.e. `break`).\n cleanup();\n throw e;\n } finally {\n this.stats.totalDiffMs += performance.now() - nextStart;\n }\n },\n\n return: (value: unknown) => {\n cleanup();\n return {value, done: true};\n },\n };\n }\n\n checkThatDiffIsValid(\n stateVersion: string,\n op: string,\n prevValues: readonly SQLiteRow[],\n nextValue: SQLiteRow | null,\n ) {\n // Sanity checks to detect that the diff is not being accessed after\n // the Snapshots have advanced.\n if (stateVersion > this.curr.version) {\n throw new InvalidDiffError(\n `Diff is no longer valid. curr db has advanced past ${this.curr.version}`,\n );\n }\n if (\n prevValues.findIndex(\n prevValue => (prevValue[ROW_VERSION] ?? '~') > this.prev.version,\n ) !== -1\n ) {\n throw new InvalidDiffError(\n `Diff is no longer valid. prev db has advanced past ${this.prev.version}.`,\n );\n }\n if (op === SET_OP && nextValue?.[ROW_VERSION] !== stateVersion) {\n throw new InvalidDiffError(\n 'Diff is no longer valid. curr db has advanced.',\n );\n }\n }\n}\n\nexport class InvalidDiffError extends Error {\n constructor(msg: string) {\n super(msg);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2FA,IAAa,cAAb,MAAyB;CACvB;CACA;CACA;CACA;CACA;CACA;CACA;;;;;;;CAQA,YACE,IACA,QACA,EAAC,SACD,kBACA,UACA;EACA,KAAK,MAAM;EACX,KAAK,UAAU;EACf,KAAK,SAAS;EACd,KAAK,oBAAoB;EACzB,KAAK,YAAY;CACnB;;;;;;CAOA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EF,IAAI,OAAA,OAAiB,YAAA,kBAA6B,cAChD,MAAM,IAAI,iBACR,gDACF;CAEJ;AACF;AAEA,IAAa,mBAAb,cAAsC,MAAM;CAC1C,YAAY,KAAa;EACvB,MAAM,GAAG;CACX;AACF"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { assert, unreachable } from "../../../../shared/src/asserts.js";
|
|
2
|
+
import { resolver } from "../../../../shared/src/resolver.js";
|
|
2
3
|
import { getOrInsertComputed } from "../../../../shared/src/map.js";
|
|
3
4
|
import { newArray } from "../../../../shared/src/arrays.js";
|
|
4
5
|
import { must } from "../../../../shared/src/must.js";
|
|
@@ -36,7 +37,6 @@ import { HydrationBudget } from "./hydration-budget.js";
|
|
|
36
37
|
import { HydrationCircuitBreaker } from "./hydration-circuit-breaker.js";
|
|
37
38
|
import { handleInspect } from "./inspect-handler.js";
|
|
38
39
|
import { QueryCoveringIndex } from "./query-covering.js";
|
|
39
|
-
import { resolver } from "@rocicorp/resolver";
|
|
40
40
|
import { Lock } from "@rocicorp/lock";
|
|
41
41
|
//#region ../zero-cache/src/services/view-syncer/view-syncer.ts
|
|
42
42
|
var PROTOCOL_VERSION_ATTR = "protocol.version";
|
|
@@ -537,6 +537,9 @@ var ViewSyncerService = class {
|
|
|
537
537
|
get queryCount() {
|
|
538
538
|
return this.#pipelines.initialized() ? this.#pipelines.queries().size : 0;
|
|
539
539
|
}
|
|
540
|
+
get hydrationTimeMs() {
|
|
541
|
+
return this.#totalHydrationTimeMs();
|
|
542
|
+
}
|
|
540
543
|
pipelineHashes() {
|
|
541
544
|
if (!this.#pipelines.initialized()) return [];
|
|
542
545
|
const cvrQueries = this.#cvr?.queries;
|
|
@@ -632,11 +635,12 @@ var ViewSyncerService = class {
|
|
|
632
635
|
return this.#clients.size === 0;
|
|
633
636
|
}
|
|
634
637
|
#deleteClientDueToDisconnect(clientID, client) {
|
|
635
|
-
this.
|
|
638
|
+
const c = this.#clients.get(clientID);
|
|
639
|
+
if (c === client || c?.wsID !== client.wsID) this.connContextManager.closeConnection({
|
|
636
640
|
clientID,
|
|
637
641
|
wsID: client.wsID
|
|
638
642
|
});
|
|
639
|
-
if (
|
|
643
|
+
if (c === client) {
|
|
640
644
|
this.#clients.delete(clientID);
|
|
641
645
|
if (this.#clients.size === 0) {
|
|
642
646
|
if (this.#ttlClock !== void 0) this.#updateTTLClockInCVRWithoutLock(this.#lc);
|
|
@@ -725,8 +729,9 @@ var ViewSyncerService = class {
|
|
|
725
729
|
const now = Date.now();
|
|
726
730
|
this.#ttlClockBase = now;
|
|
727
731
|
}
|
|
728
|
-
this.#clients.get(connCtx.clientID)
|
|
732
|
+
const replaced = this.#clients.get(connCtx.clientID);
|
|
729
733
|
this.#clients.set(connCtx.clientID, newClient);
|
|
734
|
+
replaced?.close(`replaced by wsID: ${connCtx.wsID}`);
|
|
730
735
|
startAsyncSpan(tracer, "vs.initConnection.async", () => this.#runInLockForClient(connCtx, initConnectionMessage, async (lc, clientID, msg, cvr) => {
|
|
731
736
|
if (cvr.clientSchema === null && !msg.clientSchema) throw new ProtocolErrorWithLevel({
|
|
732
737
|
kind: InvalidConnectionRequest,
|
|
@@ -868,15 +873,13 @@ var ViewSyncerService = class {
|
|
|
868
873
|
lc = lc.withContext("clientID", clientID).withContext("wsID", wsID).withContext("cmd", cmd);
|
|
869
874
|
lc.debug?.("acquired lock for cvr");
|
|
870
875
|
client = this.#clients.get(clientID);
|
|
871
|
-
if (client?.wsID !== wsID) {
|
|
872
|
-
lc.debug?.("mismatched
|
|
876
|
+
if (client?.wsID !== wsID || newClient && client !== newClient) {
|
|
877
|
+
lc.debug?.("mismatched or superseded client", client?.wsID, wsID);
|
|
873
878
|
return;
|
|
874
879
|
}
|
|
875
880
|
connCtx = this.connContextManager.getConnectionContext(selector);
|
|
876
|
-
if (newClient)
|
|
877
|
-
|
|
878
|
-
checkClientAndCVRVersions(client.version(), cvr.version);
|
|
879
|
-
} else if (!this.#clients.has(clientID)) lc.warn?.(`Processing ${cmd} before initConnection was received`);
|
|
881
|
+
if (newClient) checkClientAndCVRVersions(client.version(), cvr.version);
|
|
882
|
+
else if (!this.#clients.has(clientID)) lc.warn?.(`Processing ${cmd} before initConnection was received`);
|
|
880
883
|
lc.debug?.(cmd, body);
|
|
881
884
|
result = await fn(lc, clientID, body, cvr);
|
|
882
885
|
});
|