LibWeb/IDB: Implement storing of index records

This commit is contained in:
stelar7 2025-05-13 23:06:42 +02:00 committed by Jelle Raaijmakers
commit 46ecf239c4
Notes: github-actions[bot] 2025-05-14 15:18:57 +00:00
3 changed files with 34 additions and 2 deletions

View file

@ -1412,13 +1412,29 @@ WebIDL::ExceptionOr<GC::Ptr<Key>> store_a_record_into_an_object_store(JS::Realm&
}
}
// FIXME: 5. If indexs multiEntry flag is false, or if index key is not an array key
// 5. If indexs multiEntry flag is false, or if index key is not an array key
// then store a record in index containing index key as its key and key as its value.
// The record is stored in indexs list of records such that the list is sorted primarily on the records keys,
// and secondarily on the records values, in ascending order.
if (!index_multi_entry || !index_key_is_array) {
IndexRecord index_record = {
.key = *index_key,
.value = *key,
};
index->store_a_record(index_record);
}
// // FIXME: 6. If indexs multiEntry flag is true and index key is an array key,
// 6. If indexs multiEntry flag is true and index key is an array key,
// then for each subkey of the subkeys of index key store a record in index containing subkey as its key and key as its value.
if (index_multi_entry && index_key_is_array) {
for (auto const& subkey : index_key->subkeys()) {
IndexRecord index_record = {
.key = *subkey,
.value = *key,
};
index->store_a_record(index_record);
}
}
}
// 6. Return key.

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/QuickSort.h>
#include <LibWeb/IndexedDB/Internal/Index.h>
#include <LibWeb/IndexedDB/Internal/ObjectStore.h>
@ -107,4 +108,18 @@ u64 Index::count_records_in_range(GC::Ref<IDBKeyRange> range)
return count;
}
void Index::store_a_record(IndexRecord const& record)
{
m_records.append(record);
// NOTE: The record is stored in indexs list of records such that the list is sorted primarily on the records keys, and secondarily on the records values, in ascending order.
AK::quick_sort(m_records, [](auto const& a, auto const& b) {
auto key_comparison = Key::compare_two_keys(a.key, b.key);
if (key_comparison != 0)
return key_comparison < 0;
return Key::compare_two_keys(a.value, b.value) < 0;
});
}
}

View file

@ -44,6 +44,7 @@ public:
Optional<IndexRecord&> first_in_range(GC::Ref<IDBKeyRange> range);
GC::ConservativeVector<IndexRecord> first_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);
HTML::SerializationRecord referenced_value(IndexRecord const& index_record) const;