Skip to content

Commit

Permalink
Fix transaction sort order
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed May 31, 2024
1 parent 4770200 commit 36a7bb3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.15.6

## Additions

- Fixed order of transactions returned by `iterate` and `listAll`

## 0.15.5

## Additions
Expand Down
Binary file removed db.db
Binary file not shown.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cross/kv",
"version": "0.15.5",
"version": "0.15.6",
"exports": {
".": "./mod.ts",
"./cli": "./src/cli/mod.ts"
Expand Down
6 changes: 2 additions & 4 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,8 @@ export class KVIndex {
recurse(this.index, 0);

// Sort the array by transaction offset, to give results sorted in insertion order
resultSet.sort();

// Reverse if requested, after sorting
if (reverse) resultSet.reverse();
// - Reverse if requested
resultSet.sort((a, b) => reverse ? b - a : a - b);

// Limit if requested, after sorting and reversing
if (limit !== undefined) resultSet.splice(limit);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ export class KV extends EventEmitter {
this.ensureOpen();
this.ensureIndex();
const validatedKey = new KVKeyInstance(key, true);

const offsets = this.index!.get(validatedKey, limit, reverse)!;

if (offsets === null || offsets.length === 0) {
Expand All @@ -547,7 +548,6 @@ export class KV extends EventEmitter {
yield result.transaction.asResult();
count++;
}
if (limit && count >= limit) break;
}
}

Expand Down
34 changes: 34 additions & 0 deletions test/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,3 +702,37 @@ test("KV: listAll in reverse order with limit", async () => {
assertEquals(results, expectedValues);
await kvStore.close();
});

test("KV: iterate and listAll respect reverse insertion order with multiple key matches", async () => {
const tempFilePrefix = await tempfile();
const kvStore = new KV();
await kvStore.open(tempFilePrefix);

// Inserting items with a common prefix and different suffixes
await kvStore.set(["data", "d", "b"], "Value A");
await kvStore.set(["data", "c", "a"], "Value B");
await kvStore.set(["data", "c", "b"], "Value C");
await kvStore.set(["data", "a"], "Value D");

// Iterate in reverse order
const iterateResults = [];
for await (const entry of kvStore.iterate(["data"], undefined, true)) {
iterateResults.push(entry.data);
}
assertEquals(iterateResults, ["Value D", "Value C", "Value B", "Value A"]);

// ListAll in reverse order
const listAllResults = (await kvStore.listAll(["data"], undefined, true)).map(
(entry) => entry.data,
);
assertEquals(listAllResults, ["Value D", "Value C", "Value B", "Value A"]);

// ListAll in insertion order
const listAllResults2 = (await kvStore.listAll(["data"], undefined, false))
.map(
(entry) => entry.data,
);
assertEquals(listAllResults2, ["Value A", "Value B", "Value C", "Value D"]);

await kvStore.close();
});

0 comments on commit 36a7bb3

Please sign in to comment.