refactor(indexeddb): add lifetime to IndexedKey::KeyComponents

Signed-off-by: Michael Goldenberg <m@mgoldenberg.net>
This commit is contained in:
Michael Goldenberg
2025-08-05 12:52:37 -04:00
committed by Ivan Enderlin
parent 0a3fe939c5
commit e9dcdb7176
5 changed files with 102 additions and 107 deletions

View File

@@ -195,7 +195,7 @@ impl_event_cache_store! {
}
Update::RemoveChunk(chunk_id) => {
trace!("Removing chunk {chunk_id:?}");
transaction.delete_chunk_by_id(room_id, &chunk_id).await?;
transaction.delete_chunk_by_id(room_id, chunk_id).await?;
}
Update::PushItems { at, items } => {
let chunk_identifier = at.chunk_identifier().index();
@@ -239,7 +239,7 @@ impl_event_cache_store! {
trace!(%room_id, "removing item @ {chunk_id}:{index}");
transaction.delete_event_by_position(room_id, &at.into()).await?;
transaction.delete_event_by_position(room_id, at.into()).await?;
}
Update::DetachLastItems { at } => {
let chunk_id = at.chunk_identifier().index();
@@ -247,7 +247,7 @@ impl_event_cache_store! {
trace!(%room_id, "detaching last items @ {chunk_id}:{index}");
transaction.delete_events_by_chunk_from_index(room_id, &at.into()).await?;
transaction.delete_events_by_chunk_from_index(room_id, at.into()).await?;
}
Update::StartReattachItems | Update::EndReattachItems => {
// Nothing? See sqlite implementation
@@ -283,7 +283,7 @@ impl_event_cache_store! {
let chunks = transaction.get_chunks_in_room(room_id).await?;
for chunk in chunks {
if let Some(raw_chunk) = transaction
.load_chunk_by_id(room_id, &ChunkIdentifier::new(chunk.identifier))
.load_chunk_by_id(room_id, ChunkIdentifier::new(chunk.identifier))
.await?
{
raw_chunks.push(raw_chunk);
@@ -321,7 +321,7 @@ impl_event_cache_store! {
let chunks = transaction.get_chunks_in_room(room_id).await?;
for chunk in chunks {
let chunk_id = ChunkIdentifier::new(chunk.identifier);
let num_items = transaction.get_events_count_by_chunk(room_id, &chunk_id).await?;
let num_items = transaction.get_events_count_by_chunk(room_id, chunk_id).await?;
raw_chunks.push(ChunkMetadata {
num_items,
previous: chunk.previous.map(ChunkIdentifier::new),
@@ -355,7 +355,7 @@ impl_event_cache_store! {
// Now that we know we have some chunks in the room, we query IndexedDB
// for the last chunk in the room by getting the chunk which does not
// have a next chunk.
match transaction.get_chunk_by_next_chunk_id(room_id, &None).await {
match transaction.get_chunk_by_next_chunk_id(room_id, None).await {
Err(IndexeddbEventCacheStoreTransactionError::ItemIsNotUnique) => {
// If there are multiple chunks that do not have a next chunk, that
// means we have more than one last chunk, which means that we have
@@ -375,7 +375,7 @@ impl_event_cache_store! {
Ok(Some(last_chunk)) => {
let last_chunk_identifier = ChunkIdentifier::new(last_chunk.identifier);
let last_raw_chunk = transaction
.load_chunk_by_id(room_id, &last_chunk_identifier)
.load_chunk_by_id(room_id, last_chunk_identifier)
.await?
.ok_or(IndexeddbEventCacheStoreError::UnableToLoadChunk)?;
let max_chunk_id = transaction
@@ -404,10 +404,10 @@ impl_event_cache_store! {
&[keys::LINKED_CHUNKS, keys::EVENTS, keys::GAPS],
IdbTransactionMode::Readonly,
)?;
if let Some(chunk) = transaction.get_chunk_by_id(room_id, &before_chunk_identifier).await? {
if let Some(chunk) = transaction.get_chunk_by_id(room_id, before_chunk_identifier).await? {
if let Some(previous_identifier) = chunk.previous {
let previous_identifier = ChunkIdentifier::new(previous_identifier);
return Ok(transaction.load_chunk_by_id(room_id, &previous_identifier).await?);
return Ok(transaction.load_chunk_by_id(room_id, previous_identifier).await?);
}
}
Ok(None)
@@ -447,7 +447,7 @@ impl_event_cache_store! {
let mut duplicated = Vec::new();
for event_id in events {
if let Some(types::Event::InBand(event)) =
transaction.get_event_by_id(room_id, &event_id).await?
transaction.get_event_by_id(room_id, event_id.clone()).await?
{
duplicated.push((event_id, event.position.into()));
}
@@ -466,7 +466,7 @@ impl_event_cache_store! {
let transaction =
self.transaction(&[keys::EVENTS], IdbTransactionMode::Readonly)?;
transaction
.get_event_by_id(room_id, &event_id.to_owned())
.get_event_by_id(room_id, event_id.to_owned())
.await
.map(|ok| ok.map(Into::into))
.map_err(Into::into)
@@ -489,7 +489,7 @@ impl_event_cache_store! {
Some(relation_types) if !relation_types.is_empty() => {
for relation_type in relation_types {
let relation = (event_id.to_owned(), relation_type.clone());
let events = transaction.get_events_by_relation(room_id, &relation).await?;
let events = transaction.get_events_by_relation(room_id, relation).await?;
for event in events {
let position = event.position().map(Into::into);
related_events.push((event.into(), position));
@@ -498,7 +498,7 @@ impl_event_cache_store! {
}
_ => {
for event in
transaction.get_events_by_related_event(room_id, &event_id.to_owned()).await?
transaction.get_events_by_related_event(room_id, event_id.to_owned()).await?
{
let position = event.position().map(Into::into);
related_events.push((event.into(), position));
@@ -522,7 +522,7 @@ impl_event_cache_store! {
};
let transaction =
self.transaction(&[keys::EVENTS], IdbTransactionMode::Readwrite)?;
let event = match transaction.get_event_by_id(room_id, &event_id).await? {
let event = match transaction.get_event_by_id(room_id, event_id).await? {
Some(mut inner) => inner.with_content(event),
None => types::Event::OutOfBand(OutOfBandEvent { content: event, position: () }),
};

View File

@@ -71,7 +71,7 @@ impl IndexeddbEventCacheStoreSerializer {
///
/// Note that the particular key which is encoded is defined by the type
/// `K`.
pub fn encode_key<T, K>(&self, room_id: &RoomId, components: &K::KeyComponents) -> K
pub fn encode_key<T, K>(&self, room_id: &RoomId, components: K::KeyComponents<'_>) -> K
where
T: Indexed,
K: IndexedKey<T>,
@@ -86,7 +86,7 @@ impl IndexeddbEventCacheStoreSerializer {
pub fn encode_key_as_value<T, K>(
&self,
room_id: &RoomId,
components: &K::KeyComponents,
components: K::KeyComponents<'_>,
) -> Result<JsValue, serde_wasm_bindgen::Error>
where
T: Indexed,
@@ -129,12 +129,11 @@ impl IndexeddbEventCacheStoreSerializer {
pub fn encode_key_component_range<'a, T, K>(
&self,
room_id: &RoomId,
range: impl Into<IndexedKeyRange<&'a K::KeyComponents>>,
range: impl Into<IndexedKeyRange<K::KeyComponents<'a>>>,
) -> Result<IdbKeyRange, serde_wasm_bindgen::Error>
where
T: Indexed,
K: IndexedKeyComponentBounds<T> + Serialize,
K::KeyComponents: 'a,
{
let range = match range.into() {
IndexedKeyRange::Only(components) => {

View File

@@ -54,7 +54,7 @@ pub trait IndexedKey<T: Indexed> {
const INDEX: Option<&'static str> = None;
/// Any extra data used to construct the key.
type KeyComponents;
type KeyComponents<'a>;
/// Encodes the key components into a type that can be used as a key in
/// IndexedDB.
@@ -65,7 +65,7 @@ pub trait IndexedKey<T: Indexed> {
/// encrypted before storage.
fn encode(
room_id: &RoomId,
components: &Self::KeyComponents,
components: Self::KeyComponents<'_>,
serializer: &IndexeddbSerializer,
) -> Self;
}
@@ -104,12 +104,12 @@ where
{
/// Constructs the lower bound of the key.
fn lower_key(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self {
<Self as IndexedKey<T>>::encode(room_id, &Self::lower_key_components(), serializer)
<Self as IndexedKey<T>>::encode(room_id, Self::lower_key_components(), serializer)
}
/// Constructs the upper bound of the key.
fn upper_key(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self {
<Self as IndexedKey<T>>::encode(room_id, &Self::upper_key_components(), serializer)
<Self as IndexedKey<T>>::encode(room_id, Self::upper_key_components(), serializer)
}
}
@@ -123,8 +123,8 @@ where
/// get a better overview of how these two interact.
pub trait IndexedKeyComponentBounds<T: Indexed>: IndexedKeyBounds<T> {
/// Constructs the lower bound of the key components.
fn lower_key_components() -> Self::KeyComponents;
fn lower_key_components() -> Self::KeyComponents<'static>;
/// Constructs the upper bound of the key components.
fn upper_key_components() -> Self::KeyComponents;
fn upper_key_components() -> Self::KeyComponents<'static>;
}

View File

@@ -91,17 +91,17 @@ pub enum IndexedKeyRange<K> {
All,
}
impl<C> IndexedKeyRange<&C> {
impl<'a, C: 'a> IndexedKeyRange<C> {
/// Encodes a range of key components of type `K::KeyComponents`
/// into a range of keys of type `K`.
pub fn encoded<T, K>(
&self,
self,
room_id: &RoomId,
serializer: &IndexeddbSerializer,
) -> IndexedKeyRange<K>
where
T: Indexed,
K: IndexedKey<T, KeyComponents = C>,
K: IndexedKey<T, KeyComponents<'a> = C>,
{
match self {
Self::Only(components) => {
@@ -157,12 +157,12 @@ impl Indexed for Chunk {
Ok(IndexedChunk {
id: <IndexedChunkIdKey as IndexedKey<Chunk>>::encode(
room_id,
&ChunkIdentifier::new(self.identifier),
ChunkIdentifier::new(self.identifier),
serializer,
),
next: IndexedNextChunkIdKey::encode(
room_id,
&self.next.map(ChunkIdentifier::new),
self.next.map(ChunkIdentifier::new),
serializer,
),
content: serializer.maybe_encrypt_value(self)?,
@@ -188,11 +188,11 @@ impl Indexed for Chunk {
pub struct IndexedChunkIdKey(IndexedRoomId, IndexedChunkId);
impl IndexedKey<Chunk> for IndexedChunkIdKey {
type KeyComponents = ChunkIdentifier;
type KeyComponents<'a> = ChunkIdentifier;
fn encode(
room_id: &RoomId,
chunk_id: &ChunkIdentifier,
chunk_id: ChunkIdentifier,
serializer: &IndexeddbSerializer,
) -> Self {
let room_id = serializer.encode_key_as_string(keys::ROOMS, room_id);
@@ -202,11 +202,11 @@ impl IndexedKey<Chunk> for IndexedChunkIdKey {
}
impl IndexedKeyComponentBounds<Chunk> for IndexedChunkIdKey {
fn lower_key_components() -> Self::KeyComponents {
fn lower_key_components() -> Self::KeyComponents<'static> {
ChunkIdentifier::new(0)
}
fn upper_key_components() -> Self::KeyComponents {
fn upper_key_components() -> Self::KeyComponents<'static> {
ChunkIdentifier::new(js_sys::Number::MAX_SAFE_INTEGER as u64)
}
}
@@ -248,17 +248,17 @@ impl IndexedNextChunkIdKey {
impl IndexedKey<Chunk> for IndexedNextChunkIdKey {
const INDEX: Option<&'static str> = Some(keys::LINKED_CHUNKS_NEXT);
type KeyComponents = Option<ChunkIdentifier>;
type KeyComponents<'a> = Option<ChunkIdentifier>;
fn encode(
room_id: &RoomId,
next_chunk_id: &Option<ChunkIdentifier>,
next_chunk_id: Option<ChunkIdentifier>,
serializer: &IndexeddbSerializer,
) -> Self {
next_chunk_id
.map(|id| {
Self::Some(<IndexedChunkIdKey as IndexedKey<Chunk>>::encode(
room_id, &id, serializer,
room_id, id, serializer,
))
})
.unwrap_or_else(|| {
@@ -269,11 +269,11 @@ impl IndexedKey<Chunk> for IndexedNextChunkIdKey {
}
impl IndexedKeyComponentBounds<Chunk> for IndexedNextChunkIdKey {
fn lower_key_components() -> Self::KeyComponents {
fn lower_key_components() -> Self::KeyComponents<'static> {
None
}
fn upper_key_components() -> Self::KeyComponents {
fn upper_key_components() -> Self::KeyComponents<'static> {
Some(ChunkIdentifier::new(js_sys::Number::MAX_SAFE_INTEGER as u64))
}
}
@@ -315,14 +315,14 @@ impl Indexed for Event {
serializer: &IndexeddbSerializer,
) -> Result<Self::IndexedType, Self::Error> {
let event_id = self.event_id().ok_or(Self::Error::NoEventId)?;
let id = IndexedEventIdKey::encode(room_id, &event_id, serializer);
let id = IndexedEventIdKey::encode(room_id, event_id, serializer);
let position = self
.position()
.map(|position| IndexedEventPositionKey::encode(room_id, &position, serializer));
.map(|position| IndexedEventPositionKey::encode(room_id, position, serializer));
let relation = self.relation().map(|(related_event, relation_type)| {
IndexedEventRelationKey::encode(
room_id,
&(related_event, RelationType::from(relation_type)),
(related_event, RelationType::from(relation_type)),
serializer,
)
});
@@ -348,9 +348,9 @@ impl Indexed for Event {
pub struct IndexedEventIdKey(IndexedRoomId, IndexedEventId);
impl IndexedKey<Event> for IndexedEventIdKey {
type KeyComponents = OwnedEventId;
type KeyComponents<'a> = OwnedEventId;
fn encode(room_id: &RoomId, event_id: &OwnedEventId, serializer: &IndexeddbSerializer) -> Self {
fn encode(room_id: &RoomId, event_id: OwnedEventId, serializer: &IndexeddbSerializer) -> Self {
let room_id = serializer.encode_key_as_string(keys::ROOMS, room_id);
let event_id = serializer.encode_key_as_string(keys::EVENTS, event_id);
Self(room_id, event_id)
@@ -358,11 +358,11 @@ impl IndexedKey<Event> for IndexedEventIdKey {
}
impl IndexedKeyComponentBounds<Event> for IndexedEventIdKey {
fn lower_key_components() -> Self::KeyComponents {
fn lower_key_components() -> Self::KeyComponents<'static> {
OwnedEventId::try_from(format!("${INDEXED_KEY_LOWER_CHARACTER}")).expect("valid event id")
}
fn upper_key_components() -> Self::KeyComponents {
fn upper_key_components() -> Self::KeyComponents<'static> {
OwnedEventId::try_from(format!("${INDEXED_KEY_UPPER_CHARACTER}")).expect("valid event id")
}
}
@@ -383,20 +383,20 @@ pub struct IndexedEventPositionKey(IndexedRoomId, IndexedChunkId, IndexedEventPo
impl IndexedKey<Event> for IndexedEventPositionKey {
const INDEX: Option<&'static str> = Some(keys::EVENTS_POSITION);
type KeyComponents = Position;
type KeyComponents<'a> = Position;
fn encode(room_id: &RoomId, position: &Position, serializer: &IndexeddbSerializer) -> Self {
fn encode(room_id: &RoomId, position: Position, serializer: &IndexeddbSerializer) -> Self {
let room_id = serializer.encode_key_as_string(keys::ROOMS, room_id);
Self(room_id, position.chunk_identifier, position.index)
}
}
impl IndexedKeyComponentBounds<Event> for IndexedEventPositionKey {
fn lower_key_components() -> Self::KeyComponents {
fn lower_key_components() -> Self::KeyComponents<'static> {
Position { chunk_identifier: 0, index: 0 }
}
fn upper_key_components() -> Self::KeyComponents {
fn upper_key_components() -> Self::KeyComponents<'static> {
Position {
chunk_identifier: js_sys::Number::MAX_SAFE_INTEGER as u64,
index: js_sys::Number::MAX_SAFE_INTEGER as usize,
@@ -437,11 +437,11 @@ impl IndexedEventRelationKey {
impl IndexedKey<Event> for IndexedEventRelationKey {
const INDEX: Option<&'static str> = Some(keys::EVENTS_RELATION);
type KeyComponents = (OwnedEventId, RelationType);
type KeyComponents<'a> = (OwnedEventId, RelationType);
fn encode(
room_id: &RoomId,
(related_event_id, relation_type): &(OwnedEventId, RelationType),
(related_event_id, relation_type): (OwnedEventId, RelationType),
serializer: &IndexeddbSerializer,
) -> Self {
let room_id = serializer.encode_key_as_string(keys::ROOMS, room_id);
@@ -500,7 +500,7 @@ impl Indexed for Gap {
Ok(IndexedGap {
id: <IndexedGapIdKey as IndexedKey<Gap>>::encode(
room_id,
&ChunkIdentifier::new(self.chunk_identifier),
ChunkIdentifier::new(self.chunk_identifier),
serializer,
),
content: serializer.maybe_encrypt_value(self)?,
@@ -524,11 +524,11 @@ impl Indexed for Gap {
pub type IndexedGapIdKey = IndexedChunkIdKey;
impl IndexedKey<Gap> for IndexedGapIdKey {
type KeyComponents = <IndexedChunkIdKey as IndexedKey<Chunk>>::KeyComponents;
type KeyComponents<'a> = <IndexedChunkIdKey as IndexedKey<Chunk>>::KeyComponents<'a>;
fn encode(
room_id: &RoomId,
components: &Self::KeyComponents,
components: Self::KeyComponents<'_>,
serializer: &IndexeddbSerializer,
) -> Self {
<IndexedChunkIdKey as IndexedKey<Chunk>>::encode(room_id, components, serializer)
@@ -536,11 +536,11 @@ impl IndexedKey<Gap> for IndexedGapIdKey {
}
impl IndexedKeyComponentBounds<Gap> for IndexedGapIdKey {
fn lower_key_components() -> Self::KeyComponents {
fn lower_key_components() -> Self::KeyComponents<'static> {
<Self as IndexedKeyComponentBounds<Chunk>>::lower_key_components()
}
fn upper_key_components() -> Self::KeyComponents {
fn upper_key_components() -> Self::KeyComponents<'static> {
<Self as IndexedKeyComponentBounds<Chunk>>::upper_key_components()
}
}

View File

@@ -135,14 +135,13 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn get_items_by_key_components<'b, T, K>(
&self,
room_id: &RoomId,
range: impl Into<IndexedKeyRange<&'b K::KeyComponents>>,
range: impl Into<IndexedKeyRange<K::KeyComponents<'b>>>,
) -> Result<Vec<T>, IndexeddbEventCacheStoreTransactionError>
where
T: Indexed,
T: Indexed + 'b,
T::IndexedType: DeserializeOwned,
T::Error: AsyncErrorDeps,
K: IndexedKeyComponentBounds<T> + Serialize,
K::KeyComponents: 'b,
K: IndexedKeyComponentBounds<T> + Serialize + 'b,
{
let range: IndexedKeyRange<K> = range.into().encoded(room_id, self.serializer.inner());
self.get_items_by_key::<T, K>(room_id, range).await
@@ -184,16 +183,16 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
/// Query IndexedDB for items that match the given key components in the
/// given room. If more than one item is found, an error is returned.
pub async fn get_item_by_key_components<T, K>(
pub async fn get_item_by_key_components<'b, T, K>(
&self,
room_id: &RoomId,
components: &K::KeyComponents,
components: K::KeyComponents<'b>,
) -> Result<Option<T>, IndexeddbEventCacheStoreTransactionError>
where
T: Indexed,
T: Indexed + 'b,
T::IndexedType: DeserializeOwned,
T::Error: AsyncErrorDeps,
K: IndexedKeyComponentBounds<T> + Serialize,
K: IndexedKeyComponentBounds<T> + Serialize + 'b,
{
let mut items = self.get_items_by_key_components::<T, K>(room_id, components).await?;
if items.len() > 1 {
@@ -230,14 +229,13 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn get_items_count_by_key_components<'b, T, K>(
&self,
room_id: &RoomId,
range: impl Into<IndexedKeyRange<&'b K::KeyComponents>>,
range: impl Into<IndexedKeyRange<K::KeyComponents<'b>>>,
) -> Result<usize, IndexeddbEventCacheStoreTransactionError>
where
T: Indexed,
T: Indexed + 'b,
T::IndexedType: DeserializeOwned,
T::Error: AsyncErrorDeps,
K: IndexedKeyBounds<T> + Serialize,
K::KeyComponents: 'b,
K: IndexedKeyBounds<T> + Serialize + 'b,
{
let range: IndexedKeyRange<K> = range.into().encoded(room_id, self.serializer.inner());
self.get_items_count_by_key::<T, K>(room_id, range).await
@@ -364,12 +362,11 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn delete_items_by_key_components<'b, T, K>(
&self,
room_id: &RoomId,
range: impl Into<IndexedKeyRange<&'b K::KeyComponents>>,
range: impl Into<IndexedKeyRange<K::KeyComponents<'b>>>,
) -> Result<(), IndexeddbEventCacheStoreTransactionError>
where
T: Indexed,
K: IndexedKeyBounds<T> + Serialize,
K::KeyComponents: 'b,
T: Indexed + 'b,
K: IndexedKeyBounds<T> + Serialize + 'b,
{
let range: IndexedKeyRange<K> = range.into().encoded(room_id, self.serializer.inner());
self.delete_items_by_key::<T, K>(room_id, range).await
@@ -389,14 +386,14 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
/// Delete item that matches the given key components in the given room from
/// IndexedDB
pub async fn delete_item_by_key<T, K>(
pub async fn delete_item_by_key<'b, T, K>(
&self,
room_id: &RoomId,
key: &K::KeyComponents,
key: K::KeyComponents<'b>,
) -> Result<(), IndexeddbEventCacheStoreTransactionError>
where
T: Indexed,
K: IndexedKeyBounds<T> + Serialize,
T: Indexed + 'b,
K: IndexedKeyBounds<T> + Serialize + 'b,
{
self.delete_items_by_key_components::<T, K>(room_id, key).await
}
@@ -414,7 +411,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn get_chunk_by_id(
&self,
room_id: &RoomId,
chunk_id: &ChunkIdentifier,
chunk_id: ChunkIdentifier,
) -> Result<Option<Chunk>, IndexeddbEventCacheStoreTransactionError> {
self.get_item_by_key_components::<Chunk, IndexedChunkIdKey>(room_id, chunk_id).await
}
@@ -425,7 +422,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn get_chunk_by_next_chunk_id(
&self,
room_id: &RoomId,
next_chunk_id: &Option<ChunkIdentifier>,
next_chunk_id: Option<ChunkIdentifier>,
) -> Result<Option<Chunk>, IndexeddbEventCacheStoreTransactionError> {
self.get_item_by_key_components::<Chunk, IndexedNextChunkIdKey>(room_id, next_chunk_id)
.await
@@ -461,13 +458,13 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn load_chunk_by_id(
&self,
room_id: &RoomId,
chunk_id: &ChunkIdentifier,
chunk_id: ChunkIdentifier,
) -> Result<Option<RawChunk<RawEvent, RawGap>>, IndexeddbEventCacheStoreTransactionError> {
if let Some(chunk) = self.get_chunk_by_id(room_id, chunk_id).await? {
let content = match chunk.chunk_type {
ChunkType::Event => {
let events = self
.get_events_by_chunk(room_id, &ChunkIdentifier::new(chunk.identifier))
.get_events_by_chunk(room_id, ChunkIdentifier::new(chunk.identifier))
.await?
.into_iter()
.map(RawEvent::from)
@@ -476,7 +473,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
}
ChunkType::Gap => {
let gap = self
.get_gap_by_id(room_id, &ChunkIdentifier::new(chunk.identifier))
.get_gap_by_id(room_id, ChunkIdentifier::new(chunk.identifier))
.await?
.ok_or(IndexeddbEventCacheStoreTransactionError::ItemNotFound)?;
ChunkContent::Gap(RawGap { prev_token: gap.prev_token })
@@ -505,7 +502,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
if let Some(previous) = chunk.previous {
let previous_identifier = ChunkIdentifier::new(previous);
if let Some(mut previous_chunk) =
self.get_chunk_by_id(room_id, &previous_identifier).await?
self.get_chunk_by_id(room_id, previous_identifier).await?
{
previous_chunk.next = Some(chunk.identifier);
self.put_item(room_id, &previous_chunk).await?;
@@ -513,7 +510,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
}
if let Some(next) = chunk.next {
let next_identifier = ChunkIdentifier::new(next);
if let Some(mut next_chunk) = self.get_chunk_by_id(room_id, &next_identifier).await? {
if let Some(mut next_chunk) = self.get_chunk_by_id(room_id, next_identifier).await? {
next_chunk.previous = Some(chunk.identifier);
self.put_item(room_id, &next_chunk).await?;
}
@@ -528,13 +525,13 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn delete_chunk_by_id(
&self,
room_id: &RoomId,
chunk_id: &ChunkIdentifier,
chunk_id: ChunkIdentifier,
) -> Result<(), IndexeddbEventCacheStoreTransactionError> {
if let Some(chunk) = self.get_chunk_by_id(room_id, chunk_id).await? {
if let Some(previous) = chunk.previous {
let previous_identifier = ChunkIdentifier::new(previous);
if let Some(mut previous_chunk) =
self.get_chunk_by_id(room_id, &previous_identifier).await?
self.get_chunk_by_id(room_id, previous_identifier).await?
{
previous_chunk.next = chunk.next;
self.put_item(room_id, &previous_chunk).await?;
@@ -542,8 +539,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
}
if let Some(next) = chunk.next {
let next_identifier = ChunkIdentifier::new(next);
if let Some(mut next_chunk) =
self.get_chunk_by_id(room_id, &next_identifier).await?
if let Some(mut next_chunk) = self.get_chunk_by_id(room_id, next_identifier).await?
{
next_chunk.previous = chunk.previous;
self.put_item(room_id, &next_chunk).await?;
@@ -575,7 +571,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn get_event_by_id(
&self,
room_id: &RoomId,
event_id: &OwnedEventId,
event_id: OwnedEventId,
) -> Result<Option<Event>, IndexeddbEventCacheStoreTransactionError> {
let key = self.serializer.encode_key(room_id, event_id);
self.get_item_by_key::<Event, IndexedEventIdKey>(room_id, key).await
@@ -586,7 +582,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn get_events_by_position(
&self,
room_id: &RoomId,
range: impl Into<IndexedKeyRange<&Position>>,
range: impl Into<IndexedKeyRange<Position>>,
) -> Result<Vec<Event>, IndexeddbEventCacheStoreTransactionError> {
self.get_items_by_key_components::<Event, IndexedEventPositionKey>(room_id, range).await
}
@@ -596,7 +592,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn get_events_count_by_position(
&self,
room_id: &RoomId,
range: impl Into<IndexedKeyRange<&Position>>,
range: impl Into<IndexedKeyRange<Position>>,
) -> Result<usize, IndexeddbEventCacheStoreTransactionError> {
self.get_items_count_by_key_components::<Event, IndexedEventPositionKey>(room_id, range)
.await
@@ -606,13 +602,13 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn get_events_by_chunk(
&self,
room_id: &RoomId,
chunk_id: &ChunkIdentifier,
chunk_id: ChunkIdentifier,
) -> Result<Vec<Event>, IndexeddbEventCacheStoreTransactionError> {
let mut lower = IndexedEventPositionKey::lower_key_components();
lower.chunk_identifier = chunk_id.index();
let mut upper = IndexedEventPositionKey::upper_key_components();
upper.chunk_identifier = chunk_id.index();
let range = IndexedKeyRange::Bound(&lower, &upper);
let range = IndexedKeyRange::Bound(lower, upper);
self.get_events_by_position(room_id, range).await
}
@@ -621,13 +617,13 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn get_events_count_by_chunk(
&self,
room_id: &RoomId,
chunk_id: &ChunkIdentifier,
chunk_id: ChunkIdentifier,
) -> Result<usize, IndexeddbEventCacheStoreTransactionError> {
let mut lower = IndexedEventPositionKey::lower_key_components();
lower.chunk_identifier = chunk_id.index();
let mut upper = IndexedEventPositionKey::upper_key_components();
upper.chunk_identifier = chunk_id.index();
let range = IndexedKeyRange::Bound(&lower, &upper);
let range = IndexedKeyRange::Bound(lower, upper);
self.get_events_count_by_position(room_id, range).await
}
@@ -636,7 +632,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn get_events_by_relation(
&self,
room_id: &RoomId,
range: impl Into<IndexedKeyRange<&(OwnedEventId, RelationType)>>,
range: impl Into<IndexedKeyRange<(OwnedEventId, RelationType)>>,
) -> Result<Vec<Event>, IndexeddbEventCacheStoreTransactionError> {
let range = range.into().encoded(room_id, self.serializer.inner());
self.get_items_by_key::<Event, IndexedEventRelationKey>(room_id, range).await
@@ -647,12 +643,12 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn get_events_by_related_event(
&self,
room_id: &RoomId,
related_event_id: &OwnedEventId,
related_event_id: OwnedEventId,
) -> Result<Vec<Event>, IndexeddbEventCacheStoreTransactionError> {
let lower = IndexedEventRelationKey::lower_key(room_id, self.serializer.inner())
.with_related_event_id(related_event_id, self.serializer.inner());
.with_related_event_id(&related_event_id, self.serializer.inner());
let upper = IndexedEventRelationKey::upper_key(room_id, self.serializer.inner())
.with_related_event_id(related_event_id, self.serializer.inner());
.with_related_event_id(&related_event_id, self.serializer.inner());
let range = IndexedKeyRange::Bound(lower, upper);
self.get_items_by_key::<Event, IndexedEventRelationKey>(room_id, range).await
}
@@ -674,7 +670,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
// As a workaround, if the event has a position, we delete it first and
// then call `put_item`. This should be fine as it all happens within the
// context of a single transaction.
self.delete_event_by_position(room_id, &position).await?;
self.delete_event_by_position(room_id, position).await?;
}
self.put_item(room_id, event).await
}
@@ -683,7 +679,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn delete_events_by_position(
&self,
room_id: &RoomId,
range: impl Into<IndexedKeyRange<&Position>>,
range: impl Into<IndexedKeyRange<Position>>,
) -> Result<(), IndexeddbEventCacheStoreTransactionError> {
self.delete_items_by_key_components::<Event, IndexedEventPositionKey>(room_id, range).await
}
@@ -692,7 +688,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn delete_event_by_position(
&self,
room_id: &RoomId,
position: &Position,
position: Position,
) -> Result<(), IndexeddbEventCacheStoreTransactionError> {
self.delete_item_by_key::<Event, IndexedEventPositionKey>(room_id, position).await
}
@@ -701,13 +697,13 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn delete_events_by_chunk(
&self,
room_id: &RoomId,
chunk_id: &ChunkIdentifier,
chunk_id: ChunkIdentifier,
) -> Result<(), IndexeddbEventCacheStoreTransactionError> {
let mut lower = IndexedEventPositionKey::lower_key_components();
lower.chunk_identifier = chunk_id.index();
let mut upper = IndexedEventPositionKey::upper_key_components();
upper.chunk_identifier = chunk_id.index();
let range = IndexedKeyRange::Bound(&lower, &upper);
let range = IndexedKeyRange::Bound(lower, upper);
self.delete_events_by_position(room_id, range).await
}
@@ -716,11 +712,11 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn delete_events_by_chunk_from_index(
&self,
room_id: &RoomId,
position: &Position,
position: Position,
) -> Result<(), IndexeddbEventCacheStoreTransactionError> {
let mut upper = IndexedEventPositionKey::upper_key_components();
upper.chunk_identifier = position.chunk_identifier;
let range = IndexedKeyRange::Bound(position, &upper);
let range = IndexedKeyRange::Bound(position, upper);
self.delete_events_by_position(room_id, range).await
}
@@ -736,7 +732,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn get_gap_by_id(
&self,
room_id: &RoomId,
chunk_id: &ChunkIdentifier,
chunk_id: ChunkIdentifier,
) -> Result<Option<Gap>, IndexeddbEventCacheStoreTransactionError> {
self.get_item_by_key_components::<Gap, IndexedGapIdKey>(room_id, chunk_id).await
}
@@ -745,7 +741,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
pub async fn delete_gap_by_id(
&self,
room_id: &RoomId,
chunk_id: &ChunkIdentifier,
chunk_id: ChunkIdentifier,
) -> Result<(), IndexeddbEventCacheStoreTransactionError> {
self.delete_item_by_key::<Gap, IndexedGapIdKey>(room_id, chunk_id).await
}