1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

refactor(ext/kv): make commit() return an ok field

This commit is contained in:
losfair 2023-04-27 22:05:33 +08:00
parent 03132e19da
commit bce5afd814
3 changed files with 42 additions and 39 deletions

View file

@ -183,7 +183,7 @@ dbTest("compare and mutate", async (db) => {
.check({ key: ["t"], versionstamp: currentValue.versionstamp }) .check({ key: ["t"], versionstamp: currentValue.versionstamp })
.set(currentValue.key, "2") .set(currentValue.key, "2")
.commit(); .commit();
assert(res); assert(res.ok);
assertEquals(res.versionstamp, "00000000000000020000"); assertEquals(res.versionstamp, "00000000000000020000");
const newValue = await db.get(["t"]); const newValue = await db.get(["t"]);
@ -194,7 +194,7 @@ dbTest("compare and mutate", async (db) => {
.check({ key: ["t"], versionstamp: currentValue.versionstamp }) .check({ key: ["t"], versionstamp: currentValue.versionstamp })
.set(currentValue.key, "3") .set(currentValue.key, "3")
.commit(); .commit();
assertEquals(res, null); assert(!res.ok);
const newValue2 = await db.get(["t"]); const newValue2 = await db.get(["t"]);
assertEquals(newValue2.versionstamp, "00000000000000020000"); assertEquals(newValue2.versionstamp, "00000000000000020000");
@ -206,7 +206,7 @@ dbTest("compare and mutate not exists", async (db) => {
.check({ key: ["t"], versionstamp: null }) .check({ key: ["t"], versionstamp: null })
.set(["t"], "1") .set(["t"], "1")
.commit(); .commit();
assert(res); assert(res.ok);
const newValue = await db.get(["t"]); const newValue = await db.get(["t"]);
assertEquals(newValue.versionstamp, "00000000000000010000"); assertEquals(newValue.versionstamp, "00000000000000010000");
@ -216,7 +216,7 @@ dbTest("compare and mutate not exists", async (db) => {
.check({ key: ["t"], versionstamp: null }) .check({ key: ["t"], versionstamp: null })
.set(["t"], "2") .set(["t"], "2")
.commit(); .commit();
assertEquals(res, null); assert(!res.ok);
}); });
dbTest("atomic mutation helper (sum)", async (db) => { dbTest("atomic mutation helper (sum)", async (db) => {
@ -264,7 +264,7 @@ dbTest("compare multiple and mutate", async (db) => {
.set(currentValue1.key, "3") .set(currentValue1.key, "3")
.set(currentValue2.key, "4") .set(currentValue2.key, "4")
.commit(); .commit();
assert(res); assert(res.ok);
const newValue1 = await db.get(["t1"]); const newValue1 = await db.get(["t1"]);
assertEquals(newValue1.versionstamp, "00000000000000030000"); assertEquals(newValue1.versionstamp, "00000000000000030000");
@ -280,7 +280,7 @@ dbTest("compare multiple and mutate", async (db) => {
.set(newValue1.key, "5") .set(newValue1.key, "5")
.set(newValue2.key, "6") .set(newValue2.key, "6")
.commit(); .commit();
assertEquals(res2, null); assert(!res2.ok);
const newValue3 = await db.get(["t1"]); const newValue3 = await db.get(["t1"]);
assertEquals(newValue3.versionstamp, "00000000000000030000"); assertEquals(newValue3.versionstamp, "00000000000000030000");
@ -296,7 +296,7 @@ dbTest("atomic mutation ordering (set before delete)", async (db) => {
.set(["a"], "2") .set(["a"], "2")
.delete(["a"]) .delete(["a"])
.commit(); .commit();
assert(res); assert(res.ok);
const result = await db.get(["a"]); const result = await db.get(["a"]);
assertEquals(result.value, null); assertEquals(result.value, null);
}); });
@ -307,7 +307,7 @@ dbTest("atomic mutation ordering (delete before set)", async (db) => {
.delete(["a"]) .delete(["a"])
.set(["a"], "2") .set(["a"], "2")
.commit(); .commit();
assert(res); assert(res.ok);
const result = await db.get(["a"]); const result = await db.get(["a"]);
assertEquals(result.value, "2"); assertEquals(result.value, "2");
}); });
@ -316,7 +316,7 @@ dbTest("atomic mutation type=set", async (db) => {
const res = await db.atomic() const res = await db.atomic()
.mutate({ key: ["a"], value: "1", type: "set" }) .mutate({ key: ["a"], value: "1", type: "set" })
.commit(); .commit();
assert(res); assert(res.ok);
const result = await db.get(["a"]); const result = await db.get(["a"]);
assertEquals(result.value, "1"); assertEquals(result.value, "1");
}); });
@ -326,7 +326,7 @@ dbTest("atomic mutation type=set overwrite", async (db) => {
const res = await db.atomic() const res = await db.atomic()
.mutate({ key: ["a"], value: "2", type: "set" }) .mutate({ key: ["a"], value: "2", type: "set" })
.commit(); .commit();
assert(res); assert(res.ok);
const result = await db.get(["a"]); const result = await db.get(["a"]);
assertEquals(result.value, "2"); assertEquals(result.value, "2");
}); });
@ -336,7 +336,7 @@ dbTest("atomic mutation type=delete", async (db) => {
const res = await db.atomic() const res = await db.atomic()
.mutate({ key: ["a"], type: "delete" }) .mutate({ key: ["a"], type: "delete" })
.commit(); .commit();
assert(res); assert(res.ok);
const result = await db.get(["a"]); const result = await db.get(["a"]);
assertEquals(result.value, null); assertEquals(result.value, null);
}); });
@ -345,7 +345,7 @@ dbTest("atomic mutation type=delete no exists", async (db) => {
const res = await db.atomic() const res = await db.atomic()
.mutate({ key: ["a"], type: "delete" }) .mutate({ key: ["a"], type: "delete" })
.commit(); .commit();
assert(res); assert(res.ok);
const result = await db.get(["a"]); const result = await db.get(["a"]);
assertEquals(result.value, null); assertEquals(result.value, null);
}); });
@ -355,7 +355,7 @@ dbTest("atomic mutation type=sum", async (db) => {
const res = await db.atomic() const res = await db.atomic()
.mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "sum" }) .mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "sum" })
.commit(); .commit();
assert(res); assert(res.ok);
const result = await db.get(["a"]); const result = await db.get(["a"]);
assertEquals(result.value, new Deno.KvU64(11n)); assertEquals(result.value, new Deno.KvU64(11n));
}); });
@ -364,7 +364,7 @@ dbTest("atomic mutation type=sum no exists", async (db) => {
const res = await db.atomic() const res = await db.atomic()
.mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "sum" }) .mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "sum" })
.commit(); .commit();
assert(res); assert(res.ok);
const result = await db.get(["a"]); const result = await db.get(["a"]);
assert(result.value); assert(result.value);
assertEquals(result.value, new Deno.KvU64(1n)); assertEquals(result.value, new Deno.KvU64(1n));
@ -375,7 +375,7 @@ dbTest("atomic mutation type=sum wrap around", async (db) => {
const res = await db.atomic() const res = await db.atomic()
.mutate({ key: ["a"], value: new Deno.KvU64(10n), type: "sum" }) .mutate({ key: ["a"], value: new Deno.KvU64(10n), type: "sum" })
.commit(); .commit();
assert(res); assert(res.ok);
const result = await db.get(["a"]); const result = await db.get(["a"]);
assertEquals(result.value, new Deno.KvU64(9n)); assertEquals(result.value, new Deno.KvU64(9n));
@ -386,7 +386,7 @@ dbTest("atomic mutation type=sum wrap around", async (db) => {
type: "sum", type: "sum",
}) })
.commit(); .commit();
assert(res2); assert(res2.ok);
const result2 = await db.get(["a"]); const result2 = await db.get(["a"]);
assertEquals(result2.value, new Deno.KvU64(8n)); assertEquals(result2.value, new Deno.KvU64(8n));
}); });
@ -423,14 +423,14 @@ dbTest("atomic mutation type=min", async (db) => {
const res = await db.atomic() const res = await db.atomic()
.mutate({ key: ["a"], value: new Deno.KvU64(5n), type: "min" }) .mutate({ key: ["a"], value: new Deno.KvU64(5n), type: "min" })
.commit(); .commit();
assert(res); assert(res.ok);
const result = await db.get(["a"]); const result = await db.get(["a"]);
assertEquals(result.value, new Deno.KvU64(5n)); assertEquals(result.value, new Deno.KvU64(5n));
const res2 = await db.atomic() const res2 = await db.atomic()
.mutate({ key: ["a"], value: new Deno.KvU64(15n), type: "min" }) .mutate({ key: ["a"], value: new Deno.KvU64(15n), type: "min" })
.commit(); .commit();
assert(res2); assert(res2.ok);
const result2 = await db.get(["a"]); const result2 = await db.get(["a"]);
assertEquals(result2.value, new Deno.KvU64(5n)); assertEquals(result2.value, new Deno.KvU64(5n));
}); });
@ -439,7 +439,7 @@ dbTest("atomic mutation type=min no exists", async (db) => {
const res = await db.atomic() const res = await db.atomic()
.mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "min" }) .mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "min" })
.commit(); .commit();
assert(res); assert(res.ok);
const result = await db.get(["a"]); const result = await db.get(["a"]);
assert(result.value); assert(result.value);
assertEquals(result.value, new Deno.KvU64(1n)); assertEquals(result.value, new Deno.KvU64(1n));
@ -477,14 +477,14 @@ dbTest("atomic mutation type=max", async (db) => {
const res = await db.atomic() const res = await db.atomic()
.mutate({ key: ["a"], value: new Deno.KvU64(5n), type: "max" }) .mutate({ key: ["a"], value: new Deno.KvU64(5n), type: "max" })
.commit(); .commit();
assert(res); assert(res.ok);
const result = await db.get(["a"]); const result = await db.get(["a"]);
assertEquals(result.value, new Deno.KvU64(10n)); assertEquals(result.value, new Deno.KvU64(10n));
const res2 = await db.atomic() const res2 = await db.atomic()
.mutate({ key: ["a"], value: new Deno.KvU64(15n), type: "max" }) .mutate({ key: ["a"], value: new Deno.KvU64(15n), type: "max" })
.commit(); .commit();
assert(res2); assert(res2.ok);
const result2 = await db.get(["a"]); const result2 = await db.get(["a"]);
assertEquals(result2.value, new Deno.KvU64(15n)); assertEquals(result2.value, new Deno.KvU64(15n));
}); });
@ -493,7 +493,7 @@ dbTest("atomic mutation type=max no exists", async (db) => {
const res = await db.atomic() const res = await db.atomic()
.mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "max" }) .mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "max" })
.commit(); .commit();
assert(res); assert(res.ok);
const result = await db.get(["a"]); const result = await db.get(["a"]);
assert(result.value); assert(result.value);
assertEquals(result.value, new Deno.KvU64(1n)); assertEquals(result.value, new Deno.KvU64(1n));

