Skip to content

Commit

Permalink
task(event cache): rename a few things
Browse files Browse the repository at this point in the history
- rename RawLinkedChunk -> RawChunk
- rename RawChunk::id -> RawChunk::identifier
- precise that a `RawChunk` is mostly a `Chunk` with different
previous/next links.
  • Loading branch information
bnjbvr committed Dec 11, 2024
1 parent d42c449 commit 0264e49
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ use matrix_sdk_common::{
AlgorithmInfo, DecryptedRoomEvent, EncryptionInfo, SyncTimelineEvent, TimelineEventKind,
VerificationState,
},
linked_chunk::{
ChunkContent, LinkedChunk, LinkedChunkBuilder, Position, RawLinkedChunk, Update,
},
linked_chunk::{ChunkContent, LinkedChunk, LinkedChunkBuilder, Position, RawChunk, Update},
};
use matrix_sdk_test::{event_factory::EventFactory, ALICE, DEFAULT_TEST_ROOM_ID};
use ruma::{
Expand Down Expand Up @@ -121,9 +119,7 @@ pub trait EventCacheStoreIntegrationTests {
async fn test_clear_all_rooms_chunks(&self);
}

fn rebuild_linked_chunk(
raws: Vec<RawLinkedChunk<Event, Gap>>,
) -> Option<LinkedChunk<3, Event, Gap>> {
fn rebuild_linked_chunk(raws: Vec<RawChunk<Event, Gap>>) -> Option<LinkedChunk<3, Event, Gap>> {
LinkedChunkBuilder::from_raw_parts(raws).build().unwrap()
}

Expand Down
4 changes: 2 additions & 2 deletions crates/matrix-sdk-base/src/event_cache/store/memory_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{collections::HashMap, num::NonZeroUsize, sync::RwLock as StdRwLock, ti

use async_trait::async_trait;
use matrix_sdk_common::{
linked_chunk::{relational::RelationalLinkedChunk, RawLinkedChunk, Update},
linked_chunk::{relational::RelationalLinkedChunk, RawChunk, Update},
ring_buffer::RingBuffer,
store_locks::memory_store_helper::try_take_leased_lock,
};
Expand Down Expand Up @@ -96,7 +96,7 @@ impl EventCacheStore for MemoryStore {
async fn reload_linked_chunk(
&self,
room_id: &RoomId,
) -> Result<Vec<RawLinkedChunk<Event, Gap>>, Self::Error> {
) -> Result<Vec<RawChunk<Event, Gap>>, Self::Error> {
let inner = self.inner.read().unwrap();
inner
.events
Expand Down
6 changes: 3 additions & 3 deletions crates/matrix-sdk-base/src/event_cache/store/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{fmt, sync::Arc};

use async_trait::async_trait;
use matrix_sdk_common::{
linked_chunk::{RawLinkedChunk, Update},
linked_chunk::{RawChunk, Update},
AsyncTraitDeps,
};
use ruma::{MxcUri, RoomId};
Expand Down Expand Up @@ -62,7 +62,7 @@ pub trait EventCacheStore: AsyncTraitDeps {
async fn reload_linked_chunk(
&self,
room_id: &RoomId,
) -> Result<Vec<RawLinkedChunk<Event, Gap>>, Self::Error>;
) -> Result<Vec<RawChunk<Event, Gap>>, Self::Error>;

/// Clear persisted events for all the rooms.
///
Expand Down Expand Up @@ -192,7 +192,7 @@ impl<T: EventCacheStore> EventCacheStore for EraseEventCacheStoreError<T> {
async fn reload_linked_chunk(
&self,
room_id: &RoomId,
) -> Result<Vec<RawLinkedChunk<Event, Gap>>, Self::Error> {
) -> Result<Vec<RawChunk<Event, Gap>>, Self::Error> {
self.0.reload_linked_chunk(room_id).await.map_err(Into::into)
}

Expand Down
8 changes: 4 additions & 4 deletions crates/matrix-sdk-common/src/linked_chunk/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use tracing::error;

use super::{
Chunk, ChunkContent, ChunkIdentifier, ChunkIdentifierGenerator, Ends, LinkedChunk,
ObservableUpdates, RawLinkedChunk,
ObservableUpdates, RawChunk,
};

/// A temporary chunk representation in the [`LinkedChunkBuilder`].
Expand Down Expand Up @@ -262,15 +262,15 @@ impl<const CAP: usize, Item, Gap> LinkedChunkBuilder<CAP, Item, Gap> {
}

/// Fills a linked chunk builder from all the given raw parts.
pub fn from_raw_parts(raws: Vec<RawLinkedChunk<Item, Gap>>) -> Self {
pub fn from_raw_parts(raws: Vec<RawChunk<Item, Gap>>) -> Self {
let mut this = Self::new();
for raw in raws {
match raw.content {
ChunkContent::Gap(gap) => {
this.push_gap(raw.previous, raw.id, raw.next, gap);
this.push_gap(raw.previous, raw.identifier, raw.next, gap);
}
ChunkContent::Items(vec) => {
this.push_items(raw.previous, raw.id, raw.next, vec);
this.push_items(raw.previous, raw.identifier, raw.next, vec);
}
}
}
Expand Down
32 changes: 8 additions & 24 deletions crates/matrix-sdk-common/src/linked_chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ impl<'a, const CAP: usize, Item, Gap> Iterator for Iter<'a, CAP, Item, Gap> {
}

/// This enum represents the content of a [`Chunk`].
#[derive(Debug)]
#[derive(Clone, Debug)]
pub enum ChunkContent<Item, Gap> {
/// The chunk represents a gap in the linked chunk, i.e. a hole. It
/// means that some items are missing in this location.
Expand All @@ -1074,15 +1074,6 @@ pub enum ChunkContent<Item, Gap> {
Items(Vec<Item>),
}

impl<Item: Clone, Gap: Clone> Clone for ChunkContent<Item, Gap> {
fn clone(&self) -> Self {
match self {
Self::Gap(gap) => Self::Gap(gap.clone()),
Self::Items(items) => Self::Items(items.clone()),
}
}
}

/// A chunk is a node in the [`LinkedChunk`].
pub struct Chunk<const CAPACITY: usize, Item, Gap> {
/// The previous chunk.
Expand Down Expand Up @@ -1429,32 +1420,25 @@ impl EmptyChunk {
}

/// The raw representation of a linked chunk, as persisted in storage.
#[derive(Debug)]
pub struct RawLinkedChunk<Item, Gap> {
///
/// It may rebuilt into [`Chunk`] and shares the same internal representation,
/// except that links are materialized using [`ChunkIdentifier`] instead of raw
/// pointers to the previous and next chunks.
#[derive(Clone, Debug)]
pub struct RawChunk<Item, Gap> {
/// Content section of the linked chunk.
pub content: ChunkContent<Item, Gap>,

/// Link to the previous chunk, via its identifier.
pub previous: Option<ChunkIdentifier>,

/// Current chunk's identifier.
pub id: ChunkIdentifier,
pub identifier: ChunkIdentifier,

/// Link to the next chunk, via its identifier.
pub next: Option<ChunkIdentifier>,
}

impl<Item: Clone, Gap: Clone> Clone for RawLinkedChunk<Item, Gap> {
fn clone(&self) -> Self {
Self {
content: self.content.clone(),
previous: self.previous,
id: self.id,
next: self.next,
}
}
}

#[cfg(test)]
mod tests {
use std::{
Expand Down
19 changes: 8 additions & 11 deletions crates/matrix-sdk-common/src/linked_chunk/relational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use ruma::{OwnedRoomId, RoomId};

use super::{ChunkContent, RawLinkedChunk};
use super::{ChunkContent, RawChunk};
use crate::linked_chunk::{ChunkIdentifier, Position, Update};

/// A row of the [`RelationalLinkedChunk::chunks`].
Expand Down Expand Up @@ -290,10 +290,7 @@ where
///
/// Return an error result if the data was malformed in the struct, with a
/// string message explaining details about the error.
pub fn reload_chunks(
&self,
room_id: &RoomId,
) -> Result<Vec<RawLinkedChunk<Item, Gap>>, String> {
pub fn reload_chunks(&self, room_id: &RoomId) -> Result<Vec<RawChunk<Item, Gap>>, String> {
let mut result = Vec::new();

for chunk_row in self.chunks.iter().filter(|chunk| chunk.room_id == room_id) {
Expand All @@ -310,10 +307,10 @@ where
let Some(first) = items.peek() else {
// The only possibility is that we created an empty items chunk; mark it as
// such, and continue.
result.push(RawLinkedChunk {
result.push(RawChunk {
content: ChunkContent::Items(Vec::new()),
previous: chunk_row.previous_chunk,
id: chunk_row.chunk,
identifier: chunk_row.chunk,
next: chunk_row.next_chunk,
});
continue;
Expand All @@ -340,12 +337,12 @@ where
// Sort them by their position.
collected_items.sort_unstable_by_key(|(_item, index)| *index);

result.push(RawLinkedChunk {
result.push(RawChunk {
content: ChunkContent::Items(
collected_items.into_iter().map(|(item, _index)| item).collect(),
),
previous: chunk_row.previous_chunk,
id: chunk_row.chunk,
identifier: chunk_row.chunk,
next: chunk_row.next_chunk,
});
}
Expand All @@ -361,10 +358,10 @@ where
));
}

result.push(RawLinkedChunk {
result.push(RawChunk {
content: ChunkContent::Gap(gap.clone()),
previous: chunk_row.previous_chunk,
id: chunk_row.chunk,
identifier: chunk_row.chunk,
next: chunk_row.next_chunk,
});
}
Expand Down
39 changes: 22 additions & 17 deletions crates/matrix-sdk-sqlite/src/event_cache_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use async_trait::async_trait;
use deadpool_sqlite::{Object as SqliteAsyncConn, Pool as SqlitePool, Runtime};
use matrix_sdk_base::{
event_cache::{store::EventCacheStore, Event, Gap},
linked_chunk::{ChunkContent, ChunkIdentifier, RawLinkedChunk, Update},
linked_chunk::{ChunkContent, ChunkIdentifier, RawChunk, Update},
media::{MediaRequestParameters, UniqueKey},
};
use matrix_sdk_store_encryption::StoreCipher;
Expand Down Expand Up @@ -148,7 +148,7 @@ impl SqliteEventCacheStore {
&self,
room_id: &RoomId,
chunk_id: ChunkIdentifier,
) -> Result<RawLinkedChunk<Event, Gap>> {
) -> Result<RawChunk<Event, Gap>> {
let hashed_room_id = self.encode_key(keys::LINKED_CHUNKS, room_id);

let this = self.clone();
Expand Down Expand Up @@ -176,7 +176,7 @@ trait TransactionExtForLinkedChunks {
index: u64,
next: Option<u64>,
chunk_type: &str,
) -> Result<RawLinkedChunk<Event, Gap>>;
) -> Result<RawChunk<Event, Gap>>;

fn load_gap_content(
&self,
Expand All @@ -202,7 +202,7 @@ impl TransactionExtForLinkedChunks for Transaction<'_> {
id: u64,
next: Option<u64>,
chunk_type: &str,
) -> Result<RawLinkedChunk<Event, Gap>> {
) -> Result<RawChunk<Event, Gap>> {
let previous = previous.map(ChunkIdentifier::new);
let next = next.map(ChunkIdentifier::new);
let id = ChunkIdentifier::new(id);
Expand All @@ -212,13 +212,18 @@ impl TransactionExtForLinkedChunks for Transaction<'_> {
// It's a gap! There's at most one row for it in the database, so a
// call to `query_row` is sufficient.
let gap = self.load_gap_content(store, room_id, id)?;
Ok(RawLinkedChunk { content: ChunkContent::Gap(gap), previous, id, next })
Ok(RawChunk { content: ChunkContent::Gap(gap), previous, identifier: id, next })
}

CHUNK_TYPE_EVENT_TYPE_STRING => {
// It's events!
let events = self.load_events_content(store, room_id, id)?;
Ok(RawLinkedChunk { content: ChunkContent::Items(events), previous, id, next })
Ok(RawChunk {
content: ChunkContent::Items(events),
previous,
identifier: id,
next,
})
}

other => {
Expand Down Expand Up @@ -537,7 +542,7 @@ impl EventCacheStore for SqliteEventCacheStore {
async fn reload_linked_chunk(
&self,
room_id: &RoomId,
) -> Result<Vec<RawLinkedChunk<Event, Gap>>, Self::Error> {
) -> Result<Vec<RawChunk<Event, Gap>>, Self::Error> {
let room_id = room_id.to_owned();
let hashed_room_id = self.encode_key(keys::LINKED_CHUNKS, &room_id);

Expand Down Expand Up @@ -904,23 +909,23 @@ mod tests {
{
// Chunks are ordered from smaller to bigger IDs.
let c = chunks.remove(0);
assert_eq!(c.id, ChunkIdentifier::new(13));
assert_eq!(c.identifier, ChunkIdentifier::new(13));
assert_eq!(c.previous, Some(ChunkIdentifier::new(42)));
assert_eq!(c.next, Some(ChunkIdentifier::new(37)));
assert_matches!(c.content, ChunkContent::Items(events) => {
assert!(events.is_empty());
});

let c = chunks.remove(0);
assert_eq!(c.id, ChunkIdentifier::new(37));
assert_eq!(c.identifier, ChunkIdentifier::new(37));
assert_eq!(c.previous, Some(ChunkIdentifier::new(13)));
assert_eq!(c.next, None);
assert_matches!(c.content, ChunkContent::Items(events) => {
assert!(events.is_empty());
});

let c = chunks.remove(0);
assert_eq!(c.id, ChunkIdentifier::new(42));
assert_eq!(c.identifier, ChunkIdentifier::new(42));
assert_eq!(c.previous, None);
assert_eq!(c.next, Some(ChunkIdentifier::new(13)));
assert_matches!(c.content, ChunkContent::Items(events) => {
Expand Down Expand Up @@ -954,7 +959,7 @@ mod tests {

// Chunks are ordered from smaller to bigger IDs.
let c = chunks.remove(0);
assert_eq!(c.id, ChunkIdentifier::new(42));
assert_eq!(c.identifier, ChunkIdentifier::new(42));
assert_eq!(c.previous, None);
assert_eq!(c.next, None);
assert_matches!(c.content, ChunkContent::Gap(gap) => {
Expand Down Expand Up @@ -1002,15 +1007,15 @@ mod tests {

// Chunks are ordered from smaller to bigger IDs.
let c = chunks.remove(0);
assert_eq!(c.id, ChunkIdentifier::new(42));
assert_eq!(c.identifier, ChunkIdentifier::new(42));
assert_eq!(c.previous, None);
assert_eq!(c.next, Some(ChunkIdentifier::new(44)));
assert_matches!(c.content, ChunkContent::Gap(gap) => {
assert_eq!(gap.prev_token, "raclette");
});

let c = chunks.remove(0);
assert_eq!(c.id, ChunkIdentifier::new(44));
assert_eq!(c.identifier, ChunkIdentifier::new(44));
assert_eq!(c.previous, Some(ChunkIdentifier::new(42)));
assert_eq!(c.next, None);
assert_matches!(c.content, ChunkContent::Gap(gap) => {
Expand Down Expand Up @@ -1074,7 +1079,7 @@ mod tests {
assert_eq!(chunks.len(), 1);

let c = chunks.remove(0);
assert_eq!(c.id, ChunkIdentifier::new(42));
assert_eq!(c.identifier, ChunkIdentifier::new(42));
assert_eq!(c.previous, None);
assert_eq!(c.next, None);
assert_matches!(c.content, ChunkContent::Items(events) => {
Expand Down Expand Up @@ -1119,7 +1124,7 @@ mod tests {
assert_eq!(chunks.len(), 1);

let c = chunks.remove(0);
assert_eq!(c.id, ChunkIdentifier::new(42));
assert_eq!(c.identifier, ChunkIdentifier::new(42));
assert_eq!(c.previous, None);
assert_eq!(c.next, None);
assert_matches!(c.content, ChunkContent::Items(events) => {
Expand Down Expand Up @@ -1178,7 +1183,7 @@ mod tests {
assert_eq!(chunks.len(), 1);

let c = chunks.remove(0);
assert_eq!(c.id, ChunkIdentifier::new(42));
assert_eq!(c.identifier, ChunkIdentifier::new(42));
assert_eq!(c.previous, None);
assert_eq!(c.next, None);
assert_matches!(c.content, ChunkContent::Items(events) => {
Expand Down Expand Up @@ -1225,7 +1230,7 @@ mod tests {
assert_eq!(chunks.len(), 1);

let c = chunks.remove(0);
assert_eq!(c.id, ChunkIdentifier::new(42));
assert_eq!(c.identifier, ChunkIdentifier::new(42));
assert_eq!(c.previous, None);
assert_eq!(c.next, None);
assert_matches!(c.content, ChunkContent::Items(events) => {
Expand Down
Loading

0 comments on commit 0264e49

Please sign in to comment.