Skip to content

Lightpanda: the headless browser designed for AI and automation

indexeddb: support nextunique and prevunique cursor directions

Karl Seguin8888be1229ea
diff --git a/src/browser/tests/indexeddb.html b/src/browser/tests/indexeddb.html
index 791daf7e0..fd0d0637a 100644
--- a/src/browser/tests/indexeddb.html
+++ b/src/browser/tests/indexeddb.html
@@ -1315,4 +1315,55 @@
});
}
</script>
+
+<!-- nextunique/prevunique visit one record per distinct index key — the one
+ with the lowest primary key, in both directions — and advance() counts
+ keys, not records. continue(undefined) is continue(). -->
+<script id="index_cursor_unique_directions" type=module>
+{
+ const state = await testing.async();
+ const open = indexedDB.open("unique-cursor-db", 1);
+ open.onupgradeneeded = (e) => {
+ const store = e.target.result.createObjectStore("s", { keyPath: "id" });
+ store.createIndex("by_v", "v");
+ for (const [id, v] of [[1, "a"], [2, "a"], [3, "b"], [4, "b"], [5, "c"], [6, "c"]]) {
+ store.put({ id, v });
+ }
+ };
+ const walk = (index, direction, step) => new Promise((resolve) => {
+ const seen = [];
+ const req = index.openCursor(null, direction);
+ req.onsuccess = () => {
+ const cursor = req.result;
+ if (!cursor) return resolve(seen);
+ seen.push(cursor.key + cursor.primaryKey);
+ step(cursor);
+ };
+ });
+ open.onsuccess = async () => {
+ const index = open.result.transaction("s").objectStore("s").index("by_v");
+ state.resolve({
+ next: await walk(index, "nextunique", (c) => c.continue()),
+ prev: await walk(index, "prevunique", (c) => c.continue(undefined)),
+ advance: await walk(index, "nextunique", (c) => c.advance(2)),
+ keyCursor: await new Promise((resolve) => {
+ const seen = [];
+ const req = index.openKeyCursor(null, "prevunique");
+ req.onsuccess = () => {
+ const cursor = req.result;
+ if (!cursor) return resolve(seen);
+ seen.push(cursor.primaryKey);
+ cursor.continue();
+ };
+ }),
+ });
+ };
+ await state.done((got) => {
+ testing.expectEqual(["a1", "b3", "c5"], got.next);
+ testing.expectEqual(["c5", "b3", "a1"], got.prev);
+ testing.expectEqual(["a1", "c5"], got.advance);
+ testing.expectEqual([5, 3, 1], got.keyCursor);
+ });
+}
+</script>
</body>
diff --git a/src/browser/webapi/storage/idb/Engine.zig b/src/browser/webapi/storage/idb/Engine.zig
index 168e65ed2..d43b93c82 100644
--- a/src/browser/webapi/storage/idb/Engine.zig
+++ b/src/browser/webapi/storage/idb/Engine.zig
@@ -570,6 +570,7 @@ pub fn indexCursorSeek(
index_id: i64,
b: Bounds,
reverse: bool,
+ unique: bool,
from_key: []const u8,
from_pk: []const u8,
pk_inclusive: bool,
@@ -583,19 +584,26 @@ pub fn indexCursorSeek(
const key_op = if (reverse) "< " else "> ";
const pk_op = if (reverse) (if (pk_inclusive) "<= " else "< ") else (if (pk_inclusive) ">= " else "> ");
- var buf: [640]u8 = undefined;
- const select = if (with_value)
- "select ir.key, ir.primary_key, r.value from idb_index_records ir join idb_records r on r.object_store_id = ?1 and r.key = ir.primary_key"
- else
- "select ir.key, ir.primary_key from idb_index_records ir";
-
+ var buf: [768]u8 = undefined;
const sql = try std.fmt.bufPrint(
&buf,
- \\ {s} where ir.index_id = ?2 and ir.key {s} ?3 and ir.key {s} ?4 and (ir.key {s}?5 or (ir.key = ?5 and ir.primary_key {s}?6))
- \\ order by ir.key {s}, ir.primary_key {s}
+ \\ select ir.key, {s}{s} from idb_index_records ir {s}
+ \\ where ir.index_id = ?2 and ir.key {s} ?3 and ir.key {s} ?4 and (ir.key {s}?5 or (ir.key = ?5 and ir.primary_key {s}?6))
+ \\ {s} order by ir.key {s}, ir.primary_key {s}
\\ limit 1 offset ?7
,
- .{ select, ops.lo, ops.hi, key_op, pk_op, order, order },
+ .{
+ if (unique) "min(ir.primary_key)" else "ir.primary_key",
+ if (with_value) ", r.value" else "",
+ if (with_value) "join idb_records r on r.object_store_id = ?1 and r.key = ir.primary_key" else "",
+ ops.lo,
+ ops.hi,
+ key_op,
+ pk_op,
+ if (unique) "group by ir.key" else "",
+ order,
+ if (unique) "asc" else order,
+ },
);
var row = (try self.conn.row(sql, .{ object_store_id, index_id, b.lower, b.upper, from_key, from_pk, @as(i64, offset) })) orelse return null;
diff --git a/src/browser/webapi/storage/idb/IDBCursor.zig b/src/browser/webapi/storage/idb/IDBCursor.zig
index 816c4e04c..c282a0452 100644
--- a/src/browser/webapi/storage/idb/IDBCursor.zig
+++ b/src/browser/webapi/storage/idb/IDBCursor.zig
@@ -88,6 +88,10 @@ pub const Direction = enum {
fn reverse(self: Direction) bool {
return self == .prev or self == .prevunique;
}
+
+ fn unique(self: Direction) bool {
+ return self == .nextunique or self == .prevunique;
+ }
};
// What this cursor iterates — used only by the `source` accessor.
@@ -186,7 +190,9 @@ pub fn beforeDeliver(self: *IDBCursor) void {
pub fn @"continue"(self: *IDBCursor, key_arg: ?js.Value, exec: *Execution) !void {
try self.assertIterable();
- if (key_arg) |k| {
+ // An explicit `undefined` is an absent optional argument, not a key.
+ if (key_arg != null and !key_arg.?.isUndefined()) {
+ const k = key_arg.?;
// Key conversion (DataError) must run before the got-value flag is
// cleared, so a failing continue() leaves the cursor re-iterable.
const encoded = try Key.encodeValue(self._txn._arena.allocator(), k);
@@ -205,7 +211,7 @@ pub fn @"continue"(self: *IDBCursor, key_arg: ?js.Value, exec: *Execution) !void
pub fn continuePrimaryKey(self: *IDBCursor, key_arg: js.Value, primary_key_arg: js.Value, exec: *Execution) !void {
// Only meaningful on an index cursor with a directed (non-unique) direction.
- if (self._index_id == null or self._direction == .nextunique or self._direction == .prevunique) {
+ if (self._index_id == null or self._direction.unique()) {
return error.InvalidAccessError;
}
try self.assertIterable();
@@ -330,13 +336,16 @@ fn iterate(self: *IDBCursor, seek: Seek, offset: u32, exec: *Execution) !void {
const store_id = self._store._store_id;
if (self._index_id) |index_id| {
+ const unique = self._direction.unique();
const from_key, const from_pk, const pk_inclusive = switch (seek) {
.first => .{ startSentinel(reverse), startSentinel(reverse), false },
- .next => .{ self._key.?, self._primary_key.?, false },
+ // A unique direction moves past every record of the current key:
+ // the far sentinel makes the same-key branch of the seek unmatchable.
+ .next => .{ self._key.?, if (unique) startSentinel(!reverse) else self._primary_key.?, false },
.to => |t| .{ t, startSentinel(reverse), false },
.to_primary => |tp| .{ tp.key, tp.primary_key, true },
};
- const rec = try self._engine.indexCursorSeek(arena, store_id, index_id, self._bounds, reverse, from_key, from_pk, pk_inclusive, !self._key_only, offset);
+ const rec = try self._engine.indexCursorSeek(arena, store_id, index_id, self._bounds, reverse, unique, from_key, from_pk, pk_inclusive, !self._key_only, offset);
if (rec) |r| try self.position(r.key, r.primary_key, r.value) else self.exhaust();
} else {
const from_op, const from_key = switch (seek) {