From 94ef428b564f8ba60c2752fb7ca64f804f88baf2 Mon Sep 17 00:00:00 2001 From: Heyang Zhou Date: Fri, 24 Mar 2023 20:06:27 +0800 Subject: [PATCH] fix(ext/kv): add missing `getMany` method (#18410) The `getMany` method was missing from the implementation of the `Deno.Kv` class. This patch fixes it. --- cli/tests/unit/kv_test.ts | 10 ++++++++++ ext/kv/01_db.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/cli/tests/unit/kv_test.ts b/cli/tests/unit/kv_test.ts index 1c0eed0021..fcb0c78940 100644 --- a/cli/tests/unit/kv_test.ts +++ b/cli/tests/unit/kv_test.ts @@ -548,6 +548,16 @@ async function setupData(db: Deno.Kv) { .commit(); } +dbTest("get many", async (db) => { + await setupData(db); + const entries = await db.getMany([["b", "a"], ["a"], ["c"]]); + assertEquals(entries, [ + { key: ["b", "a"], value: 100, versionstamp: "00000000000000010000" }, + { key: ["a"], value: -1, versionstamp: "00000000000000010000" }, + { key: ["c"], value: null, versionstamp: null }, + ]); +}); + dbTest("list prefix", async (db) => { await setupData(db); const entries = await collect(db.list({ prefix: ["a"] })); diff --git a/ext/kv/01_db.ts b/ext/kv/01_db.ts index 571a1b3cd5..b423a25532 100644 --- a/ext/kv/01_db.ts +++ b/ext/kv/01_db.ts @@ -72,6 +72,36 @@ class Kv { return deserializeValue(entries[0]); } + async getMany( + keys: Deno.KvKey[], + opts?: { consistency?: Deno.KvConsistencyLevel }, + ): Promise { + keys = keys.map(convertKey); + const ranges: RawKvEntry[][] = await core.opAsync( + "op_kv_snapshot_read", + this.#rid, + keys.map((key) => [ + null, + key, + null, + 1, + false, + null, + ]), + opts?.consistency ?? "strong", + ); + return ranges.map((entries, i) => { + if (!entries.length) { + return { + key: keys[i], + value: null, + versionstamp: null, + }; + } + return deserializeValue(entries[0]); + }); + } + async set(key: Deno.KvKey, value: unknown) { key = convertKey(key); value = serializeValue(value);