LibWeb/IDB: Implement retrieve_multiple_items_from_an_object_store

This commit is contained in:
stelar7 2025-07-09 11:52:44 +02:00 committed by Jelle Raaijmakers
commit 839ffd45f3
Notes: github-actions[bot] 2025-08-27 14:15:36 +00:00
4 changed files with 95 additions and 0 deletions

View file

@ -25,6 +25,7 @@
#include <LibWeb/IndexedDB/IDBDatabase.h> #include <LibWeb/IndexedDB/IDBDatabase.h>
#include <LibWeb/IndexedDB/IDBIndex.h> #include <LibWeb/IndexedDB/IDBIndex.h>
#include <LibWeb/IndexedDB/IDBObjectStore.h> #include <LibWeb/IndexedDB/IDBObjectStore.h>
#include <LibWeb/IndexedDB/IDBRecord.h>
#include <LibWeb/IndexedDB/IDBRequest.h> #include <LibWeb/IndexedDB/IDBRequest.h>
#include <LibWeb/IndexedDB/IDBTransaction.h> #include <LibWeb/IndexedDB/IDBTransaction.h>
#include <LibWeb/IndexedDB/IDBVersionChangeEvent.h> #include <LibWeb/IndexedDB/IDBVersionChangeEvent.h>
@ -1930,6 +1931,77 @@ GC::Ref<JS::Array> retrieve_multiple_values_from_an_object_store(JS::Realm& real
return list; return list;
} }
// https://pr-preview.s3.amazonaws.com/w3c/IndexedDB/pull/461.html#retrieve-multiple-items-from-an-object-store
GC::Ref<JS::Array> retrieve_multiple_items_from_an_object_store(JS::Realm& realm, GC::Ref<ObjectStore> store, GC::Ref<IDBKeyRange> range, RecordKind kind, Bindings::IDBCursorDirection direction, Optional<WebIDL::UnsignedLong> count)
{
// 1. If count is not given or is 0 (zero), let count be infinity.
if (count.has_value() && *count == 0)
count = OptionalNone();
// 2. Let records an empty list.
GC::ConservativeVector<ObjectStoreRecord> records(realm.heap());
// 3. If direction is "next" or "nextunique", set records to the first count of stores list of records whose key is in range.
if (direction == Bindings::IDBCursorDirection::Next || direction == Bindings::IDBCursorDirection::Nextunique) {
records.extend(store->first_n_in_range(range, count));
}
// 4. If direction is "prev" or "prevunique", set records to the last count of stores list of records whose key is in range.
if (direction == Bindings::IDBCursorDirection::Prev || direction == Bindings::IDBCursorDirection::Prevunique) {
records.extend(store->last_n_in_range(range, count));
}
// 5. Let list be an empty list.
auto list = MUST(JS::Array::create(realm, records.size()));
// 6. For each record of records, switching on kind:
for (u32 i = 0; i < records.size(); ++i) {
auto& record = records[i];
switch (kind) {
case RecordKind::Key: {
// 1. Let key be the result of converting a key to a value with records key.
auto key = convert_a_key_to_a_value(realm, record.key);
// 2. Append key to list.
MUST(list->create_data_property_or_throw(i, key));
break;
}
case RecordKind::Value: {
// 1. Let serialized be records value.
auto serialized = record.value;
// 2. Let value be ! StructuredDeserialize(serialized, targetRealm).
auto entry = MUST(HTML::structured_deserialize(realm.vm(), serialized, realm));
// 3. Append entry to list.
MUST(list->create_data_property_or_throw(i, entry));
break;
}
case RecordKind::Record: {
// 1. Let key be the records key.
auto key = record.key;
// 2. Let serialized be records value.
auto serialized = record.value;
// 3. Let value be ! StructuredDeserialize(serialized, targetRealm).
auto value = MUST(HTML::structured_deserialize(realm.vm(), serialized, realm));
// 4. Let record snapshot be a new record snapshot with its key set to key, value set to value, and primary key set to key.
auto record_snapshot = IDBRecord::create(realm, key, value, key);
// 5. Append record snapshot to list.
MUST(list->create_data_property_or_throw(i, record_snapshot));
break;
}
}
}
// 5. Return list.
return list;
}
// https://w3c.github.io/IndexedDB/#retrieve-multiple-keys-from-an-object-store // https://w3c.github.io/IndexedDB/#retrieve-multiple-keys-from-an-object-store
GC::Ref<JS::Array> retrieve_multiple_keys_from_an_object_store(JS::Realm& realm, GC::Ref<ObjectStore> store, GC::Ref<IDBKeyRange> range, Optional<WebIDL::UnsignedLong> count) GC::Ref<JS::Array> retrieve_multiple_keys_from_an_object_store(JS::Realm& realm, GC::Ref<ObjectStore> store, GC::Ref<IDBKeyRange> range, Optional<WebIDL::UnsignedLong> count)
{ {

View file

@ -8,6 +8,7 @@
#include <AK/Variant.h> #include <AK/Variant.h>
#include <LibJS/Runtime/Realm.h> #include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/IDBCursorPrototype.h>
#include <LibWeb/HTML/DOMStringList.h> #include <LibWeb/HTML/DOMStringList.h>
#include <LibWeb/IndexedDB/IDBKeyRange.h> #include <LibWeb/IndexedDB/IDBKeyRange.h>
#include <LibWeb/IndexedDB/IDBRequest.h> #include <LibWeb/IndexedDB/IDBRequest.h>
@ -17,6 +18,12 @@
namespace Web::IndexedDB { namespace Web::IndexedDB {
enum class RecordKind {
Key,
Value,
Record
};
using KeyPath = Variant<String, Vector<String>>; using KeyPath = Variant<String, Vector<String>>;
using RecordSource = Variant<GC::Ref<ObjectStore>, GC::Ref<Index>>; using RecordSource = Variant<GC::Ref<ObjectStore>, GC::Ref<Index>>;
@ -59,5 +66,6 @@ GC::Ref<JS::Array> retrieve_multiple_values_from_an_index(JS::Realm&, GC::Ref<In
void queue_a_database_task(GC::Ref<GC::Function<void()>>); void queue_a_database_task(GC::Ref<GC::Function<void()>>);
bool cleanup_indexed_database_transactions(GC::Ref<HTML::EventLoop>); bool cleanup_indexed_database_transactions(GC::Ref<HTML::EventLoop>);
bool is_a_potentially_valid_key_range(JS::Realm&, JS::Value); bool is_a_potentially_valid_key_range(JS::Realm&, JS::Value);
GC::Ref<JS::Array> retrieve_multiple_items_from_an_object_store(JS::Realm&, GC::Ref<ObjectStore>, GC::Ref<IDBKeyRange>, RecordKind, Bindings::IDBCursorDirection, Optional<WebIDL::UnsignedLong>);
} }

View file

@ -103,4 +103,18 @@ GC::ConservativeVector<ObjectStoreRecord> ObjectStore::first_n_in_range(GC::Ref<
return records; return records;
} }
GC::ConservativeVector<ObjectStoreRecord> ObjectStore::last_n_in_range(GC::Ref<IDBKeyRange> range, Optional<WebIDL::UnsignedLong> count)
{
GC::ConservativeVector<ObjectStoreRecord> records(range->heap());
for (auto const& record : m_records.in_reverse()) {
if (range->is_in_range(record.key))
records.append(record);
if (count.has_value() && records.size() >= *count)
break;
}
return records;
}
} }

View file

@ -52,6 +52,7 @@ public:
Optional<ObjectStoreRecord&> first_in_range(GC::Ref<IDBKeyRange> range); Optional<ObjectStoreRecord&> first_in_range(GC::Ref<IDBKeyRange> range);
void clear_records(); void clear_records();
GC::ConservativeVector<ObjectStoreRecord> first_n_in_range(GC::Ref<IDBKeyRange> range, Optional<WebIDL::UnsignedLong> count); GC::ConservativeVector<ObjectStoreRecord> first_n_in_range(GC::Ref<IDBKeyRange> range, Optional<WebIDL::UnsignedLong> count);
GC::ConservativeVector<ObjectStoreRecord> last_n_in_range(GC::Ref<IDBKeyRange> range, Optional<WebIDL::UnsignedLong> count);
protected: protected:
virtual void visit_edges(Visitor&) override; virtual void visit_edges(Visitor&) override;