Skip to content

Commit

Permalink
Merge pull request #28 from earthstar-project/idb-fixes
Browse files Browse the repository at this point in the history
IndexedDB fixes
  • Loading branch information
sgwilym authored Sep 10, 2024
2 parents 1526560 + 787aa26 commit 94c9cbc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
19 changes: 16 additions & 3 deletions src/store/storage/kv/kv_driver_indexeddb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -226,7 +227,7 @@ type BatchOp = {
value: unknown;
} | { kind: "delete"; key: KvKey };

class BatchIndexedDB<Value> implements KvBatch {
class BatchIndexedDB implements KvBatch {
private db: Promise<IDBDatabase>;

private ops: BatchOp[] = [];
Expand Down Expand Up @@ -320,7 +321,19 @@ function selectorToIdbBound({ start, end, prefix }: {
}
}

return IDBKeyRange.bound(actualPackedStart, actualPackedEnd, false, true);
if (
actualPackedStart && actualPackedEnd &&
equalsBytes(actualPackedStart, actualPackedEnd)
) {
return "notMatchingAnything";
}

return IDBKeyRange.bound(
actualPackedStart,
actualPackedEnd,
false,
true,
);
} else {
// The simple cases: no prefix to consider.
if (
Expand Down
4 changes: 3 additions & 1 deletion src/store/storage/kv/prefixed_driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ export class PrefixedDriver implements KvDriver {
delete(key) {
return batch.delete([...prefix, ...key]);
},
commit: batch.commit,
commit() {
return batch.commit();
},
};
}
}

0 comments on commit 94c9cbc

Please sign in to comment.