2025-03-24 20:29:33 +01:00
/*
* Copyright ( c ) 2025 , stelar7 < dudedbz @ gmail . com >
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
2025-04-11 11:38:29 +02:00
# include <AK/QuickSort.h>
2025-04-11 11:34:52 +02:00
# include <LibWeb/IndexedDB/IDBKeyRange.h>
2025-03-24 20:29:33 +01:00
# include <LibWeb/IndexedDB/Internal/ObjectStore.h>
namespace Web : : IndexedDB {
GC_DEFINE_ALLOCATOR ( ObjectStore ) ;
ObjectStore : : ~ ObjectStore ( ) = default ;
2025-03-24 21:18:26 +01:00
GC : : Ref < ObjectStore > ObjectStore : : create ( JS : : Realm & realm , GC : : Ref < Database > database , String name , bool auto_increment , Optional < KeyPath > const & key_path )
2025-03-24 20:29:33 +01:00
{
2025-03-24 21:18:26 +01:00
return realm . create < ObjectStore > ( database , name , auto_increment , key_path ) ;
}
ObjectStore : : ObjectStore ( GC : : Ref < Database > database , String name , bool auto_increment , Optional < KeyPath > const & key_path )
: m_database ( database )
, m_name ( move ( name ) )
, m_key_path ( key_path )
{
database - > add_object_store ( * this ) ;
if ( auto_increment )
m_key_generator = KeyGenerator { } ;
}
void ObjectStore : : visit_edges ( Visitor & visitor )
{
Base : : visit_edges ( visitor ) ;
visitor . visit ( m_database ) ;
2025-04-01 18:26:13 +02:00
visitor . visit ( m_indexes ) ;
2025-04-11 11:34:52 +02:00
for ( auto & record : m_records ) {
visitor . visit ( record . key ) ;
}
}
void ObjectStore : : remove_records_in_range ( GC : : Ref < IDBKeyRange > range )
{
m_records . remove_all_matching ( [ & ] ( auto const & record ) {
return range - > is_in_range ( record . key ) ;
} ) ;
2025-03-24 20:29:33 +01:00
}
2025-04-11 11:38:29 +02:00
bool ObjectStore : : has_record_with_key ( GC : : Ref < Key > key )
{
auto index = m_records . find_if ( [ & key ] ( auto const & record ) {
return record . key = = key ;
} ) ;
return index ! = m_records . end ( ) ;
}
void ObjectStore : : store_a_record ( Record const & record )
{
m_records . append ( record ) ;
// NOTE: 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.
AK : : quick_sort ( m_records , [ ] ( auto const & a , auto const & b ) {
return Key : : compare_two_keys ( a . key , b . key ) < 0 ;
} ) ;
}
2025-04-28 15:51:35 +02:00
u64 ObjectStore : : 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-28 15:55:46 +02:00
Optional < Record & > ObjectStore : : first_in_range ( GC : : Ref < IDBKeyRange > range )
{
return m_records . first_matching ( [ & ] ( auto const & record ) {
return range - > is_in_range ( record . key ) ;
} ) ;
}
2025-03-24 20:29:33 +01:00
}