From fe5d5639efc3d6170a2ee8949cca47df6c57d33c Mon Sep 17 00:00:00 2001 From: stelar7 Date: Wed, 9 Jul 2025 11:49:05 +0200 Subject: [PATCH] LibWeb/IDB: Move Records and give more descriptive names --- .../LibWeb/IndexedDB/Internal/Algorithms.cpp | 34 +++++++++---------- Libraries/LibWeb/IndexedDB/Internal/Index.h | 7 +--- .../LibWeb/IndexedDB/Internal/ObjectStore.cpp | 8 ++--- .../LibWeb/IndexedDB/Internal/ObjectStore.h | 17 ++++------ Libraries/LibWeb/IndexedDB/Internal/Record.h | 27 +++++++++++++++ 5 files changed, 55 insertions(+), 38 deletions(-) create mode 100644 Libraries/LibWeb/IndexedDB/Internal/Record.h diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp index ed6abd8f1f4..62fb515e0ae 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp @@ -1372,7 +1372,7 @@ WebIDL::ExceptionOr> store_a_record_into_an_object_store(JS::Realm& // 4. Store a record in store containing key as its key and ! StructuredSerializeForStorage(value) as its value. // The record is stored in the object store’s list of records such that the list is sorted according to the key of the records in ascending order. - Record record = { + ObjectStoreRecord record = { .key = *key, .value = MUST(HTML::structured_serialize_for_storage(realm.vm(), value)), }; @@ -1523,11 +1523,11 @@ GC::Ptr iterate_a_cursor(JS::Realm& realm, GC::Ref cursor, VERIFY(source.has>() && direction_is_next_or_prev); // 4. Let records be the list of records in source. - Variant, ReadonlySpan> records = source.visit( - [](GC::Ref object_store) -> Variant, ReadonlySpan> { + Variant, ReadonlySpan> records = source.visit( + [](GC::Ref object_store) -> Variant, ReadonlySpan> { return object_store->records(); }, - [](GC::Ref index) -> Variant, ReadonlySpan> { + [](GC::Ref index) -> Variant, ReadonlySpan> { return index->records(); }); @@ -1543,7 +1543,7 @@ GC::Ptr iterate_a_cursor(JS::Realm& realm, GC::Ref cursor, // 8. If count is not given, let count be 1. // NOTE: This is handled by the default parameter - auto next_requirements = [&](Variant const& record) -> bool { + auto next_requirements = [&](Variant const& record) -> bool { // * If key is defined: if (key) { // * The record’s key is greater than or equal to key. @@ -1572,7 +1572,7 @@ GC::Ptr iterate_a_cursor(JS::Realm& realm, GC::Ref cursor, // * If position is defined and source is an object store: if (position && source.has>()) { - auto const& inner_record = record.get(); + auto const& inner_record = record.get(); // * The record’s key is greater than position. if (!Key::greater_than(inner_record.key, *position)) @@ -1602,7 +1602,7 @@ GC::Ptr iterate_a_cursor(JS::Realm& realm, GC::Ref cursor, return is_in_range; }; - auto next_unique_requirements = [&](Variant const& record) -> bool { + auto next_unique_requirements = [&](Variant const& record) -> bool { // * If key is defined: if (key) { // * The record’s key is greater than or equal to key. @@ -1639,7 +1639,7 @@ GC::Ptr iterate_a_cursor(JS::Realm& realm, GC::Ref cursor, return is_in_range; }; - auto prev_requirements = [&](Variant const& record) -> bool { + auto prev_requirements = [&](Variant const& record) -> bool { // * If key is defined: if (key) { // * The record’s key is less than or equal to key. @@ -1668,7 +1668,7 @@ GC::Ptr iterate_a_cursor(JS::Realm& realm, GC::Ref cursor, // * If position is defined and source is an object store: if (position && source.has>()) { - auto const& inner_record = record.get(); + auto const& inner_record = record.get(); // * The record’s key is less than position. if (!Key::less_than(inner_record.key, *position)) @@ -1698,7 +1698,7 @@ GC::Ptr iterate_a_cursor(JS::Realm& realm, GC::Ref cursor, return is_in_range; }; - auto prev_unique_requirements = [&](Variant const& record) -> bool { + auto prev_unique_requirements = [&](Variant const& record) -> bool { // * If key is defined: if (key) { // * The record’s key is less than or equal to key. @@ -1736,13 +1736,13 @@ GC::Ptr iterate_a_cursor(JS::Realm& realm, GC::Ref cursor, }; // 9. While count is greater than 0: - Variant found_record; + Variant found_record; while (count > 0) { // 1. Switch on direction: switch (direction) { case Bindings::IDBCursorDirection::Next: { // Let found record be the first record in records which satisfy all of the following requirements: - found_record = records.visit([&](auto content) -> Variant { + found_record = records.visit([&](auto content) -> Variant { auto value = content.first_matching(next_requirements); if (value.has_value()) return *value; @@ -1753,7 +1753,7 @@ GC::Ptr iterate_a_cursor(JS::Realm& realm, GC::Ref cursor, } case Bindings::IDBCursorDirection::Nextunique: { // Let found record be the first record in records which satisfy all of the following requirements: - found_record = records.visit([&](auto content) -> Variant { + found_record = records.visit([&](auto content) -> Variant { auto value = content.first_matching(next_unique_requirements); if (value.has_value()) return *value; @@ -1764,7 +1764,7 @@ GC::Ptr iterate_a_cursor(JS::Realm& realm, GC::Ref cursor, } case Bindings::IDBCursorDirection::Prev: { // Let found record be the last record in records which satisfy all of the following requirements: - found_record = records.visit([&](auto content) -> Variant { + found_record = records.visit([&](auto content) -> Variant { auto value = content.last_matching(prev_requirements); if (value.has_value()) return *value; @@ -1776,7 +1776,7 @@ GC::Ptr iterate_a_cursor(JS::Realm& realm, GC::Ref cursor, case Bindings::IDBCursorDirection::Prevunique: { // Let temp record be the last record in records which satisfy all of the following requirements: - auto temp_record = records.visit([&](auto content) -> Variant { + auto temp_record = records.visit([&](auto content) -> Variant { auto value = content.last_matching(prev_unique_requirements); if (value.has_value()) return *value; @@ -1790,7 +1790,7 @@ GC::Ptr iterate_a_cursor(JS::Realm& realm, GC::Ref cursor, [](Empty) -> GC::Ref { VERIFY_NOT_REACHED(); }, [](auto const& record) { return record.key; }); - found_record = records.visit([&](auto content) -> Variant { + found_record = records.visit([&](auto content) -> Variant { auto value = content.first_matching([&](auto const& content_record) { return Key::equals(content_record.key, temp_record_key); }); @@ -1853,7 +1853,7 @@ GC::Ptr iterate_a_cursor(JS::Realm& realm, GC::Ref cursor, // 1. Let serialized be found record’s value if source is an object store, or found record’s referenced value otherwise. auto serialized = source.visit( [&](GC::Ref) { - return found_record.get().value; + return found_record.get().value; }, [&](GC::Ref index) { return index->referenced_value(found_record.get()); diff --git a/Libraries/LibWeb/IndexedDB/Internal/Index.h b/Libraries/LibWeb/IndexedDB/Internal/Index.h index 8be499b1058..6f6d63c8ca7 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Index.h +++ b/Libraries/LibWeb/IndexedDB/Internal/Index.h @@ -11,17 +11,12 @@ #include #include #include +#include namespace Web::IndexedDB { using KeyPath = Variant>; -// https://w3c.github.io/IndexedDB/#index-list-of-records -struct IndexRecord { - GC::Ref key; - GC::Ref value; -}; - // https://w3c.github.io/IndexedDB/#index-construct class Index : public JS::Cell { GC_CELL(Index, JS::Cell); diff --git a/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.cpp b/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.cpp index 7423b934348..81e31c2338b 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.cpp +++ b/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.cpp @@ -57,7 +57,7 @@ bool ObjectStore::has_record_with_key(GC::Ref key) return index != m_records.end(); } -void ObjectStore::store_a_record(Record const& record) +void ObjectStore::store_a_record(ObjectStoreRecord const& record) { m_records.append(record); @@ -77,7 +77,7 @@ u64 ObjectStore::count_records_in_range(GC::Ref range) return count; } -Optional ObjectStore::first_in_range(GC::Ref range) +Optional ObjectStore::first_in_range(GC::Ref range) { return m_records.first_matching([&](auto const& record) { return range->is_in_range(record.key); @@ -89,9 +89,9 @@ void ObjectStore::clear_records() m_records.clear(); } -GC::ConservativeVector ObjectStore::first_n_in_range(GC::Ref range, Optional count) +GC::ConservativeVector ObjectStore::first_n_in_range(GC::Ref range, Optional count) { - GC::ConservativeVector records(range->heap()); + GC::ConservativeVector records(range->heap()); for (auto const& record : m_records) { if (range->is_in_range(record.key)) records.append(record); diff --git a/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h b/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h index 0376636af97..2f02da71cd5 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h +++ b/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h @@ -18,17 +18,12 @@ #include #include #include +#include namespace Web::IndexedDB { using KeyPath = Variant>; -// https://w3c.github.io/IndexedDB/#object-store-record -struct Record { - GC::Ref key; - HTML::SerializationRecord value; -}; - // https://w3c.github.io/IndexedDB/#object-store-construct class ObjectStore : public JS::Cell { GC_CELL(ObjectStore, JS::Cell); @@ -48,15 +43,15 @@ public: AK::HashMap>& index_set() { return m_indexes; } GC::Ref database() const { return m_database; } - ReadonlySpan records() const { return m_records; } + ReadonlySpan records() const { return m_records; } void remove_records_in_range(GC::Ref range); bool has_record_with_key(GC::Ref key); - void store_a_record(Record const& record); + void store_a_record(ObjectStoreRecord const& record); u64 count_records_in_range(GC::Ref range); - Optional first_in_range(GC::Ref range); + Optional first_in_range(GC::Ref range); void clear_records(); - GC::ConservativeVector first_n_in_range(GC::Ref range, Optional count); + GC::ConservativeVector first_n_in_range(GC::Ref range, Optional count); protected: virtual void visit_edges(Visitor&) override; @@ -80,7 +75,7 @@ private: Optional m_key_generator; // An object store has a list of records - Vector m_records; + Vector m_records; }; } diff --git a/Libraries/LibWeb/IndexedDB/Internal/Record.h b/Libraries/LibWeb/IndexedDB/Internal/Record.h new file mode 100644 index 00000000000..d4ef82e797c --- /dev/null +++ b/Libraries/LibWeb/IndexedDB/Internal/Record.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025, stelar7 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Web::IndexedDB { + +// https://w3c.github.io/IndexedDB/#object-store-record +struct ObjectStoreRecord { + GC::Ref key; + HTML::SerializationRecord value; +}; + +// https://w3c.github.io/IndexedDB/#index-list-of-records +struct IndexRecord { + GC::Ref key; + GC::Ref value; +}; + +} \ No newline at end of file