LibWeb/IDB: Implement retrieve_multiple_items_from_an_index

This commit is contained in:
stelar7 2025-07-10 09:15:01 +02:00 committed by Jelle Raaijmakers
commit bac1c84241
Notes: github-actions[bot] 2025-08-27 14:15:08 +00:00
4 changed files with 159 additions and 0 deletions

View file

@ -2186,4 +2186,147 @@ bool is_a_potentially_valid_key_range(JS::Realm& realm, JS::Value value)
return false;
}
GC::Ref<JS::Array> retrieve_multiple_items_from_an_index(JS::Realm& target_realm, GC::Ref<Index> index, 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 be a an empty list.
GC::ConservativeVector<IndexRecord> records(target_realm.heap());
// 3. Switching on direction:
switch (direction) {
// "next"
case Bindings::IDBCursorDirection::Next: {
// 1. Set records to the first count of indexs list of records whose key is in range.
records.extend(index->first_n_in_range(range, count));
break;
}
// "nextunique"
case Bindings::IDBCursorDirection::Nextunique: {
// 1. Let range records be a list containing the indexs list of records whose key is in range.
auto range_records = index->first_n_in_range(range, OptionalNone());
// 2. Let range records length be range recordss size.
auto range_records_length = range_records.size();
// 3. Let i be 0.
size_t i = 0;
// 4. While i is less than range records length, then:
while (i < range_records_length) {
// 1. Increase i by 1.
i++;
// 2. if records size is equal to count, then break.
if (records.size() == count)
break;
// 3. If the result of comparing two keys using the keys from |range records[i]| and |range records[i-1]| is equal, then continue.
if (Key::equals(range_records[i].key, range_records[i - 1].key))
continue;
// 4. Else append |range records[i]| to records.
records.append(range_records[i]);
}
break;
}
// "prev"
case Bindings::IDBCursorDirection::Prev: {
// 1. Set records to the last count of indexs list of records whose key is in range.
records.extend(index->last_n_in_range(range, count));
break;
}
// "prevunique"
case Bindings::IDBCursorDirection::Prevunique: {
// 1. Let range records be a list containing the indexs list of records whose key is in range.
auto range_records = index->first_n_in_range(range, OptionalNone());
// 2. Let range records length be range recordss size.
auto range_records_length = range_records.size();
// 3. Let i be 0.
size_t i = 0;
// 4. While i is less than range records length, then:
while (i < range_records_length) {
// 1. Increase i by 1.
i++;
// 2. if records size is equal to count, then break.
if (records.size() == count)
break;
// 3. If the result of comparing two keys using the keys from |range records[i]| and |range records[i-1]| is equal, then continue.
if (i > 0 && Key::equals(range_records[i].key, range_records[i - 1].key))
continue;
// 4. Else prepend |range records[i]| to records.
records.prepend(range_records[i]);
}
break;
}
}
// 4. Let list be an empty list.
auto list = MUST(JS::Array::create(target_realm, records.size()));
// 5. For each record of records, switching on kind:
for (u32 i = 0; i < records.size(); ++i) {
auto& record = records[i];
switch (kind) {
// "key"
case RecordKind::Key: {
// 1. Let key be the result of converting a key to a value with records value.
auto key = convert_a_key_to_a_value(target_realm, record.value);
// 2. Append key to list.
MUST(list->create_data_property_or_throw(i, key));
break;
}
// "value"
case RecordKind::Value: {
// 1. Let serialized be records referenced value.
auto serialized = index->referenced_value(record);
// 2. Let value be ! StructuredDeserialize(serialized, targetRealm).
auto value = MUST(HTML::structured_deserialize(target_realm.vm(), serialized, target_realm));
// 3. Append value to list.
MUST(list->create_data_property_or_throw(i, value));
break;
}
// "record"
case RecordKind::Record: {
// 1. Let index key be the records key.
auto index_key = record.key;
// 2. Let key be the records value.
auto key = record.value;
// 3. Let serialized be records referenced value.
auto serialized = index->referenced_value(record);
// 4. Let value be ! StructuredDeserialize(serialized, targetRealm).
auto value = MUST(HTML::structured_deserialize(target_realm.vm(), serialized, target_realm));
// 5. Let record snapshot be a new record snapshot with its key set to index key, value set to value, and primary key set to key.
auto record_snapshot = IDBRecord::create(target_realm, index_key, value, key);
// 6. Append record snapshot to list.
MUST(list->create_data_property_or_throw(i, record_snapshot));
break;
}
}
}
// 6. Return list.
return list;
}
}

View file

@ -67,5 +67,6 @@ void queue_a_database_task(GC::Ref<GC::Function<void()>>);
bool cleanup_indexed_database_transactions(GC::Ref<HTML::EventLoop>);
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>);
GC::Ref<JS::Array> retrieve_multiple_items_from_an_index(JS::Realm&, GC::Ref<Index>, GC::Ref<IDBKeyRange>, RecordKind, Bindings::IDBCursorDirection, Optional<WebIDL::UnsignedLong>);
}

View file

@ -98,6 +98,20 @@ GC::ConservativeVector<IndexRecord> Index::first_n_in_range(GC::Ref<IDBKeyRange>
return records;
}
GC::ConservativeVector<IndexRecord> Index::last_n_in_range(GC::Ref<IDBKeyRange> range, Optional<WebIDL::UnsignedLong> count)
{
GC::ConservativeVector<IndexRecord> 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;
}
u64 Index::count_records_in_range(GC::Ref<IDBKeyRange> range)
{
u64 count = 0;

View file

@ -38,6 +38,7 @@ public:
void clear_records();
Optional<IndexRecord&> first_in_range(GC::Ref<IDBKeyRange> range);
GC::ConservativeVector<IndexRecord> first_n_in_range(GC::Ref<IDBKeyRange> range, Optional<WebIDL::UnsignedLong> count);
GC::ConservativeVector<IndexRecord> last_n_in_range(GC::Ref<IDBKeyRange> range, Optional<WebIDL::UnsignedLong> count);
u64 count_records_in_range(GC::Ref<IDBKeyRange> range);
void store_a_record(IndexRecord const& record);
void remove_records_with_value_in_range(GC::Ref<IDBKeyRange> range);