Skip to content

Truly independent web browser

LibWeb: Create IndexedDB indexes as a request of their transaction

Andreas Kling3dd9863092cf
diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp b/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp
index e1bcc264eae..6b4e56d8ab0 100644
--- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp
+++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp
@@ -201,6 +201,9 @@ WebIDL::ExceptionOr<GC::Ref<IDBIndex>> IDBObjectStore::create_index(Utf16String
// AD-HOC: Log the creation for potential revert on abort.
store->mutation_log()->note_index_created(index);
+ // AD-HOC: Fill the index with the records of store, as a request after the ones made before.
+ queue_the_creation_of_an_index(*this, index);
+
// 13. Return a new index handle associated with index and this.
return IDBIndex::create(index, *this);
}
diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp
index cd54ac9f6c4..846fec39edd 100644
--- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp
+++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp
@@ -1524,6 +1524,79 @@ JS::Value delete_records_from_an_object_store(GC::Ref<ObjectStore> store, GC::Re
return JS::js_undefined();
}
+// https://w3c.github.io/IndexedDB/#store-a-record-into-an-object-store
+// NB: The steps of storing a record into an object store for one index which references store, as step 5 runs them for
+// each such index.
+static WebIDL::ExceptionOr<void> store_a_record_into_an_index(JS::Object& global_object, Index& index, JS::Value value, GC::Ref<Key> key)
+{
+ auto& realm = HTML::relevant_realm(global_object);
+
+ // 1. Let index key be the result of extracting a key from a value using a key path with value, index’s key path, and index’s multiEntry flag.
+ auto completion_index_key = extract_a_key_from_a_value_using_a_key_path(realm, value, index.key_path(), index.multi_entry());
+
+ // 2. If index key is an exception, or invalid, or failure, take no further actions for index, and continue these steps for the next index.
+ if (completion_index_key.is_error())
+ return {};
+
+ auto failure_index_key = completion_index_key.release_value();
+ if (failure_index_key.is_error())
+ return {};
+
+ auto index_key = failure_index_key.release_value();
+ if (index_key->is_invalid())
+ return {};
+
+ auto index_multi_entry = index.multi_entry();
+ auto index_key_is_array = index_key->type() == Key::KeyType::Array;
+ auto index_is_unique = index.unique();
+
+ // 3. If index’s multiEntry flag is false, or if index key is not an array key,
+ // and if index already contains a record with key equal to index key,
+ // and index’s unique flag is true,
+ // then this operation failed with a "ConstraintError" DOMException.
+ // Abort this algorithm without taking any further steps.
+ if ((!index_multi_entry || !index_key_is_array) && index_is_unique && index.has_record_with_key(index_key))
+ return WebIDL::ConstraintError::create("Record already exists in index"_utf16);
+
+ // 4. If index’s multiEntry flag is true and index key is an array key,
+ // and if index already contains a record with key equal to any of the subkeys of index key,
+ // and index’s unique flag is true,
+ // then this operation failed with a "ConstraintError" DOMException.
+ // Abort this algorithm without taking any further steps.
+ if (index_multi_entry && index_key_is_array && index_is_unique) {
+ for (auto const& subkey : index_key->subkeys()) {
+ if (index.has_record_with_key(*subkey))
+ return WebIDL::ConstraintError::create("Record already exists in index"_utf16);
+ }
+ }
+
+ // 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);
+ }
+
+ // 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);
+ }
+ }
+
+ return {};
+}
+
// https://w3c.github.io/IndexedDB/#store-a-record-into-an-object-store
WebIDL::ExceptionOr<GC::Ptr<Key>> store_a_record_into_an_object_store(JS::Realm& realm, GC::Ref<ObjectStore> store, JS::Value value, GC::Ptr<Key> key, bool no_overwrite)
{
@@ -1570,72 +1643,77 @@ WebIDL::ExceptionOr<GC::Ptr<Key>> store_a_record_into_an_object_store(JS::Realm&
// 5. For each index which references store:
for (auto const& [name, index] : store->index_set()) {
- // 1. Let index key be the result of extracting a key from a value using a key path with value, index’s key path, and index’s multiEntry flag.
- auto completion_index_key = extract_a_key_from_a_value_using_a_key_path(realm, value, index->key_path(), index->multi_entry());
-
- // 2. If index key is an exception, or invalid, or failure, take no further actions for index, and continue these steps for the next index.
- if (completion_index_key.is_error())
+ // AD-HOC: An index whose creation request has not been processed yet gets the records of store once it is.
+ if (index->is_pending_creation())
continue;
- auto failure_index_key = completion_index_key.release_value();
- if (failure_index_key.is_error())
- continue;
+ TRY(store_a_record_into_an_index(realm.global_object(), index, value, *key));
+ }
- auto index_key = failure_index_key.release_value();
- if (index_key->is_invalid())
- continue;
+ // 6. Return key.
+ return key;
+}
- auto index_multi_entry = index->multi_entry();
- auto index_key_is_array = index_key->type() == Key::KeyType::Array;
- auto index_is_unique = index->unique();
-
- // 3. If index’s multiEntry flag is false, or if index key is not an array key,
- // and if index already contains a record with key equal to index key,
- // and index’s unique flag is true,
- // then this operation failed with a "ConstraintError" DOMException.
- // Abort this algorithm without taking any further steps.
- if ((!index_multi_entry || !index_key_is_array) && index_is_unique && index->has_record_with_key(index_key))
- return WebIDL::ConstraintError::create("Record already exists in index"_utf16);
-
- // 4. If index’s multiEntry flag is true and index key is an array key,
- // and if index already contains a record with key equal to any of the subkeys of index key,
- // and index’s unique flag is true,
- // then this operation failed with a "ConstraintError" DOMException.
- // Abort this algorithm without taking any further steps.
- if (index_multi_entry && index_key_is_array && index_is_unique) {
- for (auto const& subkey : index_key->subkeys()) {
- if (index->has_record_with_key(*subkey))
- return WebIDL::ConstraintError::create("Record already exists in index"_utf16);
- }
- }
+// AD-HOC: The spec deserializes stored records infallibly, but a record read back from storage can be
+// corrupt or written by a newer format version, so treat failure as the "error occurs while
+// reading the value from the underlying storage" case: a "NotReadableError" DOMException.
+static WebIDL::ExceptionOr<JS::Value> deserialize_a_stored_record(JS::Realm& realm, HTML::StorageSerializationRecord const& serialized)
+{
+ auto value = HTML::structured_deserialize(realm.vm(), serialized, realm);
+ if (value.is_exception())
+ return WebIDL::NotReadableError::create(realm, "Stored value could not be read"_utf16);
+ return value;
+}
- // 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);
- }
+// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-createindex
+// AD-HOC: The index creation itself is processed as an asynchronous request within the upgrade transaction, so the
+// index gets the records that its object store has once the requests made before createIndex() are processed.
+// If those records violate the index's unique flag, the upgrade transaction is aborted with a "ConstraintError"
+// DOMException.
+void queue_the_creation_of_an_index(GC::Ref<IDBObjectStore> handle, GC::Ref<Index> index)
+{
+ auto& realm = HTML::relevant_realm(handle->relevant_global_object());
+ auto transaction = handle->transaction();
+ index->set_pending_creation(true);
+
+ // NB: The request has no transaction, so that nothing observes it, and the events fired at it if the transaction
+ // aborts first go nowhere.
+ auto request = IDBRequest::create(transaction->connection()->relevant_global_scope().this_impl(), handle);
+ transaction->request_list().enqueue(request, GC::create_function(GC::Heap::the(), [&realm, transaction, index, request]() {
+ if (request->aborted())
+ return;
- // 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);
+ index->set_pending_creation(false);
+ auto store_the_records_into_the_index = [&]() -> WebIDL::ExceptionOr<void> {
+ for (auto const& record : index->object_store()->records()) {
+ auto value = TRY(deserialize_a_stored_record(realm, *record.value));
+ TRY(store_a_record_into_an_index(realm.global_object(), index, value, record.key));
}
+ return {};
+ };
+ auto result = store_the_records_into_the_index();
+
+ request->set_processed(true);
+
+ if (result.is_exception()) {
+ abort_a_transaction(transaction, result.exception().get<GC::Ref<WebIDL::DOMException>>());
+ return;
}
- }
- // 6. Return key.
- return key;
+ // NB: Finish up like a request whose success event nobody listens to: the transaction commits once its request
+ // list is empty, if it is inactive by then.
+ queue_a_database_task(GC::create_function(GC::Heap::the(), [&realm, transaction, request]() {
+ if (request->aborted())
+ return;
+
+ transaction->request_list().remove(request);
+ if (transaction->state() == IDBTransaction::TransactionState::Inactive && transaction->request_list().is_empty())
+ commit_a_transaction(realm, transaction);
+ transaction->request_list().check_all_processed();
+ }));
+
+ transaction->request_list().on_request_processed();
+ }));
}
// https://w3c.github.io/IndexedDB/#convert-a-value-to-a-key-range
@@ -1682,17 +1760,6 @@ JS::Value count_the_records_in_a_range(RecordSource source, GC::Ref<IDBKeyRange>
return JS::Value(count);
}
-// AD-HOC: The spec deserializes stored records infallibly, but a record read back from storage can be
-// corrupt or written by a newer format version, so treat failure as the "error occurs while
-// reading the value from the underlying storage" case: a "NotReadableError" DOMException.
-static WebIDL::ExceptionOr<JS::Value> deserialize_a_stored_record(JS::Realm& realm, HTML::StorageSerializationRecord const& serialized)
-{
- auto value = HTML::structured_deserialize(realm.vm(), serialized, realm);
- if (value.is_exception())
- return WebIDL::NotReadableError::create(realm, "Stored value could not be read"_utf16);
- return value;
-}
-
// https://w3c.github.io/IndexedDB/#retrieve-a-value-from-an-object-store
WebIDL::ExceptionOr<JS::Value> retrieve_a_value_from_an_object_store(JS::Realm& realm, GC::Ref<ObjectStore> store, GC::Ref<IDBKeyRange> range)
{
diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h
index 2b5c38cb76f..5d13ffe3942 100644
--- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h
+++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h
@@ -59,6 +59,7 @@ GC::Ref<IDBRequest> asynchronously_execute_a_request(JS::Realm&, IDBRequestSourc
void inject_a_key_into_a_value_using_a_key_path(JS::Realm&, JS::Value, GC::Ref<Key>, KeyPath const&);
JS::Value delete_records_from_an_object_store(GC::Ref<ObjectStore>, GC::Ref<IDBKeyRange>);
WebIDL::ExceptionOr<GC::Ptr<Key>> store_a_record_into_an_object_store(JS::Realm&, GC::Ref<ObjectStore>, JS::Value, GC::Ptr<Key>, bool);
+void queue_the_creation_of_an_index(GC::Ref<IDBObjectStore>, GC::Ref<Index>);
WebIDL::ExceptionOr<GC::Ref<IDBKeyRange>> convert_a_value_to_a_key_range(JS::Realm&, Optional<JS::Value>, bool = false);
JS::Value count_the_records_in_a_range(RecordSource, GC::Ref<IDBKeyRange>);
WebIDL::ExceptionOr<JS::Value> retrieve_a_value_from_an_object_store(JS::Realm&, GC::Ref<ObjectStore>, GC::Ref<IDBKeyRange>);
diff --git a/Libraries/LibWeb/IndexedDB/Internal/Index.h b/Libraries/LibWeb/IndexedDB/Internal/Index.h
index b36de9e91d8..473c87973ea 100644
--- a/Libraries/LibWeb/IndexedDB/Internal/Index.h
+++ b/Libraries/LibWeb/IndexedDB/Internal/Index.h
@@ -36,6 +36,9 @@ public:
[[nodiscard]] bool is_deleted() const { return m_deleted; }
void set_deleted(bool deleted) { m_deleted = deleted; }
+ [[nodiscard]] bool is_pending_creation() const { return m_pending_creation; }
+ void set_pending_creation(bool pending_creation) { m_pending_creation = pending_creation; }
+
[[nodiscard]] bool has_record_with_key(GC::Ref<Key> key);
void clear_records();
Optional<IndexRecord&> first_in_range(GC::Ref<IDBKeyRange> range);
@@ -73,6 +76,10 @@ private:
KeyPath m_key_path;
bool m_deleted { false };
+
+ // NB: Whether the request that creates the index in its upgrade transaction has yet to be processed. Until then,
+ // storing records into the object store leaves the index alone.
+ bool m_pending_creation { false };
};
}
diff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_createIndex.any.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_createIndex.any.txt
index dc5296f4263..a707ef583af 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_createIndex.any.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_createIndex.any.txt
@@ -2,10 +2,10 @@ Harness status: OK
Found 21 tests
-17 Pass
-4 Fail
+18 Pass
+3 Fail
Pass Returns an IDBIndex and the properties are set correctly
-Fail Attempt to create an index that requires unique values on an object store already contains duplicates
+Pass Attempt to create an index that requires unique values on an object store already contains duplicates
Pass The index is usable right after being made
Fail Event ordering for a later deleted index
Pass Empty keyPath
diff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_createIndex.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_createIndex.any.worker.txt
index 4003f34f79b..a707ef583af 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_createIndex.any.worker.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_createIndex.any.worker.txt
@@ -1,11 +1,11 @@
-Harness status: Error
+Harness status: OK
Found 21 tests
-17 Pass
-4 Fail
+18 Pass
+3 Fail
Pass Returns an IDBIndex and the properties are set correctly
-Fail Attempt to create an index that requires unique values on an object store already contains duplicates
+Pass Attempt to create an index that requires unique values on an object store already contains duplicates
Pass The index is usable right after being made
Fail Event ordering for a later deleted index
Pass Empty keyPath