mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 12:19:54 +00:00
LibWeb/IDB: Implement IDBIndex::count
This commit is contained in:
parent
b8bb8345a9
commit
852323009f
Notes:
github-actions[bot]
2025-05-14 15:19:13 +00:00
Author: https://github.com/stelar7
Commit: 852323009f
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4727
Reviewed-by: https://github.com/gmta ✅
Reviewed-by: https://github.com/shannonbooth
7 changed files with 54 additions and 4 deletions
|
@ -265,4 +265,35 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBIndex::get_all_keys(Optional<JS::Val
|
|||
return result;
|
||||
}
|
||||
|
||||
// https://w3c.github.io/IndexedDB/#dom-idbindex-count
|
||||
WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBIndex::count(JS::Value query)
|
||||
{
|
||||
auto& realm = this->realm();
|
||||
|
||||
// 1. Let transaction be this’s transaction.
|
||||
auto transaction = this->transaction();
|
||||
|
||||
// 2. Let index be this’s index.
|
||||
auto index = this->index();
|
||||
|
||||
// FIXME: 3. If index or index’s object store has been deleted, throw an "InvalidStateError" DOMException.
|
||||
|
||||
// 4. If transaction’s state is not active, then throw a "TransactionInactiveError" DOMException.
|
||||
if (transaction->state() != IDBTransaction::TransactionState::Active)
|
||||
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while counting"_string);
|
||||
|
||||
// 5. Let range be the result of converting a value to a key range with query. Rethrow any exceptions.
|
||||
auto range = TRY(convert_a_value_to_a_key_range(realm, query));
|
||||
|
||||
// 6. Let operation be an algorithm to run count the records in a range with index and range.
|
||||
auto operation = GC::Function<WebIDL::ExceptionOr<JS::Value>()>::create(realm.heap(), [index, range] -> WebIDL::ExceptionOr<JS::Value> {
|
||||
return count_the_records_in_a_range(index, range);
|
||||
});
|
||||
|
||||
// 7. Return the result (an IDBRequest) of running asynchronously execute a request with this and operation.
|
||||
auto result = asynchronously_execute_a_request(realm, GC::Ref(*this), operation);
|
||||
dbgln_if(IDB_DEBUG, "Executing request for count with uuid {}", result->uuid());
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ public:
|
|||
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> get_key(JS::Value);
|
||||
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> get_all(Optional<JS::Value>, Optional<WebIDL::UnsignedLong>);
|
||||
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> get_all_keys(Optional<JS::Value>, Optional<WebIDL::UnsignedLong>);
|
||||
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> count(JS::Value);
|
||||
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> open_cursor(JS::Value, Bindings::IDBCursorDirection = Bindings::IDBCursorDirection::Next);
|
||||
|
||||
// The transaction of an index handle is the transaction of its associated object store handle.
|
||||
|
|
|
@ -11,7 +11,7 @@ interface IDBIndex {
|
|||
[NewObject] IDBRequest getKey(any query);
|
||||
[NewObject] IDBRequest getAll(optional any query, optional [EnforceRange] unsigned long count);
|
||||
[NewObject] IDBRequest getAllKeys(optional any query, optional [EnforceRange] unsigned long count);
|
||||
[FIXME, NewObject] IDBRequest count(optional any query);
|
||||
[NewObject] IDBRequest count(optional any query);
|
||||
[NewObject] IDBRequest openCursor(optional any query, optional IDBCursorDirection direction = "next");
|
||||
[FIXME, NewObject] IDBRequest openKeyCursor(optional any query, optional IDBCursorDirection direction = "next");
|
||||
};
|
||||
|
|
|
@ -1453,10 +1453,16 @@ WebIDL::ExceptionOr<GC::Ref<IDBKeyRange>> convert_a_value_to_a_key_range(JS::Rea
|
|||
}
|
||||
|
||||
// https://w3c.github.io/IndexedDB/#count-the-records-in-a-range
|
||||
JS::Value count_the_records_in_a_range(GC::Ref<ObjectStore> source, GC::Ref<IDBKeyRange> range)
|
||||
JS::Value count_the_records_in_a_range(RecordSource source, GC::Ref<IDBKeyRange> range)
|
||||
{
|
||||
// 1. Let count be the number of records, if any, in source’s list of records with key in range.
|
||||
auto count = source->count_records_in_range(range);
|
||||
auto count = source.visit(
|
||||
[range](GC::Ref<ObjectStore> object_store) {
|
||||
return object_store->count_records_in_range(range);
|
||||
},
|
||||
[range](GC::Ref<Index> index) {
|
||||
return index->count_records_in_range(range);
|
||||
});
|
||||
|
||||
// 2. Return count.
|
||||
return JS::Value(count);
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
namespace Web::IndexedDB {
|
||||
|
||||
using KeyPath = Variant<String, Vector<String>>;
|
||||
using RecordSource = Variant<GC::Ref<ObjectStore>, GC::Ref<Index>>;
|
||||
|
||||
WebIDL::ExceptionOr<GC::Ref<IDBDatabase>> open_a_database_connection(JS::Realm&, StorageAPI::StorageKey, String, Optional<u64>, GC::Ref<IDBRequest>);
|
||||
bool fire_a_version_change_event(JS::Realm&, FlyString const&, GC::Ref<DOM::EventTarget>, u64, Optional<u64>);
|
||||
|
@ -44,7 +45,7 @@ void inject_a_key_into_a_value_using_a_key_path(JS::Realm&, JS::Value, GC::Ref<K
|
|||
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);
|
||||
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(GC::Ref<ObjectStore>, GC::Ref<IDBKeyRange>);
|
||||
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>);
|
||||
GC::Ptr<IDBCursor> iterate_a_cursor(JS::Realm&, GC::Ref<IDBCursor>, GC::Ptr<Key> = nullptr, GC::Ptr<Key> = nullptr, u64 = 1);
|
||||
JS::Value clear_an_object_store(GC::Ref<ObjectStore>);
|
||||
|
|
|
@ -97,4 +97,14 @@ GC::ConservativeVector<IndexRecord> Index::first_n_in_range(GC::Ref<IDBKeyRange>
|
|||
return records;
|
||||
}
|
||||
|
||||
u64 Index::count_records_in_range(GC::Ref<IDBKeyRange> range)
|
||||
{
|
||||
u64 count = 0;
|
||||
for (auto const& record : m_records) {
|
||||
if (range->is_in_range(record.key))
|
||||
++count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -43,6 +43,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);
|
||||
u64 count_records_in_range(GC::Ref<IDBKeyRange> range);
|
||||
|
||||
HTML::SerializationRecord referenced_value(IndexRecord const& index_record) const;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue