diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp index 4037204a7e5..045fad8d10e 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp @@ -1412,13 +1412,29 @@ WebIDL::ExceptionOr> store_a_record_into_an_object_store(JS::Realm& } } - // FIXME: 5. If index’s multiEntry flag is false, or if index key is not an array key + // 5. If index’s 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 index’s 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 index’s multiEntry flag is true and index key is an array key, + // 6. If index’s 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. diff --git a/Libraries/LibWeb/IndexedDB/Internal/Index.cpp b/Libraries/LibWeb/IndexedDB/Internal/Index.cpp index ebd280b08d0..70cde9ea2aa 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Index.cpp +++ b/Libraries/LibWeb/IndexedDB/Internal/Index.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include @@ -107,4 +108,18 @@ u64 Index::count_records_in_range(GC::Ref range) return count; } +void Index::store_a_record(IndexRecord const& record) +{ + m_records.append(record); + + // NOTE: The record is stored in index’s 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; + }); +} + } diff --git a/Libraries/LibWeb/IndexedDB/Internal/Index.h b/Libraries/LibWeb/IndexedDB/Internal/Index.h index 730c4ad2ba0..b8632a70c45 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Index.h +++ b/Libraries/LibWeb/IndexedDB/Internal/Index.h @@ -44,6 +44,7 @@ public: Optional first_in_range(GC::Ref range); GC::ConservativeVector first_n_in_range(GC::Ref range, Optional count); u64 count_records_in_range(GC::Ref range); + void store_a_record(IndexRecord const& record); HTML::SerializationRecord referenced_value(IndexRecord const& index_record) const;