diff --git a/batched_atomic.test.ts b/batched_atomic.test.ts index 0e117a1..52dddc2 100644 --- a/batched_atomic.test.ts +++ b/batched_atomic.test.ts @@ -155,3 +155,22 @@ Deno.test({ return teardown(); }, }); + +Deno.test({ + name: "batched atomic mutate handles many items", + async fn() { + const kv = await setup(); + const items = Array + .from({ length: 1000 }, (_, index) => index) + .map((i) => ({ + key: [i], + value: "x".repeat(2000), + type: "set" as const, + })); + const op = batchedAtomic(kv); + op.mutate(...items); + const actual = await op.commit(); + assert(actual.every(({ ok }) => ok)); + return teardown(); + }, +}); diff --git a/batched_atomic.ts b/batched_atomic.ts index 0a05f39..de1b028 100644 --- a/batched_atomic.ts +++ b/batched_atomic.ts @@ -142,7 +142,28 @@ export class BatchedAtomicOperation { * {@linkcode Deno.KvMutation}. */ mutate(...mutations: Deno.KvMutation[]): this { - return this.#enqueue("mutate", mutations); + for (const mutation of mutations) { + switch (mutation.type) { + case "set": + this.set(mutation.key, mutation.value, { + expireIn: mutation.expireIn, + }); + break; + case "delete": + this.delete(mutation.key); + break; + case "sum": + this.sum(mutation.key, mutation.value.value); + break; + case "max": + this.max(mutation.key, mutation.value.value); + break; + case "min": + this.min(mutation.key, mutation.value.value); + break; + } + } + return this; } /** @@ -297,18 +318,7 @@ export class BatchedAtomicOperation { hasCheck = true; } else { mutations++; - if (method === "mutate") { - for (const mutation of args as Deno.KvMutation[]) { - const keyLen = estimateSize(mutation.key); - payloadBytes += keyLen; - keyBytes += keyLen; - if (mutation.type === "set") { - payloadBytes += estimateSize(mutation.value); - } else if (mutation.type !== "delete") { - payloadBytes += 8; - } - } - } else if (method === "max" || method === "min" || method === "sum") { + if (method === "max" || method === "min" || method === "sum") { const [key] = args as [Deno.KvKey]; const keyLen = estimateSize(key); keyBytes += keyLen;