Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ impl_event_cache_store! {
trace!(%room_id, "Inserting new chunk (prev={previous:?}, new={new:?}, next={next:?})");
transaction
.add_chunk(
room_id,
&types::Chunk {
room_id: room_id.to_owned(),
identifier: new.index(),
previous: previous.map(|i| i.index()),
next: next.map(|i| i.index()),
Expand All @@ -174,17 +174,17 @@ impl_event_cache_store! {
trace!(%room_id, "Inserting new gap (prev={previous:?}, new={new:?}, next={next:?})");
transaction
.add_item(
room_id,
&types::Gap {
room_id: room_id.to_owned(),
chunk_identifier: new.index(),
prev_token: gap.prev_token,
},
)
.await?;
transaction
.add_chunk(
room_id,
&types::Chunk {
room_id: room_id.to_owned(),
identifier: new.index(),
previous: previous.map(|i| i.index()),
next: next.map(|i| i.index()),
Expand All @@ -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();
Expand All @@ -204,9 +204,9 @@ impl_event_cache_store! {

for (i, item) in items.into_iter().enumerate() {
transaction
.put_item(
room_id,
.put_event(
&types::Event::InBand(InBandEvent {
room_id: room_id.to_owned(),
content: item,
position: types::Position {
chunk_identifier,
Expand All @@ -225,8 +225,8 @@ impl_event_cache_store! {

transaction
.put_event(
room_id,
&types::Event::InBand(InBandEvent {
room_id: room_id.to_owned(),
content: item,
position: at.into(),
}),
Expand All @@ -239,15 +239,15 @@ 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();
let index = at.index();

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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
.await
.map(|ok| ok.map(Into::into))
.map_err(Into::into)
Expand All @@ -488,8 +488,8 @@ impl_event_cache_store! {
match filters {
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 relation = (event_id, relation_type);
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));
Expand All @@ -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).await?
{
let position = event.position().map(Into::into);
related_events.push((event.into(), position));
Expand All @@ -524,9 +524,9 @@ impl_event_cache_store! {
self.transaction(&[keys::EVENTS], IdbTransactionMode::Readwrite)?;
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: () }),
None => types::Event::OutOfBand(OutOfBandEvent { room_id: room_id.to_owned(), content: event, position: () }),
};
transaction.put_event(room_id, &event).await?;
transaction.put_event(&event).await?;
transaction.commit().await?;
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ 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, components: K::KeyComponents<'_>) -> K
where
T: Indexed,
K: IndexedKey<T>,
{
K::encode(room_id, components, &self.inner)
K::encode(components, &self.inner)
}

/// Encodes a key for a [`Indexed`] type as a [`JsValue`].
Expand All @@ -85,14 +85,13 @@ impl IndexeddbEventCacheStoreSerializer {
/// `K`.
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,
K: IndexedKey<T> + Serialize,
{
serde_wasm_bindgen::to_value(&self.encode_key::<T, K>(room_id, components))
serde_wasm_bindgen::to_value(&self.encode_key::<T, K>(components))
}

/// Encodes a key component range for an [`Indexed`] type.
Expand All @@ -101,24 +100,18 @@ impl IndexeddbEventCacheStoreSerializer {
/// `K`.
pub fn encode_key_range<T, K>(
&self,
room_id: &RoomId,
range: impl Into<IndexedKeyRange<K>>,
) -> Result<IdbKeyRange, serde_wasm_bindgen::Error>
where
T: Indexed,
K: IndexedKeyBounds<T> + Serialize,
K: Serialize,
{
use serde_wasm_bindgen::to_value;
Ok(match range.into() {
IndexedKeyRange::Only(key) => IdbKeyRange::only(&to_value(&key)?)?,
IndexedKeyRange::Bound(lower, upper) => {
IdbKeyRange::bound(&to_value(&lower)?, &to_value(&upper)?)?
}
IndexedKeyRange::All => {
let lower = to_value(&K::lower_key(room_id, &self.inner))?;
let upper = to_value(&K::upper_key(room_id, &self.inner))?;
IdbKeyRange::bound(&lower, &upper).expect("construct key range")
}
})
}

Expand All @@ -128,45 +121,36 @@ impl IndexeddbEventCacheStoreSerializer {
/// `K`.
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,
K: IndexedKey<T> + Serialize,
{
let range = match range.into() {
IndexedKeyRange::Only(components) => {
IndexedKeyRange::Only(K::encode(room_id, components, &self.inner))
IndexedKeyRange::Only(K::encode(components, &self.inner))
}
IndexedKeyRange::Bound(lower, upper) => {
let lower = K::encode(room_id, lower, &self.inner);
let upper = K::encode(room_id, upper, &self.inner);
IndexedKeyRange::Bound(lower, upper)
}
IndexedKeyRange::All => {
let lower = K::lower_key(room_id, &self.inner);
let upper = K::upper_key(room_id, &self.inner);
let lower = K::encode(lower, &self.inner);
let upper = K::encode(upper, &self.inner);
IndexedKeyRange::Bound(lower, upper)
}
};
self.encode_key_range::<T, K>(room_id, range)
self.encode_key_range::<T, K>(range)
}

/// Serializes an [`Indexed`] type into a [`JsValue`]
pub fn serialize<T>(
&self,
room_id: &RoomId,
t: &T,
) -> Result<JsValue, IndexeddbEventCacheStoreSerializerError<T::Error>>
where
T: Indexed,
T::IndexedType: Serialize,
{
let indexed = t
.to_indexed(room_id, &self.inner)
.map_err(IndexeddbEventCacheStoreSerializerError::Indexing)?;
let indexed =
t.to_indexed(&self.inner).map_err(IndexeddbEventCacheStoreSerializerError::Indexing)?;
serde_wasm_bindgen::to_value(&indexed).map_err(Into::into)
}

Expand Down
Loading
Loading