@rocicorp/zero 1.10.0-canary.22 → 1.10.0-canary.23
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/analyze-query/src/analyze-cli.js +2 -2
- package/out/analyze-query/src/analyze-cli.js.map +1 -1
- package/out/analyze-query/src/bin-analyze.js +1 -1
- package/out/analyze-query/src/bin-analyze.js.map +1 -1
- package/out/zero/package.js +1 -1
- package/out/zero/package.js.map +1 -1
- package/out/zero-cache/src/config/zero-config.d.ts +8 -0
- package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
- package/out/zero-cache/src/config/zero-config.js +28 -1
- package/out/zero-cache/src/config/zero-config.js.map +1 -1
- package/out/zero-cache/src/server/change-streamer.d.ts.map +1 -1
- package/out/zero-cache/src/server/change-streamer.js +11 -3
- package/out/zero-cache/src/server/change-streamer.js.map +1 -1
- package/out/zero-cache/src/server/inspector-delegate.d.ts +18 -2
- package/out/zero-cache/src/server/inspector-delegate.d.ts.map +1 -1
- package/out/zero-cache/src/server/inspector-delegate.js +20 -7
- package/out/zero-cache/src/server/inspector-delegate.js.map +1 -1
- package/out/zero-cache/src/server/main.d.ts.map +1 -1
- package/out/zero-cache/src/server/main.js +8 -1
- package/out/zero-cache/src/server/main.js.map +1 -1
- package/out/zero-cache/src/server/mutator.js +2 -0
- package/out/zero-cache/src/server/mutator.js.map +1 -1
- package/out/zero-cache/src/server/reaper.d.ts.map +1 -1
- package/out/zero-cache/src/server/reaper.js +2 -0
- package/out/zero-cache/src/server/reaper.js.map +1 -1
- package/out/zero-cache/src/server/replicator.d.ts.map +1 -1
- package/out/zero-cache/src/server/replicator.js +2 -0
- package/out/zero-cache/src/server/replicator.js.map +1 -1
- package/out/zero-cache/src/server/runner/zero-dispatcher.d.ts.map +1 -1
- package/out/zero-cache/src/server/runner/zero-dispatcher.js +3 -0
- package/out/zero-cache/src/server/runner/zero-dispatcher.js.map +1 -1
- package/out/zero-cache/src/server/syncer.d.ts.map +1 -1
- package/out/zero-cache/src/server/syncer.js +2 -0
- package/out/zero-cache/src/server/syncer.js.map +1 -1
- package/out/zero-cache/src/services/analyze.d.ts.map +1 -1
- package/out/zero-cache/src/services/analyze.js +3 -2
- package/out/zero-cache/src/services/analyze.js.map +1 -1
- package/out/zero-cache/src/services/change-source/common/backfill-manager.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/common/backfill-manager.js +14 -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.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js +15 -3
- package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js.map +1 -1
- package/out/zero-cache/src/services/change-source/custom/change-source.js +2 -2
- package/out/zero-cache/src/services/change-source/custom/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-streamer/change-streamer-http.d.ts +3 -0
- package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer-http.js +6 -4
- package/out/zero-cache/src/services/change-streamer/change-streamer-http.js.map +1 -1
- package/out/zero-cache/src/services/mutagen/mutagen.d.ts +5 -0
- package/out/zero-cache/src/services/mutagen/mutagen.d.ts.map +1 -1
- package/out/zero-cache/src/services/mutagen/mutagen.js +7 -1
- package/out/zero-cache/src/services/mutagen/mutagen.js.map +1 -1
- package/out/zero-cache/src/services/mutagen/pusher.js +2 -1
- package/out/zero-cache/src/services/mutagen/pusher.js.map +1 -1
- package/out/zero-cache/src/services/profz.d.ts +7 -0
- package/out/zero-cache/src/services/profz.d.ts.map +1 -0
- package/out/zero-cache/src/services/profz.js +103 -0
- package/out/zero-cache/src/services/profz.js.map +1 -0
- package/out/zero-cache/src/services/run-ast.d.ts +12 -0
- package/out/zero-cache/src/services/run-ast.d.ts.map +1 -1
- package/out/zero-cache/src/services/run-ast.js +26 -14
- package/out/zero-cache/src/services/run-ast.js.map +1 -1
- package/out/zero-cache/src/services/runner.d.ts.map +1 -1
- package/out/zero-cache/src/services/runner.js +1 -1
- package/out/zero-cache/src/services/runner.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/cvr.d.ts +45 -0
- package/out/zero-cache/src/services/view-syncer/cvr.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/cvr.js +133 -1
- package/out/zero-cache/src/services/view-syncer/cvr.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/drain-coordinator.d.ts +14 -1
- package/out/zero-cache/src/services/view-syncer/drain-coordinator.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/drain-coordinator.js +30 -4
- package/out/zero-cache/src/services/view-syncer/drain-coordinator.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/hydration-budget.d.ts +33 -0
- package/out/zero-cache/src/services/view-syncer/hydration-budget.d.ts.map +1 -0
- package/out/zero-cache/src/services/view-syncer/hydration-budget.js +62 -0
- package/out/zero-cache/src/services/view-syncer/hydration-budget.js.map +1 -0
- package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.d.ts +51 -0
- package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.d.ts.map +1 -0
- package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.js +78 -0
- package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.js.map +1 -0
- package/out/zero-cache/src/services/view-syncer/inspect-handler.d.ts +1 -1
- package/out/zero-cache/src/services/view-syncer/inspect-handler.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/inspect-handler.js +2 -2
- package/out/zero-cache/src/services/view-syncer/inspect-handler.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts +2 -1
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +27 -8
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts +4 -1
- package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/snapshotter.js +9 -3
- package/out/zero-cache/src/services/view-syncer/snapshotter.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts +11 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.js +470 -90
- package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
- package/out/zero-cache/src/types/processes.d.ts +32 -0
- package/out/zero-cache/src/types/processes.d.ts.map +1 -1
- package/out/zero-cache/src/types/processes.js +43 -4
- package/out/zero-cache/src/types/processes.js.map +1 -1
- package/out/zero-cache/src/types/profiler.d.ts +12 -0
- package/out/zero-cache/src/types/profiler.d.ts.map +1 -1
- package/out/zero-cache/src/types/profiler.js +46 -1
- package/out/zero-cache/src/types/profiler.js.map +1 -1
- package/out/zero-cache/src/workers/connection.js +3 -3
- package/out/zero-cache/src/workers/connection.js.map +1 -1
- package/out/zero-cache/src/workers/syncer-ws-message-handler.d.ts.map +1 -1
- package/out/zero-cache/src/workers/syncer-ws-message-handler.js +21 -16
- package/out/zero-cache/src/workers/syncer-ws-message-handler.js.map +1 -1
- package/out/zero-cache/src/workers/syncer.d.ts.map +1 -1
- package/out/zero-cache/src/workers/syncer.js +10 -1
- package/out/zero-cache/src/workers/syncer.js.map +1 -1
- package/out/zero-client/src/client/context.d.ts +16 -3
- package/out/zero-client/src/client/context.d.ts.map +1 -1
- package/out/zero-client/src/client/context.js +85 -14
- package/out/zero-client/src/client/context.js.map +1 -1
- package/out/zero-client/src/client/ivm-branch.d.ts.map +1 -1
- package/out/zero-client/src/client/ivm-branch.js +20 -3
- package/out/zero-client/src/client/ivm-branch.js.map +1 -1
- package/out/zero-client/src/client/version.js +1 -1
- package/out/zero-client/src/client/zero-rep.d.ts.map +1 -1
- package/out/zero-client/src/client/zero-rep.js +1 -1
- package/out/zero-client/src/client/zero-rep.js.map +1 -1
- package/out/zero-client/src/client/zero.d.ts.map +1 -1
- package/out/zero-client/src/client/zero.js +2 -2
- package/out/zero-client/src/client/zero.js.map +1 -1
- package/out/zero-protocol/src/error.d.ts +1 -1
- package/out/zero-solid/src/solid-view.d.ts +7 -0
- package/out/zero-solid/src/solid-view.d.ts.map +1 -1
- package/out/zero-solid/src/solid-view.js +13 -2
- package/out/zero-solid/src/solid-view.js.map +1 -1
- package/out/zql/src/builder/debug-delegate.d.ts +6 -1
- package/out/zql/src/builder/debug-delegate.d.ts.map +1 -1
- package/out/zql/src/builder/debug-delegate.js +14 -2
- package/out/zql/src/builder/debug-delegate.js.map +1 -1
- package/out/zql/src/ivm/array-view.d.ts +19 -0
- package/out/zql/src/ivm/array-view.d.ts.map +1 -1
- package/out/zql/src/ivm/array-view.js +39 -2
- package/out/zql/src/ivm/array-view.js.map +1 -1
- package/out/zql/src/ivm/memory-source.d.ts +13 -1
- package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
- package/out/zql/src/ivm/memory-source.js +26 -0
- package/out/zql/src/ivm/memory-source.js.map +1 -1
- package/out/zql/src/ivm/view.d.ts +15 -0
- package/out/zql/src/ivm/view.d.ts.map +1 -1
- package/out/zql/src/query/query-delegate-base.d.ts +2 -2
- package/out/zql/src/query/query-delegate-base.d.ts.map +1 -1
- package/out/zql/src/query/query-delegate-base.js +16 -10
- package/out/zql/src/query/query-delegate-base.js.map +1 -1
- package/out/zql/src/query/query-delegate.d.ts +22 -5
- package/out/zql/src/query/query-delegate.d.ts.map +1 -1
- package/out/zql/src/query/query.d.ts +4 -1
- package/out/zql/src/query/query.d.ts.map +1 -1
- package/out/zqlite/src/db.d.ts +9 -1
- package/out/zqlite/src/db.d.ts.map +1 -1
- package/out/zqlite/src/db.js +8 -2
- package/out/zqlite/src/db.js.map +1 -1
- package/out/zqlite/src/table-source.d.ts +1 -0
- package/out/zqlite/src/table-source.d.ts.map +1 -1
- package/out/zqlite/src/table-source.js +3 -0
- package/out/zqlite/src/table-source.js.map +1 -1
- package/package.json +1 -1
|
@@ -94,7 +94,7 @@ export declare const transformFailedBodySchema: v.UnionType<[v.ObjectType<Omit<{
|
|
|
94
94
|
reason: v.Type<"parse" | "timeout" | "internal">;
|
|
95
95
|
}, undefined>]>;
|
|
96
96
|
export declare const errorBodySchema: v.UnionType<[v.ObjectType<{
|
|
97
|
-
kind: v.Type<"AuthInvalidated" | "ClientNotFound" | "InvalidConnectionRequest" | "InvalidConnectionRequestBaseCookie" | "InvalidConnectionRequestLastMutationID" | "InvalidConnectionRequestClientDeleted" | "InvalidMessage" | "InvalidPush" | "MutationFailed" | "MutationRateLimited" | "
|
|
97
|
+
kind: v.Type<"Unauthorized" | "AuthInvalidated" | "ClientNotFound" | "InvalidConnectionRequest" | "InvalidConnectionRequestBaseCookie" | "InvalidConnectionRequestLastMutationID" | "InvalidConnectionRequestClientDeleted" | "InvalidMessage" | "InvalidPush" | "MutationFailed" | "MutationRateLimited" | "VersionNotSupported" | "SchemaVersionNotSupported" | "Internal">;
|
|
98
98
|
message: v.Type<string>;
|
|
99
99
|
origin: v.Optional<"server" | "zeroCache">;
|
|
100
100
|
}, undefined>, v.ObjectType<{
|
|
@@ -59,6 +59,13 @@ export declare class SolidView implements Output {
|
|
|
59
59
|
markCached(): void;
|
|
60
60
|
/** The got key was deleted (eviction) before this connection confirmed it. */
|
|
61
61
|
unmarkCached(): void;
|
|
62
|
+
/**
|
|
63
|
+
* Called before a deferred pipeline hydrates this view over several tasks
|
|
64
|
+
* (see `ViewFactory`). Rows already only reach the store at commit; this
|
|
65
|
+
* makes result type transitions wait for that commit too, including when
|
|
66
|
+
* the hydration produced no rows.
|
|
67
|
+
*/
|
|
68
|
+
holdData(): void;
|
|
62
69
|
push(change: Change): readonly never[];
|
|
63
70
|
updateTTL(ttl: TTL): void;
|
|
64
71
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"solid-view.d.ts","sourceRoot":"","sources":["../../../../zero-solid/src/solid-view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,gBAAgB,EAAC,MAAM,gBAAgB,CAAC;AAUzE,OAAO,EAEL,KAAK,MAAM,EACX,KAAK,KAAK,EACV,KAAK,YAAY,EACjB,KAAK,MAAM,EACX,KAAK,KAAK,EAEV,KAAK,MAAM,EACX,KAAK,KAAK,EAEV,KAAK,kBAAkB,EACvB,KAAK,MAAM,EAEX,KAAK,GAAG,EACT,MAAM,WAAW,CAAC;AAEnB,MAAM,MAAM,KAAK,GAAG,CAAC,KAAK,EAAE,kBAAkB,CAAC,CAAC;AAEhD,eAAO,MAAM,QAAQ,EAAE,kBAAsD,CAAC;AAC9E,eAAO,MAAM,OAAO,EAAE,kBAAqD,CAAC;AAC5E,eAAO,MAAM,MAAM,EAAE,kBAAoD,CAAC;AAE1E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,qBAAa,SAAU,YAAW,MAAM;;
|
|
1
|
+
{"version":3,"file":"solid-view.d.ts","sourceRoot":"","sources":["../../../../zero-solid/src/solid-view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,gBAAgB,EAAC,MAAM,gBAAgB,CAAC;AAUzE,OAAO,EAEL,KAAK,MAAM,EACX,KAAK,KAAK,EACV,KAAK,YAAY,EACjB,KAAK,MAAM,EACX,KAAK,KAAK,EAEV,KAAK,MAAM,EACX,KAAK,KAAK,EAEV,KAAK,kBAAkB,EACvB,KAAK,MAAM,EAEX,KAAK,GAAG,EACT,MAAM,WAAW,CAAC;AAEnB,MAAM,MAAM,KAAK,GAAG,CAAC,KAAK,EAAE,kBAAkB,CAAC,CAAC;AAEhD,eAAO,MAAM,QAAQ,EAAE,kBAAsD,CAAC;AAC9E,eAAO,MAAM,OAAO,EAAE,kBAAqD,CAAC;AAC5E,eAAO,MAAM,MAAM,EAAE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anges = [];
|
|
61
61
|
#pendingResultType;
|
|
62
|
+
#held = false;
|
|
62
63
|
#updateTTL;
|
|
63
64
|
constructor(input, onTransactionCommit, format, onDestroy, queryComplete, updateTTL, setState, retry) {
|
|
64
65
|
this.#input = input;
|
|
@@ -79,7 +80,7 @@ var SolidView = class {
|
|
|
79
80
|
if (queryComplete !== true && !("error" in queryComplete)) queryComplete.then(() => {
|
|
80
81
|
this.#setState((prev) => [prev[0], COMPLETE]);
|
|
81
82
|
}).catch((error) => {
|
|
82
|
-
this.#
|
|
83
|
+
this.#transitionResultType((prev) => [prev[0], this.#makeError(error)]);
|
|
83
84
|
});
|
|
84
85
|
}
|
|
85
86
|
#makeError(error) {
|
|
@@ -111,14 +112,24 @@ var SolidView = class {
|
|
|
111
112
|
unmarkCached() {
|
|
112
113
|
this.#transitionResultType((prev) => prev[1].type === "cached" ? [prev[0], UNKNOWN] : prev);
|
|
113
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Called before a deferred pipeline hydrates this view over several tasks
|
|
117
|
+
* (see `ViewFactory`). Rows already only reach the store at commit; this
|
|
118
|
+
* makes result type transitions wait for that commit too, including when
|
|
119
|
+
* the hydration produced no rows.
|
|
120
|
+
*/
|
|
121
|
+
holdData() {
|
|
122
|
+
this.#held = true;
|
|
123
|
+
}
|
|
114
124
|
#transitionResultType(transition) {
|
|
115
|
-
if (this.#hasUncommittedChanges()) this.#pendingResultType = transition;
|
|
125
|
+
if (this.#held || this.#hasUncommittedChanges()) this.#pendingResultType = transition;
|
|
116
126
|
else this.#setState(transition);
|
|
117
127
|
}
|
|
118
128
|
#hasUncommittedChanges() {
|
|
119
129
|
return this.#builderRoot !== void 0 && !isEmptyRoot(this.#builderRoot) || this.#pendingChanges.length > 0;
|
|
120
130
|
}
|
|
121
131
|
#onTransactionCommit = () => {
|
|
132
|
+
this.#held = false;
|
|
122
133
|
const builderRoot = this.#builderRoot;
|
|
123
134
|
if (builderRoot) {
|
|
124
135
|
if (!isEmptyRoot(builderRoot)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"solid-view.js","names":["#input","#format","#onDestroy","#retry","#updateTTL","#onTransactionCommit","#createEmptyRoot","#applyChangesToRoot","#setState","#makeError","#builderRoot","#transitionResultType","#hasUncommittedChanges","#pendingResultType","#pendingChanges","#applyChanges","#applyChangeToRoot"],"sources":["../../../../zero-solid/src/solid-view.ts"],"sourcesContent":["import {produce, reconcile, type SetStoreFunction} from 'solid-js/store';\nimport {emptyArray} from '../../shared/src/sentinels.ts';\nimport {ChangeIndex} from '../../zql/src/ivm/change-index.ts';\nimport {ChangeType} from '../../zql/src/ivm/change-type.ts';\nimport {\n applyChange,\n idSymbol,\n skipYields,\n type ViewChange,\n} from './bindings.ts';\nimport {\n type AnyViewFactory,\n type Change,\n type Entry,\n type ErroredQuery,\n type Format,\n type Input,\n type Node,\n type Output,\n type Query,\n type QueryErrorDetails,\n type QueryResultDetails,\n type Schema,\n type Stream,\n type TTL,\n} from './zero.ts';\n\nexport type State = [Entry, QueryResultDetails];\n\nexport const COMPLETE: QueryResultDetails = Object.freeze({type: 'complete'});\nexport const UNKNOWN: QueryResultDetails = Object.freeze({type: 'unknown'});\nexport const CACHED: QueryResultDetails = Object.freeze({type: 'cached'});\n\n/**\n * SolidView bridges Zero's incremental view updates with Solid's reactive\n * store.\n *\n * The challenge: Zero pushes individual row changes (add/remove/edit) as they\n * arrive, but updating Solid's store on each push is expensive. We batch\n * changes until transaction commit, then apply them all at once.\n *\n * Two code paths optimize for different scenarios:\n *\n * 1. BULK INSERT (builderRoot path): When building from empty, we skip\n * Solid's reactive tracking entirely and build a plain JS object. This is\n * 5x faster for large initial loads. Uses reconcile() to diff the final\n * result into Solid.\n *\n * 2. INCREMENTAL UPDATE (#pendingChanges path): For existing views, we queue\n * changes and apply them in-place within a produce() draft. produce()\n * creates a mutable draft, applyChange mutates it directly, then produce()\n * handles diffing and reactivity.\n *\n *\n * push(change)\n * │\n * ▼\n * ┌─────────────────────────────────┐\n * │ builderRoot defined? │\n * │ (building from empty) │\n * └─────────────────────────────────┘\n * │yes │no\n * ▼ ▼\n * Build plain JS Queue in\n * object (fast) #pendingChanges\n * │ │\n * └───────┬───────────┘\n * ▼\n * onTransactionCommit()\n * │\n * ┌───────┴──────────┐\n * │ │\n * builderRoot produce(draft =>\n * → reconcile() applyChange(draft, ..., mutate: true))\n */\nexport class SolidView implements Output {\n readonly #input: Input;\n readonly #format: Format;\n readonly #onDestroy: () => void;\n readonly #retry: () => void;\n\n #setState: SetStoreFunction<State>;\n\n // Optimization: if the store is currently empty we build up\n // the view on a plain old JS object stored at #builderRoot, and return\n // that for the new state on transaction commit. This avoids building up\n // large views from scratch via solid produce. The proxy object used by\n // solid produce is slow and in this case we don't care about solid tracking\n // the fine grained changes (everything has changed, it's all new). For a\n // test case with a view with 3000 rows, each row having 2 children, this\n // optimization reduced #applyChanges time from 743ms to 133ms.\n #builderRoot: Entry | undefined;\n #pendingChanges: ViewChange[] = [];\n // A result type transition requested while row changes are waiting for the\n // next commit. It is applied with them, so a cached claim never reaches the\n // store before the rows it is about.\n #pendingResultType: ((prev: State) => State) | undefined;\n readonly #updateTTL: (ttl: TTL) => void;\n\n constructor(\n input: Input,\n onTransactionCommit: (cb: () => void) => void,\n format: Format,\n onDestroy: () => void,\n queryComplete: true | ErroredQuery | Promise<true>,\n updateTTL: (ttl: TTL) => void,\n setState: SetStoreFunction<State>,\n retry: () => void,\n ) {\n this.#input = input;\n onTransactionCommit(this.#onTransactionCommit);\n this.#format = format;\n this.#onDestroy = onDestroy;\n this.#updateTTL = updateTTL;\n this.#retry = retry;\n\n input.setOutput(this);\n\n const initialRoot = this.#createEmptyRoot();\n this.#applyChangesToRoot(\n skipYields(input.fetch({})),\n node => ({type: 'add', node}),\n initialRoot,\n );\n\n this.#setState = setState;\n this.#setState(\n reconcile(\n [\n initialRoot,\n queryComplete === true\n ? COMPLETE\n : 'error' in queryComplete\n ? this.#makeError(queryComplete)\n : UNKNOWN,\n ],\n {\n // solidjs's types want a string, but a symbol works\n key: idSymbol as unknown as string,\n },\n ),\n );\n\n if (isEmptyRoot(initialRoot)) {\n this.#builderRoot = this.#createEmptyRoot();\n }\n\n if (queryComplete !== true && !('error' in queryComplete)) {\n void queryComplete\n .then(() => {\n this.#setState(prev => [prev[0], COMPLETE]);\n })\n .catch((error: ErroredQuery) => {\n this.#setState(prev => [prev[0], this.#makeError(error)]);\n });\n }\n }\n\n #makeError(error: ErroredQuery): QueryErrorDetails {\n const message = error.message ?? 'An unknown error occurred';\n return {\n type: 'error',\n retry: this.#retry,\n refetch: this.#retry,\n error: {\n type: error.error,\n message,\n ...(error.details ? {details: error.details} : {}),\n },\n };\n }\n\n destroy(): void {\n this.#onDestroy();\n }\n\n /**\n * The store holds the server-confirmed result of this query from a\n * previous connection. Never downgrades 'complete'/'error'; freshness stays\n * a promise only the connection can keep, so nothing that waits on\n * 'complete' resolves here.\n */\n markCached(): void {\n this.#transitionResultType(prev =>\n prev[1].type === 'unknown' ? [prev[0], CACHED] : prev,\n );\n }\n\n /** The got key was deleted (eviction) before this connection confirmed it. */\n unmarkCached(): void {\n this.#transitionResultType(prev =>\n prev[1].type === 'cached' ? [prev[0], UNKNOWN] : prev,\n );\n }\n\n #transitionResultType(transition: (prev: State) => State): void {\n if (this.#hasUncommittedChanges()) {\n // The last requested transition wins; each is a no-op unless the state\n // it expects is current, so the net effect at commit is correct.\n this.#pendingResultType = transition;\n } else {\n this.#setState(transition);\n }\n }\n\n #hasUncommittedChanges(): boolean {\n return (\n (this.#builderRoot !== undefined && !isEmptyRoot(this.#builderRoot)) ||\n this.#pendingChanges.length > 0\n );\n }\n\n #onTransactionCommit = () => {\n const builderRoot = this.#builderRoot;\n if (builderRoot) {\n if (!isEmptyRoot(builderRoot)) {\n this.#setState(\n 0,\n reconcile(builderRoot, {\n // solidjs's types want a string, but a symbol works\n key: idSymbol as unknown as string,\n }),\n );\n this.#builderRoot = undefined;\n }\n } else {\n try {\n this.#applyChanges(this.#pendingChanges, c => c);\n } finally {\n this.#pendingChanges = [];\n }\n }\n const pendingResultType = this.#pendingResultType;\n if (pendingResultType) {\n this.#pendingResultType = undefined;\n this.#setState(pendingResultType);\n }\n };\n\n push(change: Change) {\n // Delay updating the solid store state until the transaction commit\n // (because each update of the solid store is quite expensive). If\n // this.#builderRoot is defined apply the changes to it (we are building\n // from an empty root), otherwise queue the changes to be applied\n // using produce at the end of the transaction but read the relationships\n // now as they are only valid to read when the push is received.\n if (this.#builderRoot) {\n this.#applyChangeToRoot(\n materializeRelationships(change),\n this.#builderRoot,\n );\n } else {\n this.#pendingChanges.push(materializeRelationships(change));\n }\n return emptyArray;\n }\n\n #applyChanges<T>(changes: Iterable<T>, mapper: (v: T) => ViewChange): void {\n this.#setState(\n produce((draftState: State) => {\n this.#applyChangesToRoot<T>(changes, mapper, draftState[0]);\n if (isEmptyRoot(draftState[0])) {\n this.#builderRoot = this.#createEmptyRoot();\n }\n }),\n );\n }\n\n #applyChangesToRoot<T>(\n changes: Iterable<T>,\n mapper: (v: T) => ViewChange,\n root: Entry,\n ) {\n for (const change of changes) {\n this.#applyChangeToRoot(mapper(change), root);\n }\n }\n\n #applyChangeToRoot(change: ViewChange, root: Entry) {\n applyChange(\n root,\n change,\n this.#input.getSchema(),\n '',\n this.#format,\n true /* withIDs */,\n true /* mutate */,\n );\n }\n\n #createEmptyRoot(): Entry {\n return {\n '': this.#format.singular ? undefined : [],\n };\n }\n\n updateTTL(ttl: TTL): void {\n this.#updateTTL(ttl);\n }\n}\n\nfunction materializeRelationships(change: Change): ViewChange {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n return {\n type: 'add',\n node: materializeNodeRelationships(change[ChangeIndex.NODE]),\n };\n case ChangeType.REMOVE:\n return {\n type: 'remove',\n node: materializeNodeRelationships(change[ChangeIndex.NODE]),\n };\n case ChangeType.CHILD:\n return {\n type: 'child',\n node: {row: change[ChangeIndex.NODE].row},\n child: {\n relationshipName: change[ChangeIndex.CHILD_DATA].relationshipName,\n change: materializeRelationships(\n change[ChangeIndex.CHILD_DATA].change,\n ),\n },\n };\n case ChangeType.EDIT:\n return {\n type: 'edit',\n node: {row: change[ChangeIndex.NODE].row},\n oldNode: {row: change[ChangeIndex.OLD_NODE].row},\n };\n }\n}\n\nfunction materializeNodeRelationships(node: Node): Node {\n const relationships: Record<string, () => Stream<Node>> = {};\n for (const relationship in node.relationships) {\n const materialized: Node[] = [];\n for (const n of skipYields(node.relationships[relationship]())) {\n materialized.push(materializeNodeRelationships(n));\n }\n relationships[relationship] = () => materialized;\n }\n return {\n row: node.row,\n relationships,\n };\n}\n\nfunction isEmptyRoot(entry: Entry) {\n const data = entry[''];\n return data === undefined || (Array.isArray(data) && data.length === 0);\n}\n\nexport function createSolidViewFactory(\n setState: SetStoreFunction<State>,\n retry?: () => void,\n) {\n function solidViewFactory<\n TTable extends keyof TSchema['tables'] & string,\n TSchema extends Schema,\n TReturn,\n >(\n _query: Query<TTable, TSchema, TReturn>,\n input: Input,\n format: Format,\n onDestroy: () => void,\n onTransactionCommit: (cb: () => void) => void,\n queryComplete: true | ErroredQuery | Promise<true>,\n updateTTL: (ttl: TTL) => void,\n ) {\n return new SolidView(\n input,\n onTransactionCommit,\n format,\n onDestroy,\n queryComplete,\n updateTTL,\n setState,\n retry || (() => {}),\n );\n }\n\n solidViewFactory satisfies AnyViewFactory;\n\n return solidViewFactory;\n}\n"],"mappings":";;;;;;;AA6BA,IAAa,WAA+B,OAAO,OAAO,EAAC,MAAM,WAAU,CAAC;AAC5E,IAAa,UAA8B,OAAO,OAAO,EAAC,MAAM,UAAS,CAAC;AAC1E,IAAa,SAA6B,OAAO,OAAO,EAAC,MAAM,SAAQ,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CxE,IAAa,YAAb,MAAyC;CACvC;CACA;CACA;CACA;CAEA;CAUA;CACA,kBAAgC,CAAC;CAIjC;CACA;CAEA,YACE,OACA,qBACA,QAC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y","#retry","#updateTTL","#onTransactionCommit","#createEmptyRoot","#applyChangesToRoot","#setState","#makeError","#builderRoot","#transitionResultType","#held","#hasUncommittedChanges","#pendingResultType","#pendingChanges","#applyChanges","#applyChangeToRoot"],"sources":["../../../../zero-solid/src/solid-view.ts"],"sourcesContent":["import {produce, reconcile, type SetStoreFunction} from 'solid-js/store';\nimport {emptyArray} from '../../shared/src/sentinels.ts';\nimport {ChangeIndex} from '../../zql/src/ivm/change-index.ts';\nimport {ChangeType} from '../../zql/src/ivm/change-type.ts';\nimport {\n applyChange,\n idSymbol,\n skipYields,\n type ViewChange,\n} from './bindings.ts';\nimport {\n type AnyViewFactory,\n type Change,\n type Entry,\n type ErroredQuery,\n type Format,\n type Input,\n type Node,\n type Output,\n type Query,\n type QueryErrorDetails,\n type QueryResultDetails,\n type Schema,\n type Stream,\n type TTL,\n} from './zero.ts';\n\nexport type State = [Entry, QueryResultDetails];\n\nexport const COMPLETE: QueryResultDetails = Object.freeze({type: 'complete'});\nexport const UNKNOWN: QueryResultDetails = Object.freeze({type: 'unknown'});\nexport const CACHED: QueryResultDetails = Object.freeze({type: 'cached'});\n\n/**\n * SolidView bridges Zero's incremental view updates with Solid's reactive\n * store.\n *\n * The challenge: Zero pushes individual row changes (add/remove/edit) as they\n * arrive, but updating Solid's store on each push is expensive. We batch\n * changes until transaction commit, then apply them all at once.\n *\n * Two code paths optimize for different scenarios:\n *\n * 1. BULK INSERT (builderRoot path): When building from empty, we skip\n * Solid's reactive tracking entirely and build a plain JS object. This is\n * 5x faster for large initial loads. Uses reconcile() to diff the final\n * result into Solid.\n *\n * 2. INCREMENTAL UPDATE (#pendingChanges path): For existing views, we queue\n * changes and apply them in-place within a produce() draft. produce()\n * creates a mutable draft, applyChange mutates it directly, then produce()\n * handles diffing and reactivity.\n *\n *\n * push(change)\n * │\n * ▼\n * ┌─────────────────────────────────┐\n * │ builderRoot defined? │\n * │ (building from empty) │\n * └─────────────────────────────────┘\n * │yes │no\n * ▼ ▼\n * Build plain JS Queue in\n * object (fast) #pendingChanges\n * │ │\n * └───────┬───────────┘\n * ▼\n * onTransactionCommit()\n * │\n * ┌───────┴──────────┐\n * │ │\n * builderRoot produce(draft =>\n * → reconcile() applyChange(draft, ..., mutate: true))\n */\nexport class SolidView implements Output {\n readonly #input: Input;\n readonly #format: Format;\n readonly #onDestroy: () => void;\n readonly #retry: () => void;\n\n #setState: SetStoreFunction<State>;\n\n // Optimization: if the store is currently empty we build up\n // the view on a plain old JS object stored at #builderRoot, and return\n // that for the new state on transaction commit. This avoids building up\n // large views from scratch via solid produce. The proxy object used by\n // solid produce is slow and in this case we don't care about solid tracking\n // the fine grained changes (everything has changed, it's all new). For a\n // test case with a view with 3000 rows, each row having 2 children, this\n // optimization reduced #applyChanges time from 743ms to 133ms.\n #builderRoot: Entry | undefined;\n #pendingChanges: ViewChange[] = [];\n // A result type transition requested while row changes are waiting for the\n // next commit. It is applied with them, so a cached claim never reaches the\n // store before the rows it is about.\n #pendingResultType: ((prev: State) => State) | undefined;\n #held = false;\n readonly #updateTTL: (ttl: TTL) => void;\n\n constructor(\n input: Input,\n onTransactionCommit: (cb: () => void) => void,\n format: Format,\n onDestroy: () => void,\n queryComplete: true | ErroredQuery | Promise<true>,\n updateTTL: (ttl: TTL) => void,\n setState: SetStoreFunction<State>,\n retry: () => void,\n ) {\n this.#input = input;\n onTransactionCommit(this.#onTransactionCommit);\n this.#format = format;\n this.#onDestroy = onDestroy;\n this.#updateTTL = updateTTL;\n this.#retry = retry;\n\n input.setOutput(this);\n\n const initialRoot = this.#createEmptyRoot();\n this.#applyChangesToRoot(\n skipYields(input.fetch({})),\n node => ({type: 'add', node}),\n initialRoot,\n );\n\n this.#setState = setState;\n this.#setState(\n reconcile(\n [\n initialRoot,\n queryComplete === true\n ? COMPLETE\n : 'error' in queryComplete\n ? this.#makeError(queryComplete)\n : UNKNOWN,\n ],\n {\n // solidjs's types want a string, but a symbol works\n key: idSymbol as unknown as string,\n },\n ),\n );\n\n if (isEmptyRoot(initialRoot)) {\n this.#builderRoot = this.#createEmptyRoot();\n }\n\n if (queryComplete !== true && !('error' in queryComplete)) {\n void queryComplete\n .then(() => {\n this.#setState(prev => [prev[0], COMPLETE]);\n })\n .catch((error: ErroredQuery) => {\n // Through the pending path: a deferred attach that fails is\n // published with the commit of its batch, not during it.\n this.#transitionResultType(prev => [prev[0], this.#makeError(error)]);\n });\n }\n }\n\n #makeError(error: ErroredQuery): QueryErrorDetails {\n const message = error.message ?? 'An unknown error occurred';\n return {\n type: 'error',\n retry: this.#retry,\n refetch: this.#retry,\n error: {\n type: error.error,\n message,\n ...(error.details ? {details: error.details} : {}),\n },\n };\n }\n\n destroy(): void {\n this.#onDestroy();\n }\n\n /**\n * The store holds the server-confirmed result of this query from a\n * previous connection. Never downgrades 'complete'/'error'; freshness stays\n * a promise only the connection can keep, so nothing that waits on\n * 'complete' resolves here.\n */\n markCached(): void {\n this.#transitionResultType(prev =>\n prev[1].type === 'unknown' ? [prev[0], CACHED] : prev,\n );\n }\n\n /** The got key was deleted (eviction) before this connection confirmed it. */\n unmarkCached(): void {\n this.#transitionResultType(prev =>\n prev[1].type === 'cached' ? [prev[0], UNKNOWN] : prev,\n );\n }\n\n /**\n * Called before a deferred pipeline hydrates this view over several tasks\n * (see `ViewFactory`). Rows already only reach the store at commit; this\n * makes result type transitions wait for that commit too, including when\n * the hydration produced no rows.\n */\n holdData(): void {\n this.#held = true;\n }\n\n #transitionResultType(transition: (prev: State) => State): void {\n if (this.#held || this.#hasUncommittedChanges()) {\n // The last requested transition wins; each is a no-op unless the state\n // it expects is current, so the net effect at commit is correct.\n this.#pendingResultType = transition;\n } else {\n this.#setState(transition);\n }\n }\n\n #hasUncommittedChanges(): boolean {\n return (\n (this.#builderRoot !== undefined && !isEmptyRoot(this.#builderRoot)) ||\n this.#pendingChanges.length > 0\n );\n }\n\n #onTransactionCommit = () => {\n this.#held = false;\n const builderRoot = this.#builderRoot;\n if (builderRoot) {\n if (!isEmptyRoot(builderRoot)) {\n this.#setState(\n 0,\n reconcile(builderRoot, {\n // solidjs's types want a string, but a symbol works\n key: idSymbol as unknown as string,\n }),\n );\n this.#builderRoot = undefined;\n }\n } else {\n try {\n this.#applyChanges(this.#pendingChanges, c => c);\n } finally {\n this.#pendingChanges = [];\n }\n }\n const pendingResultType = this.#pendingResultType;\n if (pendingResultType) {\n this.#pendingResultType = undefined;\n this.#setState(pendingResultType);\n }\n };\n\n push(change: Change) {\n // Delay updating the solid store state until the transaction commit\n // (because each update of the solid store is quite expensive). If\n // this.#builderRoot is defined apply the changes to it (we are building\n // from an empty root), otherwise queue the changes to be applied\n // using produce at the end of the transaction but read the relationships\n // now as they are only valid to read when the push is received.\n if (this.#builderRoot) {\n this.#applyChangeToRoot(\n materializeRelationships(change),\n this.#builderRoot,\n );\n } else {\n this.#pendingChanges.push(materializeRelationships(change));\n }\n return emptyArray;\n }\n\n #applyChanges<T>(changes: Iterable<T>, mapper: (v: T) => ViewChange): void {\n this.#setState(\n produce((draftState: State) => {\n this.#applyChangesToRoot<T>(changes, mapper, draftState[0]);\n if (isEmptyRoot(draftState[0])) {\n this.#builderRoot = this.#createEmptyRoot();\n }\n }),\n );\n }\n\n #applyChangesToRoot<T>(\n changes: Iterable<T>,\n mapper: (v: T) => ViewChange,\n root: Entry,\n ) {\n for (const change of changes) {\n this.#applyChangeToRoot(mapper(change), root);\n }\n }\n\n #applyChangeToRoot(change: ViewChange, root: Entry) {\n applyChange(\n root,\n change,\n this.#input.getSchema(),\n '',\n this.#format,\n true /* withIDs */,\n true /* mutate */,\n );\n }\n\n #createEmptyRoot(): Entry {\n return {\n '': this.#format.singular ? undefined : [],\n };\n }\n\n updateTTL(ttl: TTL): void {\n this.#updateTTL(ttl);\n }\n}\n\nfunction materializeRelationships(change: Change): ViewChange {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n return {\n type: 'add',\n node: materializeNodeRelationships(change[ChangeIndex.NODE]),\n };\n case ChangeType.REMOVE:\n return {\n type: 'remove',\n node: materializeNodeRelationships(change[ChangeIndex.NODE]),\n };\n case ChangeType.CHILD:\n return {\n type: 'child',\n node: {row: change[ChangeIndex.NODE].row},\n child: {\n relationshipName: change[ChangeIndex.CHILD_DATA].relationshipName,\n change: materializeRelationships(\n change[ChangeIndex.CHILD_DATA].change,\n ),\n },\n };\n case ChangeType.EDIT:\n return {\n type: 'edit',\n node: {row: change[ChangeIndex.NODE].row},\n oldNode: {row: change[ChangeIndex.OLD_NODE].row},\n };\n }\n}\n\nfunction materializeNodeRelationships(node: Node): Node {\n const relationships: Record<string, () => Stream<Node>> = {};\n for (const relationship in node.relationships) {\n const materialized: Node[] = [];\n for (const n of skipYields(node.relationships[relationship]())) {\n materialized.push(materializeNodeRelationships(n));\n }\n relationships[relationship] = () => materialized;\n }\n return {\n row: node.row,\n relationships,\n };\n}\n\nfunction isEmptyRoot(entry: Entry) {\n const data = entry[''];\n return data === undefined || (Array.isArray(data) && data.length === 0);\n}\n\nexport function createSolidViewFactory(\n setState: SetStoreFunction<State>,\n retry?: () => void,\n) {\n function solidViewFactory<\n TTable extends keyof TSchema['tables'] & string,\n TSchema extends Schema,\n TReturn,\n >(\n _query: Query<TTable, TSchema, TReturn>,\n input: Input,\n format: Format,\n onDestroy: () => void,\n onTransactionCommit: (cb: () => void) => void,\n queryComplete: true | ErroredQuery | Promise<true>,\n updateTTL: (ttl: TTL) => void,\n ) {\n return new SolidView(\n input,\n onTransactionCommit,\n format,\n onDestroy,\n queryComplete,\n updateTTL,\n setState,\n retry || (() => {}),\n );\n }\n\n solidViewFactory satisfies AnyViewFactory;\n\n return solidViewFactory;\n}\n"],"mappings":";;;;;;;AA6BA,IAAa,WAA+B,OAAO,OAAO,EAAC,MAAM,WAAU,CAAC;AAC5E,IAAa,UAA8B,OAAO,OAAO,EAAC,MAAM,UAAS,CAAC;AAC1E,IAAa,SAA6B,OAAO,OAAO,EAAC,MAAM,SAAQ,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;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AAgB,uBACd,UACA,OACA;CACA,SAAS,iBAKP,QACA,OACA,QACA,WACA,qBACA,eACA,WACA;EACA,OAAO,IAAI,UACT,OACA,qBACA,QACA,WACA,eACA,WACA,UACA,gBAAgB,CAAC,EACnB;CACF;CAIA,OAAO;AACT"}
|
|
@@ -20,7 +20,12 @@ export interface DebugDelegate {
|
|
|
20
20
|
}
|
|
21
21
|
export declare class Debug implements DebugDelegate {
|
|
22
22
|
#private;
|
|
23
|
-
|
|
23
|
+
/**
|
|
24
|
+
* @param collectRows Whether to keep the vended rows.
|
|
25
|
+
* @param maxRowsPerQuery Maximum number of rows kept per table/query. Rows
|
|
26
|
+
* past this are still counted but not kept.
|
|
27
|
+
*/
|
|
28
|
+
constructor(collectRows: boolean, maxRowsPerQuery?: number);
|
|
24
29
|
getVendedRowCounts(): RowCountsBySource;
|
|
25
30
|
getVendedRows(): RowsBySource;
|
|
26
31
|
getNVisitCounts(): RowCountsBySource;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"debug-delegate.d.ts","sourceRoot":"","sources":["../../../../../zql/src/builder/debug-delegate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,iBAAiB,EAEjB,YAAY,EACb,MAAM,oDAAoD,CAAC;AAC5D,OAAO,EAAC,KAAK,GAAG,EAAC,MAAM,oCAAoC,CAAC;AAE5D,eAAO,MAAM,iBAAiB;;;CAG7B,CAAC;AAEF,KAAK,UAAU,GAAG,MAAM,CAAC;AACzB,KAAK,GAAG,GAAG,MAAM,CAAC;AAElB,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,CAAC,CAAC;AAEhD,MAAM,WAAW,aAAa;IAC5B,SAAS,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC;IAC/C,SAAS,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,IAAI,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,CAAC;IACrC,aAAa,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IACnE,cAAc,IAAI,WAAW,CAAC;IAE9B,KAAK,IAAI,IAAI,CAAC;CACf;AAED,qBAAa,KAAM,YAAW,aAAa;;IAQzC;;;;OAIG;gBACS,WAAW,EAAE,OAAO,EAAE,eAAe,SAAW;IAS5D,kBAAkB,IAAI,iBAAiB;IAIvC,aAAa,IAAI,YAAY;IAI7B,eAAe,IAAI,iBAAiB;IAIpC,cAAc,IAAI,WAAW;IAI7B,SAAS,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI;IAS9C,KAAK,IAAI,IAAI;IAOb,SAAS,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,IAAI;IAaxD,YAAY,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI;IAYjE,aAAa,CAAC,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI;CAmBpE"}
|
|
@@ -9,7 +9,16 @@ var Debug = class {
|
|
|
9
9
|
#rowsBySource;
|
|
10
10
|
#nvisitBySource;
|
|
11
11
|
#plans;
|
|
12
|
-
|
|
12
|
+
#collectRows;
|
|
13
|
+
#maxRowsPerQuery;
|
|
14
|
+
/**
|
|
15
|
+
* @param collectRows Whether to keep the vended rows.
|
|
16
|
+
* @param maxRowsPerQuery Maximum number of rows kept per table/query. Rows
|
|
17
|
+
* past this are still counted but not kept.
|
|
18
|
+
*/
|
|
19
|
+
constructor(collectRows, maxRowsPerQuery = Infinity) {
|
|
20
|
+
this.#collectRows = collectRows;
|
|
21
|
+
this.#maxRowsPerQuery = maxRowsPerQuery;
|
|
13
22
|
this.#rowCountsBySource = {};
|
|
14
23
|
this.#rowsBySource = {};
|
|
15
24
|
this.#nvisitBySource = {};
|
|
@@ -42,7 +51,10 @@ var Debug = class {
|
|
|
42
51
|
rowVended(table, query, row) {
|
|
43
52
|
const { counts, rows } = this.#getRowStats(table);
|
|
44
53
|
if (counts) counts[query] = (counts[query] ?? 0) + 1;
|
|
45
|
-
if (
|
|
54
|
+
if (this.#collectRows) {
|
|
55
|
+
const queryRows = rows[query] ??= [];
|
|
56
|
+
if (queryRows.length < this.#maxRowsPerQuery) queryRows.push(row);
|
|
57
|
+
}
|
|
46
58
|
}
|
|
47
59
|
recordNVisit(table, query, nvisit) {
|
|
48
60
|
let nvisitCounts = this.#nvisitBySource[table];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"debug-delegate.js","names":["#rowCountsBySource","#rowsBySource","#nvisitBySource","#plans","#getRowStats"],"sources":["../../../../../zql/src/builder/debug-delegate.ts"],"sourcesContent":["import type {\n RowCountsByQuery,\n RowCountsBySource,\n RowsByQuery,\n RowsBySource,\n} from '../../../zero-protocol/src/analyze-query-result.ts';\nimport {type Row} from '../../../zero-protocol/src/data.ts';\n\nexport const runtimeDebugFlags = {\n trackRowCountsVended: false,\n trackRowsVended: false,\n};\n\ntype SourceName = string;\ntype SQL = string;\n\nexport type SQLitePlans = Record<SQL, string[]>;\n\nexport interface DebugDelegate {\n initQuery(table: SourceName, query: SQL): void;\n rowVended(table: SourceName, query: SQL, row: Row): void;\n getVendedRowCounts(): RowCountsBySource;\n getVendedRows(): RowsBySource;\n recordNVisit(table: SourceName, query: SQL, nvisit: number): void;\n getNVisitCounts(): RowCountsBySource;\n recordExplain(table: SourceName, query: SQL, plan: string[]): void;\n getSQLitePlans(): SQLitePlans;\n // clears all internal state\n reset(): void;\n}\n\nexport class Debug implements DebugDelegate {\n #rowCountsBySource: RowCountsBySource;\n #rowsBySource: RowsBySource;\n #nvisitBySource: RowCountsBySource;\n #plans: SQLitePlans;\n\n constructor() {\n this.#rowCountsBySource = {};\n this.#rowsBySource = {};\n this.#nvisitBySource = {};\n this.#plans = {};\n }\n\n getVendedRowCounts(): RowCountsBySource {\n return this.#rowCountsBySource;\n }\n\n getVendedRows(): RowsBySource {\n return this.#rowsBySource;\n }\n\n getNVisitCounts(): RowCountsBySource {\n return this.#nvisitBySource;\n }\n\n getSQLitePlans(): SQLitePlans {\n return this.#plans;\n }\n\n initQuery(table: SourceName, query: SQL): void {\n const {counts} = this.#getRowStats(table);\n if (counts) {\n if (!counts[query]) {\n counts[query] = 0;\n }\n }\n }\n\n reset(): void {\n this.#rowCountsBySource = {};\n this.#rowsBySource = {};\n this.#nvisitBySource = {};\n this.#plans = {};\n }\n\n rowVended(table: SourceName, query: SQL, row: Row): void {\n const {counts, rows} = this.#getRowStats(table);\n if (counts) {\n counts[query] = (counts[query] ?? 0) + 1;\n }\n if (
|
|
1
|
+
{"version":3,"file":"debug-delegate.js","names":["#collectRows","#maxRowsPerQuery","#rowCountsBySource","#rowsBySource","#nvisitBySource","#plans","#getRowStats"],"sources":["../../../../../zql/src/builder/debug-delegate.ts"],"sourcesContent":["import type {\n RowCountsByQuery,\n RowCountsBySource,\n RowsByQuery,\n RowsBySource,\n} from '../../../zero-protocol/src/analyze-query-result.ts';\nimport {type Row} from '../../../zero-protocol/src/data.ts';\n\nexport const runtimeDebugFlags = {\n trackRowCountsVended: false,\n trackRowsVended: false,\n};\n\ntype SourceName = string;\ntype SQL = string;\n\nexport type SQLitePlans = Record<SQL, string[]>;\n\nexport interface DebugDelegate {\n initQuery(table: SourceName, query: SQL): void;\n rowVended(table: SourceName, query: SQL, row: Row): void;\n getVendedRowCounts(): RowCountsBySource;\n getVendedRows(): RowsBySource;\n recordNVisit(table: SourceName, query: SQL, nvisit: number): void;\n getNVisitCounts(): RowCountsBySource;\n recordExplain(table: SourceName, query: SQL, plan: string[]): void;\n getSQLitePlans(): SQLitePlans;\n // clears all internal state\n reset(): void;\n}\n\nexport class Debug implements DebugDelegate {\n #rowCountsBySource: RowCountsBySource;\n #rowsBySource: RowsBySource;\n #nvisitBySource: RowCountsBySource;\n #plans: SQLitePlans;\n readonly #collectRows: boolean;\n readonly #maxRowsPerQuery: number;\n\n /**\n * @param collectRows Whether to keep the vended rows.\n * @param maxRowsPerQuery Maximum number of rows kept per table/query. Rows\n * past this are still counted but not kept.\n */\n constructor(collectRows: boolean, maxRowsPerQuery = Infinity) {\n this.#collectRows = collectRows;\n this.#maxRowsPerQuery = maxRowsPerQuery;\n this.#rowCountsBySource = {};\n this.#rowsBySource = {};\n this.#nvisitBySource = {};\n this.#plans = {};\n }\n\n getVendedRowCounts(): RowCountsBySource {\n return this.#rowCountsBySource;\n }\n\n getVendedRows(): RowsBySource {\n return this.#rowsBySource;\n }\n\n getNVisitCounts(): RowCountsBySource {\n return this.#nvisitBySource;\n }\n\n getSQLitePlans(): SQLitePlans {\n return this.#plans;\n }\n\n initQuery(table: SourceName, query: SQL): void {\n const {counts} = this.#getRowStats(table);\n if (counts) {\n if (!counts[query]) {\n counts[query] = 0;\n }\n }\n }\n\n reset(): void {\n this.#rowCountsBySource = {};\n this.#rowsBySource = {};\n this.#nvisitBySource = {};\n this.#plans = {};\n }\n\n rowVended(table: SourceName, query: SQL, row: Row): void {\n const {counts, rows} = this.#getRowStats(table);\n if (counts) {\n counts[query] = (counts[query] ?? 0) + 1;\n }\n if (this.#collectRows) {\n const queryRows = (rows[query] ??= []);\n if (queryRows.length < this.#maxRowsPerQuery) {\n queryRows.push(row);\n }\n }\n }\n\n recordNVisit(table: SourceName, query: SQL, nvisit: number): void {\n let nvisitCounts = this.#nvisitBySource[table];\n if (!nvisitCounts) {\n nvisitCounts = {};\n this.#nvisitBySource[table] = nvisitCounts;\n }\n if (!nvisitCounts[query]) {\n nvisitCounts[query] = 0;\n }\n nvisitCounts[query] += nvisit;\n }\n\n recordExplain(_table: SourceName, query: SQL, plan: string[]): void {\n this.#plans[query] = plan;\n }\n\n #getRowStats(source: SourceName) {\n let counts: RowCountsByQuery | undefined;\n let rows: RowsByQuery | undefined;\n counts = this.#rowCountsBySource[source];\n if (!counts) {\n counts = {};\n this.#rowCountsBySource[source] = counts;\n }\n rows = this.#rowsBySource[source];\n if (!rows) {\n rows = {};\n this.#rowsBySource[source] = rows;\n }\n return {counts, rows};\n }\n}\n"],"mappings":";;AAQA,IAAa,oBAAoB;CAC/B,sBAAsB;CACtB,iBAAiB;AACnB;AAoBA,IAAa,QAAb,MAA4C;CAC1C;CACA;CACA;CACA;CACA;CACA;;;;;;CAOA,YAAY,aAAsB,kBAAkB,UAAU;EAC5D,KAAKA,eAAe;EACpB,KAAKC,mBAAmB;EACxB,KAAKC,qBAAqB,CAAC;EAC3B,KAAKC,gBAAgB,CAAC;EACtB,KAAKC,kBAAkB,CAAC;EACxB,KAAKC,SAAS,CAAC;CACjB;CAEA,qBAAwC;EACtC,OAAO,KAAKH;CACd;CAEA,gBAA8B;EAC5B,OAAO,KAAKC;CACd;CAEA,kBAAqC;EACnC,OAAO,KAAKC;CACd;CAEA,iBAA8B;EAC5B,OAAO,KAAKC;CACd;CAEA,UAAU,OAAmB,OAAkB;EAC7C,MAAM,EAAC,WAAU,KAAKC,aAAa,KAAK;EACxC,IAAI;OACE,CAAC,OAAO,QACV,OAAO,SAAS;EAAA;CAGtB;CAEA,QAAc;EACZ,KAAKJ,qBAAqB,CAAC;EAC3B,KAAKC,gBAAgB,CAAC;EACtB,KAAKC,kBAAkB,CAAC;EACxB,KAAKC,SAAS,CAAC;CACjB;CAEA,UAAU,OAAmB,OAAY,KAAgB;EACvD,MAAM,EAAC,QAAQ,SAAQ,KAAKC,aAAa,KAAK;EAC9C,IAAI,QACF,OAAO,UAAU,OAAO,UAAU,KAAK;EAEzC,IAAI,KAAKN,cAAc;GACrB,MAAM,YAAa,KAAK,WAAW,CAAC;GACpC,IAAI,UAAU,SAAS,KAAKC,kBAC1B,UAAU,KAAK,GAAG;EAEtB;CACF;CAEA,aAAa,OAAmB,OAAY,QAAsB;EAChE,IAAI,eAAe,KAAKG,gBAAgB;EACxC,IAAI,CAAC,cAAc;GACjB,eAAe,CAAC;GAChB,KAAKA,gBAAgB,SAAS;EAChC;EACA,IAAI,CAAC,aAAa,QAChB,aAAa,SAAS;EAExB,aAAa,UAAU;CACzB;CAEA,cAAc,QAAoB,OAAY,MAAsB;EAClE,KAAKC,OAAO,SAAS;CACvB;CAEA,aAAa,QAAoB;EAC/B,IAAI;EACJ,IAAI;EACJ,SAAS,KAAKH,mBAAmB;EACjC,IAAI,CAAC,QAAQ;GACX,SAAS,CAAC;GACV,KAAKA,mBAAmB,UAAU;EACpC;EACA,OAAO,KAAKC,cAAc;EAC1B,IAAI,CAAC,MAAM;GACT,OAAO,CAAC;GACR,KAAKA,cAAc,UAAU;EAC/B;EACA,OAAO;GAAC;GAAQ;EAAI;CACtB;AACF"}
|
|
@@ -23,6 +23,25 @@ export declare class ArrayView<V extends View> implements Output, TypedView<V> {
|
|
|
23
23
|
addListener(listener: Listener<V>): () => void;
|
|
24
24
|
destroy(): void;
|
|
25
25
|
push(change: Change): readonly never[];
|
|
26
|
+
/**
|
|
27
|
+
* Keep exposing the current snapshot, whatever is pushed in the meantime,
|
|
28
|
+
* until {@link releaseData} or the next {@link flush}. Result type
|
|
29
|
+
* notifications are held back until that flush.
|
|
30
|
+
*
|
|
31
|
+
* A transaction is normally one synchronous task, so nobody can look at a
|
|
32
|
+
* view between its pushes and its flush. Deferred pipelines are different:
|
|
33
|
+
* they are hydrated over several tasks and flushed together (see
|
|
34
|
+
* `QueryDelegate.onPipelinesReady`), and a view that already has its rows
|
|
35
|
+
* must not show them early.
|
|
36
|
+
*/
|
|
37
|
+
holdData(): void;
|
|
38
|
+
/**
|
|
39
|
+
* Make `data` current again without notifying anyone yet; listeners are
|
|
40
|
+
* still only called by the next {@link flush}. Views released together get
|
|
41
|
+
* this called on all of them before any is flushed, so a listener of one
|
|
42
|
+
* that reads another sees its rows too.
|
|
43
|
+
*/
|
|
44
|
+
releaseData(): void;
|
|
26
45
|
flush(): void;
|
|
27
46
|
updateTTL(ttl: TTL): void;
|
|
28
47
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array-view.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/array-view.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,8CAA8C,CAAC;AAC/E,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,iBAAiB,CAAC;AACzC,OAAO,KAAK,EAAC,QAAQ,EAAc,SAAS,EAAC,MAAM,wBAAwB,CAAC;AAG5E,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AACxC,OAAO,EAAa,KAAK,KAAK,EAAE,KAAK,MAAM,EAAC,MAAM,eAAe,CAAC;AAGlE,OAAO,KAAK,EAAQ,MAAM,EAAE,IAAI,EAAC,MAAM,WAAW,CAAC;AA0BnD;;;;;;;;;;GAUG;AACH,qBAAa,SAAS,CAAC,CAAC,SAAS,IAAI,CAAE,YAAW,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;;IAgBpE,SAAS,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"array-view.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/array-view.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,8CAA8C,CAAC;AAC/E,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,iBAAiB,CAAC;AACzC,OAAO,KAAK,EAAC,QAAQ,EAAc,SAAS,EAAC,MAAM,wBAAwB,CAAC;AAG5E,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AACxC,OAAO,EAAa,KAAK,KAAK,EAAE,KAAK,MAAM,EAAC,MAAM,eAAe,CAAC;AAGlE,OAAO,KAAK,EAAQ,MAAM,EAAE,IAAI,EAAC,MAAM,WAAW,CAAC;AA0BnD;;;;;;;;;;GAUG;AACH,qBAAa,SAAS,CAAC,CAAC,SAAS,IAAI,CAAE,YAAW,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;;IAgBpE,SAAS,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS,CAAC;gBA0BlC,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,MAAM,EACd,aAAa,EAAE,IAAI,GAAG,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,EAClD,SAAS,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,IAAI;IAqB/B,IAAI,IAAI,IAC4C,CAAC,CACpD;IAMD,WAAW,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC;IA8BjC,OAAO;IAuBP,IAAI,CAAC,MAAM,EAAE,MAAM;IAcnB;;;;;;;;;;OAUG;IACH,QAAQ,IAAI,IAAI;IAOhB;;;;;OAKG;IACH,WAAW,IAAI,IAAI;IAInB,KAAK;IAcL,SAAS,CAAC,GAAG,EAAE,GAAG;IAIlB;;;;;;OAMG;IACH,UAAU,IAAI,IAAI;IAMlB,wEAAwE;IACxE,YAAY,IAAI,IAAI;CAsBrB"}
|
|
@@ -47,6 +47,10 @@ var ArrayView = class {
|
|
|
47
47
|
#root;
|
|
48
48
|
onDestroy;
|
|
49
49
|
#dirty = false;
|
|
50
|
+
#committedRoot;
|
|
51
|
+
#held = false;
|
|
52
|
+
#heldResultType = "unknown";
|
|
53
|
+
#heldError;
|
|
50
54
|
#resultType = "unknown";
|
|
51
55
|
#error;
|
|
52
56
|
#updateTTL;
|
|
@@ -65,7 +69,7 @@ var ArrayView = class {
|
|
|
65
69
|
this.#hydrate();
|
|
66
70
|
}
|
|
67
71
|
get data() {
|
|
68
|
-
return this.#root[""];
|
|
72
|
+
return (this.#committedRoot ?? this.#root)[""];
|
|
69
73
|
}
|
|
70
74
|
#getSchema() {
|
|
71
75
|
return this.#schema ??= this.#input.getSchema();
|
|
@@ -82,6 +86,10 @@ var ArrayView = class {
|
|
|
82
86
|
for (const listener of this.#listeners) this.#fireListener(listener);
|
|
83
87
|
}
|
|
84
88
|
#fireListener(listener) {
|
|
89
|
+
if (this.#committedRoot !== void 0) {
|
|
90
|
+
listener(this.data, this.#heldResultType, this.#heldError);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
85
93
|
listener(this.data, this.#resultType, this.#error);
|
|
86
94
|
}
|
|
87
95
|
destroy() {
|
|
@@ -100,7 +108,35 @@ var ArrayView = class {
|
|
|
100
108
|
this.#root = applyChange(this.#root, changeToViewChange(change), this.#getSchema(), "", this.#format, false, this.#txnDirty);
|
|
101
109
|
return emptyArray;
|
|
102
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* Keep exposing the current snapshot, whatever is pushed in the meantime,
|
|
113
|
+
* until {@link releaseData} or the next {@link flush}. Result type
|
|
114
|
+
* notifications are held back until that flush.
|
|
115
|
+
*
|
|
116
|
+
* A transaction is normally one synchronous task, so nobody can look at a
|
|
117
|
+
* view between its pushes and its flush. Deferred pipelines are different:
|
|
118
|
+
* they are hydrated over several tasks and flushed together (see
|
|
119
|
+
* `QueryDelegate.onPipelinesReady`), and a view that already has its rows
|
|
120
|
+
* must not show them early.
|
|
121
|
+
*/
|
|
122
|
+
holdData() {
|
|
123
|
+
this.#committedRoot = this.#root;
|
|
124
|
+
this.#held = true;
|
|
125
|
+
this.#heldResultType = this.#resultType;
|
|
126
|
+
this.#heldError = this.#error;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Make `data` current again without notifying anyone yet; listeners are
|
|
130
|
+
* still only called by the next {@link flush}. Views released together get
|
|
131
|
+
* this called on all of them before any is flushed, so a listener of one
|
|
132
|
+
* that reads another sees its rows too.
|
|
133
|
+
*/
|
|
134
|
+
releaseData() {
|
|
135
|
+
this.#committedRoot = void 0;
|
|
136
|
+
}
|
|
103
137
|
flush() {
|
|
138
|
+
this.#committedRoot = void 0;
|
|
139
|
+
this.#held = false;
|
|
104
140
|
if (!this.#dirty) return;
|
|
105
141
|
this.#dirty = false;
|
|
106
142
|
this.#fireListeners();
|
|
@@ -126,7 +162,8 @@ var ArrayView = class {
|
|
|
126
162
|
#setResultType(resultType, error) {
|
|
127
163
|
this.#resultType = resultType;
|
|
128
164
|
this.#error = error;
|
|
129
|
-
if (
|
|
165
|
+
if (this.#held) this.#dirty = true;
|
|
166
|
+
else if (!this.#dirty) this.#fireListeners();
|
|
130
167
|
}
|
|
131
168
|
};
|
|
132
169
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array-view.js","names":["#input","#listeners","#format","#updateTTL","#root","#resultType","#error","#setResultType","#hydrate","#schema","#fireListener","#dirty","#getSchema","#txnDirty","#fireListeners"],"sources":["../../../../../zql/src/ivm/array-view.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport type {Immutable} from '../../../shared/src/immutable.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.ts';\nimport type {ErroredQuery} from '../../../zero-protocol/src/custom-queries.ts';\nimport type {TTL} from '../query/ttl.ts';\nimport type {Listener, ResultType, TypedView} from '../query/typed-view.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport type {Change} from './change.ts';\nimport {skipYields, type Input, type Output} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {applyChange, type ViewChange} from './view-apply-change.ts';\nimport type {Entry, Format, View} from './view.ts';\n\nfunction changeToViewChange(change: Change): ViewChange {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n return {type: 'add', node: change[ChangeIndex.NODE]};\n case ChangeType.REMOVE:\n return {type: 'remove', node: change[ChangeIndex.NODE]};\n case ChangeType.CHILD:\n return {\n type: 'child',\n node: change[ChangeIndex.NODE],\n child: {\n relationshipName: change[ChangeIndex.CHILD_DATA].relationshipName,\n change: changeToViewChange(change[ChangeIndex.CHILD_DATA].change),\n },\n };\n case ChangeType.EDIT:\n return {\n type: 'edit',\n node: change[ChangeIndex.NODE],\n oldNode: change[ChangeIndex.OLD_NODE],\n };\n }\n}\n\n/**\n * Implements a materialized view of the output of an operator.\n *\n * It might seem more efficient to use an immutable b-tree for the\n * materialization, but it's not so clear. Inserts in the middle are\n * asymptotically slower in an array, but can often be done with zero\n * allocations, where changes to the b-tree will often require several allocs.\n *\n * Also the plain array view is more convenient for consumers since you can dump\n * it into console to see what it is, rather than having to iterate it.\n */\nexport class ArrayView<V extends View> implements Output, TypedView<V> {\n readonly #input: Input;\n readonly #listeners = new Set<Listener<V>>();\n // Read lazily: a DeferredInput has no schema until its pipeline is attached,\n // and the schema is only needed once there is a change to apply.\n #schema: SourceSchema | undefined;\n readonly #format: Format;\n\n // Synthetic \"root\" entry that has a single \"\" relationship, so that we can\n // treat all changes, including the root change, generically.\n //\n // applyChange is immutable: it returns a new root, preserving the references\n // of unchanged subtrees so consumers (React.memo / Solid) can skip them. We\n // therefore reassign #root on every change rather than mutating in place.\n #root: Entry;\n\n onDestroy: (() => void) | undefined;\n\n #dirty = false;\n #resultType: ResultType = 'unknown';\n #error: ErroredQuery | undefined;\n readonly #updateTTL: (ttl: TTL) => void;\n\n // Objects/arrays created or cloned during the current (un-flushed)\n // transaction. applyChange may mutate these in place rather than copying\n // again, since they are not yet observed by any listener. Replaced at\n // flush(), after which the committed snapshot must be copied-on-write again.\n // A WeakSet (not a Set) so it never extends the lifetime of transient clones.\n #txnDirty: WeakSet<object> = new WeakSet();\n\n constructor(\n input: Input,\n format: Format,\n queryComplete: true | ErroredQuery | Promise<true>,\n updateTTL: (ttl: TTL) => void,\n ) {\n this.#input = input;\n this.#format = format;\n this.#updateTTL = updateTTL;\n this.#root = {'': format.singular ? undefined : []};\n input.setOutput(this);\n\n if (queryComplete === true) {\n this.#resultType = 'complete';\n } else if ('error' in queryComplete) {\n this.#resultType = 'error';\n this.#error = queryComplete;\n } else {\n void queryComplete\n .then(() => this.#setResultType('complete'))\n .catch(e => this.#setResultType('error', e));\n }\n this.#hydrate();\n }\n\n get data() {\n return this.#root[''] as V;\n }\n\n #getSchema(): SourceSchema {\n return (this.#schema ??= this.#input.getSchema());\n }\n\n addListener(listener: Listener<V>) {\n assert(!this.#listeners.has(listener), 'Listener already registered');\n this.#listeners.add(listener);\n\n this.#fireListener(listener);\n\n return () => {\n this.#listeners.delete(listener);\n };\n }\n\n #fireListeners() {\n for (const listener of this.#listeners) {\n this.#fireListener(listener);\n }\n }\n\n #fireListener(listener: Listener<V>) {\n listener(this.data as Immutable<V>, this.#resultType, this.#error);\n }\n\n destroy() {\n this.onDestroy?.();\n }\n\n #hydrate() {\n this.#dirty = true;\n for (const node of skipYields(this.#input.fetch({}))) {\n this.#root = applyChange(\n this.#root,\n {type: 'add', node},\n this.#getSchema(),\n '',\n this.#format,\n false /* withIDs */,\n true /* mutate: #root is freshly created and not yet observed by any\n consumer, so build it in place to avoid O(N^2) array copies.\n Every later push() is immutable, preserving reference\n stability for unchanged subtrees. */,\n );\n }\n this.flush();\n }\n\n push(change: Change) {\n this.#dirty = true;\n this.#root = applyChange(\n this.#root,\n changeToViewChange(change),\n this.#getSchema(),\n '',\n this.#format,\n false /* withIDs */,\n this.#txnDirty /* mutate: copy-on-write within this transaction */,\n );\n return emptyArray;\n }\n\n flush() {\n if (!this.#dirty) {\n return;\n }\n this.#dirty = false;\n this.#fireListeners();\n // The snapshot just handed to listeners is now observed; the next\n // transaction must copy-on-write rather than mutate these objects. A fresh\n // WeakSet drops all \"owned\" marks (WeakSet has no clear()).\n this.#txnDirty = new WeakSet();\n }\n\n updateTTL(ttl: TTL) {\n this.#updateTTL(ttl);\n }\n\n /**\n * The store holds the server-confirmed complete result of this query from a\n * previous sync (the persisted got-queries key exists, pre-authoritative).\n * Never downgrades 'complete'/'error', and never resolves anything that\n * waits on 'complete' — freshness stays a promise only the connection can\n * keep.\n */\n markCached(): void {\n if (this.#resultType === 'unknown') {\n this.#setResultType('cached');\n }\n }\n\n /** The got key was deleted (eviction) while still pre-authoritative. */\n unmarkCached(): void {\n if (this.#resultType === 'cached') {\n this.#setResultType('unknown');\n }\n }\n\n #setResultType(resultType: ResultType, error?: ErroredQuery) {\n this.#resultType = resultType;\n this.#error = error;\n // A dirty view is mid-transaction: rows were pushed and not yet flushed,\n // and the objects in #txnDirty are still mutable. Firing now would hand\n // those to listeners and then fire again at flush() with the same data, so\n // let the pending flush deliver the new result type instead.\n if (!this.#dirty) {\n this.#fireListeners();\n }\n }\n}\n"],"mappings":";;;;;AAcA,SAAS,mBAAmB,QAA4B;CACtD,QAAQ,OAAO,IAAf;EACE,KAAK,GACH,OAAO;GAAC,MAAM;GAAO,MAAM,OAAO;EAAiB;EACrD,KAAK,GACH,OAAO;GAAC,MAAM;GAAU,MAAM,OAAO;EAAiB;EACxD,KAAK,GACH,OAAO;GACL,MAAM;GACN,MAAM,OAAO;GACb,OAAO;IACL,kBAAkB,OAAO,GAAwB;IACjD,QAAQ,mBAAmB,OAAO,GAAwB,MAAM;GAClE;EACF;EACF,KAAK,GACH,OAAO;GACL,MAAM;GACN,MAAM,OAAO;GACb,SAAS,OAAO;EAClB;CACJ;AACF;;;;;;;;;;;;AAaA,IAAa,YAAb,MAAuE;CACrE;CACA,6BAAsB,IAAI,IAAiB;CAG3C;CACA;CAQA;CAEA;CAEA,SAAS;CACT,cAA0B;CAC1B;CACA;CAOA,4BAA6B,IAAI,QAAQ;CAEzC,YACE,OACA,QACA,eACA,WACA;EACA,KAAKA,SAAS;EACd,KAAKE,UAAU;EACf,KAAKC,aAAa;EAClB,KAAKC,QAAQ,EAAC,IAAI,OAAO,WAAW,KAAA,IAAY,CAAC,EAAC;EAClD,MAAM,UAAU,IAAI;EAEpB,IAAI,kBAAkB,MACpB,KAAKC,cAAc;OACd,IAAI,WAAW,eAAe;GACnC,KAAKA,cAAc;GACnB,KAAKC,SAAS;EAChB,OACE,cACG,WAAW,KAAKC,eAAe,UAAU,CAAC,EAC1C,OAAM,MAAK,KAAKA,eAAe,SAAS,CAAC,CAAC;EAE/C,KAAKC,SAAS;CAChB;CAEA,IAA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AwB,OAAsB;EAC3D,KAAKF,cAAc;EACnB,KAAKC,SAAS;EAKd,IAAI,CAAC,KAAKK,QACR,KAAKG,eAAe;CAExB;AACF"}
|
|
1
|
+
{"version":3,"file":"array-view.js","names":["#input","#listeners","#format","#updateTTL","#root","#resultType","#error","#setResultType","#hydrate","#committedRoot","#schema","#fireListener","#heldResultType","#heldError","#dirty","#getSchema","#txnDirty","#held","#fireListeners"],"sources":["../../../../../zql/src/ivm/array-view.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport type {Immutable} from '../../../shared/src/immutable.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.ts';\nimport type {ErroredQuery} from '../../../zero-protocol/src/custom-queries.ts';\nimport type {TTL} from '../query/ttl.ts';\nimport type {Listener, ResultType, TypedView} from '../query/typed-view.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport type {Change} from './change.ts';\nimport {skipYields, type Input, type Output} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {applyChange, type ViewChange} from './view-apply-change.ts';\nimport type {Entry, Format, View} from './view.ts';\n\nfunction changeToViewChange(change: Change): ViewChange {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n return {type: 'add', node: change[ChangeIndex.NODE]};\n case ChangeType.REMOVE:\n return {type: 'remove', node: change[ChangeIndex.NODE]};\n case ChangeType.CHILD:\n return {\n type: 'child',\n node: change[ChangeIndex.NODE],\n child: {\n relationshipName: change[ChangeIndex.CHILD_DATA].relationshipName,\n change: changeToViewChange(change[ChangeIndex.CHILD_DATA].change),\n },\n };\n case ChangeType.EDIT:\n return {\n type: 'edit',\n node: change[ChangeIndex.NODE],\n oldNode: change[ChangeIndex.OLD_NODE],\n };\n }\n}\n\n/**\n * Implements a materialized view of the output of an operator.\n *\n * It might seem more efficient to use an immutable b-tree for the\n * materialization, but it's not so clear. Inserts in the middle are\n * asymptotically slower in an array, but can often be done with zero\n * allocations, where changes to the b-tree will often require several allocs.\n *\n * Also the plain array view is more convenient for consumers since you can dump\n * it into console to see what it is, rather than having to iterate it.\n */\nexport class ArrayView<V extends View> implements Output, TypedView<V> {\n readonly #input: Input;\n readonly #listeners = new Set<Listener<V>>();\n // Read lazily: a DeferredInput has no schema until its pipeline is attached,\n // and the schema is only needed once there is a change to apply.\n #schema: SourceSchema | undefined;\n readonly #format: Format;\n\n // Synthetic \"root\" entry that has a single \"\" relationship, so that we can\n // treat all changes, including the root change, generically.\n //\n // applyChange is immutable: it returns a new root, preserving the references\n // of unchanged subtrees so consumers (React.memo / Solid) can skip them. We\n // therefore reassign #root on every change rather than mutating in place.\n #root: Entry;\n\n onDestroy: (() => void) | undefined;\n\n #dirty = false;\n // Set by holdData(): the root that `data` and newly added listeners keep\n // seeing until releaseData() or the next flush, while pushes build up #root\n // behind it.\n #committedRoot: Entry | undefined;\n // From holdData() until the next flush. Outlasts #committedRoot, which\n // releaseData() drops first.\n #held = false;\n // The result type and error as of holdData(): what a listener added while\n // held is called with, so it never sees old rows paired with a new type.\n #heldResultType: ResultType = 'unknown';\n #heldError: ErroredQuery | undefined;\n #resultType: ResultType = 'unknown';\n #error: ErroredQuery | undefined;\n readonly #updateTTL: (ttl: TTL) => void;\n\n // Objects/arrays created or cloned during the current (un-flushed)\n // transaction. applyChange may mutate these in place rather than copying\n // again, since they are not yet observed by any listener. Replaced at\n // flush(), after which the committed snapshot must be copied-on-write again.\n // A WeakSet (not a Set) so it never extends the lifetime of transient clones.\n #txnDirty: WeakSet<object> = new WeakSet();\n\n constructor(\n input: Input,\n format: Format,\n queryComplete: true | ErroredQuery | Promise<true>,\n updateTTL: (ttl: TTL) => void,\n ) {\n this.#input = input;\n this.#format = format;\n this.#updateTTL = updateTTL;\n this.#root = {'': format.singular ? undefined : []};\n input.setOutput(this);\n\n if (queryComplete === true) {\n this.#resultType = 'complete';\n } else if ('error' in queryComplete) {\n this.#resultType = 'error';\n this.#error = queryComplete;\n } else {\n void queryComplete\n .then(() => this.#setResultType('complete'))\n .catch(e => this.#setResultType('error', e));\n }\n this.#hydrate();\n }\n\n get data() {\n return (this.#committedRoot ?? this.#root)[''] as V;\n }\n\n #getSchema(): SourceSchema {\n return (this.#schema ??= this.#input.getSchema());\n }\n\n addListener(listener: Listener<V>) {\n assert(!this.#listeners.has(listener), 'Listener already registered');\n this.#listeners.add(listener);\n\n this.#fireListener(listener);\n\n return () => {\n this.#listeners.delete(listener);\n };\n }\n\n #fireListeners() {\n for (const listener of this.#listeners) {\n this.#fireListener(listener);\n }\n }\n\n #fireListener(listener: Listener<V>) {\n if (this.#committedRoot !== undefined) {\n // Held rows go with the held type; after releaseData() both are current.\n listener(\n this.data as Immutable<V>,\n this.#heldResultType,\n this.#heldError,\n );\n return;\n }\n listener(this.data as Immutable<V>, this.#resultType, this.#error);\n }\n\n destroy() {\n this.onDestroy?.();\n }\n\n #hydrate() {\n this.#dirty = true;\n for (const node of skipYields(this.#input.fetch({}))) {\n this.#root = applyChange(\n this.#root,\n {type: 'add', node},\n this.#getSchema(),\n '',\n this.#format,\n false /* withIDs */,\n true /* mutate: #root is freshly created and not yet observed by any\n consumer, so build it in place to avoid O(N^2) array copies.\n Every later push() is immutable, preserving reference\n stability for unchanged subtrees. */,\n );\n }\n this.flush();\n }\n\n push(change: Change) {\n this.#dirty = true;\n this.#root = applyChange(\n this.#root,\n changeToViewChange(change),\n this.#getSchema(),\n '',\n this.#format,\n false /* withIDs */,\n this.#txnDirty /* mutate: copy-on-write within this transaction */,\n );\n return emptyArray;\n }\n\n /**\n * Keep exposing the current snapshot, whatever is pushed in the meantime,\n * until {@link releaseData} or the next {@link flush}. Result type\n * notifications are held back until that flush.\n *\n * A transaction is normally one synchronous task, so nobody can look at a\n * view between its pushes and its flush. Deferred pipelines are different:\n * they are hydrated over several tasks and flushed together (see\n * `QueryDelegate.onPipelinesReady`), and a view that already has its rows\n * must not show them early.\n */\n holdData(): void {\n this.#committedRoot = this.#root;\n this.#held = true;\n this.#heldResultType = this.#resultType;\n this.#heldError = this.#error;\n }\n\n /**\n * Make `data` current again without notifying anyone yet; listeners are\n * still only called by the next {@link flush}. Views released together get\n * this called on all of them before any is flushed, so a listener of one\n * that reads another sees its rows too.\n */\n releaseData(): void {\n this.#committedRoot = undefined;\n }\n\n flush() {\n this.#committedRoot = undefined;\n this.#held = false;\n if (!this.#dirty) {\n return;\n }\n this.#dirty = false;\n this.#fireListeners();\n // The snapshot just handed to listeners is now observed; the next\n // transaction must copy-on-write rather than mutate these objects. A fresh\n // WeakSet drops all \"owned\" marks (WeakSet has no clear()).\n this.#txnDirty = new WeakSet();\n }\n\n updateTTL(ttl: TTL) {\n this.#updateTTL(ttl);\n }\n\n /**\n * The store holds the server-confirmed complete result of this query from a\n * previous sync (the persisted got-queries key exists, pre-authoritative).\n * Never downgrades 'complete'/'error', and never resolves anything that\n * waits on 'complete' — freshness stays a promise only the connection can\n * keep.\n */\n markCached(): void {\n if (this.#resultType === 'unknown') {\n this.#setResultType('cached');\n }\n }\n\n /** The got key was deleted (eviction) while still pre-authoritative. */\n unmarkCached(): void {\n if (this.#resultType === 'cached') {\n this.#setResultType('unknown');\n }\n }\n\n #setResultType(resultType: ResultType, error?: ErroredQuery) {\n this.#resultType = resultType;\n this.#error = error;\n // A dirty view is mid-transaction: rows were pushed and not yet flushed,\n // and the objects in #txnDirty are still mutable. Firing now would hand\n // those to listeners and then fire again at flush() with the same data, so\n // let the pending flush deliver the new result type instead.\n // A held view is waiting to be exposed together with others at the next\n // flush; that goes for its result type as much as for its rows.\n if (this.#held) {\n // Dirty so that the flush notifies even when no rows were pushed.\n this.#dirty = true;\n } else if (!this.#dirty) {\n this.#fireListeners();\n }\n }\n}\n"],"mappings":";;;;;AAcA,SAAS,mBAAmB,QAA4B;CACtD,QAAQ,OAAO,IAAf;EACE,KAAK,GACH,OAAO;GAAC,MAAM;GAAO,MAAM,OAAO;EAAiB;EACrD,KAAK,GACH,OAAO;GAAC,MAAM;GAAU,MAAM,OAAO;EAAiB;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,SAAS;EAOd,IAAI,KAAKW,OAEP,KAAKH,SAAS;OACT,IAAI,CAAC,KAAKA,QACf,KAAKI,eAAe;CAExB;AACF"}
|
|
@@ -9,7 +9,7 @@ import { type Constraint } from './constraint.ts';
|
|
|
9
9
|
import { type Comparator, type Node } from './data.ts';
|
|
10
10
|
import { type Input, type MultiConstraint, type Output, type Start } from './operator.ts';
|
|
11
11
|
import type { Source, SourceChange, SourceInput } from './source.ts';
|
|
12
|
-
import type
|
|
12
|
+
import { type Stream } from './stream.ts';
|
|
13
13
|
export type Overlay = {
|
|
14
14
|
epoch: number;
|
|
15
15
|
change: SourceChange;
|
|
@@ -50,6 +50,18 @@ export declare class MemorySource implements Source {
|
|
|
50
50
|
get data(): BTreeSet<Row>;
|
|
51
51
|
connect(sort: Ordering | undefined, filters?: Condition, splitEditKeys?: Set<string>): SourceInput;
|
|
52
52
|
getIndexKeys(): string[];
|
|
53
|
+
/**
|
|
54
|
+
* Pushes an add for each of `rows`.
|
|
55
|
+
*
|
|
56
|
+
* When the source holds no rows and nothing is connected to it (loading the
|
|
57
|
+
* replica at startup) there is no output to notify and no secondary index to
|
|
58
|
+
* keep in step, so the primary index is built bottom-up in O(N) (plus a sort
|
|
59
|
+
* when `rows` are not already in primary key order) rather than doing one
|
|
60
|
+
* tree insert, and one trip through the push machinery, per row.
|
|
61
|
+
*
|
|
62
|
+
* `rows` may be sorted in place. Rows must have distinct primary keys.
|
|
63
|
+
*/
|
|
64
|
+
pushAdds(rows: Row[]): void;
|
|
53
65
|
push(change: SourceChange): Stream<'yield'>;
|
|
54
66
|
genPush(change: SourceChange): Generator<"yield" | undefined, void, unknown>;
|
|
55
67
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory-source.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/memory-source.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,QAAQ,EAAC,MAAM,kCAAkC,CAAC;AAI1D,OAAO,KAAK,EACV,SAAS,E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AAW,EAAE,QAAQ,CAAC,GAAG,CAAC,EAC1B,QAAQ,EAAE,QAAQ,EAClB,UAAU,EAAE,UAAU;;;kBAmBvB;AAqFD,wBAAgB,SAAS,CAAC,MAAM,EAAE,YAAY,UAI7C;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAiB,kBAAkB,CACjC,OAAO,EAAE,SAAS,MAAM,CAAC,IAAI,GAAG,OAAO,CAAC,EAAE,EAC1C,OAAO,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,KAAK,MAAM,GACpC,MAAM,CAAC,IAAI,GAAG,OAAO,CAAC,CAuGxB"}
|
|
@@ -4,6 +4,7 @@ import { once, toSorted } from "../../../shared/src/iterables.js";
|
|
|
4
4
|
import { must } from "../../../shared/src/must.js";
|
|
5
5
|
import { skipYields } from "./skip-yields.js";
|
|
6
6
|
import { makeAddChange, makeEditChange, makeRemoveChange } from "./change.js";
|
|
7
|
+
import { consume } from "./stream.js";
|
|
7
8
|
import { assertOrderingIncludesPK } from "../query/complete-ordering.js";
|
|
8
9
|
import { compareValues, makeComparator, valuesEqual } from "./data.js";
|
|
9
10
|
import { filterPush } from "./filter-push.js";
|
|
@@ -201,6 +202,27 @@ var MemorySource = class MemorySource {
|
|
|
201
202
|
if (matchesAll) yield node;
|
|
202
203
|
}
|
|
203
204
|
}
|
|
205
|
+
/**
|
|
206
|
+
* Pushes an add for each of `rows`.
|
|
207
|
+
*
|
|
208
|
+
* When the source holds no rows and nothing is connected to it (loading the
|
|
209
|
+
* replica at startup) there is no output to notify and no secondary index to
|
|
210
|
+
* keep in step, so the primary index is built bottom-up in O(N) (plus a sort
|
|
211
|
+
* when `rows` are not already in primary key order) rather than doing one
|
|
212
|
+
* tree insert, and one trip through the push machinery, per row.
|
|
213
|
+
*
|
|
214
|
+
* `rows` may be sorted in place. Rows must have distinct primary keys.
|
|
215
|
+
*/
|
|
216
|
+
pushAdds(rows) {
|
|
217
|
+
const index = this.#getPrimaryIndex();
|
|
218
|
+
if (!(this.#connections.length === 0 && this.#indexes.size === 1 && index.data.size === 0)) {
|
|
219
|
+
for (const row of rows) consume(this.push(makeSourceChangeAdd(row)));
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const { comparator } = index;
|
|
223
|
+
if (!isAscending(rows, comparator)) rows.sort(comparator);
|
|
224
|
+
index.data = BTreeSet.fromSorted(comparator, rows);
|
|
225
|
+
}
|
|
204
226
|
*push(change) {
|
|
205
227
|
for (const result of this.genPush(change)) if (result === "yield") yield result;
|
|
206
228
|
}
|
|
@@ -497,6 +519,10 @@ function rowMatchesPK(a, b, primaryKey) {
|
|
|
497
519
|
}
|
|
498
520
|
var minValue = Symbol("min-value");
|
|
499
521
|
var maxValue = Symbol("max-value");
|
|
522
|
+
function isAscending(rows, comparator) {
|
|
523
|
+
for (let i = 1; i < rows.length; i++) if (comparator(rows[i - 1], rows[i]) > 0) return false;
|
|
524
|
+
return true;
|
|
525
|
+
}
|
|
500
526
|
function makeBoundComparator(sort) {
|
|
501
527
|
const len = sort.length;
|
|
502
528
|
const k0 = sort[0][0];
|