@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,292 @@
|
|
|
1
|
+
import { expiresAt, findPhysicalIndex, indexOrderColumns, rowKey } from "../../../database/adapter.mjs";
|
|
2
|
+
import { canonicalJson, compareUtf8, indexEntries, indexOrderTuple, matchesQuery } from "../../../database/values.mjs";
|
|
3
|
+
import { DatabaseAdapterContractError } from "../../../database/verifyAdapter.mjs";
|
|
4
|
+
//#region src/engine/database/kv/kvAdapter.ts
|
|
5
|
+
const SEPARATOR = "";
|
|
6
|
+
/**
|
|
7
|
+
* Order-preserving text for key values; each value ends in the lowest code
|
|
8
|
+
* point any encoding uses. `createKvAdapter` stores a row as the item with
|
|
9
|
+
* the table's name as `pk` and its key's text as `sk`, so tooling that
|
|
10
|
+
* writes a store's items itself, as the AWS setup's settings items do, keys
|
|
11
|
+
* them with it.
|
|
12
|
+
*/
|
|
13
|
+
const encodeKvKey = (values) => values.map((value) => {
|
|
14
|
+
if (typeof value === "boolean") return `${Number(value)}${SEPARATOR}`;
|
|
15
|
+
if (typeof value === "number") {
|
|
16
|
+
const view = /* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(8));
|
|
17
|
+
view.setFloat64(0, value === 0 ? 0 : value);
|
|
18
|
+
const mask = view.getUint32(0) >>> 31 ? [-1, -1] : [1 << 31, 0];
|
|
19
|
+
return `${[0, 4].map((at, word) => ((view.getUint32(at) ^ mask[word]) >>> 0).toString(16).padStart(8, "0")).join("")}${SEPARATOR}`;
|
|
20
|
+
}
|
|
21
|
+
let text = "";
|
|
22
|
+
for (const char of value) text += char < "" ? `\u0002${String.fromCharCode(char.charCodeAt(0) + 1)}` : char;
|
|
23
|
+
return `${text}${SEPARATOR}`;
|
|
24
|
+
}).join("");
|
|
25
|
+
/** The first sort key after every key that starts with `prefix`. */
|
|
26
|
+
const pastPrefix = (prefix) => `${prefix.slice(0, -1)}\u0002`;
|
|
27
|
+
/** A unique item's sort key, and an index item's when its order is empty. */
|
|
28
|
+
const ONLY = "#";
|
|
29
|
+
/** An index whose eq and order columns are the key, in order, reads the row items. */
|
|
30
|
+
const inKeyOrder = (table, index) => {
|
|
31
|
+
const columns = [...index.eq, ...indexOrderColumns(table, index)];
|
|
32
|
+
return !index.unique && columns.length === table.key.length && columns.every((column, position) => column === table.key[position]);
|
|
33
|
+
};
|
|
34
|
+
const same = (left, right, columns) => columns.every((name) => canonicalJson(left[name]) === canonicalJson(right[name]));
|
|
35
|
+
/**
|
|
36
|
+
* The database adapter over a key-value store. A row is one item at
|
|
37
|
+
* `pk = <table>`, `sk = enc(key)`; each index not in key order adds an item
|
|
38
|
+
* per entry at `pk = <table>#<index>#enc(eq)`, `sk = enc(order)`, and each
|
|
39
|
+
* unique entry one item at `sk = "#"`, written only where no other row holds
|
|
40
|
+
* it. Index items hold a copy of the row without `_v` or counters, so an
|
|
41
|
+
* increment, which changes only those, never makes them stale.
|
|
42
|
+
*/
|
|
43
|
+
const createKvAdapter = ({ store, tablePrefix = "" }) => {
|
|
44
|
+
const layouts = /* @__PURE__ */ new WeakMap();
|
|
45
|
+
const layoutOf = (table) => {
|
|
46
|
+
const known = layouts.get(table);
|
|
47
|
+
if (known) return known;
|
|
48
|
+
const counters = table.columns.filter((column) => column.default !== void 0 && column.name !== "_v").map((column) => column.name);
|
|
49
|
+
const layout = {
|
|
50
|
+
table,
|
|
51
|
+
rows: tablePrefix + table.name,
|
|
52
|
+
copied: table.columns.map((column) => column.name).filter((name) => name !== "_v" && !counters.includes(name)),
|
|
53
|
+
counters,
|
|
54
|
+
indexed: table.indexes.filter((index) => !inKeyOrder(table, index))
|
|
55
|
+
};
|
|
56
|
+
layouts.set(table, layout);
|
|
57
|
+
return layout;
|
|
58
|
+
};
|
|
59
|
+
const rowItem = (layout, key) => ({
|
|
60
|
+
pk: layout.rows,
|
|
61
|
+
sk: encodeKvKey(key)
|
|
62
|
+
});
|
|
63
|
+
const partition = (layout, index, eq) => `${layout.rows}#${index.name}#${encodeKvKey(eq)}`;
|
|
64
|
+
const copyOf = (layout, row) => Object.fromEntries(layout.copied.map((name) => [name, row[name] ?? null]));
|
|
65
|
+
/** The expiry every item of `row` carries, for a table with retention. */
|
|
66
|
+
const stamp = (table, row) => {
|
|
67
|
+
const at = row && expiresAt(table, row);
|
|
68
|
+
return at === void 0 ? {} : { expiresAt: at };
|
|
69
|
+
};
|
|
70
|
+
/** Every index and unique item a row has, by address. */
|
|
71
|
+
const itemsOf = (layout, row) => {
|
|
72
|
+
const items = /* @__PURE__ */ new Map();
|
|
73
|
+
for (const index of row === null ? [] : layout.indexed) for (const eq of indexEntries(layout.table, index, row)) {
|
|
74
|
+
const pk = partition(layout, index, eq);
|
|
75
|
+
const sk = index.unique ? ONLY : encodeKvKey(indexOrderTuple(layout.table, index, row)) || ONLY;
|
|
76
|
+
items.set(JSON.stringify([pk, sk]), {
|
|
77
|
+
key: {
|
|
78
|
+
pk,
|
|
79
|
+
sk
|
|
80
|
+
},
|
|
81
|
+
unique: index.unique === true
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return items;
|
|
85
|
+
};
|
|
86
|
+
/** Moves a row's index items from `before` to `after`; a unique entry is taken only where no row holds it. */
|
|
87
|
+
const indexOps = (layout, before, after) => {
|
|
88
|
+
const old = itemsOf(layout, before);
|
|
89
|
+
const next = itemsOf(layout, after);
|
|
90
|
+
const ops = [...old].filter(([id]) => !next.has(id)).map(([, item]) => ({
|
|
91
|
+
type: "delete",
|
|
92
|
+
key: item.key
|
|
93
|
+
}));
|
|
94
|
+
const copy = after && copyOf(layout, after);
|
|
95
|
+
const unchanged = before !== null && copy !== null && same(before, copy, layout.copied);
|
|
96
|
+
for (const [id, item] of next) {
|
|
97
|
+
if (old.has(id) && unchanged) continue;
|
|
98
|
+
const taken = item.unique && !old.has(id);
|
|
99
|
+
ops.push({
|
|
100
|
+
type: "put",
|
|
101
|
+
key: item.key,
|
|
102
|
+
value: copy,
|
|
103
|
+
condition: taken ? { exists: false } : void 0,
|
|
104
|
+
...stamp(layout.table, after)
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return ops;
|
|
108
|
+
};
|
|
109
|
+
/** The item ops of one adapter op: its row item first, then its index items. */
|
|
110
|
+
const plan = (op) => {
|
|
111
|
+
const layout = layoutOf(op.table);
|
|
112
|
+
const key = rowItem(layout, op.type === "insert" ? rowKey(op.table, op.row) : op.key);
|
|
113
|
+
const guard = op.type === "insert" ? { exists: false } : op.guard;
|
|
114
|
+
const condition = guard && ("v" in guard ? { v: guard.v } : guard);
|
|
115
|
+
switch (op.type) {
|
|
116
|
+
case "check": return [{
|
|
117
|
+
type: "check",
|
|
118
|
+
key,
|
|
119
|
+
condition
|
|
120
|
+
}];
|
|
121
|
+
case "increment":
|
|
122
|
+
if (layout.indexed.length > 0 && (op.init !== void 0 || Object.keys(op.by).some((name) => layout.copied.includes(name)))) throw new DatabaseAdapterContractError(`${op.table.name} has index copies: an increment may change only counters of an existing row.`);
|
|
123
|
+
return [{
|
|
124
|
+
type: "add",
|
|
125
|
+
key,
|
|
126
|
+
by: {
|
|
127
|
+
...op.by,
|
|
128
|
+
["_v"]: 1
|
|
129
|
+
},
|
|
130
|
+
init: op.init,
|
|
131
|
+
condition: condition ?? (op.init ? void 0 : { exists: true }),
|
|
132
|
+
...stamp(op.table, op.init)
|
|
133
|
+
}];
|
|
134
|
+
case "delete": return [{
|
|
135
|
+
type: "delete",
|
|
136
|
+
key,
|
|
137
|
+
condition
|
|
138
|
+
}, ...indexOps(layout, op.previous, null)];
|
|
139
|
+
default: {
|
|
140
|
+
const before = op.type === "insert" ? null : op.previous;
|
|
141
|
+
const row = op.type === "insert" ? op.row : {
|
|
142
|
+
...op.previous,
|
|
143
|
+
...op.set,
|
|
144
|
+
["_v"]: op.guard.v + 1
|
|
145
|
+
};
|
|
146
|
+
return [{
|
|
147
|
+
type: "put",
|
|
148
|
+
key,
|
|
149
|
+
value: row,
|
|
150
|
+
condition,
|
|
151
|
+
...stamp(op.table, row)
|
|
152
|
+
}, ...indexOps(layout, before, row)];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* One op per item. Only unique items of two rows can meet: one op frees
|
|
158
|
+
* the value and another takes it. That becomes one unconditional put, as
|
|
159
|
+
* the freeing op's guard already holds the value's owner. Two rows taking
|
|
160
|
+
* one value fail at the later op.
|
|
161
|
+
*/
|
|
162
|
+
const compile = (ops) => {
|
|
163
|
+
const items = /* @__PURE__ */ new Map();
|
|
164
|
+
for (const [origin, op] of ops.entries()) for (const kv of plan(op)) {
|
|
165
|
+
const id = JSON.stringify([kv.key.pk, kv.key.sk]);
|
|
166
|
+
const earlier = items.get(id);
|
|
167
|
+
const put = [earlier, {
|
|
168
|
+
op: kv,
|
|
169
|
+
origin
|
|
170
|
+
}].find((planned) => planned?.op.type === "put");
|
|
171
|
+
if (earlier === void 0) items.set(id, {
|
|
172
|
+
op: kv,
|
|
173
|
+
origin
|
|
174
|
+
});
|
|
175
|
+
else if (put?.op.type === "put" && [earlier.op, kv].some(({ type }) => type === "delete")) items.set(id, {
|
|
176
|
+
op: {
|
|
177
|
+
...put.op,
|
|
178
|
+
condition: void 0
|
|
179
|
+
},
|
|
180
|
+
origin
|
|
181
|
+
});
|
|
182
|
+
else return { failedOp: origin };
|
|
183
|
+
}
|
|
184
|
+
return { items: [...items.values()] };
|
|
185
|
+
};
|
|
186
|
+
const encoder = new TextEncoder();
|
|
187
|
+
const sizeOf = ({ op }) => encoder.encode(op.key.pk + op.key.sk + JSON.stringify(op.type === "put" ? op.value : op.type === "add" ? op.init : null)).length;
|
|
188
|
+
/** Fills counters from the rows; a row that still equals its copy is returned whole, `_v` included. */
|
|
189
|
+
const complete = async (layout, copies) => {
|
|
190
|
+
if (layout.counters.length === 0 || copies.length === 0) return copies;
|
|
191
|
+
const rows = await store.get(copies.map((copy) => rowItem(layout, rowKey(layout.table, copy))));
|
|
192
|
+
return copies.map((copy, at) => {
|
|
193
|
+
const row = rows[at];
|
|
194
|
+
if (row && same(row, copy, layout.copied)) return row;
|
|
195
|
+
const counters = layout.counters.map((name) => [name, row?.[name] ?? 0]);
|
|
196
|
+
return {
|
|
197
|
+
...copy,
|
|
198
|
+
...Object.fromEntries(counters)
|
|
199
|
+
};
|
|
200
|
+
});
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* A request's sort keys within its partition, from `gte` up to `lt`; null
|
|
204
|
+
* when none can match. Every encoding ends in the separator, so the keys
|
|
205
|
+
* that start with a prefix end right before `pastPrefix` of it.
|
|
206
|
+
*/
|
|
207
|
+
const rangeOf = (prefix, request) => {
|
|
208
|
+
const edge = (bound, lower) => {
|
|
209
|
+
if (bound === void 0) return prefix === "" ? void 0 : lower ? prefix : pastPrefix(prefix);
|
|
210
|
+
const text = prefix + encodeKvKey(bound.values);
|
|
211
|
+
if (text === "") return bound.inclusive ? void 0 : null;
|
|
212
|
+
return lower === bound.inclusive ? text : pastPrefix(text);
|
|
213
|
+
};
|
|
214
|
+
const gte = edge(request.lower, true);
|
|
215
|
+
const lt = edge(request.upper, false);
|
|
216
|
+
if (gte === null || lt === null) return null;
|
|
217
|
+
if (gte !== void 0 && lt !== void 0 && compareUtf8(gte, lt) >= 0) return null;
|
|
218
|
+
if (lt === void 0) return {
|
|
219
|
+
gte,
|
|
220
|
+
lt
|
|
221
|
+
};
|
|
222
|
+
return {
|
|
223
|
+
gte,
|
|
224
|
+
lt,
|
|
225
|
+
lte: lt.endsWith(SEPARATOR) ? lt.slice(0, -1) : lt
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
return {
|
|
229
|
+
id: store.id,
|
|
230
|
+
fits(ops) {
|
|
231
|
+
const { items } = compile(ops);
|
|
232
|
+
if (items === void 0) return true;
|
|
233
|
+
const { items: most, bytes, itemBytes = bytes, keyBytes } = store.limits;
|
|
234
|
+
const sizes = items.map(sizeOf);
|
|
235
|
+
const length = (text) => encoder.encode(text).length;
|
|
236
|
+
return items.length <= most && sizes.reduce((sum, size) => sum + size, 0) <= bytes && sizes.every((size) => size <= itemBytes) && (keyBytes === void 0 || items.every(({ op }) => length(op.key.pk) <= keyBytes.pk && length(op.key.sk) <= keyBytes.sk));
|
|
237
|
+
},
|
|
238
|
+
get: (table, keys) => store.get(keys.map((key) => rowItem(layoutOf(table), key))),
|
|
239
|
+
async query(table, request) {
|
|
240
|
+
const layout = layoutOf(table);
|
|
241
|
+
const index = findPhysicalIndex(table, request.index);
|
|
242
|
+
if (index.unique) {
|
|
243
|
+
const key = {
|
|
244
|
+
pk: partition(layout, index, request.eq),
|
|
245
|
+
sk: ONLY
|
|
246
|
+
};
|
|
247
|
+
const [copy] = await store.get([key]);
|
|
248
|
+
const found = copy && matchesQuery(table, index, request, copy);
|
|
249
|
+
return complete(layout, found ? [copy] : []);
|
|
250
|
+
}
|
|
251
|
+
const own = inKeyOrder(table, index);
|
|
252
|
+
const range = rangeOf(own ? encodeKvKey(request.eq) : "", request);
|
|
253
|
+
if (range === null) return [];
|
|
254
|
+
const pk = own ? layout.rows : partition(layout, index, request.eq);
|
|
255
|
+
const rows = [];
|
|
256
|
+
let after;
|
|
257
|
+
while (rows.length < request.limit) {
|
|
258
|
+
const { items, more } = await store.query({
|
|
259
|
+
pk,
|
|
260
|
+
...range,
|
|
261
|
+
order: request.order,
|
|
262
|
+
limit: request.limit - rows.length,
|
|
263
|
+
after
|
|
264
|
+
});
|
|
265
|
+
rows.push(...items.map((item) => item.value));
|
|
266
|
+
if (!more || items.length === 0) break;
|
|
267
|
+
after = items.at(-1).sk;
|
|
268
|
+
}
|
|
269
|
+
return own ? rows : complete(layout, rows);
|
|
270
|
+
},
|
|
271
|
+
async write(ops) {
|
|
272
|
+
const { items, failedOp } = compile(ops);
|
|
273
|
+
if (items === void 0) return {
|
|
274
|
+
ok: false,
|
|
275
|
+
failedOp
|
|
276
|
+
};
|
|
277
|
+
const result = await store.write(items.map(({ op }) => op));
|
|
278
|
+
return !result.ok && "failedOp" in result ? {
|
|
279
|
+
ok: false,
|
|
280
|
+
failedOp: items[result.failedOp].origin
|
|
281
|
+
} : result;
|
|
282
|
+
},
|
|
283
|
+
...store.deleteConsumed === void 0 ? {} : { deleteConsumed: (table, rows) => {
|
|
284
|
+
const layout = layoutOf(table);
|
|
285
|
+
return store.deleteConsumed(rows.flatMap((row) => [rowItem(layout, rowKey(table, row)), ...[...itemsOf(layout, row).values()].map(({ key }) => key)]));
|
|
286
|
+
} },
|
|
287
|
+
migrations: { apply: async () => store.migrations?.apply() },
|
|
288
|
+
...store.dispose === void 0 ? {} : { dispose: () => store.dispose() }
|
|
289
|
+
};
|
|
290
|
+
};
|
|
291
|
+
//#endregion
|
|
292
|
+
export { createKvAdapter, encodeKvKey };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
//#region src/engine/database/readMeter.ts
|
|
2
|
+
const METER = Symbol.for("@hot-updater/plugin-core/read-meter");
|
|
3
|
+
const NO_READS = {
|
|
4
|
+
gets: 0,
|
|
5
|
+
keys: 0,
|
|
6
|
+
queries: 0,
|
|
7
|
+
rows: 0
|
|
8
|
+
};
|
|
9
|
+
/** The meter `meterReads` gave `database`, if it did. */
|
|
10
|
+
const readMeterOf = (database) => typeof database === "object" && database !== null ? database[METER] : void 0;
|
|
11
|
+
/**
|
|
12
|
+
* `database` with read metering, for adapter cost analysis: pass it to
|
|
13
|
+
* `createHotUpdater` or `createEngine`, then `measureReads` reports what a
|
|
14
|
+
* call read at the adapter and at the engine. It works on any database on
|
|
15
|
+
* the storage engine, a provider's included, and every engine on it reports
|
|
16
|
+
* to the same meter.
|
|
17
|
+
*/
|
|
18
|
+
const meterReads = (database) => {
|
|
19
|
+
let adapterReads = NO_READS;
|
|
20
|
+
const engines = /* @__PURE__ */ new Set();
|
|
21
|
+
const add = (count) => {
|
|
22
|
+
adapterReads = {
|
|
23
|
+
gets: adapterReads.gets + (count.gets ?? 0),
|
|
24
|
+
keys: adapterReads.keys + (count.keys ?? 0),
|
|
25
|
+
queries: adapterReads.queries + (count.queries ?? 0),
|
|
26
|
+
rows: adapterReads.rows + (count.rows ?? 0)
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
const meter = {
|
|
30
|
+
wrap: (adapter) => Object.assign(Object.create(adapter), {
|
|
31
|
+
async get(...args) {
|
|
32
|
+
const rows = await adapter.get(...args);
|
|
33
|
+
add({
|
|
34
|
+
gets: 1,
|
|
35
|
+
keys: args[1].length
|
|
36
|
+
});
|
|
37
|
+
return rows;
|
|
38
|
+
},
|
|
39
|
+
async query(...args) {
|
|
40
|
+
const rows = await adapter.query(...args);
|
|
41
|
+
add({
|
|
42
|
+
queries: 1,
|
|
43
|
+
rows: rows.length
|
|
44
|
+
});
|
|
45
|
+
return rows;
|
|
46
|
+
}
|
|
47
|
+
}),
|
|
48
|
+
attach: (counter) => {
|
|
49
|
+
engines.add(counter);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
...database,
|
|
54
|
+
[METER]: meter,
|
|
55
|
+
async measureReads(read) {
|
|
56
|
+
adapterReads = NO_READS;
|
|
57
|
+
for (const engine of engines) engine.reset();
|
|
58
|
+
const result = await read();
|
|
59
|
+
let engine = {
|
|
60
|
+
calls: 0,
|
|
61
|
+
rows: 0
|
|
62
|
+
};
|
|
63
|
+
for (const counter of engines) {
|
|
64
|
+
const total = counter.total();
|
|
65
|
+
engine = {
|
|
66
|
+
calls: engine.calls + total.calls,
|
|
67
|
+
rows: engine.rows + total.rows
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
result,
|
|
72
|
+
adapter: adapterReads,
|
|
73
|
+
engine
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
//#endregion
|
|
79
|
+
exports.meterReads = meterReads;
|
|
80
|
+
exports.readMeterOf = readMeterOf;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { EngineReadCount } from "./engineReads.cjs";
|
|
2
|
+
import { EngineDatabase } from "../../types/databaseConfig.cjs";
|
|
3
|
+
import { DatabaseReadCount } from "../../database/verifyAdapter.cjs";
|
|
4
|
+
//#region src/engine/database/readMeter.d.ts
|
|
5
|
+
interface ReadMeasurement<T> {
|
|
6
|
+
readonly result: T;
|
|
7
|
+
/** Point reads and the keys they asked for, and queries and the rows they returned, at the adapter. */
|
|
8
|
+
readonly adapter: DatabaseReadCount;
|
|
9
|
+
/** Reads and the rows (logical rows for aggregates) the engine returned to its callers. */
|
|
10
|
+
readonly engine: EngineReadCount;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A database whose reads `measureReads` reports, for adapter cost analysis:
|
|
14
|
+
* what each server call costs the store that bills by read.
|
|
15
|
+
*/
|
|
16
|
+
type MeteredDatabase<TDatabase extends EngineDatabase = EngineDatabase> = TDatabase & {
|
|
17
|
+
/**
|
|
18
|
+
* Runs `read` and reports what the engines on this database read while
|
|
19
|
+
* it ran, at the adapter and at the engine. A process's schema-fence
|
|
20
|
+
* read and retention passes are not part of any call and are not
|
|
21
|
+
* counted.
|
|
22
|
+
*/
|
|
23
|
+
measureReads<T>(read: () => Promise<T>): Promise<ReadMeasurement<T>>;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* `database` with read metering, for adapter cost analysis: pass it to
|
|
27
|
+
* `createHotUpdater` or `createEngine`, then `measureReads` reports what a
|
|
28
|
+
* call read at the adapter and at the engine. It works on any database on
|
|
29
|
+
* the storage engine, a provider's included, and every engine on it reports
|
|
30
|
+
* to the same meter.
|
|
31
|
+
*/
|
|
32
|
+
declare const meterReads: <TDatabase extends EngineDatabase>(database: TDatabase) => MeteredDatabase<TDatabase>;
|
|
33
|
+
//#endregion
|
|
34
|
+
export { MeteredDatabase, ReadMeasurement, meterReads };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { EngineReadCount } from "./engineReads.mjs";
|
|
2
|
+
import { EngineDatabase } from "../../types/databaseConfig.mjs";
|
|
3
|
+
import { DatabaseReadCount } from "../../database/verifyAdapter.mjs";
|
|
4
|
+
//#region src/engine/database/readMeter.d.ts
|
|
5
|
+
interface ReadMeasurement<T> {
|
|
6
|
+
readonly result: T;
|
|
7
|
+
/** Point reads and the keys they asked for, and queries and the rows they returned, at the adapter. */
|
|
8
|
+
readonly adapter: DatabaseReadCount;
|
|
9
|
+
/** Reads and the rows (logical rows for aggregates) the engine returned to its callers. */
|
|
10
|
+
readonly engine: EngineReadCount;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A database whose reads `measureReads` reports, for adapter cost analysis:
|
|
14
|
+
* what each server call costs the store that bills by read.
|
|
15
|
+
*/
|
|
16
|
+
type MeteredDatabase<TDatabase extends EngineDatabase = EngineDatabase> = TDatabase & {
|
|
17
|
+
/**
|
|
18
|
+
* Runs `read` and reports what the engines on this database read while
|
|
19
|
+
* it ran, at the adapter and at the engine. A process's schema-fence
|
|
20
|
+
* read and retention passes are not part of any call and are not
|
|
21
|
+
* counted.
|
|
22
|
+
*/
|
|
23
|
+
measureReads<T>(read: () => Promise<T>): Promise<ReadMeasurement<T>>;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* `database` with read metering, for adapter cost analysis: pass it to
|
|
27
|
+
* `createHotUpdater` or `createEngine`, then `measureReads` reports what a
|
|
28
|
+
* call read at the adapter and at the engine. It works on any database on
|
|
29
|
+
* the storage engine, a provider's included, and every engine on it reports
|
|
30
|
+
* to the same meter.
|
|
31
|
+
*/
|
|
32
|
+
declare const meterReads: <TDatabase extends EngineDatabase>(database: TDatabase) => MeteredDatabase<TDatabase>;
|
|
33
|
+
//#endregion
|
|
34
|
+
export { MeteredDatabase, ReadMeasurement, meterReads };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
//#region src/engine/database/readMeter.ts
|
|
2
|
+
const METER = Symbol.for("@hot-updater/plugin-core/read-meter");
|
|
3
|
+
const NO_READS = {
|
|
4
|
+
gets: 0,
|
|
5
|
+
keys: 0,
|
|
6
|
+
queries: 0,
|
|
7
|
+
rows: 0
|
|
8
|
+
};
|
|
9
|
+
/** The meter `meterReads` gave `database`, if it did. */
|
|
10
|
+
const readMeterOf = (database) => typeof database === "object" && database !== null ? database[METER] : void 0;
|
|
11
|
+
/**
|
|
12
|
+
* `database` with read metering, for adapter cost analysis: pass it to
|
|
13
|
+
* `createHotUpdater` or `createEngine`, then `measureReads` reports what a
|
|
14
|
+
* call read at the adapter and at the engine. It works on any database on
|
|
15
|
+
* the storage engine, a provider's included, and every engine on it reports
|
|
16
|
+
* to the same meter.
|
|
17
|
+
*/
|
|
18
|
+
const meterReads = (database) => {
|
|
19
|
+
let adapterReads = NO_READS;
|
|
20
|
+
const engines = /* @__PURE__ */ new Set();
|
|
21
|
+
const add = (count) => {
|
|
22
|
+
adapterReads = {
|
|
23
|
+
gets: adapterReads.gets + (count.gets ?? 0),
|
|
24
|
+
keys: adapterReads.keys + (count.keys ?? 0),
|
|
25
|
+
queries: adapterReads.queries + (count.queries ?? 0),
|
|
26
|
+
rows: adapterReads.rows + (count.rows ?? 0)
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
const meter = {
|
|
30
|
+
wrap: (adapter) => Object.assign(Object.create(adapter), {
|
|
31
|
+
async get(...args) {
|
|
32
|
+
const rows = await adapter.get(...args);
|
|
33
|
+
add({
|
|
34
|
+
gets: 1,
|
|
35
|
+
keys: args[1].length
|
|
36
|
+
});
|
|
37
|
+
return rows;
|
|
38
|
+
},
|
|
39
|
+
async query(...args) {
|
|
40
|
+
const rows = await adapter.query(...args);
|
|
41
|
+
add({
|
|
42
|
+
queries: 1,
|
|
43
|
+
rows: rows.length
|
|
44
|
+
});
|
|
45
|
+
return rows;
|
|
46
|
+
}
|
|
47
|
+
}),
|
|
48
|
+
attach: (counter) => {
|
|
49
|
+
engines.add(counter);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
...database,
|
|
54
|
+
[METER]: meter,
|
|
55
|
+
async measureReads(read) {
|
|
56
|
+
adapterReads = NO_READS;
|
|
57
|
+
for (const engine of engines) engine.reset();
|
|
58
|
+
const result = await read();
|
|
59
|
+
let engine = {
|
|
60
|
+
calls: 0,
|
|
61
|
+
rows: 0
|
|
62
|
+
};
|
|
63
|
+
for (const counter of engines) {
|
|
64
|
+
const total = counter.total();
|
|
65
|
+
engine = {
|
|
66
|
+
calls: engine.calls + total.calls,
|
|
67
|
+
rows: engine.rows + total.rows
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
result,
|
|
72
|
+
adapter: adapterReads,
|
|
73
|
+
engine
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
//#endregion
|
|
79
|
+
export { meterReads, readMeterOf };
|