View file

@ -1757,10 +1757,12 @@ declare namespace Deno {
} }
/** @category KV */ /** @category KV */
export interface KvCommitResult { export type KvCommitResult = {
ok: true;
/** The versionstamp of the value committed to KV. */ /** The versionstamp of the value committed to KV. */
versionstamp: string; versionstamp: string;
} } | { ok: false };
/** **UNSTABLE**: New API, yet to be vetted. /** **UNSTABLE**: New API, yet to be vetted.
* *
@ -1855,19 +1857,17 @@ declare namespace Deno {
*/ */
delete(key: KvKey): this; delete(key: KvKey): this;
/** /**
* Commit the operation to the KV store. Returns a value indicating whether * Commit the operation to the KV store. Returns a {@linkcode Deno.KvCommitResult}
* checks passed and mutations were performed. If the operation failed * value indicating whether checks passed and mutations were performed.
* because of a failed check, the return value will be `null`. If the * If the operation failed for any other reason than a failed check (storage
* operation failed for any other reason (storage error, invalid value, * error, invalid value, etc.), an exception will be thrown.
* etc.), an exception will be thrown. If the operation succeeded, the
* return value will be a {@linkcode Deno.KvCommitResult} object containing
* the versionstamp of the value committed to KV.
* *
* If the commit returns `null`, one may create a new atomic operation with * If the `ok` field in the return value is `false`, one may create a new
* updated checks and mutations and attempt to commit it again. See the note * atomic operation with updated checks and mutations and attempt to commit it
* on optimistic locking in the documentation for {@linkcode Deno.AtomicOperation}. * again. See the note on optimistic locking in the documentation for
* {@linkcode Deno.AtomicOperation}.
*/ */
commit(): Promise<KvCommitResult | null>; commit(): Promise<KvCommitResult>;
} }
/** **UNSTABLE**: New API, yet to be vetted. /** **UNSTABLE**: New API, yet to be vetted.
@ -1967,7 +1967,10 @@ declare namespace Deno {
* await db.set(["foo"], "bar"); * await db.set(["foo"], "bar");
* ``` * ```
*/ */
set(key: KvKey, value: unknown): Promise<KvCommitResult>; set(
key: KvKey,
value: unknown,
): Promise<{ versionstamp: string }>;
/** /**
* Delete the value for the given key from the database. If no value exists * Delete the value for the given key from the database. If no value exists

View file

@ -266,7 +266,7 @@ class AtomicOperation {
return this; return this;
} }
async commit(): Promise<Deno.KvCommitResult | null> { async commit(): Promise<Deno.KvCommitResult> {
const versionstamp = await core.opAsync( const versionstamp = await core.opAsync(
"op_kv_atomic_write", "op_kv_atomic_write",
this.#rid, this.#rid,
@ -274,8 +274,8 @@ class AtomicOperation {
this.#mutations, this.#mutations,
[], // TODO(@losfair): enqueue [], // TODO(@losfair): enqueue
); );
if (versionstamp === null) return null; if (versionstamp === null) return { ok: false };
return { versionstamp }; return { ok: true, versionstamp };
} }
then() { then() {