@gaganref/confect-test 10.3.1-fork.90 → 10.3.1-fork.92
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/dist/internal/Attempt.d.ts +29 -1
- package/dist/internal/Attempt.d.ts.map +1 -1
- package/dist/internal/Attempt.js +45 -1
- package/dist/internal/Attempt.js.map +1 -1
- package/dist/internal/ConvexTest.d.ts +23 -2
- package/dist/internal/ConvexTest.d.ts.map +1 -1
- package/dist/internal/ConvexTest.js +11 -5
- package/dist/internal/ConvexTest.js.map +1 -1
- package/dist/internal/Execution.d.ts +43 -0
- package/dist/internal/Execution.d.ts.map +1 -1
- package/dist/internal/Execution.js +200 -41
- package/dist/internal/Execution.js.map +1 -1
- package/dist/internal/Invocation.d.ts.map +1 -1
- package/dist/internal/Invocation.js.map +1 -1
- package/dist/internal/KernelQuery.d.ts +8 -3
- package/dist/internal/KernelQuery.d.ts.map +1 -1
- package/dist/internal/KernelQuery.js +10 -3
- package/dist/internal/KernelQuery.js.map +1 -1
- package/dist/internal/Metering.d.ts +186 -0
- package/dist/internal/Metering.d.ts.map +1 -0
- package/dist/internal/Metering.js +401 -0
- package/dist/internal/Metering.js.map +1 -0
- package/dist/internal/Subscriptions.d.ts +116 -0
- package/dist/internal/Subscriptions.d.ts.map +1 -0
- package/dist/internal/Subscriptions.js +186 -0
- package/dist/internal/Subscriptions.js.map +1 -0
- package/package.json +3 -3
- package/src/internal/Attempt.ts +52 -0
- package/src/internal/ConvexTest.ts +33 -5
- package/src/internal/Execution.ts +375 -54
- package/src/internal/Invocation.ts +2 -1
- package/src/internal/KernelQuery.ts +17 -6
- package/src/internal/Metering.ts +520 -0
- package/src/internal/Subscriptions.ts +302 -0
|
@@ -35,9 +35,11 @@ const resolveSource = (view, query) => {
|
|
|
35
35
|
let order;
|
|
36
36
|
let table;
|
|
37
37
|
let range = [];
|
|
38
|
+
let index;
|
|
38
39
|
switch (source.type) {
|
|
39
40
|
case "FullTableScan":
|
|
40
41
|
table = source.tableName;
|
|
42
|
+
index = "by_creation_time";
|
|
41
43
|
docs = [...view.scan(source.tableName)];
|
|
42
44
|
order = source.order ?? "asc";
|
|
43
45
|
fieldPathsToSortBy = ["_creationTime"];
|
|
@@ -45,15 +47,16 @@ const resolveSource = (view, query) => {
|
|
|
45
47
|
case "IndexRange": {
|
|
46
48
|
const [tableName, indexName] = source.indexName.split(".");
|
|
47
49
|
table = tableName;
|
|
50
|
+
index = indexName;
|
|
48
51
|
range = source.range;
|
|
49
52
|
order = source.order ?? "asc";
|
|
50
53
|
let fields;
|
|
51
54
|
if (indexName === "by_creation_time") fields = ["_creationTime", "_id"];
|
|
52
55
|
else if (indexName === "by_id") fields = ["_id"];
|
|
53
56
|
else {
|
|
54
|
-
const
|
|
55
|
-
if (
|
|
56
|
-
fields =
|
|
57
|
+
const declared = view.schema?.tables.get(tableName)?.indexes.find(({ indexDescriptor }) => indexDescriptor === indexName);
|
|
58
|
+
if (declared === void 0) throw new Error(`Cannot use index "${indexName}" for table "${tableName}" because it is not declared in the schema.`);
|
|
59
|
+
fields = declared.fields.concat(["_creationTime", "_id"]);
|
|
57
60
|
}
|
|
58
61
|
fieldPathsToSortBy = fields;
|
|
59
62
|
validateIndexRangeExpression(source, fields);
|
|
@@ -79,6 +82,7 @@ const resolveSource = (view, query) => {
|
|
|
79
82
|
order,
|
|
80
83
|
interval: {
|
|
81
84
|
kind: "interval",
|
|
85
|
+
index,
|
|
82
86
|
table,
|
|
83
87
|
fields: fieldPathsToSortBy.includes("_id") ? fieldPathsToSortBy : [...fieldPathsToSortBy, "_id"],
|
|
84
88
|
order,
|
|
@@ -98,6 +102,7 @@ const stream = (view, query) => {
|
|
|
98
102
|
let returned = 0;
|
|
99
103
|
for (const doc of sortedDocs) {
|
|
100
104
|
if (limit !== null && returned >= limit) return;
|
|
105
|
+
view.examine(interval.table, doc);
|
|
101
106
|
examined(keyOf(interval.fields, doc));
|
|
102
107
|
if (filterFn(doc)) {
|
|
103
108
|
returned++;
|
|
@@ -158,6 +163,7 @@ const paginate = (view, { query, cursor, endCursor, pageSize, maximumRowsRead, m
|
|
|
158
163
|
reachedEnd = false;
|
|
159
164
|
examined(endCursorKeys);
|
|
160
165
|
if (cmp === 0) {
|
|
166
|
+
view.examine(interval.table, doc);
|
|
161
167
|
rowsRead += 1;
|
|
162
168
|
bytesRead += getDocumentSize(doc);
|
|
163
169
|
readDocCursors.push(encodeCursor(doc, cursorFields));
|
|
@@ -167,6 +173,7 @@ const paginate = (view, { query, cursor, endCursor, pageSize, maximumRowsRead, m
|
|
|
167
173
|
break;
|
|
168
174
|
}
|
|
169
175
|
}
|
|
176
|
+
view.examine(interval.table, doc);
|
|
170
177
|
rowsRead += 1;
|
|
171
178
|
bytesRead += getDocumentSize(doc);
|
|
172
179
|
readDocCursors.push(encodeCursor(doc, cursorFields));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"KernelQuery.js","names":["KernelRefusal.requiresBinary","ReadSet.keyOf"],"sources":["../../src/internal/KernelQuery.ts"],"sourcesContent":["// Derived from convex-test 0.0.65 (index.ts, `DatabaseFake`), Copyright Convex,\n// Inc., licensed under the Apache License, Version 2.0\n// (https://www.apache.org/licenses/LICENSE-2.0). The ordering, index range and\n// pagination rules are convex-test's. They read a table through the attempt\n// that runs the query instead of convex-test's database, and record the\n// interval each query examined.\n\nimport type {\n GenericDocument,\n GenericSchema,\n SchemaDefinition,\n} from \"convex/server\";\nimport {\n compareValues,\n convexToJson,\n getDocumentSize,\n jsonToConvex,\n} from \"convex/values\";\nimport * as KernelRefusal from \"./KernelRefusal.js\";\nimport * as ReadSet from \"./ReadSet.js\";\nimport type {\n FilterJson,\n Index,\n SerializedQuery,\n SerializedRangeExpression,\n StoredDocument,\n ValidatorJSON,\n} from \"./KernelValues.js\";\nimport {\n evaluateFieldPath,\n evaluateFilter,\n evaluateRangeFilter,\n isValidIdentifier,\n resolvePendingValues,\n validateFieldNames,\n validateIndexRangeExpression,\n validateValidator,\n} from \"./KernelValues.js\";\n\n/**\n * A component's tables as its schema declares them.\n */\nexport interface KernelSchema {\n readonly schemaValidation: boolean;\n readonly tables: ReadonlyMap<\n string,\n {\n readonly indexes: ReadonlyArray<Index>;\n readonly documentType: ValidatorJSON;\n }\n >;\n}\n\nexport const exportSchema = (\n schema: SchemaDefinition<GenericSchema, boolean> | null | undefined,\n): KernelSchema | null => {\n if (schema === null || schema === undefined) return null;\n const exported: KernelSchema = {\n schemaValidation: (schema as unknown as { schemaValidation: boolean })\n .schemaValidation,\n tables: new Map(\n Object.entries(schema.tables).map(([name, table]) => [\n name,\n (table as unknown as { export: () => any }).export(),\n ]),\n ),\n };\n exported.tables.forEach((table, tableName) => {\n if (!isValidIdentifier(tableName)) {\n throw new Error(\n `Table names must be valid identifiers, got \"${tableName}\"`,\n );\n }\n validateFieldNames(table.documentType);\n table.indexes.forEach(({ indexDescriptor }) => {\n if (!isValidIdentifier(indexDescriptor)) {\n throw new Error(\n `Index names must be valid identifiers, got \"${indexDescriptor}\"`,\n );\n }\n });\n });\n return exported;\n};\n\n/**\n * Check a document against its table's declared type, as the backend does\n * before a write.\n */\nexport const validateDocument = (\n schema: KernelSchema | null,\n tableName: string,\n doc: Record<string, unknown>,\n) => {\n if (schema === null || !schema.schemaValidation) return;\n const validator = schema.tables.get(tableName)?.documentType;\n if (validator === undefined) return;\n validateValidator(validator, resolvePendingValues(doc as GenericDocument));\n};\n\n/**\n * An interval a query reads, before it says where it starts and how far it got.\n */\nexport type Interval = Omit<\n ReadSet.IntervalRead,\n \"componentPath\" | \"after\" | \"through\"\n>;\n\n/**\n * What a query reads: one table of one component, as the running attempt sees\n * it. `read` records an interval in the attempt's read set and returns how to\n * advance it.\n */\nexport interface TableView {\n readonly schema: KernelSchema | null;\n readonly scan: (tableName: string) => ReadonlyArray<StoredDocument>;\n readonly read: (\n interval: Interval,\n after: ReadSet.Key | null,\n ) => (through: ReadSet.Key | \"end\") => void;\n}\n\ninterface ResolvedSource {\n readonly sortedDocs: ReadonlyArray<GenericDocument>;\n readonly filterFn: (doc: GenericDocument) => boolean;\n readonly limit: number | null;\n readonly order: \"asc\" | \"desc\";\n readonly interval: Interval;\n}\n\nconst resolveSource = (\n view: TableView,\n query: SerializedQuery,\n): ResolvedSource => {\n const source = query.source;\n let docs: Array<GenericDocument>;\n let fieldPathsToSortBy: Array<string>;\n let order: \"asc\" | \"desc\";\n let table: string;\n let range: ReadonlyArray<SerializedRangeExpression> = [];\n switch (source.type) {\n case \"FullTableScan\": {\n table = source.tableName;\n docs = [...view.scan(source.tableName)];\n order = source.order ?? \"asc\";\n fieldPathsToSortBy = [\"_creationTime\"];\n break;\n }\n case \"IndexRange\": {\n const [tableName, indexName] = source.indexName.split(\".\") as [\n string,\n string,\n ];\n table = tableName;\n range = source.range;\n order = source.order ?? \"asc\";\n let fields: Array<string>;\n if (indexName === \"by_creation_time\") {\n fields = [\"_creationTime\", \"_id\"];\n } else if (indexName === \"by_id\") {\n fields = [\"_id\"];\n } else {\n const index = view.schema?.tables\n .get(tableName)\n ?.indexes.find(\n ({ indexDescriptor }) => indexDescriptor === indexName,\n );\n if (index === undefined) {\n throw new Error(\n `Cannot use index \"${indexName}\" for table \"${tableName}\" because ` +\n `it is not declared in the schema.`,\n );\n }\n fields = index.fields.concat([\"_creationTime\", \"_id\"]);\n }\n fieldPathsToSortBy = fields;\n validateIndexRangeExpression(source, fields);\n docs = view\n .scan(tableName)\n .filter((doc) =>\n source.range.every((filter) => evaluateRangeFilter(doc, filter)),\n );\n break;\n }\n case \"Search\": {\n throw KernelRefusal.requiresBinary(\"textSearch\");\n }\n }\n\n const filters = query.operators\n .filter(\n (operator): operator is { filter: FilterJson } => \"filter\" in operator,\n )\n .map((operator) => operator.filter);\n const limit =\n query.operators.find(\n (operator): operator is { limit: number } => \"limit\" in operator,\n ) ?? null;\n\n const orderMultiplier = order === \"asc\" ? 1 : -1;\n docs.sort((a, b) => {\n for (const fp of fieldPathsToSortBy) {\n const v = compareValues(\n evaluateFieldPath(fp, a),\n evaluateFieldPath(fp, b),\n );\n if (v !== 0) return v * orderMultiplier;\n }\n return 0;\n });\n\n return {\n sortedDocs: docs,\n filterFn: (doc) => filters.every((f) => evaluateFilter(doc, f)),\n limit: limit?.limit ?? null,\n order,\n interval: {\n kind: \"interval\",\n table,\n fields: fieldPathsToSortBy.includes(\"_id\")\n ? fieldPathsToSortBy\n : [...fieldPathsToSortBy, \"_id\"],\n order,\n range,\n },\n };\n};\n\n/**\n * A query's documents in order. The source is read when the query starts, as a\n * snapshot, and consumed lazily. The read set holds the prefix the stream has\n * examined, filtered documents included, or the whole range once it ends.\n */\nexport const stream = (\n view: TableView,\n query: SerializedQuery,\n): Iterator<GenericDocument> => {\n const { sortedDocs, filterFn, limit, interval } = resolveSource(view, query);\n const examined = view.read(interval, null);\n return (function* () {\n let returned = 0;\n for (const doc of sortedDocs) {\n if (limit !== null && returned >= limit) return;\n examined(ReadSet.keyOf(interval.fields, doc as StoredDocument));\n if (filterFn(doc)) {\n returned++;\n yield doc;\n }\n }\n examined(\"end\");\n })();\n};\n\nconst encodeCursor = (\n doc: GenericDocument,\n fieldPaths: ReadonlyArray<string>,\n): string =>\n JSON.stringify(\n fieldPaths.map((fp) => {\n const k = evaluateFieldPath(fp, doc);\n return k === undefined ? null : JSON.stringify(convexToJson(k));\n }),\n );\n\nconst parseCursor = (c: string | null | undefined) =>\n c\n ? (JSON.parse(c) as Array<string | null>).map((k) =>\n k === null ? undefined : jsonToConvex(JSON.parse(k)),\n )\n : null;\n\nconst compareToCursor = (\n doc: GenericDocument,\n cursorKeys: ReadonlyArray<unknown>,\n fieldPaths: ReadonlyArray<string>,\n order: \"asc\" | \"desc\",\n): number => {\n const orderMultiplier = order === \"asc\" ? 1 : -1;\n for (let i = 0; i < fieldPaths.length; i++) {\n const cmp = compareValues(\n evaluateFieldPath(fieldPaths[i]!, doc),\n cursorKeys[i] as any,\n );\n if (cmp !== 0) return cmp * orderMultiplier;\n }\n return 0;\n};\n\nexport interface PageRequest {\n readonly query: SerializedQuery;\n readonly cursor: string | null;\n readonly endCursor?: string | null;\n readonly pageSize: number;\n readonly maximumRowsRead?: number | null;\n readonly maximumBytesRead?: number | null;\n}\n\nexport const paginate = (\n view: TableView,\n {\n query,\n cursor,\n endCursor,\n pageSize,\n maximumRowsRead,\n maximumBytesRead,\n }: PageRequest,\n) => {\n const { sortedDocs, filterFn, order, interval } = resolveSource(view, query);\n // A page from the terminal cursor reads the empty interval past the end, so\n // it depends on nothing.\n if (cursor === \"_end_cursor\") {\n return {\n page: [],\n isDone: true,\n continueCursor: \"_end_cursor\",\n splitCursor: null,\n pageStatus: null,\n };\n }\n const cursorFields = interval.fields;\n const cursorKeys = parseCursor(cursor);\n const hasEndBoundary = !!endCursor && endCursor !== \"_end_cursor\";\n const endCursorKeys = hasEndBoundary ? parseCursor(endCursor) : null;\n const examined = view.read(interval, cursorKeys);\n let reachedEnd = true;\n\n const page: Array<GenericDocument> = [];\n let isInPage = cursor === null;\n let continueCursor = \"_end_cursor\";\n let splitCursor: string | null = null;\n let pageStatus: \"SplitRecommended\" | \"SplitRequired\" | null = null;\n let rowsRead = 0;\n let bytesRead = 0;\n const readDocCursors: Array<string> = [];\n\n for (let i = 0; i < sortedDocs.length; i++) {\n const doc = sortedDocs[i]!;\n const isLastDoc = i === sortedDocs.length - 1;\n if (!isInPage) {\n if (cursorKeys) {\n if (compareToCursor(doc, cursorKeys, cursorFields, order) > 0) {\n isInPage = true;\n } else {\n continue;\n }\n }\n }\n if (hasEndBoundary && endCursorKeys) {\n const cmp = compareToCursor(doc, endCursorKeys, cursorFields, order);\n if (cmp >= 0) {\n reachedEnd = false;\n examined(endCursorKeys);\n if (cmp === 0) {\n rowsRead += 1;\n bytesRead += getDocumentSize(doc);\n readDocCursors.push(encodeCursor(doc, cursorFields));\n if (filterFn(doc)) page.push(doc);\n continueCursor = encodeCursor(doc, cursorFields);\n }\n break;\n }\n }\n rowsRead += 1;\n bytesRead += getDocumentSize(doc);\n readDocCursors.push(encodeCursor(doc, cursorFields));\n examined(ReadSet.keyOf(cursorFields, doc as StoredDocument));\n const hitLimit =\n (!!maximumRowsRead && rowsRead >= maximumRowsRead) ||\n (!!maximumBytesRead && bytesRead >= maximumBytesRead);\n if (filterFn(doc)) page.push(doc);\n if (hitLimit) {\n reachedEnd = false;\n pageStatus = \"SplitRequired\";\n continueCursor = encodeCursor(doc, cursorFields);\n break;\n }\n if (!endCursor && page.length >= pageSize) {\n reachedEnd = isLastDoc;\n if (!isLastDoc) continueCursor = encodeCursor(doc, cursorFields);\n break;\n }\n }\n // A page bounded by an end cursor examined the range only up to it.\n if (reachedEnd) examined(endCursorKeys ?? \"end\");\n\n if (pageStatus === \"SplitRequired\" && readDocCursors.length >= 2) {\n splitCursor = readDocCursors[Math.floor((readDocCursors.length - 1) / 2)]!;\n } else if (\n pageStatus === null &&\n rowsRead > pageSize + 1 &&\n readDocCursors.length >= 2\n ) {\n pageStatus = \"SplitRecommended\";\n splitCursor = readDocCursors[Math.floor((readDocCursors.length - 1) / 2)]!;\n }\n\n return {\n page,\n isDone: continueCursor === \"_end_cursor\",\n continueCursor,\n splitCursor,\n pageStatus,\n };\n};\n"],"mappings":";;;;;AAqDA,MAAa,gBACX,WACwB;CACxB,IAAI,WAAW,QAAQ,WAAW,KAAA,GAAW,OAAO;CACpD,MAAM,WAAyB;EAC7B,kBAAmB,OAChB;EACH,QAAQ,IAAI,IACV,OAAO,QAAQ,OAAO,MAAM,CAAC,CAAC,KAAK,CAAC,MAAM,WAAW,CACnD,MACC,MAA2C,OAAO,CACrD,CAAC,CACH;CACF;CACA,SAAS,OAAO,SAAS,OAAO,cAAc;EAC5C,IAAI,CAAC,kBAAkB,SAAS,GAC9B,MAAM,IAAI,MACR,+CAA+C,UAAU,EAC3D;EAEF,mBAAmB,MAAM,YAAY;EACrC,MAAM,QAAQ,SAAS,EAAE,sBAAsB;GAC7C,IAAI,CAAC,kBAAkB,eAAe,GACpC,MAAM,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J;;EAEF,IAAI,kBAAkB,eAAe;GACnC,MAAM,MAAM,gBAAgB,KAAK,eAAe,cAAc,KAAK;GACnE,IAAI,OAAO,GAAG;IACZ,aAAa;IACb,SAAS,aAAa;IACtB,IAAI,QAAQ,GAAG;KACb,YAAY;KACZ,aAAa,gBAAgB,GAAG;KAChC,eAAe,KAAK,aAAa,KAAK,YAAY,CAAC;KACnD,IAAI,SAAS,GAAG,GAAG,KAAK,KAAK,GAAG;KAChC,iBAAiB,aAAa,KAAK,YAAY;IACjD;IACA;GACF;EACF;EACA,YAAY;EACZ,aAAa,gBAAgB,GAAG;EAChC,eAAe,KAAK,aAAa,KAAK,YAAY,CAAC;EACnD,SAASA,MAAc,cAAc,GAAqB,CAAC;EAC3D,MAAM,WACH,CAAC,CAAC,mBAAmB,YAAY,mBACjC,CAAC,CAAC,oBAAoB,aAAa;EACtC,IAAI,SAAS,GAAG,GAAG,KAAK,KAAK,GAAG;EAChC,IAAI,UAAU;GACZ,aAAa;GACb,aAAa;GACb,iBAAiB,aAAa,KAAK,YAAY;GAC/C;EACF;EACA,IAAI,CAAC,aAAa,KAAK,UAAU,UAAU;GACzC,aAAa;GACb,IAAI,CAAC,WAAW,iBAAiB,aAAa,KAAK,YAAY;GAC/D;EACF;CACF;CAEA,IAAI,YAAY,SAAS,iBAAiB,KAAK;CAE/C,IAAI,eAAe,mBAAmB,eAAe,UAAU,GAC7D,cAAc,eAAe,KAAK,OAAO,eAAe,SAAS,KAAK,CAAC;MAClE,IACL,eAAe,QACf,WAAW,WAAW,KACtB,eAAe,UAAU,GACzB;EACA,aAAa;EACb,cAAc,eAAe,KAAK,OAAO,eAAe,SAAS,KAAK,CAAC;CACzE;CAEA,OAAO;EACL;EACA,QAAQ,mBAAmB;EAC3B;EACA;EACA;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"KernelQuery.js","names":["KernelRefusal.requiresBinary","ReadSet.keyOf"],"sources":["../../src/internal/KernelQuery.ts"],"sourcesContent":["// Derived from convex-test 0.0.65 (index.ts, `DatabaseFake`), Copyright Convex,\n// Inc., licensed under the Apache License, Version 2.0\n// (https://www.apache.org/licenses/LICENSE-2.0). The ordering, index range and\n// pagination rules are convex-test's. They read a table through the attempt\n// that runs the query instead of convex-test's database, and record the\n// interval each query examined.\n\nimport type {\n GenericDocument,\n GenericSchema,\n SchemaDefinition,\n} from \"convex/server\";\nimport {\n compareValues,\n convexToJson,\n getDocumentSize,\n jsonToConvex,\n} from \"convex/values\";\nimport * as KernelRefusal from \"./KernelRefusal.js\";\nimport * as ReadSet from \"./ReadSet.js\";\nimport type {\n FilterJson,\n Index,\n SerializedQuery,\n SerializedRangeExpression,\n StoredDocument,\n ValidatorJSON,\n} from \"./KernelValues.js\";\nimport {\n evaluateFieldPath,\n evaluateFilter,\n evaluateRangeFilter,\n isValidIdentifier,\n resolvePendingValues,\n validateFieldNames,\n validateIndexRangeExpression,\n validateValidator,\n} from \"./KernelValues.js\";\n\n/**\n * A component's tables as its schema declares them.\n */\nexport interface KernelSchema {\n readonly schemaValidation: boolean;\n readonly tables: ReadonlyMap<\n string,\n {\n readonly indexes: ReadonlyArray<Index>;\n readonly documentType: ValidatorJSON;\n }\n >;\n}\n\nexport const exportSchema = (\n schema: SchemaDefinition<GenericSchema, boolean> | null | undefined,\n): KernelSchema | null => {\n if (schema === null || schema === undefined) return null;\n const exported: KernelSchema = {\n schemaValidation: (schema as unknown as { schemaValidation: boolean })\n .schemaValidation,\n tables: new Map(\n Object.entries(schema.tables).map(([name, table]) => [\n name,\n (table as unknown as { export: () => any }).export(),\n ]),\n ),\n };\n exported.tables.forEach((table, tableName) => {\n if (!isValidIdentifier(tableName)) {\n throw new Error(\n `Table names must be valid identifiers, got \"${tableName}\"`,\n );\n }\n validateFieldNames(table.documentType);\n table.indexes.forEach(({ indexDescriptor }) => {\n if (!isValidIdentifier(indexDescriptor)) {\n throw new Error(\n `Index names must be valid identifiers, got \"${indexDescriptor}\"`,\n );\n }\n });\n });\n return exported;\n};\n\n/**\n * Check a document against its table's declared type, as the backend does\n * before a write.\n */\nexport const validateDocument = (\n schema: KernelSchema | null,\n tableName: string,\n doc: Record<string, unknown>,\n) => {\n if (schema === null || !schema.schemaValidation) return;\n const validator = schema.tables.get(tableName)?.documentType;\n if (validator === undefined) return;\n validateValidator(validator, resolvePendingValues(doc as GenericDocument));\n};\n\n/**\n * An interval a query reads, before it says where it starts and how far it got,\n * and the index it reads it from.\n */\nexport type Interval = Omit<\n ReadSet.IntervalRead,\n \"componentPath\" | \"after\" | \"through\"\n> & { readonly index: string };\n\n/**\n * What a query reads: one table of one component, as the running attempt sees\n * it. `read` records an interval in the attempt's read set and returns how to\n * advance it. `examine` charges one document the query examined, before the\n * query's filters see it.\n */\nexport interface TableView {\n readonly schema: KernelSchema | null;\n readonly scan: (tableName: string) => ReadonlyArray<StoredDocument>;\n readonly read: (\n interval: Interval,\n after: ReadSet.Key | null,\n ) => (through: ReadSet.Key | \"end\") => void;\n readonly examine: (table: string, doc: GenericDocument) => void;\n}\n\ninterface ResolvedSource {\n readonly sortedDocs: ReadonlyArray<GenericDocument>;\n readonly filterFn: (doc: GenericDocument) => boolean;\n readonly limit: number | null;\n readonly order: \"asc\" | \"desc\";\n readonly interval: Interval;\n}\n\nconst resolveSource = (\n view: TableView,\n query: SerializedQuery,\n): ResolvedSource => {\n const source = query.source;\n let docs: Array<GenericDocument>;\n let fieldPathsToSortBy: Array<string>;\n let order: \"asc\" | \"desc\";\n let table: string;\n let range: ReadonlyArray<SerializedRangeExpression> = [];\n let index: string;\n switch (source.type) {\n case \"FullTableScan\": {\n table = source.tableName;\n // The binary scans a whole table through its creation-time index.\n index = \"by_creation_time\";\n docs = [...view.scan(source.tableName)];\n order = source.order ?? \"asc\";\n fieldPathsToSortBy = [\"_creationTime\"];\n break;\n }\n case \"IndexRange\": {\n const [tableName, indexName] = source.indexName.split(\".\") as [\n string,\n string,\n ];\n table = tableName;\n index = indexName;\n range = source.range;\n order = source.order ?? \"asc\";\n let fields: Array<string>;\n if (indexName === \"by_creation_time\") {\n fields = [\"_creationTime\", \"_id\"];\n } else if (indexName === \"by_id\") {\n fields = [\"_id\"];\n } else {\n const declared = view.schema?.tables\n .get(tableName)\n ?.indexes.find(\n ({ indexDescriptor }) => indexDescriptor === indexName,\n );\n if (declared === undefined) {\n throw new Error(\n `Cannot use index \"${indexName}\" for table \"${tableName}\" because ` +\n `it is not declared in the schema.`,\n );\n }\n fields = declared.fields.concat([\"_creationTime\", \"_id\"]);\n }\n fieldPathsToSortBy = fields;\n validateIndexRangeExpression(source, fields);\n docs = view\n .scan(tableName)\n .filter((doc) =>\n source.range.every((filter) => evaluateRangeFilter(doc, filter)),\n );\n break;\n }\n case \"Search\": {\n throw KernelRefusal.requiresBinary(\"textSearch\");\n }\n }\n\n const filters = query.operators\n .filter(\n (operator): operator is { filter: FilterJson } => \"filter\" in operator,\n )\n .map((operator) => operator.filter);\n const limit =\n query.operators.find(\n (operator): operator is { limit: number } => \"limit\" in operator,\n ) ?? null;\n\n const orderMultiplier = order === \"asc\" ? 1 : -1;\n docs.sort((a, b) => {\n for (const fp of fieldPathsToSortBy) {\n const v = compareValues(\n evaluateFieldPath(fp, a),\n evaluateFieldPath(fp, b),\n );\n if (v !== 0) return v * orderMultiplier;\n }\n return 0;\n });\n\n return {\n sortedDocs: docs,\n filterFn: (doc) => filters.every((f) => evaluateFilter(doc, f)),\n limit: limit?.limit ?? null,\n order,\n interval: {\n kind: \"interval\",\n index,\n table,\n fields: fieldPathsToSortBy.includes(\"_id\")\n ? fieldPathsToSortBy\n : [...fieldPathsToSortBy, \"_id\"],\n order,\n range,\n },\n };\n};\n\n/**\n * A query's documents in order. The source is read when the query starts, as a\n * snapshot, and consumed lazily. The read set holds the prefix the stream has\n * examined, filtered documents included, or the whole range once it ends.\n */\nexport const stream = (\n view: TableView,\n query: SerializedQuery,\n): Iterator<GenericDocument> => {\n const { sortedDocs, filterFn, limit, interval } = resolveSource(view, query);\n const examined = view.read(interval, null);\n return (function* () {\n let returned = 0;\n for (const doc of sortedDocs) {\n if (limit !== null && returned >= limit) return;\n view.examine(interval.table, doc);\n examined(ReadSet.keyOf(interval.fields, doc as StoredDocument));\n if (filterFn(doc)) {\n returned++;\n yield doc;\n }\n }\n examined(\"end\");\n })();\n};\n\nconst encodeCursor = (\n doc: GenericDocument,\n fieldPaths: ReadonlyArray<string>,\n): string =>\n JSON.stringify(\n fieldPaths.map((fp) => {\n const k = evaluateFieldPath(fp, doc);\n return k === undefined ? null : JSON.stringify(convexToJson(k));\n }),\n );\n\nconst parseCursor = (c: string | null | undefined) =>\n c\n ? (JSON.parse(c) as Array<string | null>).map((k) =>\n k === null ? undefined : jsonToConvex(JSON.parse(k)),\n )\n : null;\n\nconst compareToCursor = (\n doc: GenericDocument,\n cursorKeys: ReadonlyArray<unknown>,\n fieldPaths: ReadonlyArray<string>,\n order: \"asc\" | \"desc\",\n): number => {\n const orderMultiplier = order === \"asc\" ? 1 : -1;\n for (let i = 0; i < fieldPaths.length; i++) {\n const cmp = compareValues(\n evaluateFieldPath(fieldPaths[i]!, doc),\n cursorKeys[i] as any,\n );\n if (cmp !== 0) return cmp * orderMultiplier;\n }\n return 0;\n};\n\nexport interface PageRequest {\n readonly query: SerializedQuery;\n readonly cursor: string | null;\n readonly endCursor?: string | null;\n readonly pageSize: number;\n readonly maximumRowsRead?: number | null;\n readonly maximumBytesRead?: number | null;\n}\n\nexport const paginate = (\n view: TableView,\n {\n query,\n cursor,\n endCursor,\n pageSize,\n maximumRowsRead,\n maximumBytesRead,\n }: PageRequest,\n) => {\n const { sortedDocs, filterFn, order, interval } = resolveSource(view, query);\n // A page from the terminal cursor reads the empty interval past the end, so\n // it depends on nothing.\n if (cursor === \"_end_cursor\") {\n return {\n page: [],\n isDone: true,\n continueCursor: \"_end_cursor\",\n splitCursor: null,\n pageStatus: null,\n };\n }\n const cursorFields = interval.fields;\n const cursorKeys = parseCursor(cursor);\n const hasEndBoundary = !!endCursor && endCursor !== \"_end_cursor\";\n const endCursorKeys = hasEndBoundary ? parseCursor(endCursor) : null;\n const examined = view.read(interval, cursorKeys);\n let reachedEnd = true;\n\n const page: Array<GenericDocument> = [];\n let isInPage = cursor === null;\n let continueCursor = \"_end_cursor\";\n let splitCursor: string | null = null;\n let pageStatus: \"SplitRecommended\" | \"SplitRequired\" | null = null;\n let rowsRead = 0;\n let bytesRead = 0;\n const readDocCursors: Array<string> = [];\n\n for (let i = 0; i < sortedDocs.length; i++) {\n const doc = sortedDocs[i]!;\n const isLastDoc = i === sortedDocs.length - 1;\n if (!isInPage) {\n if (cursorKeys) {\n if (compareToCursor(doc, cursorKeys, cursorFields, order) > 0) {\n isInPage = true;\n } else {\n continue;\n }\n }\n }\n if (hasEndBoundary && endCursorKeys) {\n const cmp = compareToCursor(doc, endCursorKeys, cursorFields, order);\n if (cmp >= 0) {\n reachedEnd = false;\n examined(endCursorKeys);\n if (cmp === 0) {\n view.examine(interval.table, doc);\n rowsRead += 1;\n bytesRead += getDocumentSize(doc);\n readDocCursors.push(encodeCursor(doc, cursorFields));\n if (filterFn(doc)) page.push(doc);\n continueCursor = encodeCursor(doc, cursorFields);\n }\n break;\n }\n }\n view.examine(interval.table, doc);\n rowsRead += 1;\n bytesRead += getDocumentSize(doc);\n readDocCursors.push(encodeCursor(doc, cursorFields));\n examined(ReadSet.keyOf(cursorFields, doc as StoredDocument));\n const hitLimit =\n (!!maximumRowsRead && rowsRead >= maximumRowsRead) ||\n (!!maximumBytesRead && bytesRead >= maximumBytesRead);\n if (filterFn(doc)) page.push(doc);\n if (hitLimit) {\n reachedEnd = false;\n pageStatus = \"SplitRequired\";\n continueCursor = encodeCursor(doc, cursorFields);\n break;\n }\n if (!endCursor && page.length >= pageSize) {\n reachedEnd = isLastDoc;\n if (!isLastDoc) continueCursor = encodeCursor(doc, cursorFields);\n break;\n }\n }\n // A page bounded by an end cursor examined the range only up to it.\n if (reachedEnd) examined(endCursorKeys ?? \"end\");\n\n if (pageStatus === \"SplitRequired\" && readDocCursors.length >= 2) {\n splitCursor = readDocCursors[Math.floor((readDocCursors.length - 1) / 2)]!;\n } else if (\n pageStatus === null &&\n rowsRead > pageSize + 1 &&\n readDocCursors.length >= 2\n ) {\n pageStatus = \"SplitRecommended\";\n splitCursor = readDocCursors[Math.floor((readDocCursors.length - 1) / 2)]!;\n }\n\n return {\n page,\n isDone: continueCursor === \"_end_cursor\",\n continueCursor,\n splitCursor,\n pageStatus,\n };\n};\n"],"mappings":";;;;;AAqDA,MAAa,gBACX,WACwB;CACxB,IAAI,WAAW,QAAQ,WAAW,KAAA,GAAW,OAAO;CACpD,MAAM,WAAyB;EAC7B,kBAAmB,OAChB;EACH,QAAQ,IAAI,IACV,OAAO,QAAQ,OAAO,MAAM,CAAC,CAAC,KAAK,CAAC,MAAM,WAAW,CACnD,MACC,MAA2C,OAAO,CACrD,CAAC,CACH;CACF;CACA,SAAS,OAAO,SAAS,OAAO,cAAc;EAC5C,IAAI,CAAC,kBAAkB,SAAS,GAC9B,MAAM,IAAI,MACR,+CAA+C,UAAU,EAC3D;EAEF,mBAAmB,MAAM,YAAY;EACrC,MAAM,QAAQ,SAAS,EAAE,sBAAsB;GAC7C,IAAI,CAAC,kBAAkB,eAAe,GACpC,MAAM,IAAI,MACR,+CAA+C,gBAAgB,EACjE;EAEJ,CAAC;CACH,CAAC;CACD,OAAO;AACT;;;;;AAMA,MAAa,oBACX,QACA,WACA,QACG;CACH,IAAI,WAAW,QAAQ,CAAC,OAAO,kBAAkB;CACjD,MAAM,YAAY,OAAO,OAAO,IAAI,SAAS,CAAC,EAAE;CAChD,IAAI,cAAc,KAAA,GAAW;CAC7B,kBAAkB,WAAW,qBAAqB,GAAsB,CAAC;AAC3E;AAmCA,MAAM,iBACJ,MACA,UACmB;CACnB,MAAM,SAAS,MAAM;CACrB,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI,QAAkD,CAAC;CACvD,IAAI;CACJ,QAAQ,OAAO,MAAf;EACE,KAAK;GAC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AAQ,SAAS,OAAO,GAAG;EAChC,YAAY;EACZ,aAAa,gBAAgB,GAAG;EAChC,eAAe,KAAK,aAAa,KAAK,YAAY,CAAC;EACnD,SAASA,MAAc,cAAc,GAAqB,CAAC;EAC3D,MAAM,WACH,CAAC,CAAC,mBAAmB,YAAY,mBACjC,CAAC,CAAC,oBAAoB,aAAa;EACtC,IAAI,SAAS,GAAG,GAAG,KAAK,KAAK,GAAG;EAChC,IAAI,UAAU;GACZ,aAAa;GACb,aAAa;GACb,iBAAiB,aAAa,KAAK,YAAY;GAC/C;EACF;EACA,IAAI,CAAC,aAAa,KAAK,UAAU,UAAU;GACzC,aAAa;GACb,IAAI,CAAC,WAAW,iBAAiB,aAAa,KAAK,YAAY;GAC/D;EACF;CACF;CAEA,IAAI,YAAY,SAAS,iBAAiB,KAAK;CAE/C,IAAI,eAAe,mBAAmB,eAAe,UAAU,GAC7D,cAAc,eAAe,KAAK,OAAO,eAAe,SAAS,KAAK,CAAC;MAClE,IACL,eAAe,QACf,WAAW,WAAW,KACtB,eAAe,UAAU,GACzB;EACA,aAAa;EACb,cAAc,eAAe,KAAK,OAAO,eAAe,SAAS,KAAK,CAAC;CACzE;CAEA,OAAO;EACL;EACA,QAAQ,mBAAmB;EAC3B;EACA;EACA;CACF;AACF"}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import type { TransactionMetrics } from "convex/server";
|
|
2
|
+
import { type Value } from "convex/values";
|
|
3
|
+
import * as Schema from "effect/Schema";
|
|
4
|
+
import type { SerializedRangeExpression } from "./KernelValues.js";
|
|
5
|
+
/**
|
|
6
|
+
* The ceilings one function execution runs under, the binary's
|
|
7
|
+
* `TransactionLimits` (`crates/database/src/execution_size.rs:62-95`).
|
|
8
|
+
*/
|
|
9
|
+
export type Limits = {
|
|
10
|
+
readonly [K in keyof TransactionMetrics]: number;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The binary's default ceilings (`crates/common/src/knobs.rs`).
|
|
14
|
+
*/
|
|
15
|
+
export declare const defaults: Limits;
|
|
16
|
+
/**
|
|
17
|
+
* What a nested call may use on top of what its transaction has used, as
|
|
18
|
+
* `ctx.runQuery` and `ctx.runMutation` take it in `transactionLimits`.
|
|
19
|
+
*/
|
|
20
|
+
export type Budget = Partial<Limits>;
|
|
21
|
+
/**
|
|
22
|
+
* A nested call's ceilings: each one what the transaction has used plus the
|
|
23
|
+
* call's budget, never above the caller's (`TransactionLimits::from_budget`,
|
|
24
|
+
* execution_size.rs:97-160). An unset field leaves the caller's ceiling.
|
|
25
|
+
*/
|
|
26
|
+
export declare const fromBudget: (budget: Budget, used: Limits, ceiling: Limits) => Limits;
|
|
27
|
+
declare const TransactionLimitExceeded_base: Schema.Class<TransactionLimitExceeded, Schema.TaggedStruct<"TransactionLimitExceeded", {
|
|
28
|
+
readonly code: Schema.Literals<readonly ["TooManyReads", "TooManyDocumentsRead", "TooManyBytesRead", "TooManyWrites", "TooManyBytesWritten", "TooManyFunctionsScheduled", "ScheduledFunctionsArgumentsTooLarge"]>;
|
|
29
|
+
readonly limit: Schema.Int;
|
|
30
|
+
readonly message: Schema.String;
|
|
31
|
+
}>, import("effect/Cause").YieldableError>;
|
|
32
|
+
/**
|
|
33
|
+
* A function execution crossed one of its ceilings. `code` is the binary's
|
|
34
|
+
* short message and `message` its text, which the function receives as a
|
|
35
|
+
* catchable error.
|
|
36
|
+
*/
|
|
37
|
+
export declare class TransactionLimitExceeded extends TransactionLimitExceeded_base {
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* `common::fmt::format_bytes` (`crates/common/src/fmt.rs:29-47`).
|
|
41
|
+
*/
|
|
42
|
+
export declare const formatBytes: (n: number) => string;
|
|
43
|
+
/**
|
|
44
|
+
* A position in an index's key space: just before, or just after, every key
|
|
45
|
+
* that starts with `key`.
|
|
46
|
+
*/
|
|
47
|
+
interface Bound {
|
|
48
|
+
readonly key: ReadonlyArray<Value | undefined>;
|
|
49
|
+
readonly side: -1 | 1;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The keys from `start` up to `end`.
|
|
53
|
+
*/
|
|
54
|
+
export interface KeyInterval {
|
|
55
|
+
readonly start: Bound;
|
|
56
|
+
readonly end: Bound;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The one key `key`, as a read or write of a document by ID records it.
|
|
60
|
+
*/
|
|
61
|
+
export declare const point: (key: ReadonlyArray<Value | undefined>) => KeyInterval;
|
|
62
|
+
/**
|
|
63
|
+
* Every key of an index.
|
|
64
|
+
*/
|
|
65
|
+
export declare const everything: KeyInterval;
|
|
66
|
+
/**
|
|
67
|
+
* The part of an index range a query has examined: from where it starts, past
|
|
68
|
+
* the cursor `after`, through `through` in its order. `null` when it has
|
|
69
|
+
* examined nothing.
|
|
70
|
+
*/
|
|
71
|
+
export declare const examined: (range: ReadonlyArray<SerializedRangeExpression>, order: "asc" | "desc", after: ReadonlyArray<Value | undefined> | null, through: ReadonlyArray<Value | undefined> | "end") => KeyInterval | null;
|
|
72
|
+
/**
|
|
73
|
+
* The intervals read from one index, overlapping and adjacent ones merged
|
|
74
|
+
* (`IntervalSet::add`, `crates/common/src/interval/interval_set.rs:150-230`).
|
|
75
|
+
* They stay sorted and disjoint, so an add touches only its neighbours.
|
|
76
|
+
*/
|
|
77
|
+
declare class IntervalSet {
|
|
78
|
+
private readonly intervals;
|
|
79
|
+
get size(): number;
|
|
80
|
+
add(interval: KeyInterval): void;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The counters every frame of one attempt shares: reads and schedules stay when
|
|
84
|
+
* a nested mutation fails, as the binary keeps them outside its subtransaction
|
|
85
|
+
* token (`crates/database/src/transaction.rs:418-454`).
|
|
86
|
+
*/
|
|
87
|
+
declare class Shared {
|
|
88
|
+
readonly indexes: Map<string, IntervalSet>;
|
|
89
|
+
intervals: number;
|
|
90
|
+
documentsRead: number;
|
|
91
|
+
bytesRead: number;
|
|
92
|
+
functionsScheduled: number;
|
|
93
|
+
scheduledFunctionArgsBytes: number;
|
|
94
|
+
readonly filesRead = 0;
|
|
95
|
+
readonly fileReadBytes = 0;
|
|
96
|
+
/**
|
|
97
|
+
* A stale-snapshot query's counters: the scheduling and file-read counters
|
|
98
|
+
* carried over, reads and intervals from zero (`clone_for_snapshot_query`,
|
|
99
|
+
* `crates/database/src/transaction.rs:1460-1485`).
|
|
100
|
+
*/
|
|
101
|
+
forStaleSnapshot(): Shared;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* One frame's share of an attempt's accounting. The outermost frame belongs to
|
|
105
|
+
* the top-level function and each nested mutation gets a child. A child's
|
|
106
|
+
* writes count toward its caller's once it merges and are forgotten when it
|
|
107
|
+
* fails, as the binary's nested write set rolls its sizes back
|
|
108
|
+
* (`crates/database/src/writes.rs:96-136`).
|
|
109
|
+
*/
|
|
110
|
+
export declare class Meter {
|
|
111
|
+
private readonly shared;
|
|
112
|
+
private readonly parent?;
|
|
113
|
+
private documentsWritten;
|
|
114
|
+
private bytesWritten;
|
|
115
|
+
private readonly filesWritten;
|
|
116
|
+
private readonly fileWriteBytes;
|
|
117
|
+
constructor(shared?: Shared, parent?: Meter | undefined);
|
|
118
|
+
child(): Meter;
|
|
119
|
+
/**
|
|
120
|
+
* The meter of a query run on a stale snapshot. The binary runs it on a clone
|
|
121
|
+
* of the transaction that keeps the scheduling counters and starts reads and
|
|
122
|
+
* writes from zero, then drops the clone, so nothing it counts reaches this
|
|
123
|
+
* meter (`clone_for_snapshot_query`,
|
|
124
|
+
* `crates/database/src/transaction.rs:1460-1485`; `run_udf`,
|
|
125
|
+
* `crates/isolate/src/environment/udf/async_syscall.rs:695-776`).
|
|
126
|
+
*/
|
|
127
|
+
forStaleSnapshot(): Meter;
|
|
128
|
+
/**
|
|
129
|
+
* The child's writes become its caller's.
|
|
130
|
+
*/
|
|
131
|
+
merge(): void;
|
|
132
|
+
private chain;
|
|
133
|
+
/**
|
|
134
|
+
* How much of each limit the execution has used.
|
|
135
|
+
*/
|
|
136
|
+
used(): Limits;
|
|
137
|
+
/**
|
|
138
|
+
* What `getTransactionMetrics` answers (`tx_metrics`,
|
|
139
|
+
* `crates/isolate/src/environment/udf/async_syscall.rs:947-993`).
|
|
140
|
+
*/
|
|
141
|
+
metrics(limits: Limits): TransactionMetrics;
|
|
142
|
+
private set;
|
|
143
|
+
/**
|
|
144
|
+
* A read the function made of `interval` in `index`, counted as the binary
|
|
145
|
+
* counts it: the total drops by the index's interval count before the read,
|
|
146
|
+
* saturating at zero, and rises by its count after
|
|
147
|
+
* (`record_indexed_directly`, `crates/database/src/reads.rs:447-480`). The
|
|
148
|
+
* index's count includes the intervals of IDs this execution inserted, which
|
|
149
|
+
* joined it without counting (`deriveInterval`), so a read that covers them
|
|
150
|
+
* can move the total by more than the read itself. The binary starts the
|
|
151
|
+
* total at the system-table intervals it reads to run the function, which the
|
|
152
|
+
* kernel does not simulate. While neither total saturates they differ by
|
|
153
|
+
* exactly that share. A read that saturates only the kernel's lower total
|
|
154
|
+
* narrows the gap, and it never widens or turns negative (see the
|
|
155
|
+
* `SystemTableReadIntervals` boundary).
|
|
156
|
+
*/
|
|
157
|
+
readInterval(index: string, interval: KeyInterval, limits: Limits): TransactionLimitExceeded | undefined;
|
|
158
|
+
/**
|
|
159
|
+
* A read the binary takes for a write rather than for the function: a new
|
|
160
|
+
* document's ID (`register_new_id`, writes.rs:426-441). It joins the index's
|
|
161
|
+
* intervals and leaves the total as it was (`record_indexed_derived` calls
|
|
162
|
+
* `_record_indexed` and ignores its counts, reads.rs:333-344), so a later
|
|
163
|
+
* read of that index starts from a count that includes it.
|
|
164
|
+
*/
|
|
165
|
+
deriveInterval(index: string, interval: KeyInterval): void;
|
|
166
|
+
/**
|
|
167
|
+
* A document the function read. A system table's document counts toward no
|
|
168
|
+
* limit (`record_read_document`, reads.rs:365-445).
|
|
169
|
+
*/
|
|
170
|
+
readDocument(document: Value, system: boolean, limits: Limits): TransactionLimitExceeded | undefined;
|
|
171
|
+
/**
|
|
172
|
+
* A write to a user table: the new document, or `null` for a delete. Each
|
|
173
|
+
* write counts, even to a document this execution already wrote
|
|
174
|
+
* (`Writes::update`, writes.rs:241-310).
|
|
175
|
+
*/
|
|
176
|
+
write(document: Value | null, limits: Limits): TransactionLimitExceeded | undefined;
|
|
177
|
+
/**
|
|
178
|
+
* A function scheduled with `args`. The binary checks before it counts, so a
|
|
179
|
+
* refused schedule counts nothing (`check_scheduling_limits`,
|
|
180
|
+
* `crates/model/src/scheduled_jobs/mod.rs:162-196`). Its arguments are sized
|
|
181
|
+
* as the one-element array the scheduler stores.
|
|
182
|
+
*/
|
|
183
|
+
schedule(args: Value, limits: Limits): TransactionLimitExceeded | undefined;
|
|
184
|
+
}
|
|
185
|
+
export {};
|
|
186
|
+
//# sourceMappingURL=Metering.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Metering.d.ts","sourceRoot":"","sources":["../../src/internal/Metering.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAgC,KAAK,KAAK,EAAE,MAAM,eAAe,CAAC;AACzE,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAC;AAGnE;;;GAGG;AACH,MAAM,MAAM,MAAM,GAAG;IAAE,QAAQ,EAAE,CAAC,IAAI,MAAM,kBAAkB,GAAG,MAAM;CAAE,CAAC;AAE1E;;GAEG;AACH,eAAO,MAAM,QAAQ,EAAE,MAuBtB,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AAErC;;;;GAIG;AACH,eAAO,MAAM,UAAU,WACb,MAAM,QACR,MAAM,WACH,MAAM,KACd,MASqB,CAAC;;;;;;AAKzB;;;;GAIG;AACH,qBAAa,wBAAyB,SAAQ,6BAe7C;CAAG;AAWJ;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