@hot-updater/plugin-core 1.0.0-rc.2 → 1.0.0-rc.21
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/README.md +33 -0
- package/dist/baseCandidateKey.cjs +50 -0
- package/dist/baseCandidateKey.d.cts +27 -0
- package/dist/baseCandidateKey.d.mts +27 -0
- package/dist/baseCandidateKey.mjs +49 -0
- package/dist/bundleStorageLayout.cjs +7 -0
- package/dist/bundleStorageLayout.d.cts +5 -1
- package/dist/bundleStorageLayout.d.mts +5 -1
- package/dist/bundleStorageLayout.mjs +7 -1
- package/dist/contentType.cjs +9 -0
- package/dist/contentType.d.cts +4 -0
- package/dist/contentType.d.mts +4 -0
- package/dist/contentType.mjs +7 -0
- package/dist/coreApi.d.cts +131 -0
- package/dist/coreApi.d.mts +131 -0
- package/dist/createStorageAdapter.cjs +8 -0
- package/dist/createStorageAdapter.d.cts +9 -0
- package/dist/createStorageAdapter.d.mts +9 -0
- package/dist/createStorageAdapter.mjs +7 -0
- package/dist/database/adapter.cjs +61 -0
- package/dist/database/adapter.d.cts +200 -0
- package/dist/database/adapter.d.mts +200 -0
- package/dist/database/adapter.mjs +55 -0
- package/dist/database/distinct.cjs +88 -0
- package/dist/database/distinct.d.cts +18 -0
- package/dist/database/distinct.d.mts +18 -0
- package/dist/database/distinct.mjs +85 -0
- package/dist/database/memoryAdapter.cjs +107 -0
- package/dist/database/memoryAdapter.d.cts +14 -0
- package/dist/database/memoryAdapter.d.mts +14 -0
- package/dist/database/memoryAdapter.mjs +107 -0
- package/dist/database/values.cjs +114 -0
- package/dist/database/values.d.cts +17 -0
- package/dist/database/values.d.mts +17 -0
- package/dist/database/values.mjs +104 -0
- package/dist/database/verifyAdapter.cjs +137 -0
- package/dist/database/verifyAdapter.d.cts +37 -0
- package/dist/database/verifyAdapter.d.mts +37 -0
- package/dist/database/verifyAdapter.mjs +136 -0
- package/dist/databaseErrors.cjs +30 -0
- package/dist/databaseErrors.d.cts +21 -0
- package/dist/databaseErrors.d.mts +21 -0
- package/dist/databaseErrors.mjs +28 -0
- package/dist/databaseJsonValue.cjs +6 -0
- package/dist/databaseJsonValue.d.cts +9 -2
- package/dist/databaseJsonValue.d.mts +9 -2
- package/dist/databaseJsonValue.mjs +6 -0
- package/dist/databaseMetadata.cjs +4 -4
- package/dist/databaseMetadata.mjs +3 -3
- package/dist/databaseRows.cjs +6 -12
- package/dist/databaseRows.d.cts +1 -1
- package/dist/databaseRows.d.mts +1 -1
- package/dist/databaseRows.mjs +1 -7
- package/dist/engine/core/schema.cjs +279 -0
- package/dist/engine/core/schema.d.cts +312 -0
- package/dist/engine/core/schema.d.mts +312 -0
- package/dist/engine/core/schema.mjs +277 -0
- package/dist/engine/createEngine.cjs +72 -0
- package/dist/engine/createEngine.d.cts +45 -0
- package/dist/engine/createEngine.d.mts +45 -0
- package/dist/engine/createEngine.mjs +72 -0
- package/dist/engine/database/aggregateBatching.cjs +627 -0
- package/dist/engine/database/aggregateBatching.mjs +626 -0
- package/dist/engine/database/cursor.cjs +42 -0
- package/dist/engine/database/cursor.mjs +38 -0
- package/dist/engine/database/database.cjs +68 -0
- package/dist/engine/database/database.mjs +68 -0
- package/dist/engine/database/engine.cjs +37 -0
- package/dist/engine/database/engine.mjs +37 -0
- package/dist/engine/database/engineAggregates.cjs +120 -0
- package/dist/engine/database/engineAggregates.mjs +117 -0
- package/dist/engine/database/engineReads.cjs +182 -0
- package/dist/engine/database/engineReads.d.cts +10 -0
- package/dist/engine/database/engineReads.d.mts +10 -0
- package/dist/engine/database/engineReads.mjs +181 -0
- package/dist/engine/database/engineTransaction.cjs +399 -0
- package/dist/engine/database/engineTransaction.d.cts +18 -0
- package/dist/engine/database/engineTransaction.d.mts +18 -0
- package/dist/engine/database/engineTransaction.mjs +398 -0
- package/dist/engine/database/fence.cjs +156 -0
- package/dist/engine/database/fence.d.cts +41 -0
- package/dist/engine/database/fence.d.mts +41 -0
- package/dist/engine/database/fence.mjs +149 -0
- package/dist/engine/database/kv/kvAdapter.cjs +293 -0
- package/dist/engine/database/kv/kvAdapter.d.cts +116 -0
- package/dist/engine/database/kv/kvAdapter.d.mts +116 -0
- package/dist/engine/database/kv/kvAdapter.mjs +292 -0
- package/dist/engine/database/readMeter.cjs +80 -0
- package/dist/engine/database/readMeter.d.cts +34 -0
- package/dist/engine/database/readMeter.d.mts +34 -0
- package/dist/engine/database/readMeter.mjs +79 -0
- package/dist/engine/database/resolveSchema.cjs +197 -0
- package/dist/engine/database/resolveSchema.d.cts +35 -0
- package/dist/engine/database/resolveSchema.d.mts +35 -0
- package/dist/engine/database/resolveSchema.mjs +195 -0
- package/dist/engine/database/retention.cjs +100 -0
- package/dist/engine/database/retention.mjs +98 -0
- package/dist/engine/database/sql/sqlAdapter.cjs +290 -0
- package/dist/engine/database/sql/sqlAdapter.d.cts +45 -0
- package/dist/engine/database/sql/sqlAdapter.d.mts +45 -0
- package/dist/engine/database/sql/sqlAdapter.mjs +288 -0
- package/dist/engine/database/sql/sqlSchema.cjs +175 -0
- package/dist/engine/database/sql/sqlSchema.d.cts +45 -0
- package/dist/engine/database/sql/sqlSchema.d.mts +45 -0
- package/dist/engine/database/sql/sqlSchema.mjs +169 -0
- package/dist/engine/db/coreDatabase.cjs +94 -0
- package/dist/engine/db/coreDatabase.d.cts +78 -0
- package/dist/engine/db/coreDatabase.d.mts +78 -0
- package/dist/engine/db/coreDatabase.mjs +86 -0
- package/dist/engine/db/engineMigrator.cjs +53 -0
- package/dist/engine/db/engineMigrator.mjs +53 -0
- package/dist/engine/db/engineSql.cjs +26 -0
- package/dist/engine/db/engineSql.d.cts +16 -0
- package/dist/engine/db/engineSql.d.mts +16 -0
- package/dist/engine/db/engineSql.mjs +25 -0
- package/dist/engine/db/engineSqlMigrator.cjs +67 -0
- package/dist/engine/db/engineSqlMigrator.d.cts +21 -0
- package/dist/engine/db/engineSqlMigrator.d.mts +21 -0
- package/dist/engine/db/engineSqlMigrator.mjs +67 -0
- package/dist/engine/db/fixedMigratorShared.cjs +12 -0
- package/dist/engine/db/fixedMigratorShared.mjs +11 -0
- package/dist/engine/db/schemaSettings.cjs +63 -0
- package/dist/engine/db/schemaSettings.d.cts +7 -0
- package/dist/engine/db/schemaSettings.d.mts +7 -0
- package/dist/engine/db/schemaSettings.mjs +62 -0
- package/dist/engine/db/settingsMigrator.cjs +94 -0
- package/dist/engine/db/settingsMigrator.d.cts +25 -0
- package/dist/engine/db/settingsMigrator.d.mts +25 -0
- package/dist/engine/db/settingsMigrator.mjs +91 -0
- package/dist/engine/db/types.d.cts +78 -0
- package/dist/engine/db/types.d.mts +78 -0
- package/dist/index.cjs +97 -49
- package/dist/index.d.cts +46 -22
- package/dist/index.d.mts +46 -22
- package/dist/index.mjs +32 -17
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/comparison-DenM3wCn.cjs +0 -5
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/comparison-DenM3wCn.mjs +1 -5
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/range-DvX-Y6iv.cjs +8 -0
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/range-DvX-Y6iv.mjs +9 -2
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/set-CC5YeoYX.cjs +4 -0
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/set-CC5YeoYX.mjs +4 -1
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/version-CQ98ZBpL.cjs +0 -5
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/version-CQ98ZBpL.mjs +2 -6
- package/dist/releaseCatalogCompiler.cjs +27 -26
- package/dist/releaseCatalogCompiler.d.cts +1 -2
- package/dist/releaseCatalogCompiler.d.mts +2 -3
- package/dist/releaseCatalogCompiler.mjs +5 -9
- package/dist/releaseCatalogMutation.cjs +0 -292
- package/dist/releaseCatalogMutation.d.cts +4 -45
- package/dist/releaseCatalogMutation.d.mts +4 -45
- package/dist/releaseCatalogMutation.mjs +1 -288
- package/dist/releaseManagement.cjs +0 -221
- package/dist/releaseManagement.d.cts +4 -28
- package/dist/releaseManagement.d.mts +4 -28
- package/dist/releaseManagement.mjs +1 -216
- package/dist/remoteBundleSigning.d.cts +2 -2
- package/dist/remoteBundleSigning.d.mts +2 -2
- package/dist/serverPlugin/busy.cjs +36 -0
- package/dist/serverPlugin/busy.d.cts +12 -0
- package/dist/serverPlugin/busy.d.mts +12 -0
- package/dist/serverPlugin/busy.mjs +36 -0
- package/dist/serverPlugin/configError.cjs +7 -0
- package/dist/serverPlugin/configError.d.cts +7 -0
- package/dist/serverPlugin/configError.d.mts +7 -0
- package/dist/serverPlugin/configError.mjs +7 -0
- package/dist/serverPlugin/coreReads.d.cts +48 -0
- package/dist/serverPlugin/coreReads.d.mts +48 -0
- package/dist/serverPlugin/databaseHandle.d.cts +105 -0
- package/dist/serverPlugin/databaseHandle.d.mts +105 -0
- package/dist/serverPlugin/definePlugin.cjs +5 -0
- package/dist/serverPlugin/definePlugin.d.cts +130 -0
- package/dist/serverPlugin/definePlugin.d.mts +130 -0
- package/dist/serverPlugin/definePlugin.mjs +5 -0
- package/dist/serverPlugin/definitions.d.cts +76 -0
- package/dist/serverPlugin/definitions.d.mts +76 -0
- package/dist/serverPlugin/errors.cjs +37 -0
- package/dist/serverPlugin/errors.d.cts +29 -0
- package/dist/serverPlugin/errors.d.mts +29 -0
- package/dist/serverPlugin/errors.mjs +32 -0
- package/dist/serverPlugin/schema.cjs +24 -0
- package/dist/serverPlugin/schema.d.cts +90 -0
- package/dist/serverPlugin/schema.d.mts +90 -0
- package/dist/serverPlugin/schema.mjs +23 -0
- package/dist/types/databaseConfig.cjs +5 -0
- package/dist/types/databaseConfig.d.cts +68 -0
- package/dist/types/databaseConfig.d.mts +68 -0
- package/dist/types/databaseConfig.mjs +5 -0
- package/dist/types/databaseOperations.d.cts +5 -180
- package/dist/types/databaseOperations.d.mts +5 -180
- package/dist/types/databaseRows.d.cts +5 -56
- package/dist/types/databaseRows.d.mts +5 -56
- package/dist/types/index.cjs +2 -0
- package/dist/types/index.d.cts +31 -97
- package/dist/types/index.d.mts +31 -97
- package/dist/types/index.mjs +2 -1
- package/dist/types/models.d.cts +14 -0
- package/dist/types/models.d.mts +14 -0
- package/dist/types/public.d.cts +4 -3
- package/dist/types/public.d.mts +4 -3
- package/dist/uuidv7.cjs +3 -3
- package/dist/uuidv7.d.cts +1 -1
- package/dist/uuidv7.d.mts +1 -1
- package/dist/uuidv7.mjs +1 -1
- package/package.json +10 -7
- package/dist/calculatePagination.cjs +0 -25
- package/dist/calculatePagination.d.cts +0 -12
- package/dist/calculatePagination.d.mts +0 -12
- package/dist/calculatePagination.mjs +0 -25
- package/dist/compressionFormat.cjs +0 -62
- package/dist/compressionFormat.d.cts +0 -33
- package/dist/compressionFormat.d.mts +0 -33
- package/dist/compressionFormat.mjs +0 -58
- package/dist/createDatabasePlugin.cjs +0 -920
- package/dist/createDatabasePlugin.d.cts +0 -28
- package/dist/createDatabasePlugin.d.mts +0 -28
- package/dist/createDatabasePlugin.mjs +0 -916
- package/dist/createStoragePlugin.cjs +0 -8
- package/dist/createStoragePlugin.d.cts +0 -9
- package/dist/createStoragePlugin.d.mts +0 -9
- package/dist/createStoragePlugin.mjs +0 -7
- package/dist/databaseClient.cjs +0 -117
- package/dist/databaseClient.d.cts +0 -32
- package/dist/databaseClient.d.mts +0 -32
- package/dist/databaseClient.mjs +0 -114
- package/dist/databaseClientReads.cjs +0 -115
- package/dist/databaseClientReads.mjs +0 -113
- package/dist/databaseClientUpdates.cjs +0 -40
- package/dist/databaseClientUpdates.d.cts +0 -11
- package/dist/databaseClientUpdates.d.mts +0 -11
- package/dist/databaseClientUpdates.mjs +0 -38
- package/dist/databasePluginCrud.cjs +0 -88
- package/dist/databasePluginCrud.d.cts +0 -4
- package/dist/databasePluginCrud.d.mts +0 -4
- package/dist/databasePluginCrud.mjs +0 -87
- package/dist/databasePluginCrudValidation.d.cts +0 -6
- package/dist/databasePluginCrudValidation.d.mts +0 -6
- package/dist/databasePluginCrudValidationErrors.cjs +0 -11
- package/dist/databasePluginCrudValidationErrors.d.cts +0 -9
- package/dist/databasePluginCrudValidationErrors.d.mts +0 -9
- package/dist/databasePluginCrudValidationErrors.mjs +0 -11
- package/dist/databasePluginCrudValidationFields.cjs +0 -321
- package/dist/databasePluginCrudValidationFields.d.cts +0 -1
- package/dist/databasePluginCrudValidationFields.d.mts +0 -1
- package/dist/databasePluginCrudValidationFields.mjs +0 -309
- package/dist/databasePluginCrudValidationMutations.cjs +0 -107
- package/dist/databasePluginCrudValidationMutations.d.cts +0 -1
- package/dist/databasePluginCrudValidationMutations.d.mts +0 -1
- package/dist/databasePluginCrudValidationMutations.mjs +0 -100
- package/dist/databasePluginCrudValidationQueries.cjs +0 -103
- package/dist/databasePluginCrudValidationQueries.d.cts +0 -2
- package/dist/databasePluginCrudValidationQueries.d.mts +0 -2
- package/dist/databasePluginCrudValidationQueries.mjs +0 -97
- package/dist/databasePluginCrudValidationRows.cjs +0 -55
- package/dist/databasePluginCrudValidationRows.d.cts +0 -1
- package/dist/databasePluginCrudValidationRows.d.mts +0 -1
- package/dist/databasePluginCrudValidationRows.mjs +0 -53
- package/dist/databasePluginTransaction.cjs +0 -7
- package/dist/databasePluginTransaction.mjs +0 -7
- package/dist/insightsContract.cjs +0 -157
- package/dist/insightsContract.d.cts +0 -11
- package/dist/insightsContract.d.mts +0 -11
- package/dist/insightsContract.mjs +0 -153
- package/dist/internal.cjs +0 -6
- package/dist/internal.d.cts +0 -10
- package/dist/internal.d.mts +0 -10
- package/dist/internal.mjs +0 -3
- package/dist/paginateBundles.cjs +0 -53
- package/dist/paginateBundles.d.cts +0 -13
- package/dist/paginateBundles.d.mts +0 -13
- package/dist/paginateBundles.mjs +0 -53
- package/dist/queryBundles.cjs +0 -34
- package/dist/queryBundles.d.cts +0 -9
- package/dist/queryBundles.d.mts +0 -9
- package/dist/queryBundles.mjs +0 -32
- package/dist/requestBundleCache.cjs +0 -22
- package/dist/requestBundleCache.d.cts +0 -8
- package/dist/requestBundleCache.d.mts +0 -8
- package/dist/requestBundleCache.mjs +0 -22
- package/dist/types/database.d.cts +0 -5
- package/dist/types/database.d.mts +0 -5
- package/dist/types/databaseFields.cjs +0 -105
- package/dist/types/databaseFields.d.cts +0 -13
- package/dist/types/databaseFields.d.mts +0 -13
- package/dist/types/databaseFields.mjs +0 -105
- package/dist/types/databasePlugin.d.cts +0 -298
- package/dist/types/databasePlugin.d.mts +0 -298
- package/dist/types/databaseQuery.d.cts +0 -46
- package/dist/types/databaseQuery.d.mts +0 -46
- package/dist/types/internal.d.cts +0 -8
- package/dist/types/internal.d.mts +0 -8
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
//#region src/database/adapter.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The database adapter contract. An adapter implements `get`, `query`, and
|
|
4
|
+
* `write` with its backend's native features and knows nothing about Hot
|
|
5
|
+
* Updater's domain or plugins. Adapter authors import it from
|
|
6
|
+
* `@hot-updater/plugin-core`.
|
|
7
|
+
*/
|
|
8
|
+
type DatabaseJson = null | boolean | number | string | readonly DatabaseJson[] | {
|
|
9
|
+
readonly [key: string]: DatabaseJson;
|
|
10
|
+
};
|
|
11
|
+
/** A stored value: a string, number, boolean, null, or JSON document. */
|
|
12
|
+
type DatabaseValue = DatabaseJson;
|
|
13
|
+
/** A value allowed in keys and indexes. */
|
|
14
|
+
type DatabaseKeyValue = string | number | boolean;
|
|
15
|
+
/** Primary-key values in the table's key-column order. */
|
|
16
|
+
type DatabaseKey = readonly DatabaseKeyValue[];
|
|
17
|
+
type StoredRow = {
|
|
18
|
+
readonly [column: string]: DatabaseValue;
|
|
19
|
+
};
|
|
20
|
+
/** `integer` holds safe integers; `number` holds any finite number. */
|
|
21
|
+
type PhysicalColumnType = "string" | "integer" | "number" | "boolean" | "json";
|
|
22
|
+
interface PhysicalColumn {
|
|
23
|
+
readonly name: string;
|
|
24
|
+
readonly type: PhysicalColumnType;
|
|
25
|
+
readonly nullable: boolean;
|
|
26
|
+
/** Strings only: the maximum length in UTF-16 code units; unbounded text when absent. */
|
|
27
|
+
readonly maxLength?: number;
|
|
28
|
+
/** Strings only: ASCII text, stored in single bytes where a backend limits index size (MySQL). */
|
|
29
|
+
readonly ascii?: true;
|
|
30
|
+
/** A whole number the backend stores when a writer omits the column; the engine's own columns default to 0. */
|
|
31
|
+
readonly default?: number;
|
|
32
|
+
/** A multi-valued field: an array of 1–16 values of `type`, indexable in `eq` only. */
|
|
33
|
+
readonly multi?: true;
|
|
34
|
+
}
|
|
35
|
+
interface PhysicalIndex {
|
|
36
|
+
readonly name: string;
|
|
37
|
+
readonly eq: readonly string[];
|
|
38
|
+
readonly sort: readonly string[];
|
|
39
|
+
/** The `eq` values identify at most one row. */
|
|
40
|
+
readonly unique?: true;
|
|
41
|
+
}
|
|
42
|
+
/** How long a table's rows live: `ms` past the epoch milliseconds in `column`. */
|
|
43
|
+
interface PhysicalRetention {
|
|
44
|
+
/** An integer column; a row whose value is null never expires. */
|
|
45
|
+
readonly column: string;
|
|
46
|
+
readonly ms: number;
|
|
47
|
+
}
|
|
48
|
+
interface PhysicalTable {
|
|
49
|
+
/** The logical name; each adapter applies its own table prefix. */
|
|
50
|
+
readonly name: string;
|
|
51
|
+
readonly columns: readonly PhysicalColumn[];
|
|
52
|
+
/** Primary-key columns, in order. */
|
|
53
|
+
readonly key: readonly string[];
|
|
54
|
+
readonly indexes: readonly PhysicalIndex[];
|
|
55
|
+
/**
|
|
56
|
+
* Rows expire at {@link expiresAt}. A backend with a native TTL stamps it
|
|
57
|
+
* on every item the row has, its index copies included, and deletes them
|
|
58
|
+
* itself; any other adapter implements `prune`, which the engine calls.
|
|
59
|
+
*/
|
|
60
|
+
readonly retention?: PhysicalRetention;
|
|
61
|
+
}
|
|
62
|
+
interface QueryBound {
|
|
63
|
+
/** A prefix of the index's order columns (see `indexOrderColumns`). */
|
|
64
|
+
readonly values: readonly DatabaseKeyValue[];
|
|
65
|
+
readonly inclusive: boolean;
|
|
66
|
+
}
|
|
67
|
+
interface QueryRequest {
|
|
68
|
+
readonly index: string;
|
|
69
|
+
/** Values for the index's `eq` columns, in order. */
|
|
70
|
+
readonly eq: readonly DatabaseKeyValue[];
|
|
71
|
+
readonly lower?: QueryBound;
|
|
72
|
+
readonly upper?: QueryBound;
|
|
73
|
+
readonly order: "asc" | "desc";
|
|
74
|
+
/** 1–500 rows. */
|
|
75
|
+
readonly limit: number;
|
|
76
|
+
}
|
|
77
|
+
/** The row's `_v` at read time. */
|
|
78
|
+
interface WriteGuard {
|
|
79
|
+
readonly v: number;
|
|
80
|
+
}
|
|
81
|
+
/** At most one op per (table, key); guards see the pre-write state. */
|
|
82
|
+
type WriteOp = {
|
|
83
|
+
readonly type: "insert";
|
|
84
|
+
readonly table: PhysicalTable;
|
|
85
|
+
/** The complete row, `_v` included; the key must not exist. */
|
|
86
|
+
readonly row: StoredRow;
|
|
87
|
+
} | {
|
|
88
|
+
readonly type: "patch";
|
|
89
|
+
readonly table: PhysicalTable;
|
|
90
|
+
readonly key: DatabaseKey;
|
|
91
|
+
/** Named non-key columns only; the adapter sets `_v` to `guard.v + 1`. */
|
|
92
|
+
readonly set: StoredRow;
|
|
93
|
+
readonly guard: WriteGuard;
|
|
94
|
+
readonly previous: StoredRow;
|
|
95
|
+
} | {
|
|
96
|
+
readonly type: "delete";
|
|
97
|
+
readonly table: PhysicalTable;
|
|
98
|
+
readonly key: DatabaseKey;
|
|
99
|
+
readonly guard: WriteGuard;
|
|
100
|
+
readonly previous: StoredRow;
|
|
101
|
+
} | {
|
|
102
|
+
readonly type: "increment";
|
|
103
|
+
readonly table: PhysicalTable;
|
|
104
|
+
readonly key: DatabaseKey;
|
|
105
|
+
readonly by: Readonly<Record<string, number>>;
|
|
106
|
+
/** The row to create, before `by` applies, when the key does not exist. */
|
|
107
|
+
readonly init?: StoredRow;
|
|
108
|
+
readonly guard?: WriteGuard;
|
|
109
|
+
} | {
|
|
110
|
+
readonly type: "check";
|
|
111
|
+
readonly table: PhysicalTable;
|
|
112
|
+
readonly key: DatabaseKey;
|
|
113
|
+
readonly guard: WriteGuard;
|
|
114
|
+
};
|
|
115
|
+
type WriteResult = {
|
|
116
|
+
readonly ok: true;
|
|
117
|
+
} |
|
|
118
|
+
/** A guard failed, unique violations and missing rows included. */
|
|
119
|
+
{
|
|
120
|
+
readonly ok: false;
|
|
121
|
+
readonly failedOp: number;
|
|
122
|
+
} |
|
|
123
|
+
/** Transient: deadlock, serialization, SQLITE_BUSY, TransactionConflict, ABORTED. */
|
|
124
|
+
{
|
|
125
|
+
readonly ok: false;
|
|
126
|
+
readonly retry: true;
|
|
127
|
+
};
|
|
128
|
+
interface DatabaseAdapter {
|
|
129
|
+
readonly id: string;
|
|
130
|
+
/** True when the ops commit atomically in one write. */
|
|
131
|
+
fits(ops: readonly WriteOp[]): boolean;
|
|
132
|
+
/** Strongly consistent batch point read; results follow the order of keys. */
|
|
133
|
+
get(table: PhysicalTable, keys: readonly DatabaseKey[]): Promise<readonly (StoredRow | null)[]>;
|
|
134
|
+
/**
|
|
135
|
+
* Index range read; loops native pages until `limit` rows or the range ends, never a short page.
|
|
136
|
+
* A row read from a key-value index copy may lack `_v`; the engine reads it whole with `get` where it guards.
|
|
137
|
+
*/
|
|
138
|
+
query(table: PhysicalTable, request: QueryRequest): Promise<readonly StoredRow[]>;
|
|
139
|
+
write(ops: readonly WriteOp[]): Promise<WriteResult>;
|
|
140
|
+
/**
|
|
141
|
+
* Deletes rows, as read, with their index entries: best effort, without
|
|
142
|
+
* guards, and not atomically. It is for rows the caller has already
|
|
143
|
+
* recorded as consumed, so deleting one twice, or one already gone, is
|
|
144
|
+
* harmless. A backend that bills transactional deletes more implements it
|
|
145
|
+
* with its plain batch delete; without it, callers delete through `write`.
|
|
146
|
+
*/
|
|
147
|
+
deleteConsumed?(table: PhysicalTable, rows: readonly StoredRow[]): Promise<void>;
|
|
148
|
+
/**
|
|
149
|
+
* Deletes up to `limit` rows of a table with retention whose retention
|
|
150
|
+
* column is at or below `before`, oldest first (ties in any order), with
|
|
151
|
+
* their index entries, rechecking the column as it deletes, so a row a
|
|
152
|
+
* concurrent write moved past `before` stays. Resolves to the number
|
|
153
|
+
* deleted. A backend that expires rows natively leaves it out.
|
|
154
|
+
*/
|
|
155
|
+
prune?(table: PhysicalTable, before: number, limit: number): Promise<number>;
|
|
156
|
+
/** Called only by migration tooling, such as `hot-updater db migrate`. */
|
|
157
|
+
readonly migrations?: {
|
|
158
|
+
apply(tables: readonly PhysicalTable[]): Promise<void>;
|
|
159
|
+
};
|
|
160
|
+
dispose?(): Promise<void>;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The version column every stored row carries, which a guarded write
|
|
164
|
+
* compares and bumps. An adapter that is neither SQL nor key-value, such as
|
|
165
|
+
* the MongoDB adapter, stores and checks it itself.
|
|
166
|
+
*/
|
|
167
|
+
declare const DATABASE_VERSION_COLUMN = "_v";
|
|
168
|
+
declare class DatabaseSchemaError extends Error {
|
|
169
|
+
readonly name = "DatabaseSchemaError";
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* The column `name` of `table`, or a `DatabaseSchemaError`: how an adapter
|
|
173
|
+
* that is neither SQL nor key-value, such as the MongoDB adapter, reads a
|
|
174
|
+
* column's type.
|
|
175
|
+
*/
|
|
176
|
+
declare const findPhysicalColumn: (table: PhysicalTable, name: string) => PhysicalColumn;
|
|
177
|
+
/**
|
|
178
|
+
* The index `name` of `table`, or a `DatabaseSchemaError`: how an adapter
|
|
179
|
+
* that is neither SQL nor key-value, such as the MongoDB adapter, resolves a
|
|
180
|
+
* query's `index`.
|
|
181
|
+
*/
|
|
182
|
+
declare const findPhysicalIndex: (table: PhysicalTable, name: string) => PhysicalIndex;
|
|
183
|
+
/**
|
|
184
|
+
* The columns that order an index read and close its cursors: the sort
|
|
185
|
+
* columns, then the key columns that neither `eq` nor `sort` fixes.
|
|
186
|
+
*/
|
|
187
|
+
declare const indexOrderColumns: (table: PhysicalTable, index: PhysicalIndex) => readonly string[];
|
|
188
|
+
/**
|
|
189
|
+
* When a row of a table with retention expires, in epoch milliseconds, or
|
|
190
|
+
* undefined when the table keeps its rows or the row's time is null.
|
|
191
|
+
*/
|
|
192
|
+
declare const expiresAt: (table: PhysicalTable, row: StoredRow) => number | undefined;
|
|
193
|
+
/**
|
|
194
|
+
* The primary key of a row, in key-column order: what `get` takes for it.
|
|
195
|
+
* An adapter that is neither SQL nor key-value, such as the MongoDB adapter,
|
|
196
|
+
* addresses rows by it.
|
|
197
|
+
*/
|
|
198
|
+
declare const rowKey: (table: PhysicalTable, row: StoredRow) => DatabaseKey;
|
|
199
|
+
//#endregion
|
|
200
|
+
export { DATABASE_VERSION_COLUMN, DatabaseAdapter, DatabaseJson, DatabaseKey, DatabaseKeyValue, DatabaseSchemaError, DatabaseValue, PhysicalColumn, PhysicalColumnType, PhysicalIndex, PhysicalRetention, PhysicalTable, QueryBound, QueryRequest, StoredRow, WriteGuard, WriteOp, WriteResult, expiresAt, findPhysicalColumn, findPhysicalIndex, indexOrderColumns, rowKey };
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
//#region src/database/adapter.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The database adapter contract. An adapter implements `get`, `query`, and
|
|
4
|
+
* `write` with its backend's native features and knows nothing about Hot
|
|
5
|
+
* Updater's domain or plugins. Adapter authors import it from
|
|
6
|
+
* `@hot-updater/plugin-core`.
|
|
7
|
+
*/
|
|
8
|
+
type DatabaseJson = null | boolean | number | string | readonly DatabaseJson[] | {
|
|
9
|
+
readonly [key: string]: DatabaseJson;
|
|
10
|
+
};
|
|
11
|
+
/** A stored value: a string, number, boolean, null, or JSON document. */
|
|
12
|
+
type DatabaseValue = DatabaseJson;
|
|
13
|
+
/** A value allowed in keys and indexes. */
|
|
14
|
+
type DatabaseKeyValue = string | number | boolean;
|
|
15
|
+
/** Primary-key values in the table's key-column order. */
|
|
16
|
+
type DatabaseKey = readonly DatabaseKeyValue[];
|
|
17
|
+
type StoredRow = {
|
|
18
|
+
readonly [column: string]: DatabaseValue;
|
|
19
|
+
};
|
|
20
|
+
/** `integer` holds safe integers; `number` holds any finite number. */
|
|
21
|
+
type PhysicalColumnType = "string" | "integer" | "number" | "boolean" | "json";
|
|
22
|
+
interface PhysicalColumn {
|
|
23
|
+
readonly name: string;
|
|
24
|
+
readonly type: PhysicalColumnType;
|
|
25
|
+
readonly nullable: boolean;
|
|
26
|
+
/** Strings only: the maximum length in UTF-16 code units; unbounded text when absent. */
|
|
27
|
+
readonly maxLength?: number;
|
|
28
|
+
/** Strings only: ASCII text, stored in single bytes where a backend limits index size (MySQL). */
|
|
29
|
+
readonly ascii?: true;
|
|
30
|
+
/** A whole number the backend stores when a writer omits the column; the engine's own columns default to 0. */
|
|
31
|
+
readonly default?: number;
|
|
32
|
+
/** A multi-valued field: an array of 1–16 values of `type`, indexable in `eq` only. */
|
|
33
|
+
readonly multi?: true;
|
|
34
|
+
}
|
|
35
|
+
interface PhysicalIndex {
|
|
36
|
+
readonly name: string;
|
|
37
|
+
readonly eq: readonly string[];
|
|
38
|
+
readonly sort: readonly string[];
|
|
39
|
+
/** The `eq` values identify at most one row. */
|
|
40
|
+
readonly unique?: true;
|
|
41
|
+
}
|
|
42
|
+
/** How long a table's rows live: `ms` past the epoch milliseconds in `column`. */
|
|
43
|
+
interface PhysicalRetention {
|
|
44
|
+
/** An integer column; a row whose value is null never expires. */
|
|
45
|
+
readonly column: string;
|
|
46
|
+
readonly ms: number;
|
|
47
|
+
}
|
|
48
|
+
interface PhysicalTable {
|
|
49
|
+
/** The logical name; each adapter applies its own table prefix. */
|
|
50
|
+
readonly name: string;
|
|
51
|
+
readonly columns: readonly PhysicalColumn[];
|
|
52
|
+
/** Primary-key columns, in order. */
|
|
53
|
+
readonly key: readonly string[];
|
|
54
|
+
readonly indexes: readonly PhysicalIndex[];
|
|
55
|
+
/**
|
|
56
|
+
* Rows expire at {@link expiresAt}. A backend with a native TTL stamps it
|
|
57
|
+
* on every item the row has, its index copies included, and deletes them
|
|
58
|
+
* itself; any other adapter implements `prune`, which the engine calls.
|
|
59
|
+
*/
|
|
60
|
+
readonly retention?: PhysicalRetention;
|
|
61
|
+
}
|
|
62
|
+
interface QueryBound {
|
|
63
|
+
/** A prefix of the index's order columns (see `indexOrderColumns`). */
|
|
64
|
+
readonly values: readonly DatabaseKeyValue[];
|
|
65
|
+
readonly inclusive: boolean;
|
|
66
|
+
}
|
|
67
|
+
interface QueryRequest {
|
|
68
|
+
readonly index: string;
|
|
69
|
+
/** Values for the index's `eq` columns, in order. */
|
|
70
|
+
readonly eq: readonly DatabaseKeyValue[];
|
|
71
|
+
readonly lower?: QueryBound;
|
|
72
|
+
readonly upper?: QueryBound;
|
|
73
|
+
readonly order: "asc" | "desc";
|
|
74
|
+
/** 1–500 rows. */
|
|
75
|
+
readonly limit: number;
|
|
76
|
+
}
|
|
77
|
+
/** The row's `_v` at read time. */
|
|
78
|
+
interface WriteGuard {
|
|
79
|
+
readonly v: number;
|
|
80
|
+
}
|
|
81
|
+
/** At most one op per (table, key); guards see the pre-write state. */
|
|
82
|
+
type WriteOp = {
|
|
83
|
+
readonly type: "insert";
|
|
84
|
+
readonly table: PhysicalTable;
|
|
85
|
+
/** The complete row, `_v` included; the key must not exist. */
|
|
86
|
+
readonly row: StoredRow;
|
|
87
|
+
} | {
|
|
88
|
+
readonly type: "patch";
|
|
89
|
+
readonly table: PhysicalTable;
|
|
90
|
+
readonly key: DatabaseKey;
|
|
91
|
+
/** Named non-key columns only; the adapter sets `_v` to `guard.v + 1`. */
|
|
92
|
+
readonly set: StoredRow;
|
|
93
|
+
readonly guard: WriteGuard;
|
|
94
|
+
readonly previous: StoredRow;
|
|
95
|
+
} | {
|
|
96
|
+
readonly type: "delete";
|
|
97
|
+
readonly table: PhysicalTable;
|
|
98
|
+
readonly key: DatabaseKey;
|
|
99
|
+
readonly guard: WriteGuard;
|
|
100
|
+
readonly previous: StoredRow;
|
|
101
|
+
} | {
|
|
102
|
+
readonly type: "increment";
|
|
103
|
+
readonly table: PhysicalTable;
|
|
104
|
+
readonly key: DatabaseKey;
|
|
105
|
+
readonly by: Readonly<Record<string, number>>;
|
|
106
|
+
/** The row to create, before `by` applies, when the key does not exist. */
|
|
107
|
+
readonly init?: StoredRow;
|
|
108
|
+
readonly guard?: WriteGuard;
|
|
109
|
+
} | {
|
|
110
|
+
readonly type: "check";
|
|
111
|
+
readonly table: PhysicalTable;
|
|
112
|
+
readonly key: DatabaseKey;
|
|
113
|
+
readonly guard: WriteGuard;
|
|
114
|
+
};
|
|
115
|
+
type WriteResult = {
|
|
116
|
+
readonly ok: true;
|
|
117
|
+
} |
|
|
118
|
+
/** A guard failed, unique violations and missing rows included. */
|
|
119
|
+
{
|
|
120
|
+
readonly ok: false;
|
|
121
|
+
readonly failedOp: number;
|
|
122
|
+
} |
|
|
123
|
+
/** Transient: deadlock, serialization, SQLITE_BUSY, TransactionConflict, ABORTED. */
|
|
124
|
+
{
|
|
125
|
+
readonly ok: false;
|
|
126
|
+
readonly retry: true;
|
|
127
|
+
};
|
|
128
|
+
interface DatabaseAdapter {
|
|
129
|
+
readonly id: string;
|
|
130
|
+
/** True when the ops commit atomically in one write. */
|
|
131
|
+
fits(ops: readonly WriteOp[]): boolean;
|
|
132
|
+
/** Strongly consistent batch point read; results follow the order of keys. */
|
|
133
|
+
get(table: PhysicalTable, keys: readonly DatabaseKey[]): Promise<readonly (StoredRow | null)[]>;
|
|
134
|
+
/**
|
|
135
|
+
* Index range read; loops native pages until `limit` rows or the range ends, never a short page.
|
|
136
|
+
* A row read from a key-value index copy may lack `_v`; the engine reads it whole with `get` where it guards.
|
|
137
|
+
*/
|
|
138
|
+
query(table: PhysicalTable, request: QueryRequest): Promise<readonly StoredRow[]>;
|
|
139
|
+
write(ops: readonly WriteOp[]): Promise<WriteResult>;
|
|
140
|
+
/**
|
|
141
|
+
* Deletes rows, as read, with their index entries: best effort, without
|
|
142
|
+
* guards, and not atomically. It is for rows the caller has already
|
|
143
|
+
* recorded as consumed, so deleting one twice, or one already gone, is
|
|
144
|
+
* harmless. A backend that bills transactional deletes more implements it
|
|
145
|
+
* with its plain batch delete; without it, callers delete through `write`.
|
|
146
|
+
*/
|
|
147
|
+
deleteConsumed?(table: PhysicalTable, rows: readonly StoredRow[]): Promise<void>;
|
|
148
|
+
/**
|
|
149
|
+
* Deletes up to `limit` rows of a table with retention whose retention
|
|
150
|
+
* column is at or below `before`, oldest first (ties in any order), with
|
|
151
|
+
* their index entries, rechecking the column as it deletes, so a row a
|
|
152
|
+
* concurrent write moved past `before` stays. Resolves to the number
|
|
153
|
+
* deleted. A backend that expires rows natively leaves it out.
|
|
154
|
+
*/
|
|
155
|
+
prune?(table: PhysicalTable, before: number, limit: number): Promise<number>;
|
|
156
|
+
/** Called only by migration tooling, such as `hot-updater db migrate`. */
|
|
157
|
+
readonly migrations?: {
|
|
158
|
+
apply(tables: readonly PhysicalTable[]): Promise<void>;
|
|
159
|
+
};
|
|
160
|
+
dispose?(): Promise<void>;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The version column every stored row carries, which a guarded write
|
|
164
|
+
* compares and bumps. An adapter that is neither SQL nor key-value, such as
|
|
165
|
+
* the MongoDB adapter, stores and checks it itself.
|
|
166
|
+
*/
|
|
167
|
+
declare const DATABASE_VERSION_COLUMN = "_v";
|
|
168
|
+
declare class DatabaseSchemaError extends Error {
|
|
169
|
+
readonly name = "DatabaseSchemaError";
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* The column `name` of `table`, or a `DatabaseSchemaError`: how an adapter
|
|
173
|
+
* that is neither SQL nor key-value, such as the MongoDB adapter, reads a
|
|
174
|
+
* column's type.
|
|
175
|
+
*/
|
|
176
|
+
declare const findPhysicalColumn: (table: PhysicalTable, name: string) => PhysicalColumn;
|
|
177
|
+
/**
|
|
178
|
+
* The index `name` of `table`, or a `DatabaseSchemaError`: how an adapter
|
|
179
|
+
* that is neither SQL nor key-value, such as the MongoDB adapter, resolves a
|
|
180
|
+
* query's `index`.
|
|
181
|
+
*/
|
|
182
|
+
declare const findPhysicalIndex: (table: PhysicalTable, name: string) => PhysicalIndex;
|
|
183
|
+
/**
|
|
184
|
+
* The columns that order an index read and close its cursors: the sort
|
|
185
|
+
* columns, then the key columns that neither `eq` nor `sort` fixes.
|
|
186
|
+
*/
|
|
187
|
+
declare const indexOrderColumns: (table: PhysicalTable, index: PhysicalIndex) => readonly string[];
|
|
188
|
+
/**
|
|
189
|
+
* When a row of a table with retention expires, in epoch milliseconds, or
|
|
190
|
+
* undefined when the table keeps its rows or the row's time is null.
|
|
191
|
+
*/
|
|
192
|
+
declare const expiresAt: (table: PhysicalTable, row: StoredRow) => number | undefined;
|
|
193
|
+
/**
|
|
194
|
+
* The primary key of a row, in key-column order: what `get` takes for it.
|
|
195
|
+
* An adapter that is neither SQL nor key-value, such as the MongoDB adapter,
|
|
196
|
+
* addresses rows by it.
|
|
197
|
+
*/
|
|
198
|
+
declare const rowKey: (table: PhysicalTable, row: StoredRow) => DatabaseKey;
|
|
199
|
+
//#endregion
|
|
200
|
+
export { DATABASE_VERSION_COLUMN, DatabaseAdapter, DatabaseJson, DatabaseKey, DatabaseKeyValue, DatabaseSchemaError, DatabaseValue, PhysicalColumn, PhysicalColumnType, PhysicalIndex, PhysicalRetention, PhysicalTable, QueryBound, QueryRequest, StoredRow, WriteGuard, WriteOp, WriteResult, expiresAt, findPhysicalColumn, findPhysicalIndex, indexOrderColumns, rowKey };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//#region src/database/adapter.ts
|
|
2
|
+
/**
|
|
3
|
+
* The version column every stored row carries, which a guarded write
|
|
4
|
+
* compares and bumps. An adapter that is neither SQL nor key-value, such as
|
|
5
|
+
* the MongoDB adapter, stores and checks it itself.
|
|
6
|
+
*/
|
|
7
|
+
const DATABASE_VERSION_COLUMN = "_v";
|
|
8
|
+
var DatabaseSchemaError = class extends Error {
|
|
9
|
+
name = "DatabaseSchemaError";
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* The column `name` of `table`, or a `DatabaseSchemaError`: how an adapter
|
|
13
|
+
* that is neither SQL nor key-value, such as the MongoDB adapter, reads a
|
|
14
|
+
* column's type.
|
|
15
|
+
*/
|
|
16
|
+
const findPhysicalColumn = (table, name) => {
|
|
17
|
+
const column = table.columns.find((candidate) => candidate.name === name);
|
|
18
|
+
if (column === void 0) throw new DatabaseSchemaError(`${table.name} has no column ${name}.`);
|
|
19
|
+
return column;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* The index `name` of `table`, or a `DatabaseSchemaError`: how an adapter
|
|
23
|
+
* that is neither SQL nor key-value, such as the MongoDB adapter, resolves a
|
|
24
|
+
* query's `index`.
|
|
25
|
+
*/
|
|
26
|
+
const findPhysicalIndex = (table, name) => {
|
|
27
|
+
const index = table.indexes.find((candidate) => candidate.name === name);
|
|
28
|
+
if (index === void 0) throw new DatabaseSchemaError(`${table.name} has no index ${name}.`);
|
|
29
|
+
return index;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* The columns that order an index read and close its cursors: the sort
|
|
33
|
+
* columns, then the key columns that neither `eq` nor `sort` fixes.
|
|
34
|
+
*/
|
|
35
|
+
const indexOrderColumns = (table, index) => [...index.sort, ...table.key.filter((column) => !index.sort.includes(column) && !index.eq.includes(column))];
|
|
36
|
+
/**
|
|
37
|
+
* When a row of a table with retention expires, in epoch milliseconds, or
|
|
38
|
+
* undefined when the table keeps its rows or the row's time is null.
|
|
39
|
+
*/
|
|
40
|
+
const expiresAt = (table, row) => {
|
|
41
|
+
const time = table.retention && row[table.retention.column];
|
|
42
|
+
return typeof time === "number" ? time + table.retention.ms : void 0;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* The primary key of a row, in key-column order: what `get` takes for it.
|
|
46
|
+
* An adapter that is neither SQL nor key-value, such as the MongoDB adapter,
|
|
47
|
+
* addresses rows by it.
|
|
48
|
+
*/
|
|
49
|
+
const rowKey = (table, row) => table.key.map((column) => {
|
|
50
|
+
const value = row[column];
|
|
51
|
+
if (typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean") throw new DatabaseSchemaError(`${table.name}.${column} is a key column and must be a string, number, or boolean.`);
|
|
52
|
+
return value;
|
|
53
|
+
});
|
|
54
|
+
//#endregion
|
|
55
|
+
export { DATABASE_VERSION_COLUMN, DatabaseSchemaError, expiresAt, findPhysicalColumn, findPhysicalIndex, indexOrderColumns, rowKey };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
//#region src/database/distinct.ts
|
|
2
|
+
/**
|
|
3
|
+
* HyperLogLog sketches, the storage engine's `distinct` aggregate metric: a
|
|
4
|
+
* sketch is 1,024 registers stored as a 1,024-character string, so adding a
|
|
5
|
+
* value, merging sketches, and counting one never need the values back.
|
|
6
|
+
*/
|
|
7
|
+
const PRECISION = 10;
|
|
8
|
+
const REGISTER_COUNT = 1024;
|
|
9
|
+
const MAX_RANK = 55;
|
|
10
|
+
const hash = (value) => {
|
|
11
|
+
let result = 14695981039346656037n;
|
|
12
|
+
for (const byte of new TextEncoder().encode(value)) {
|
|
13
|
+
result ^= BigInt(byte);
|
|
14
|
+
result = BigInt.asUintN(64, result * 1099511628211n);
|
|
15
|
+
}
|
|
16
|
+
return result;
|
|
17
|
+
};
|
|
18
|
+
const OFFSET = 33;
|
|
19
|
+
const decode = (summary) => {
|
|
20
|
+
if (!summary) return new Uint8Array(REGISTER_COUNT);
|
|
21
|
+
if (summary.length !== REGISTER_COUNT) throw new Error("Invalid distinct sketch");
|
|
22
|
+
const registers = new Uint8Array(REGISTER_COUNT);
|
|
23
|
+
for (let index = 0; index < REGISTER_COUNT; index += 1) {
|
|
24
|
+
const value = summary.charCodeAt(index) - OFFSET;
|
|
25
|
+
if (!Number.isInteger(value) || value < 0 || value > MAX_RANK) throw new Error("Invalid distinct sketch");
|
|
26
|
+
registers[index] = value;
|
|
27
|
+
}
|
|
28
|
+
return registers;
|
|
29
|
+
};
|
|
30
|
+
const encode = (registers) => Array.from(registers, (value) => String.fromCharCode(value + OFFSET)).join("");
|
|
31
|
+
const distinctRegister = (identity) => {
|
|
32
|
+
const value = hash(identity);
|
|
33
|
+
const index = Number(value & BigInt(1023));
|
|
34
|
+
let remainder = value >> BigInt(PRECISION);
|
|
35
|
+
let rank = 1;
|
|
36
|
+
while ((remainder & 1n) === 0n && rank < MAX_RANK) {
|
|
37
|
+
rank += 1;
|
|
38
|
+
remainder >>= 1n;
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
index,
|
|
42
|
+
rank,
|
|
43
|
+
character: String.fromCharCode(rank + OFFSET)
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* The sketch with `identity` added; a missing sketch is the empty one. A
|
|
48
|
+
* plugin passes it as a `distinct` column's change to `tx.aggregate`.
|
|
49
|
+
*/
|
|
50
|
+
const addDistinct = (summary, identity) => {
|
|
51
|
+
const registers = decode(summary);
|
|
52
|
+
const { index, rank } = distinctRegister(identity);
|
|
53
|
+
registers[index] = Math.max(registers[index], rank);
|
|
54
|
+
return encode(registers);
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* The union of sketches, register by register: how a plugin combines a
|
|
58
|
+
* `distinct` column across the aggregate rows it reads.
|
|
59
|
+
*/
|
|
60
|
+
const mergeDistinct = (summaries) => {
|
|
61
|
+
const merged = new Uint8Array(REGISTER_COUNT);
|
|
62
|
+
for (const summary of summaries) {
|
|
63
|
+
const registers = decode(summary);
|
|
64
|
+
for (let index = 0; index < REGISTER_COUNT; index += 1) merged[index] = Math.max(merged[index], registers[index]);
|
|
65
|
+
}
|
|
66
|
+
return encode(merged);
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* The estimated number of distinct values a sketch holds: how a plugin
|
|
70
|
+
* reads a `distinct` column.
|
|
71
|
+
*/
|
|
72
|
+
const countDistinct = (summary) => {
|
|
73
|
+
const registers = decode(summary);
|
|
74
|
+
let inverseSum = 0;
|
|
75
|
+
let empty = 0;
|
|
76
|
+
for (const register of registers) {
|
|
77
|
+
inverseSum += 2 ** -register;
|
|
78
|
+
if (register === 0) empty += 1;
|
|
79
|
+
}
|
|
80
|
+
const raw = .7213 / 1.0010537109375 * REGISTER_COUNT ** 2 / inverseSum;
|
|
81
|
+
const estimate = raw <= 2.5 * REGISTER_COUNT && empty > 0 ? REGISTER_COUNT * Math.log(REGISTER_COUNT / empty) : raw;
|
|
82
|
+
return Math.max(0, Math.round(estimate));
|
|
83
|
+
};
|
|
84
|
+
//#endregion
|
|
85
|
+
exports.addDistinct = addDistinct;
|
|
86
|
+
exports.countDistinct = countDistinct;
|
|
87
|
+
exports.distinctRegister = distinctRegister;
|
|
88
|
+
exports.mergeDistinct = mergeDistinct;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/database/distinct.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The sketch with `identity` added; a missing sketch is the empty one. A
|
|
4
|
+
* plugin passes it as a `distinct` column's change to `tx.aggregate`.
|
|
5
|
+
*/
|
|
6
|
+
declare const addDistinct: (summary: string | null | undefined, identity: string) => string;
|
|
7
|
+
/**
|
|
8
|
+
* The union of sketches, register by register: how a plugin combines a
|
|
9
|
+
* `distinct` column across the aggregate rows it reads.
|
|
10
|
+
*/
|
|
11
|
+
declare const mergeDistinct: (summaries: readonly (string | null | undefined)[]) => string;
|
|
12
|
+
/**
|
|
13
|
+
* The estimated number of distinct values a sketch holds: how a plugin
|
|
14
|
+
* reads a `distinct` column.
|
|
15
|
+
*/
|
|
16
|
+
declare const countDistinct: (summary: string | null | undefined) => number;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { addDistinct, countDistinct, mergeDistinct };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/database/distinct.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The sketch with `identity` added; a missing sketch is the empty one. A
|
|
4
|
+
* plugin passes it as a `distinct` column's change to `tx.aggregate`.
|
|
5
|
+
*/
|
|
6
|
+
declare const addDistinct: (summary: string | null | undefined, identity: string) => string;
|
|
7
|
+
/**
|
|
8
|
+
* The union of sketches, register by register: how a plugin combines a
|
|
9
|
+
* `distinct` column across the aggregate rows it reads.
|
|
10
|
+
*/
|
|
11
|
+
declare const mergeDistinct: (summaries: readonly (string | null | undefined)[]) => string;
|
|
12
|
+
/**
|
|
13
|
+
* The estimated number of distinct values a sketch holds: how a plugin
|
|
14
|
+
* reads a `distinct` column.
|
|
15
|
+
*/
|
|
16
|
+
declare const countDistinct: (summary: string | null | undefined) => number;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { addDistinct, countDistinct, mergeDistinct };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
//#region src/database/distinct.ts
|
|
2
|
+
/**
|
|
3
|
+
* HyperLogLog sketches, the storage engine's `distinct` aggregate metric: a
|
|
4
|
+
* sketch is 1,024 registers stored as a 1,024-character string, so adding a
|
|
5
|
+
* value, merging sketches, and counting one never need the values back.
|
|
6
|
+
*/
|
|
7
|
+
const PRECISION = 10;
|
|
8
|
+
const REGISTER_COUNT = 1024;
|
|
9
|
+
const MAX_RANK = 55;
|
|
10
|
+
const hash = (value) => {
|
|
11
|
+
let result = 14695981039346656037n;
|
|
12
|
+
for (const byte of new TextEncoder().encode(value)) {
|
|
13
|
+
result ^= BigInt(byte);
|
|
14
|
+
result = BigInt.asUintN(64, result * 1099511628211n);
|
|
15
|
+
}
|
|
16
|
+
return result;
|
|
17
|
+
};
|
|
18
|
+
const OFFSET = 33;
|
|
19
|
+
const decode = (summary) => {
|
|
20
|
+
if (!summary) return new Uint8Array(REGISTER_COUNT);
|
|
21
|
+
if (summary.length !== REGISTER_COUNT) throw new Error("Invalid distinct sketch");
|
|
22
|
+
const registers = new Uint8Array(REGISTER_COUNT);
|
|
23
|
+
for (let index = 0; index < REGISTER_COUNT; index += 1) {
|
|
24
|
+
const value = summary.charCodeAt(index) - OFFSET;
|
|
25
|
+
if (!Number.isInteger(value) || value < 0 || value > MAX_RANK) throw new Error("Invalid distinct sketch");
|
|
26
|
+
registers[index] = value;
|
|
27
|
+
}
|
|
28
|
+
return registers;
|
|
29
|
+
};
|
|
30
|
+
const encode = (registers) => Array.from(registers, (value) => String.fromCharCode(value + OFFSET)).join("");
|
|
31
|
+
const distinctRegister = (identity) => {
|
|
32
|
+
const value = hash(identity);
|
|
33
|
+
const index = Number(value & BigInt(1023));
|
|
34
|
+
let remainder = value >> BigInt(PRECISION);
|
|
35
|
+
let rank = 1;
|
|
36
|
+
while ((remainder & 1n) === 0n && rank < MAX_RANK) {
|
|
37
|
+
rank += 1;
|
|
38
|
+
remainder >>= 1n;
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
index,
|
|
42
|
+
rank,
|
|
43
|
+
character: String.fromCharCode(rank + OFFSET)
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* The sketch with `identity` added; a missing sketch is the empty one. A
|
|
48
|
+
* plugin passes it as a `distinct` column's change to `tx.aggregate`.
|
|
49
|
+
*/
|
|
50
|
+
const addDistinct = (summary, identity) => {
|
|
51
|
+
const registers = decode(summary);
|
|
52
|
+
const { index, rank } = distinctRegister(identity);
|
|
53
|
+
registers[index] = Math.max(registers[index], rank);
|
|
54
|
+
return encode(registers);
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* The union of sketches, register by register: how a plugin combines a
|
|
58
|
+
* `distinct` column across the aggregate rows it reads.
|
|
59
|
+
*/
|
|
60
|
+
const mergeDistinct = (summaries) => {
|
|
61
|
+
const merged = new Uint8Array(REGISTER_COUNT);
|
|
62
|
+
for (const summary of summaries) {
|
|
63
|
+
const registers = decode(summary);
|
|
64
|
+
for (let index = 0; index < REGISTER_COUNT; index += 1) merged[index] = Math.max(merged[index], registers[index]);
|
|
65
|
+
}
|
|
66
|
+
return encode(merged);
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* The estimated number of distinct values a sketch holds: how a plugin
|
|
70
|
+
* reads a `distinct` column.
|
|
71
|
+
*/
|
|
72
|
+
const countDistinct = (summary) => {
|
|
73
|
+
const registers = decode(summary);
|
|
74
|
+
let inverseSum = 0;
|
|
75
|
+
let empty = 0;
|
|
76
|
+
for (const register of registers) {
|
|
77
|
+
inverseSum += 2 ** -register;
|
|
78
|
+
if (register === 0) empty += 1;
|
|
79
|
+
}
|
|
80
|
+
const raw = .7213 / 1.0010537109375 * REGISTER_COUNT ** 2 / inverseSum;
|
|
81
|
+
const estimate = raw <= 2.5 * REGISTER_COUNT && empty > 0 ? REGISTER_COUNT * Math.log(REGISTER_COUNT / empty) : raw;
|
|
82
|
+
return Math.max(0, Math.round(estimate));
|
|
83
|
+
};
|
|
84
|
+
//#endregion
|
|
85
|
+
export { addDistinct, countDistinct, distinctRegister, mergeDistinct };
|