ladybird/Libraries/LibWeb/IndexedDB/Internal/Index.cpp

111 lines
2.9 KiB
C++
Raw Normal View History

2025-04-01 18:26:13 +02:00
/*
* Copyright (c) 2025, stelar7 <dudedbz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/IndexedDB/Internal/Index.h>
#include <LibWeb/IndexedDB/Internal/ObjectStore.h>
namespace Web::IndexedDB {
GC_DEFINE_ALLOCATOR(Index);
Index::~Index() = default;
GC::Ref<Index> Index::create(JS::Realm& realm, GC::Ref<ObjectStore> store, String const& name, KeyPath const& key_path, bool unique, bool multi_entry)
2025-04-01 18:26:13 +02:00
{
return realm.create<Index>(store, name, key_path, unique, multi_entry);
}
Index::Index(GC::Ref<ObjectStore> store, String const& name, KeyPath const& key_path, bool unique, bool multi_entry)
2025-04-01 18:26:13 +02:00
: m_object_store(store)
, m_name(name)
2025-04-01 18:26:13 +02:00
, m_unique(unique)
, m_multi_entry(multi_entry)
, m_key_path(key_path)
{
store->index_set().set(name, *this);
2025-04-01 18:26:13 +02:00
}
void Index::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_object_store);
for (auto& record : m_records) {
visitor.visit(record.key);
visitor.visit(record.value);
}
}
void Index::set_name(String name)
{
// NOTE: Update the key in the map so it still matches the name
auto old_value = m_object_store->index_set().take(m_name).release_value();
m_object_store->index_set().set(name, old_value);
m_name = move(name);
}
bool Index::has_record_with_key(GC::Ref<Key> key)
{
auto index = m_records.find_if([&key](auto const& record) {
return Key::equals(record.key, key);
});
return index != m_records.end();
}
// https://w3c.github.io/IndexedDB/#index-referenced-value
HTML::SerializationRecord Index::referenced_value(IndexRecord const& index_record) const
{
// Records in an index are said to have a referenced value.
// This is the value of the record in the indexs referenced object store which has a key equal to the indexs records value.
return m_object_store
->records()
.first_matching([&](auto const& store_record) {
return Key::equals(store_record.key, index_record.value);
})
.value()
.value;
}
void Index::clear_records()
{
m_records.clear();
}
2025-05-13 22:18:14 +02:00
Optional<IndexRecord&> Index::first_in_range(GC::Ref<IDBKeyRange> range)
{
return m_records.first_matching([&](auto const& record) {
return range->is_in_range(record.key);
});
}
2025-05-13 22:41:18 +02:00
GC::ConservativeVector<IndexRecord> Index::first_n_in_range(GC::Ref<IDBKeyRange> range, Optional<WebIDL::UnsignedLong> count)
{
GC::ConservativeVector<IndexRecord> records(range->heap());
for (auto const& record : m_records) {
if (range->is_in_range(record.key))
records.append(record);
if (count.has_value() && records.size() >= *count)
break;
}
return records;
}
2025-05-13 22:54:08 +02:00
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;
}
2025-04-01 18:26:13 +02:00
}