Skip to content

Commit 5193d51

Browse files
rylosfairlucacasonato
committed
feat(ext/kv): add more atomic operation helpers (denoland#18854)
Co-authored-by: losfair <[email protected]> Co-authored-by: Luca Casonato <[email protected]>
1 parent f7eb112 commit 5193d51

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

cli/tests/unit/kv_test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,36 @@ dbTest("compare and mutate not exists", async (db) => {
219219
assertEquals(res, null);
220220
});
221221

222+
dbTest("atomic mutation helper (sum)", async (db) => {
223+
await db.set(["t"], new Deno.KvU64(42n));
224+
assertEquals((await db.get(["t"])).value, new Deno.KvU64(42n));
225+
226+
await db.atomic().sum(["t"], 1n).commit();
227+
assertEquals((await db.get(["t"])).value, new Deno.KvU64(43n));
228+
});
229+
230+
dbTest("atomic mutation helper (min)", async (db) => {
231+
await db.set(["t"], new Deno.KvU64(42n));
232+
assertEquals((await db.get(["t"])).value, new Deno.KvU64(42n));
233+
234+
await db.atomic().min(["t"], 1n).commit();
235+
assertEquals((await db.get(["t"])).value, new Deno.KvU64(1n));
236+
237+
await db.atomic().min(["t"], 2n).commit();
238+
assertEquals((await db.get(["t"])).value, new Deno.KvU64(1n));
239+
});
240+
241+
dbTest("atomic mutation helper (max)", async (db) => {
242+
await db.set(["t"], new Deno.KvU64(42n));
243+
assertEquals((await db.get(["t"])).value, new Deno.KvU64(42n));
244+
245+
await db.atomic().max(["t"], 41n).commit();
246+
assertEquals((await db.get(["t"])).value, new Deno.KvU64(42n));
247+
248+
await db.atomic().max(["t"], 43n).commit();
249+
assertEquals((await db.get(["t"])).value, new Deno.KvU64(43n));
250+
});
251+
222252
dbTest("compare multiple and mutate", async (db) => {
223253
await db.set(["t1"], "1");
224254
await db.set(["t2"], "2");

cli/tsc/dts/lib.deno.unstable.d.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,8 @@ declare namespace Deno {
16301630
* - `sum` - Adds the given value to the existing value of the key. Both the
16311631
* value specified in the mutation, and any existing value must be of type
16321632
* `Deno.KvU64`. If the key does not exist, the value is set to the given
1633-
* value (summed with 0).
1633+
* value (summed with 0). If the result of the sum overflows an unsigned
1634+
* 64-bit integer, the result is wrapped around.
16341635
* - `max` - Sets the value of the key to the maximum of the existing value
16351636
* and the given value. Both the value specified in the mutation, and any
16361637
* existing value must be of type `Deno.KvU64`. If the key does not exist,
@@ -1828,9 +1829,23 @@ declare namespace Deno {
18281829
*/
18291830
mutate(...mutations: KvMutation[]): this;
18301831
/**
1831-
* Shortcut for creating a sum mutation.
1832+
* Shortcut for creating a `sum` mutation. This method wraps `n` in a
1833+
* {@linkcode Deno.KvU64}, so the value of `n` must be in the range
1834+
* `[0, 2^64-1]`.
18321835
*/
18331836
sum(key: KvKey, n: bigint): this;
1837+
/**
1838+
* Shortcut for creating a `min` mutation. This method wraps `n` in a
1839+
* {@linkcode Deno.KvU64}, so the value of `n` must be in the range
1840+
* `[0, 2^64-1]`.
1841+
*/
1842+
min(key: KvKey, n: bigint): this;
1843+
/**
1844+
* Shortcut for creating a `max` mutation. This method wraps `n` in a
1845+
* {@linkcode Deno.KvU64}, so the value of `n` must be in the range
1846+
* `[0, 2^64-1]`.
1847+
*/
1848+
max(key: KvKey, n: bigint): this;
18341849
/**
18351850
* Add to the operation a mutation that sets the value of the specified key
18361851
* to the specified value if all checks pass during the commit.

ext/kv/01_db.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,6 @@ class AtomicOperation {
211211
return this;
212212
}
213213

214-
sum(key: Deno.KvKey, n: bigint): this {
215-
return this.mutate({
216-
type: "sum",
217-
key,
218-
value: new KvU64(n),
219-
});
220-
}
221-
222214
mutate(...mutations: Deno.KvMutation[]): this {
223215
for (const mutation of mutations) {
224216
const key = mutation.key;
@@ -249,6 +241,21 @@ class AtomicOperation {
249241
return this;
250242
}
251243

244+
sum(key: Deno.KvKey, n: bigint): this {
245+
this.#mutations.push([key, "sum", serializeValue(new KvU64(n))]);
246+
return this;
247+
}
248+
249+
min(key: Deno.KvKey, n: bigint): this {
250+
this.#mutations.push([key, "min", serializeValue(new KvU64(n))]);
251+
return this;
252+
}
253+
254+
max(key: Deno.KvKey, n: bigint): this {
255+
this.#mutations.push([key, "max", serializeValue(new KvU64(n))]);
256+
return this;
257+
}
258+
252259
set(key: Deno.KvKey, value: unknown): this {
253260
this.#mutations.push([key, "set", serializeValue(value)]);
254261
return this;

0 commit comments

Comments
 (0)