Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions batched_atomic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
});
36 changes: 23 additions & 13 deletions batched_atomic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a default statement throwing invalid mutations at runtime instead of relying on TypeScript types in this exhaustive switch statement?

}
return this;
}

/**
Expand Down Expand Up @@ -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;
Expand Down