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:
parent
03132e19da
commit
bce5afd814
3 changed files with 42 additions and 39 deletions
|
@ -183,7 +183,7 @@ dbTest("compare and mutate", async (db) => {
|
|||
.check({ key: ["t"], versionstamp: currentValue.versionstamp })
|
||||
.set(currentValue.key, "2")
|
||||
.commit();
|
||||
assert(res);
|
||||
assert(res.ok);
|
||||
assertEquals(res.versionstamp, "00000000000000020000");
|
||||
|
||||
const newValue = await db.get(["t"]);
|
||||
|
@ -194,7 +194,7 @@ dbTest("compare and mutate", async (db) => {
|
|||
.check({ key: ["t"], versionstamp: currentValue.versionstamp })
|
||||
.set(currentValue.key, "3")
|
||||
.commit();
|
||||
assertEquals(res, null);
|
||||
assert(!res.ok);
|
||||
|
||||
const newValue2 = await db.get(["t"]);
|
||||
assertEquals(newValue2.versionstamp, "00000000000000020000");
|
||||
|
@ -206,7 +206,7 @@ dbTest("compare and mutate not exists", async (db) => {
|
|||
.check({ key: ["t"], versionstamp: null })
|
||||
.set(["t"], "1")
|
||||
.commit();
|
||||
assert(res);
|
||||
assert(res.ok);
|
||||
|
||||
const newValue = await db.get(["t"]);
|
||||
assertEquals(newValue.versionstamp, "00000000000000010000");
|
||||
|
@ -216,7 +216,7 @@ dbTest("compare and mutate not exists", async (db) => {
|
|||
.check({ key: ["t"], versionstamp: null })
|
||||
.set(["t"], "2")
|
||||
.commit();
|
||||
assertEquals(res, null);
|
||||
assert(!res.ok);
|
||||
});
|
||||
|
||||
dbTest("atomic mutation helper (sum)", async (db) => {
|
||||
|
@ -264,7 +264,7 @@ dbTest("compare multiple and mutate", async (db) => {
|
|||
.set(currentValue1.key, "3")
|
||||
.set(currentValue2.key, "4")
|
||||
.commit();
|
||||
assert(res);
|
||||
assert(res.ok);
|
||||
|
||||
const newValue1 = await db.get(["t1"]);
|
||||
assertEquals(newValue1.versionstamp, "00000000000000030000");
|
||||
|
@ -280,7 +280,7 @@ dbTest("compare multiple and mutate", async (db) => {
|
|||
.set(newValue1.key, "5")
|
||||
.set(newValue2.key, "6")
|
||||
.commit();
|
||||
assertEquals(res2, null);
|
||||
assert(!res2.ok);
|
||||
|
||||
const newValue3 = await db.get(["t1"]);
|
||||
assertEquals(newValue3.versionstamp, "00000000000000030000");
|
||||
|
@ -296,7 +296,7 @@ dbTest("atomic mutation ordering (set before delete)", async (db) => {
|
|||
.set(["a"], "2")
|
||||
.delete(["a"])
|
||||
.commit();
|
||||
assert(res);
|
||||
assert(res.ok);
|
||||
const result = await db.get(["a"]);
|
||||
assertEquals(result.value, null);
|
||||
});
|
||||
|
@ -307,7 +307,7 @@ dbTest("atomic mutation ordering (delete before set)", async (db) => {
|
|||
.delete(["a"])
|
||||
.set(["a"], "2")
|
||||
.commit();
|
||||
assert(res);
|
||||
assert(res.ok);
|
||||
const result = await db.get(["a"]);
|
||||
assertEquals(result.value, "2");
|
||||
});
|
||||
|
@ -316,7 +316,7 @@ dbTest("atomic mutation type=set", async (db) => {
|
|||
const res = await db.atomic()
|
||||
.mutate({ key: ["a"], value: "1", type: "set" })
|
||||
.commit();
|
||||
assert(res);
|
||||
assert(res.ok);
|
||||
const result = await db.get(["a"]);
|
||||
assertEquals(result.value, "1");
|
||||
});
|
||||
|
@ -326,7 +326,7 @@ dbTest("atomic mutation type=set overwrite", async (db) => {
|
|||
const res = await db.atomic()
|
||||
.mutate({ key: ["a"], value: "2", type: "set" })
|
||||
.commit();
|
||||
assert(res);
|
||||
assert(res.ok);
|
||||
const result = await db.get(["a"]);
|
||||
assertEquals(result.value, "2");
|
||||
});
|
||||
|
@ -336,7 +336,7 @@ dbTest("atomic mutation type=delete", async (db) => {
|
|||
const res = await db.atomic()
|
||||
.mutate({ key: ["a"], type: "delete" })
|
||||
.commit();
|
||||
assert(res);
|
||||
assert(res.ok);
|
||||
const result = await db.get(["a"]);
|
||||
assertEquals(result.value, null);
|
||||
});
|
||||
|
@ -345,7 +345,7 @@ dbTest("atomic mutation type=delete no exists", async (db) => {
|
|||
const res = await db.atomic()
|
||||
.mutate({ key: ["a"], type: "delete" })
|
||||
.commit();
|
||||
assert(res);
|
||||
assert(res.ok);
|
||||
const result = await db.get(["a"]);
|
||||
assertEquals(result.value, null);
|
||||
});
|
||||
|
@ -355,7 +355,7 @@ dbTest("atomic mutation type=sum", async (db) => {
|
|||
const res = await db.atomic()
|
||||
.mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "sum" })
|
||||
.commit();
|
||||
assert(res);
|
||||
assert(res.ok);
|
||||
const result = await db.get(["a"]);
|
||||
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()
|
||||
.mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "sum" })
|
||||
.commit();
|
||||
assert(res);
|
||||
assert(res.ok);
|
||||
const result = await db.get(["a"]);
|
||||
assert(result.value);
|
||||
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()
|
||||
.mutate({ key: ["a"], value: new Deno.KvU64(10n), type: "sum" })
|
||||
.commit();
|
||||
assert(res);
|
||||
assert(res.ok);
|
||||
const result = await db.get(["a"]);
|
||||
assertEquals(result.value, new Deno.KvU64(9n));
|
||||
|
||||
|
@ -386,7 +386,7 @@ dbTest("atomic mutation type=sum wrap around", async (db) => {
|
|||
type: "sum",
|
||||
})
|
||||
.commit();
|
||||
assert(res2);
|
||||
assert(res2.ok);
|
||||
const result2 = await db.get(["a"]);
|
||||
assertEquals(result2.value, new Deno.KvU64(8n));
|
||||
});
|
||||
|
@ -423,14 +423,14 @@ dbTest("atomic mutation type=min", async (db) => {
|
|||
const res = await db.atomic()
|
||||
.mutate({ key: ["a"], value: new Deno.KvU64(5n), type: "min" })
|
||||
.commit();
|
||||
assert(res);
|
||||
assert(res.ok);
|
||||
const result = await db.get(["a"]);
|
||||
assertEquals(result.value, new Deno.KvU64(5n));
|
||||
|
||||
const res2 = await db.atomic()
|
||||
.mutate({ key: ["a"], value: new Deno.KvU64(15n), type: "min" })
|
||||
.commit();
|
||||
assert(res2);
|
||||
assert(res2.ok);
|
||||
const result2 = await db.get(["a"]);
|
||||
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()
|
||||
.mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "min" })
|
||||
.commit();
|
||||
assert(res);
|
||||
assert(res.ok);
|
||||
const result = await db.get(["a"]);
|
||||
assert(result.value);
|
||||
assertEquals(result.value, new Deno.KvU64(1n));
|
||||
|
@ -477,14 +477,14 @@ dbTest("atomic mutation type=max", async (db) => {
|
|||
const res = await db.atomic()
|
||||
.mutate({ key: ["a"], value: new Deno.KvU64(5n), type: "max" })
|
||||
.commit();
|
||||
assert(res);
|
||||
assert(res.ok);
|
||||
const result = await db.get(["a"]);
|
||||
assertEquals(result.value, new Deno.KvU64(10n));
|
||||
|
||||
const res2 = await db.atomic()
|
||||
.mutate({ key: ["a"], value: new Deno.KvU64(15n), type: "max" })
|
||||
.commit();
|
||||
assert(res2);
|
||||
assert(res2.ok);
|
||||
const result2 = await db.get(["a"]);
|
||||
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()
|
||||
.mutate({ key: ["a"], value: new Deno.KvU64(1n), type: "max" })
|
||||
.commit();
|
||||
assert(res);
|
||||
assert(res.ok);
|
||||
const result = await db.get(["a"]);
|
||||
assert(result.value);
|
||||
assertEquals(result.value, new Deno.KvU64(1n));
|
||||
|
|
31
cli/tsc/dts/lib.deno.unstable.d.ts
vendored
31
cli/tsc/dts/lib.deno.unstable.d.ts
vendored
|
@ -1757,10 +1757,12 @@ declare namespace Deno {
|
|||
}
|
||||
|
||||
/** @category KV */
|
||||
export interface KvCommitResult {
|
||||
export type KvCommitResult = {
|
||||
ok: true;
|
||||
|
||||
/** The versionstamp of the value committed to KV. */
|
||||
versionstamp: string;
|
||||
}
|
||||
} | { ok: false };
|
||||
|
||||
/** **UNSTABLE**: New API, yet to be vetted.
|
||||
*
|
||||
|
@ -1855,19 +1857,17 @@ declare namespace Deno {
|
|||
*/
|
||||
delete(key: KvKey): this;
|
||||
/**
|
||||
* Commit the operation to the KV store. Returns a value indicating whether
|
||||
* checks passed and mutations were performed. If the operation failed
|
||||
* because of a failed check, the return value will be `null`. If the
|
||||
* operation failed for any other reason (storage error, invalid value,
|
||||
* 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.
|
||||
* Commit the operation to the KV store. Returns a {@linkcode Deno.KvCommitResult}
|
||||
* value indicating whether checks passed and mutations were performed.
|
||||
* If the operation failed for any other reason than a failed check (storage
|
||||
* error, invalid value, etc.), an exception will be thrown.
|
||||
*
|
||||
* If the commit returns `null`, one may create a new atomic operation with
|
||||
* updated checks and mutations and attempt to commit it again. See the note
|
||||
* on optimistic locking in the documentation for {@linkcode Deno.AtomicOperation}.
|
||||
* If the `ok` field in the return value is `false`, one may create a new
|
||||
* atomic operation with updated checks and mutations and attempt to commit it
|
||||
* 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.
|
||||
|
@ -1967,7 +1967,10 @@ declare namespace Deno {
|
|||
* 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
|
||||
|
|
|
@ -266,7 +266,7 @@ class AtomicOperation {
|
|||
return this;
|
||||
}
|
||||
|
||||
async commit(): Promise<Deno.KvCommitResult | null> {
|
||||
async commit(): Promise<Deno.KvCommitResult> {
|
||||
const versionstamp = await core.opAsync(
|
||||
"op_kv_atomic_write",
|
||||
this.#rid,
|
||||
|
@ -274,8 +274,8 @@ class AtomicOperation {
|
|||
this.#mutations,
|
||||
[], // TODO(@losfair): enqueue
|
||||
);
|
||||
if (versionstamp === null) return null;
|
||||
return { versionstamp };
|
||||
if (versionstamp === null) return { ok: false };
|
||||
return { ok: true, versionstamp };
|
||||
}
|
||||
|
||||
then() {
|
||||
|
|
Loading…
Reference in a new issue