From 992d4ff170b4f1e4028e94e15beacaf52ea7a2b9 Mon Sep 17 00:00:00 2001 From: Sam Gwilym Date: Tue, 10 Sep 2024 16:10:07 +0100 Subject: [PATCH 1/3] Bind KV.batch commit properly --- src/store/storage/kv/prefixed_driver.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/store/storage/kv/prefixed_driver.ts b/src/store/storage/kv/prefixed_driver.ts index 8dd796b..353e6f6 100644 --- a/src/store/storage/kv/prefixed_driver.ts +++ b/src/store/storage/kv/prefixed_driver.ts @@ -91,7 +91,9 @@ export class PrefixedDriver implements KvDriver { delete(key) { return batch.delete([...prefix, ...key]); }, - commit: batch.commit, + commit() { + return batch.commit(); + }, }; } } From 213dedaf713d1205b410cbf9c262b83623559a73 Mon Sep 17 00:00:00 2001 From: Sam Gwilym Date: Tue, 10 Sep 2024 16:12:48 +0100 Subject: [PATCH 2/3] IndexedDB: Fix cursor direction + fix creation of invalid bounds --- src/store/storage/kv/kv_driver_indexeddb.ts | 24 ++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/store/storage/kv/kv_driver_indexeddb.ts b/src/store/storage/kv/kv_driver_indexeddb.ts index dd6abad..4371f57 100644 --- a/src/store/storage/kv/kv_driver_indexeddb.ts +++ b/src/store/storage/kv/kv_driver_indexeddb.ts @@ -9,6 +9,7 @@ import { pack, unpack } from "./key_codec/kv_key_codec.ts"; import { WillowError } from "../../../errors.ts"; import { FIFO } from "@korkje/fifo"; import { successorBytesFixedWidth } from "@earthstar/willow-utils"; +import { equals as equalsBytes } from "@std/bytes"; const KV_STORE = "kv"; const END_LIST = Symbol("end_list"); @@ -130,7 +131,7 @@ export class KvDriverIndexedDB implements KvDriver { return; } - const direction = opts?.reverse ? "prevunique" : "nextunique"; + const direction = opts?.reverse ? "prev" : "next"; const resultFifo = new FIFO< { packedKey: Uint8Array; value: Value } | typeof END_LIST @@ -226,7 +227,7 @@ type BatchOp = { value: unknown; } | { kind: "delete"; key: KvKey }; -class BatchIndexedDB implements KvBatch { +class BatchIndexedDB implements KvBatch { private db: Promise; private ops: BatchOp[] = []; @@ -320,7 +321,24 @@ function selectorToIdbBound({ start, end, prefix }: { } } - return IDBKeyRange.bound(actualPackedStart, actualPackedEnd, false, true); + if ( + actualPackedStart && actualPackedEnd && + equalsBytes(actualPackedStart, actualPackedEnd) + ) { + return IDBKeyRange.bound( + actualPackedStart, + actualPackedEnd, + false, + false, + ); + } + + return IDBKeyRange.bound( + actualPackedStart, + actualPackedEnd, + false, + true, + ); } else { // The simple cases: no prefix to consider. if ( From 787aa268c813c4ace073beacaf1fbdc45e2f7fa2 Mon Sep 17 00:00:00 2001 From: Sam Gwilym Date: Tue, 10 Sep 2024 16:41:44 +0100 Subject: [PATCH 3/3] Really fix IDB bound creation --- src/store/storage/kv/kv_driver_indexeddb.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/store/storage/kv/kv_driver_indexeddb.ts b/src/store/storage/kv/kv_driver_indexeddb.ts index 4371f57..0450890 100644 --- a/src/store/storage/kv/kv_driver_indexeddb.ts +++ b/src/store/storage/kv/kv_driver_indexeddb.ts @@ -325,12 +325,7 @@ function selectorToIdbBound({ start, end, prefix }: { actualPackedStart && actualPackedEnd && equalsBytes(actualPackedStart, actualPackedEnd) ) { - return IDBKeyRange.bound( - actualPackedStart, - actualPackedEnd, - false, - false, - ); + return "notMatchingAnything"; } return IDBKeyRange.bound(