2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-22 00:13:24 -04:00
|
|
|
|
2024-01-03 23:12:38 -05:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2024-01-10 17:37:25 -05:00
|
|
|
const {
|
|
|
|
isPromise,
|
|
|
|
} = core;
|
2024-01-26 17:46:46 -05:00
|
|
|
import {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_kv_atomic_write,
|
|
|
|
op_kv_database_open,
|
|
|
|
op_kv_dequeue_next_message,
|
|
|
|
op_kv_encode_cursor,
|
|
|
|
op_kv_finish_dequeued_message,
|
|
|
|
op_kv_snapshot_read,
|
|
|
|
op_kv_watch,
|
|
|
|
op_kv_watch_next,
|
2024-01-26 17:46:46 -05:00
|
|
|
} from "ext:core/ops";
|
2023-03-22 00:13:24 -04:00
|
|
|
const {
|
2024-01-07 17:20:02 -05:00
|
|
|
ArrayFrom,
|
|
|
|
ArrayPrototypeMap,
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeReverse,
|
|
|
|
ArrayPrototypeSlice,
|
2023-03-22 00:13:24 -04:00
|
|
|
AsyncGeneratorPrototype,
|
2024-01-07 17:20:02 -05:00
|
|
|
BigInt,
|
2023-05-03 07:44:00 -04:00
|
|
|
BigIntPrototypeToString,
|
2024-01-07 17:20:02 -05:00
|
|
|
Error,
|
|
|
|
NumberIsNaN,
|
|
|
|
Object,
|
2023-05-03 07:44:00 -04:00
|
|
|
ObjectFreeze,
|
|
|
|
ObjectGetPrototypeOf,
|
2024-01-07 17:20:02 -05:00
|
|
|
ObjectHasOwn,
|
2023-05-03 07:44:00 -04:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2024-01-07 17:20:02 -05:00
|
|
|
RangeError,
|
|
|
|
SafeMap,
|
|
|
|
SafeMapIterator,
|
2023-05-03 07:44:00 -04:00
|
|
|
StringPrototypeReplace,
|
2024-01-07 17:20:02 -05:00
|
|
|
Symbol,
|
|
|
|
SymbolAsyncIterator,
|
2023-05-03 07:44:00 -04:00
|
|
|
SymbolFor,
|
|
|
|
SymbolToStringTag,
|
2024-01-07 17:20:02 -05:00
|
|
|
TypeError,
|
2024-01-03 23:12:38 -05:00
|
|
|
TypedArrayPrototypeGetSymbolToStringTag,
|
|
|
|
} = primordials;
|
2024-01-10 17:37:25 -05:00
|
|
|
|
|
|
|
import { SymbolDispose } from "ext:deno_web/00_infra.js";
|
|
|
|
import { ReadableStream } from "ext:deno_web/06_streams.js";
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
const encodeCursor: (
|
|
|
|
selector: [Deno.KvKey | null, Deno.KvKey | null, Deno.KvKey | null],
|
|
|
|
boundaryKey: Deno.KvKey,
|
|
|
|
) => string = (selector, boundaryKey) =>
|
2024-01-03 23:12:38 -05:00
|
|
|
op_kv_encode_cursor(selector, boundaryKey);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
2023-03-22 15:23:36 -04:00
|
|
|
async function openKv(path: string) {
|
2023-12-26 20:30:26 -05:00
|
|
|
const rid = await op_kv_database_open(path);
|
2023-05-03 17:08:42 -04:00
|
|
|
return new Kv(rid, kvSymbol);
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
|
2023-09-22 12:40:35 -04:00
|
|
|
const maxQueueDelay = 30 * 24 * 60 * 60 * 1000;
|
2023-06-13 20:49:57 -04:00
|
|
|
|
|
|
|
function validateQueueDelay(delay: number) {
|
|
|
|
if (delay < 0) {
|
|
|
|
throw new TypeError("delay cannot be negative");
|
|
|
|
}
|
2023-09-22 12:40:35 -04:00
|
|
|
if (delay > maxQueueDelay) {
|
|
|
|
throw new TypeError("delay cannot be greater than 30 days");
|
2023-06-13 20:49:57 -04:00
|
|
|
}
|
2024-01-07 17:20:02 -05:00
|
|
|
if (NumberIsNaN(delay)) {
|
2023-06-13 20:49:57 -04:00
|
|
|
throw new TypeError("delay cannot be NaN");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 01:51:23 -05:00
|
|
|
const maxQueueBackoffIntervals = 5;
|
|
|
|
const maxQueueBackoffInterval = 60 * 60 * 1000;
|
|
|
|
|
|
|
|
function validateBackoffSchedule(backoffSchedule: number[]) {
|
|
|
|
if (backoffSchedule.length > maxQueueBackoffIntervals) {
|
|
|
|
throw new TypeError("invalid backoffSchedule");
|
|
|
|
}
|
2024-01-07 17:20:02 -05:00
|
|
|
for (let i = 0; i < backoffSchedule.length; ++i) {
|
|
|
|
const interval = backoffSchedule[i];
|
|
|
|
if (
|
|
|
|
interval < 0 || interval > maxQueueBackoffInterval ||
|
|
|
|
NumberIsNaN(interval)
|
|
|
|
) {
|
2023-12-13 01:51:23 -05:00
|
|
|
throw new TypeError("invalid backoffSchedule");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
interface RawKvEntry {
|
|
|
|
key: Deno.KvKey;
|
|
|
|
value: RawValue;
|
|
|
|
versionstamp: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
type RawValue = {
|
|
|
|
kind: "v8";
|
|
|
|
value: Uint8Array;
|
|
|
|
} | {
|
|
|
|
kind: "bytes";
|
|
|
|
value: Uint8Array;
|
|
|
|
} | {
|
|
|
|
kind: "u64";
|
|
|
|
value: bigint;
|
|
|
|
};
|
|
|
|
|
2023-05-03 17:08:42 -04:00
|
|
|
const kvSymbol = Symbol("KvRid");
|
2023-12-13 11:58:20 -05:00
|
|
|
const commitVersionstampSymbol = Symbol("KvCommitVersionstamp");
|
2023-05-03 17:08:42 -04:00
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
class Kv {
|
|
|
|
#rid: number;
|
2023-12-05 13:16:47 -05:00
|
|
|
#isClosed: boolean;
|
2023-03-22 00:13:24 -04:00
|
|
|
|
2023-05-03 17:08:42 -04:00
|
|
|
constructor(rid: number = undefined, symbol: symbol = undefined) {
|
|
|
|
if (kvSymbol !== symbol) {
|
|
|
|
throw new TypeError(
|
|
|
|
"Deno.Kv can not be constructed, use Deno.openKv instead.",
|
|
|
|
);
|
|
|
|
}
|
2023-03-22 00:13:24 -04:00
|
|
|
this.#rid = rid;
|
2023-12-05 13:16:47 -05:00
|
|
|
this.#isClosed = false;
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
atomic() {
|
|
|
|
return new AtomicOperation(this.#rid);
|
|
|
|
}
|
|
|
|
|
2023-12-13 11:58:20 -05:00
|
|
|
commitVersionstamp(): symbol {
|
|
|
|
return commitVersionstampSymbol;
|
|
|
|
}
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
async get(key: Deno.KvKey, opts?: { consistency?: Deno.KvConsistencyLevel }) {
|
2024-01-07 17:20:02 -05:00
|
|
|
const { 0: entries }: [RawKvEntry[]] = await op_kv_snapshot_read(
|
2023-03-22 00:13:24 -04:00
|
|
|
this.#rid,
|
|
|
|
[[
|
|
|
|
null,
|
|
|
|
key,
|
|
|
|
null,
|
|
|
|
1,
|
|
|
|
false,
|
|
|
|
null,
|
|
|
|
]],
|
|
|
|
opts?.consistency ?? "strong",
|
|
|
|
);
|
|
|
|
if (!entries.length) {
|
|
|
|
return {
|
|
|
|
key,
|
|
|
|
value: null,
|
|
|
|
versionstamp: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return deserializeValue(entries[0]);
|
|
|
|
}
|
|
|
|
|
2023-03-24 08:06:27 -04:00
|
|
|
async getMany(
|
|
|
|
keys: Deno.KvKey[],
|
|
|
|
opts?: { consistency?: Deno.KvConsistencyLevel },
|
2023-03-30 16:52:31 -04:00
|
|
|
): Promise<Deno.KvEntry<unknown>[]> {
|
2023-12-26 20:30:26 -05:00
|
|
|
const ranges: RawKvEntry[][] = await op_kv_snapshot_read(
|
2023-03-24 08:06:27 -04:00
|
|
|
this.#rid,
|
2024-01-07 17:20:02 -05:00
|
|
|
ArrayPrototypeMap(keys, (key: Deno.KvKey) => [
|
2023-03-24 08:06:27 -04:00
|
|
|
null,
|
|
|
|
key,
|
|
|
|
null,
|
|
|
|
1,
|
|
|
|
false,
|
|
|
|
null,
|
|
|
|
]),
|
|
|
|
opts?.consistency ?? "strong",
|
|
|
|
);
|
2024-01-07 17:20:02 -05:00
|
|
|
return ArrayPrototypeMap(ranges, (entries: RawKvEntry[], i: number) => {
|
2023-03-24 08:06:27 -04:00
|
|
|
if (!entries.length) {
|
|
|
|
return {
|
|
|
|
key: keys[i],
|
|
|
|
value: null,
|
|
|
|
versionstamp: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return deserializeValue(entries[0]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-18 05:34:16 -04:00
|
|
|
async set(key: Deno.KvKey, value: unknown, options?: { expireIn?: number }) {
|
2023-12-13 11:58:20 -05:00
|
|
|
const versionstamp = await doAtomicWriteInPlace(
|
2023-03-22 00:13:24 -04:00
|
|
|
this.#rid,
|
2023-12-13 11:58:20 -05:00
|
|
|
[],
|
|
|
|
[[key, "set", serializeValue(value), options?.expireIn]],
|
2023-03-22 00:13:24 -04:00
|
|
|
[],
|
|
|
|
);
|
2023-03-30 14:57:21 -04:00
|
|
|
if (versionstamp === null) throw new TypeError("Failed to set value");
|
2023-04-27 10:59:02 -04:00
|
|
|
return { ok: true, versionstamp };
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async delete(key: Deno.KvKey) {
|
2023-12-13 11:58:20 -05:00
|
|
|
const result = await doAtomicWriteInPlace(
|
2023-03-22 00:13:24 -04:00
|
|
|
this.#rid,
|
2023-12-13 11:58:20 -05:00
|
|
|
[],
|
|
|
|
[[key, "delete", null, undefined]],
|
2023-03-22 00:13:24 -04:00
|
|
|
[],
|
|
|
|
);
|
|
|
|
if (!result) throw new TypeError("Failed to set value");
|
|
|
|
}
|
|
|
|
|
|
|
|
list(
|
|
|
|
selector: Deno.KvListSelector,
|
|
|
|
options: {
|
|
|
|
limit?: number;
|
|
|
|
batchSize?: number;
|
|
|
|
cursor?: string;
|
|
|
|
reverse?: boolean;
|
|
|
|
consistency?: Deno.KvConsistencyLevel;
|
2024-05-22 18:03:35 -04:00
|
|
|
} = { __proto__: null },
|
2023-03-22 00:13:24 -04:00
|
|
|
): KvListIterator {
|
|
|
|
if (options.limit !== undefined && options.limit <= 0) {
|
|
|
|
throw new Error("limit must be positive");
|
|
|
|
}
|
|
|
|
|
|
|
|
let batchSize = options.batchSize ?? (options.limit ?? 100);
|
|
|
|
if (batchSize <= 0) throw new Error("batchSize must be positive");
|
2023-03-25 03:29:36 -04:00
|
|
|
if (options.batchSize === undefined && batchSize > 500) batchSize = 500;
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
return new KvListIterator({
|
|
|
|
limit: options.limit,
|
|
|
|
selector,
|
|
|
|
cursor: options.cursor,
|
|
|
|
reverse: options.reverse ?? false,
|
|
|
|
consistency: options.consistency ?? "strong",
|
|
|
|
batchSize,
|
|
|
|
pullBatch: this.#pullBatch(batchSize),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#pullBatch(batchSize: number): (
|
|
|
|
selector: Deno.KvListSelector,
|
|
|
|
cursor: string | undefined,
|
|
|
|
reverse: boolean,
|
|
|
|
consistency: Deno.KvConsistencyLevel,
|
2023-03-30 16:52:31 -04:00
|
|
|
) => Promise<Deno.KvEntry<unknown>[]> {
|
2023-03-22 00:13:24 -04:00
|
|
|
return async (selector, cursor, reverse, consistency) => {
|
2024-01-07 17:20:02 -05:00
|
|
|
const { 0: entries }: [RawKvEntry[]] = await op_kv_snapshot_read(
|
2023-03-22 00:13:24 -04:00
|
|
|
this.#rid,
|
|
|
|
[[
|
2024-01-07 17:20:02 -05:00
|
|
|
ObjectHasOwn(selector, "prefix") ? selector.prefix : null,
|
|
|
|
ObjectHasOwn(selector, "start") ? selector.start : null,
|
|
|
|
ObjectHasOwn(selector, "end") ? selector.end : null,
|
2023-03-22 00:13:24 -04:00
|
|
|
batchSize,
|
|
|
|
reverse,
|
|
|
|
cursor,
|
|
|
|
]],
|
|
|
|
consistency,
|
|
|
|
);
|
|
|
|
|
2024-01-07 17:20:02 -05:00
|
|
|
return ArrayPrototypeMap(entries, deserializeValue);
|
2023-03-22 00:13:24 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-06-13 20:49:57 -04:00
|
|
|
async enqueue(
|
|
|
|
message: unknown,
|
2023-12-13 01:51:23 -05:00
|
|
|
opts?: {
|
|
|
|
delay?: number;
|
|
|
|
keysIfUndelivered?: Deno.KvKey[];
|
|
|
|
backoffSchedule?: number[];
|
|
|
|
},
|
2023-06-13 20:49:57 -04:00
|
|
|
) {
|
|
|
|
if (opts?.delay !== undefined) {
|
|
|
|
validateQueueDelay(opts?.delay);
|
|
|
|
}
|
2023-12-13 01:51:23 -05:00
|
|
|
if (opts?.backoffSchedule !== undefined) {
|
|
|
|
validateBackoffSchedule(opts?.backoffSchedule);
|
|
|
|
}
|
2023-06-13 20:49:57 -04:00
|
|
|
|
2023-12-13 11:58:20 -05:00
|
|
|
const versionstamp = await doAtomicWriteInPlace(
|
2023-06-13 20:49:57 -04:00
|
|
|
this.#rid,
|
|
|
|
[],
|
|
|
|
[],
|
2023-12-13 11:58:20 -05:00
|
|
|
[
|
|
|
|
[
|
|
|
|
core.serialize(message, { forStorage: true }),
|
|
|
|
opts?.delay ?? 0,
|
|
|
|
opts?.keysIfUndelivered ?? [],
|
|
|
|
opts?.backoffSchedule ?? null,
|
|
|
|
],
|
|
|
|
],
|
2023-06-13 20:49:57 -04:00
|
|
|
);
|
|
|
|
if (versionstamp === null) throw new TypeError("Failed to enqueue value");
|
|
|
|
return { ok: true, versionstamp };
|
|
|
|
}
|
|
|
|
|
|
|
|
async listenQueue(
|
|
|
|
handler: (message: unknown) => Promise<void> | void,
|
|
|
|
): Promise<void> {
|
2023-12-05 13:16:47 -05:00
|
|
|
if (this.#isClosed) {
|
|
|
|
throw new Error("already closed");
|
|
|
|
}
|
2024-01-07 17:20:02 -05:00
|
|
|
const finishMessageOps = new SafeMap<number, Promise<void>>();
|
2023-09-26 23:06:57 -04:00
|
|
|
while (true) {
|
2023-06-13 20:49:57 -04:00
|
|
|
// Wait for the next message.
|
2023-12-26 20:30:26 -05:00
|
|
|
const next: { 0: Uint8Array; 1: number } =
|
|
|
|
await op_kv_dequeue_next_message(
|
|
|
|
this.#rid,
|
|
|
|
);
|
2023-09-26 23:06:57 -04:00
|
|
|
if (next === null) {
|
|
|
|
break;
|
2023-06-13 20:49:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize the payload.
|
|
|
|
const { 0: payload, 1: handleId } = next;
|
|
|
|
const deserializedPayload = core.deserialize(payload, {
|
|
|
|
forStorage: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Dispatch the payload.
|
|
|
|
(async () => {
|
|
|
|
let success = false;
|
|
|
|
try {
|
|
|
|
const result = handler(deserializedPayload);
|
2024-01-07 17:20:02 -05:00
|
|
|
const _res = isPromise(result) ? (await result) : result;
|
2023-06-13 20:49:57 -04:00
|
|
|
success = true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Exception in queue handler", error);
|
|
|
|
} finally {
|
2023-12-26 20:30:26 -05:00
|
|
|
const promise: Promise<void> = op_kv_finish_dequeued_message(
|
2023-09-26 23:06:57 -04:00
|
|
|
handleId,
|
|
|
|
success,
|
|
|
|
);
|
|
|
|
finishMessageOps.set(handleId, promise);
|
|
|
|
try {
|
|
|
|
await promise;
|
|
|
|
} finally {
|
|
|
|
finishMessageOps.delete(handleId);
|
2023-06-17 18:02:32 -04:00
|
|
|
}
|
2023-06-13 20:49:57 -04:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
2023-06-17 18:02:32 -04:00
|
|
|
|
2024-01-07 17:20:02 -05:00
|
|
|
for (const { 1: promise } of new SafeMapIterator(finishMessageOps)) {
|
2023-06-17 18:02:32 -04:00
|
|
|
await promise;
|
|
|
|
}
|
|
|
|
finishMessageOps.clear();
|
2023-06-13 20:49:57 -04:00
|
|
|
}
|
|
|
|
|
2024-05-22 18:03:35 -04:00
|
|
|
watch(keys: Deno.KvKey[], options = { __proto__: null }) {
|
2023-12-05 08:21:46 -05:00
|
|
|
const raw = options.raw ?? false;
|
2024-01-03 23:12:38 -05:00
|
|
|
const rid = op_kv_watch(this.#rid, keys);
|
2024-01-07 17:20:02 -05:00
|
|
|
const lastEntries: (Deno.KvEntryMaybe<unknown> | undefined)[] = ArrayFrom(
|
2023-12-05 08:21:46 -05:00
|
|
|
{ length: keys.length },
|
|
|
|
);
|
|
|
|
return new ReadableStream({
|
|
|
|
async pull(controller) {
|
|
|
|
while (true) {
|
|
|
|
let updates;
|
|
|
|
try {
|
2023-12-26 20:30:26 -05:00
|
|
|
updates = await op_kv_watch_next(rid);
|
2023-12-05 08:21:46 -05:00
|
|
|
} catch (err) {
|
|
|
|
core.tryClose(rid);
|
|
|
|
controller.error(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (updates === null) {
|
|
|
|
core.tryClose(rid);
|
|
|
|
controller.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let changed = false;
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
if (updates[i] === "unchanged") {
|
|
|
|
if (lastEntries[i] === undefined) {
|
|
|
|
throw new Error(
|
|
|
|
"watch: invalid unchanged update (internal error)",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
lastEntries[i] !== undefined &&
|
|
|
|
(updates[i]?.versionstamp ?? null) ===
|
|
|
|
lastEntries[i]?.versionstamp
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
changed = true;
|
|
|
|
if (updates[i] === null) {
|
|
|
|
lastEntries[i] = {
|
2024-01-07 17:20:02 -05:00
|
|
|
key: ArrayPrototypeSlice(keys[i]),
|
2023-12-05 08:21:46 -05:00
|
|
|
value: null,
|
|
|
|
versionstamp: null,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
lastEntries[i] = updates[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!changed && !raw) continue; // no change
|
2024-01-07 17:20:02 -05:00
|
|
|
const entries = ArrayPrototypeMap(
|
|
|
|
lastEntries,
|
|
|
|
(entry) =>
|
|
|
|
entry.versionstamp === null
|
|
|
|
? { ...entry }
|
|
|
|
: deserializeValue(entry),
|
2023-12-05 08:21:46 -05:00
|
|
|
);
|
|
|
|
controller.enqueue(entries);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
cancel() {
|
|
|
|
core.tryClose(rid);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
close() {
|
|
|
|
core.close(this.#rid);
|
2023-12-05 13:16:47 -05:00
|
|
|
this.#isClosed = true;
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
2023-11-01 15:26:12 -04:00
|
|
|
|
|
|
|
[SymbolDispose]() {
|
|
|
|
core.tryClose(this.#rid);
|
|
|
|
}
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class AtomicOperation {
|
|
|
|
#rid: number;
|
|
|
|
|
|
|
|
#checks: [Deno.KvKey, string | null][] = [];
|
2023-08-18 05:34:16 -04:00
|
|
|
#mutations: [Deno.KvKey, string, RawValue | null, number | undefined][] = [];
|
2023-06-13 20:49:57 -04:00
|
|
|
#enqueues: [Uint8Array, number, Deno.KvKey[], number[] | null][] = [];
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
constructor(rid: number) {
|
|
|
|
this.#rid = rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
check(...checks: Deno.AtomicCheck[]): this {
|
2024-01-07 17:20:02 -05:00
|
|
|
for (let i = 0; i < checks.length; ++i) {
|
|
|
|
const check = checks[i];
|
|
|
|
ArrayPrototypePush(this.#checks, [check.key, check.versionstamp]);
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutate(...mutations: Deno.KvMutation[]): this {
|
2024-01-07 17:20:02 -05:00
|
|
|
for (let i = 0; i < mutations.length; ++i) {
|
|
|
|
const mutation = mutations[i];
|
2023-04-11 10:36:01 -04:00
|
|
|
const key = mutation.key;
|
2023-03-22 00:13:24 -04:00
|
|
|
let type: string;
|
|
|
|
let value: RawValue | null;
|
2023-09-08 11:20:28 -04:00
|
|
|
let expireIn: number | undefined = undefined;
|
2023-03-22 00:13:24 -04:00
|
|
|
switch (mutation.type) {
|
|
|
|
case "delete":
|
|
|
|
type = "delete";
|
|
|
|
if (mutation.value) {
|
|
|
|
throw new TypeError("invalid mutation 'delete' with value");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "set":
|
2023-08-18 05:34:16 -04:00
|
|
|
if (typeof mutation.expireIn === "number") {
|
2023-09-08 11:20:28 -04:00
|
|
|
expireIn = mutation.expireIn;
|
2023-08-18 05:34:16 -04:00
|
|
|
}
|
|
|
|
/* falls through */
|
2023-03-22 00:13:24 -04:00
|
|
|
case "sum":
|
|
|
|
case "min":
|
|
|
|
case "max":
|
|
|
|
type = mutation.type;
|
2024-01-07 17:20:02 -05:00
|
|
|
if (!ObjectHasOwn(mutation, "value")) {
|
2023-03-22 00:13:24 -04:00
|
|
|
throw new TypeError(`invalid mutation '${type}' without value`);
|
|
|
|
}
|
|
|
|
value = serializeValue(mutation.value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new TypeError("Invalid mutation type");
|
|
|
|
}
|
2024-01-07 17:20:02 -05:00
|
|
|
ArrayPrototypePush(this.#mutations, [key, type, value, expireIn]);
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-04-26 13:14:01 -04:00
|
|
|
sum(key: Deno.KvKey, n: bigint): this {
|
2024-01-07 17:20:02 -05:00
|
|
|
ArrayPrototypePush(this.#mutations, [
|
|
|
|
key,
|
|
|
|
"sum",
|
|
|
|
serializeValue(new KvU64(n)),
|
|
|
|
undefined,
|
|
|
|
]);
|
2023-04-26 13:14:01 -04:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
min(key: Deno.KvKey, n: bigint): this {
|
2024-01-07 17:20:02 -05:00
|
|
|
ArrayPrototypePush(this.#mutations, [
|
|
|
|
key,
|
|
|
|
"min",
|
|
|
|
serializeValue(new KvU64(n)),
|
|
|
|
undefined,
|
|
|
|
]);
|
2023-04-26 13:14:01 -04:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
max(key: Deno.KvKey, n: bigint): this {
|
2024-01-07 17:20:02 -05:00
|
|
|
ArrayPrototypePush(this.#mutations, [
|
|
|
|
key,
|
|
|
|
"max",
|
|
|
|
serializeValue(new KvU64(n)),
|
|
|
|
undefined,
|
|
|
|
]);
|
2023-04-26 13:14:01 -04:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-08-18 05:34:16 -04:00
|
|
|
set(
|
|
|
|
key: Deno.KvKey,
|
|
|
|
value: unknown,
|
|
|
|
options?: { expireIn?: number },
|
|
|
|
): this {
|
2024-01-07 17:20:02 -05:00
|
|
|
ArrayPrototypePush(this.#mutations, [
|
2023-09-08 11:20:28 -04:00
|
|
|
key,
|
|
|
|
"set",
|
|
|
|
serializeValue(value),
|
|
|
|
options?.expireIn,
|
|
|
|
]);
|
2023-03-22 00:13:24 -04:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(key: Deno.KvKey): this {
|
2024-01-07 17:20:02 -05:00
|
|
|
ArrayPrototypePush(this.#mutations, [key, "delete", null, undefined]);
|
2023-03-22 00:13:24 -04:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-06-13 20:49:57 -04:00
|
|
|
enqueue(
|
|
|
|
message: unknown,
|
2023-12-13 01:51:23 -05:00
|
|
|
opts?: {
|
|
|
|
delay?: number;
|
|
|
|
keysIfUndelivered?: Deno.KvKey[];
|
|
|
|
backoffSchedule?: number[];
|
|
|
|
},
|
2023-06-13 20:49:57 -04:00
|
|
|
): this {
|
|
|
|
if (opts?.delay !== undefined) {
|
|
|
|
validateQueueDelay(opts?.delay);
|
|
|
|
}
|
2023-12-13 01:51:23 -05:00
|
|
|
if (opts?.backoffSchedule !== undefined) {
|
|
|
|
validateBackoffSchedule(opts?.backoffSchedule);
|
|
|
|
}
|
2024-01-07 17:20:02 -05:00
|
|
|
ArrayPrototypePush(this.#enqueues, [
|
2023-06-13 20:49:57 -04:00
|
|
|
core.serialize(message, { forStorage: true }),
|
|
|
|
opts?.delay ?? 0,
|
|
|
|
opts?.keysIfUndelivered ?? [],
|
2023-12-13 01:51:23 -05:00
|
|
|
opts?.backoffSchedule ?? null,
|
2023-06-13 20:49:57 -04:00
|
|
|
]);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-04-27 10:59:02 -04:00
|
|
|
async commit(): Promise<Deno.KvCommitResult | Deno.KvCommitError> {
|
2023-12-13 11:58:20 -05:00
|
|
|
const versionstamp = await doAtomicWriteInPlace(
|
2023-03-22 00:13:24 -04:00
|
|
|
this.#rid,
|
|
|
|
this.#checks,
|
|
|
|
this.#mutations,
|
2023-06-13 20:49:57 -04:00
|
|
|
this.#enqueues,
|
2023-03-22 00:13:24 -04:00
|
|
|
);
|
2023-04-27 10:59:02 -04:00
|
|
|
if (versionstamp === null) return { ok: false };
|
|
|
|
return { ok: true, versionstamp };
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
then() {
|
|
|
|
throw new TypeError(
|
|
|
|
"`Deno.AtomicOperation` is not a promise. Did you forget to call `commit()`?",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 16:43:39 -04:00
|
|
|
const MIN_U64 = BigInt("0");
|
|
|
|
const MAX_U64 = BigInt("0xffffffffffffffff");
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
class KvU64 {
|
2023-05-03 07:44:00 -04:00
|
|
|
value: bigint;
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
constructor(value: bigint) {
|
|
|
|
if (typeof value !== "bigint") {
|
|
|
|
throw new TypeError("value must be a bigint");
|
|
|
|
}
|
|
|
|
if (value < MIN_U64) {
|
|
|
|
throw new RangeError("value must be a positive bigint");
|
|
|
|
}
|
|
|
|
if (value > MAX_U64) {
|
2023-05-03 07:44:00 -04:00
|
|
|
throw new RangeError("value must fit in a 64-bit unsigned integer");
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
this.value = value;
|
2024-01-07 17:20:02 -05:00
|
|
|
ObjectFreeze(this);
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
2023-05-03 07:44:00 -04:00
|
|
|
|
|
|
|
valueOf() {
|
|
|
|
return this.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
return BigIntPrototypeToString(this.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
get [SymbolToStringTag]() {
|
|
|
|
return "Deno.KvU64";
|
|
|
|
}
|
|
|
|
|
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
|
|
|
return StringPrototypeReplace(
|
|
|
|
inspect(Object(this.value), inspectOptions),
|
|
|
|
"BigInt",
|
|
|
|
"Deno.KvU64",
|
|
|
|
);
|
|
|
|
}
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
|
2023-03-30 16:52:31 -04:00
|
|
|
function deserializeValue(entry: RawKvEntry): Deno.KvEntry<unknown> {
|
2023-03-22 00:13:24 -04:00
|
|
|
const { kind, value } = entry.value;
|
|
|
|
switch (kind) {
|
|
|
|
case "v8":
|
|
|
|
return {
|
|
|
|
...entry,
|
2023-04-29 11:43:07 -04:00
|
|
|
value: core.deserialize(value, { forStorage: true }),
|
2023-03-22 00:13:24 -04:00
|
|
|
};
|
|
|
|
case "bytes":
|
|
|
|
return {
|
|
|
|
...entry,
|
|
|
|
value,
|
|
|
|
};
|
|
|
|
case "u64":
|
|
|
|
return {
|
|
|
|
...entry,
|
|
|
|
value: new KvU64(value),
|
|
|
|
};
|
|
|
|
default:
|
|
|
|
throw new TypeError("Invalid value type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function serializeValue(value: unknown): RawValue {
|
2024-01-03 23:12:38 -05:00
|
|
|
if (TypedArrayPrototypeGetSymbolToStringTag(value) === "Uint8Array") {
|
2023-03-22 00:13:24 -04:00
|
|
|
return {
|
|
|
|
kind: "bytes",
|
|
|
|
value,
|
|
|
|
};
|
2023-05-03 07:44:00 -04:00
|
|
|
} else if (ObjectPrototypeIsPrototypeOf(KvU64.prototype, value)) {
|
2023-03-22 00:13:24 -04:00
|
|
|
return {
|
|
|
|
kind: "u64",
|
2024-01-07 17:20:02 -05:00
|
|
|
// deno-lint-ignore prefer-primordials
|
2023-05-03 07:44:00 -04:00
|
|
|
value: value.valueOf(),
|
2023-03-22 00:13:24 -04:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
kind: "v8",
|
2023-04-29 11:43:07 -04:00
|
|
|
value: core.serialize(value, { forStorage: true }),
|
2023-03-22 00:13:24 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This gets the %AsyncIteratorPrototype% object (which exists but is not a
|
|
|
|
// global). We extend the KvListIterator iterator from, so that we immediately
|
|
|
|
// support async iterator helpers once they land. The %AsyncIterator% does not
|
|
|
|
// yet actually exist however, so right now the AsyncIterator binding refers to
|
|
|
|
// %Object%. I know.
|
|
|
|
// Once AsyncIterator is a global, we can just use it (from primordials), rather
|
|
|
|
// than doing this here.
|
|
|
|
const AsyncIteratorPrototype = ObjectGetPrototypeOf(AsyncGeneratorPrototype);
|
|
|
|
const AsyncIterator = AsyncIteratorPrototype.constructor;
|
|
|
|
|
|
|
|
class KvListIterator extends AsyncIterator
|
2023-03-30 16:52:31 -04:00
|
|
|
implements AsyncIterator<Deno.KvEntry<unknown>> {
|
2023-03-22 00:13:24 -04:00
|
|
|
#selector: Deno.KvListSelector;
|
2023-03-30 16:52:31 -04:00
|
|
|
#entries: Deno.KvEntry<unknown>[] | null = null;
|
2023-03-22 00:13:24 -04:00
|
|
|
#cursorGen: (() => string) | null = null;
|
|
|
|
#done = false;
|
|
|
|
#lastBatch = false;
|
|
|
|
#pullBatch: (
|
|
|
|
selector: Deno.KvListSelector,
|
|
|
|
cursor: string | undefined,
|
|
|
|
reverse: boolean,
|
|
|
|
consistency: Deno.KvConsistencyLevel,
|
2023-03-30 16:52:31 -04:00
|
|
|
) => Promise<Deno.KvEntry<unknown>[]>;
|
2023-03-22 00:13:24 -04:00
|
|
|
#limit: number | undefined;
|
|
|
|
#count = 0;
|
|
|
|
#reverse: boolean;
|
|
|
|
#batchSize: number;
|
|
|
|
#consistency: Deno.KvConsistencyLevel;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
{ limit, selector, cursor, reverse, consistency, batchSize, pullBatch }: {
|
|
|
|
limit?: number;
|
|
|
|
selector: Deno.KvListSelector;
|
|
|
|
cursor?: string;
|
|
|
|
reverse: boolean;
|
|
|
|
batchSize: number;
|
|
|
|
consistency: Deno.KvConsistencyLevel;
|
|
|
|
pullBatch: (
|
|
|
|
selector: Deno.KvListSelector,
|
|
|
|
cursor: string | undefined,
|
|
|
|
reverse: boolean,
|
|
|
|
consistency: Deno.KvConsistencyLevel,
|
2023-03-30 16:52:31 -04:00
|
|
|
) => Promise<Deno.KvEntry<unknown>[]>;
|
2023-03-22 00:13:24 -04:00
|
|
|
},
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
let prefix: Deno.KvKey | undefined;
|
|
|
|
let start: Deno.KvKey | undefined;
|
|
|
|
let end: Deno.KvKey | undefined;
|
2024-01-07 17:20:02 -05:00
|
|
|
if (ObjectHasOwn(selector, "prefix") && selector.prefix !== undefined) {
|
|
|
|
prefix = ObjectFreeze(ArrayPrototypeSlice(selector.prefix));
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
2024-01-07 17:20:02 -05:00
|
|
|
if (ObjectHasOwn(selector, "start") && selector.start !== undefined) {
|
|
|
|
start = ObjectFreeze(ArrayPrototypeSlice(selector.start));
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
2024-01-07 17:20:02 -05:00
|
|
|
if (ObjectHasOwn(selector, "end") && selector.end !== undefined) {
|
|
|
|
end = ObjectFreeze(ArrayPrototypeSlice(selector.end));
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
if (prefix) {
|
|
|
|
if (start && end) {
|
|
|
|
throw new TypeError(
|
|
|
|
"Selector can not specify both 'start' and 'end' key when specifying 'prefix'.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (start) {
|
|
|
|
this.#selector = { prefix, start };
|
|
|
|
} else if (end) {
|
|
|
|
this.#selector = { prefix, end };
|
|
|
|
} else {
|
|
|
|
this.#selector = { prefix };
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (start && end) {
|
|
|
|
this.#selector = { start, end };
|
|
|
|
} else {
|
|
|
|
throw new TypeError(
|
|
|
|
"Selector must specify either 'prefix' or both 'start' and 'end' key.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-01-07 17:20:02 -05:00
|
|
|
ObjectFreeze(this.#selector);
|
2023-03-22 00:13:24 -04:00
|
|
|
this.#pullBatch = pullBatch;
|
|
|
|
this.#limit = limit;
|
|
|
|
this.#reverse = reverse;
|
|
|
|
this.#consistency = consistency;
|
|
|
|
this.#batchSize = batchSize;
|
|
|
|
this.#cursorGen = cursor ? () => cursor : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
get cursor(): string {
|
|
|
|
if (this.#cursorGen === null) {
|
|
|
|
throw new Error("Cannot get cursor before first iteration");
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.#cursorGen();
|
|
|
|
}
|
|
|
|
|
2023-03-30 16:52:31 -04:00
|
|
|
async next(): Promise<IteratorResult<Deno.KvEntry<unknown>>> {
|
2023-03-22 00:13:24 -04:00
|
|
|
// Fused or limit exceeded
|
|
|
|
if (
|
|
|
|
this.#done ||
|
|
|
|
(this.#limit !== undefined && this.#count >= this.#limit)
|
|
|
|
) {
|
|
|
|
return { done: true, value: undefined };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to fill the buffer
|
|
|
|
if (!this.#entries?.length && !this.#lastBatch) {
|
|
|
|
const batch = await this.#pullBatch(
|
|
|
|
this.#selector,
|
|
|
|
this.#cursorGen ? this.#cursorGen() : undefined,
|
|
|
|
this.#reverse,
|
|
|
|
this.#consistency,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Reverse the batch so we can pop from the end
|
2024-01-07 17:20:02 -05:00
|
|
|
ArrayPrototypeReverse(batch);
|
2023-03-22 00:13:24 -04:00
|
|
|
this.#entries = batch;
|
|
|
|
|
|
|
|
// Last batch, do not attempt to pull more
|
|
|
|
if (batch.length < this.#batchSize) {
|
|
|
|
this.#lastBatch = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const entry = this.#entries?.pop();
|
|
|
|
if (!entry) {
|
|
|
|
this.#done = true;
|
|
|
|
this.#cursorGen = () => "";
|
|
|
|
return { done: true, value: undefined };
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#cursorGen = () => {
|
|
|
|
const selector = this.#selector;
|
|
|
|
return encodeCursor([
|
2024-01-07 17:20:02 -05:00
|
|
|
ObjectHasOwn(selector, "prefix") ? selector.prefix : null,
|
|
|
|
ObjectHasOwn(selector, "start") ? selector.start : null,
|
|
|
|
ObjectHasOwn(selector, "end") ? selector.end : null,
|
2023-03-22 00:13:24 -04:00
|
|
|
], entry.key);
|
|
|
|
};
|
|
|
|
this.#count++;
|
|
|
|
return {
|
|
|
|
done: false,
|
|
|
|
value: entry,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-07 17:20:02 -05:00
|
|
|
[SymbolAsyncIterator](): AsyncIterator<Deno.KvEntry<unknown>> {
|
2023-03-22 00:13:24 -04:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 11:58:20 -05:00
|
|
|
async function doAtomicWriteInPlace(
|
|
|
|
rid: number,
|
|
|
|
checks: [Deno.KvKey, string | null][],
|
|
|
|
mutations: [Deno.KvKey, string, RawValue | null, number | undefined][],
|
|
|
|
enqueues: [Uint8Array, number, Deno.KvKey[], number[] | null][],
|
|
|
|
): Promise<string | null> {
|
2024-01-07 17:20:02 -05:00
|
|
|
for (let i = 0; i < mutations.length; ++i) {
|
|
|
|
const mutation = mutations[i];
|
|
|
|
const key = mutation[0];
|
2023-12-13 11:58:20 -05:00
|
|
|
if (
|
2024-01-07 17:20:02 -05:00
|
|
|
key.length && mutation[1] === "set" &&
|
2023-12-13 11:58:20 -05:00
|
|
|
key[key.length - 1] === commitVersionstampSymbol
|
|
|
|
) {
|
2024-01-07 17:20:02 -05:00
|
|
|
mutation[0] = ArrayPrototypeSlice(key, 0, key.length - 1);
|
|
|
|
mutation[1] = "setSuffixVersionstampedKey";
|
2023-12-13 11:58:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-26 20:30:26 -05:00
|
|
|
return await op_kv_atomic_write(
|
2023-12-13 11:58:20 -05:00
|
|
|
rid,
|
|
|
|
checks,
|
|
|
|
mutations,
|
|
|
|
enqueues,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-01 03:24:15 -04:00
|
|
|
export { AtomicOperation, Kv, KvListIterator, KvU64, openKv };
|