Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(base) Implement RelationalLinkedChunk #4298

Merged
merged 9 commits into from
Nov 25, 2024
11 changes: 8 additions & 3 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,8 @@ use std::{collections::HashMap, num::NonZeroUsize, sync::RwLock as StdRwLock, ti

use async_trait::async_trait;
use matrix_sdk_common::{
linked_chunk::Update, ring_buffer::RingBuffer,
linked_chunk::{relational::RelationalLinkedChunk, Update},
ring_buffer::RingBuffer,
store_locks::memory_store_helper::try_take_leased_lock,
};
use ruma::{MxcUri, OwnedMxcUri};
Expand All @@ -35,6 +36,7 @@ use crate::{
pub struct MemoryStore {
media: StdRwLock<RingBuffer<(OwnedMxcUri, String /* unique key */, Vec<u8>)>>,
leases: StdRwLock<HashMap<String, (String, Instant)>>,
events: StdRwLock<RelationalLinkedChunk<Event, Gap>>,
Hywan marked this conversation as resolved.
Show resolved Hide resolved
}

// SAFETY: `new_unchecked` is safe because 20 is not zero.
Expand All @@ -45,6 +47,7 @@ impl Default for MemoryStore {
Self {
media: StdRwLock::new(RingBuffer::new(NUMBER_OF_MEDIAS)),
leases: Default::default(),
events: StdRwLock::new(RelationalLinkedChunk::new()),
}
}
}
Expand Down Expand Up @@ -72,9 +75,11 @@ impl EventCacheStore for MemoryStore {

async fn handle_linked_chunk_updates(
&self,
_updates: &[Update<Event, Gap>],
updates: &[Update<Event, Gap>],
) -> Result<(), Self::Error> {
todo!()
self.events.write().unwrap().apply_updates(updates);

Ok(())
}

async fn add_media_content(
Expand Down
19 changes: 15 additions & 4 deletions crates/matrix-sdk-common/src/linked_chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ macro_rules! assert_items_eq {
}

mod as_vector;
pub mod relational;
mod updates;

use std::{
Expand Down Expand Up @@ -871,12 +872,12 @@ impl<const CAP: usize, Item, Gap> Drop for LinkedChunk<CAP, Item, Gap> {
}

/// A [`LinkedChunk`] can be safely sent over thread boundaries if `Item: Send`
/// and `Gap: Send`. The only unsafe part if around the `NonNull`, but the API
/// and `Gap: Send`. The only unsafe part is around the `NonNull`, but the API
/// and the lifetimes to deref them are designed safely.
unsafe impl<const CAP: usize, Item: Send, Gap: Send> Send for LinkedChunk<CAP, Item, Gap> {}

/// A [`LinkedChunk`] can be safely share between threads if `Item: Sync` and
/// `Gap: Sync`. The only unsafe part if around the `NonNull`, but the API and
/// `Gap: Sync`. The only unsafe part is around the `NonNull`, but the API and
/// the lifetimes to deref them are designed safely.
unsafe impl<const CAP: usize, Item: Sync, Gap: Sync> Sync for LinkedChunk<CAP, Item, Gap> {}

Expand Down Expand Up @@ -933,7 +934,7 @@ impl ChunkIdentifierGenerator {
/// Learn more with [`ChunkIdentifierGenerator`].
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(transparent)]
pub struct ChunkIdentifier(u64);
pub struct ChunkIdentifier(pub(super) u64);
Hywan marked this conversation as resolved.
Show resolved Hide resolved

impl PartialEq<u64> for ChunkIdentifier {
fn eq(&self, other: &u64) -> bool {
Expand All @@ -945,7 +946,7 @@ impl PartialEq<u64> for ChunkIdentifier {
///
/// It's a pair of a chunk position and an item index.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Position(ChunkIdentifier, usize);
pub struct Position(pub(super) ChunkIdentifier, pub(super) usize);
Hywan marked this conversation as resolved.
Show resolved Hide resolved

impl Position {
/// Get the chunk identifier of the item.
Expand All @@ -966,6 +967,16 @@ impl Position {
pub fn decrement_index(&mut self) {
self.1 = self.1.checked_sub(1).expect("Cannot decrement the index because it's already 0");
}

/// Increment the index part (see [`Self::index`]), i.e. add 1.
///
/// # Panic
///
/// This method will panic if it will overflow, i.e. if the index is larger
/// than `usize::MAX`.
pub fn increment_index(&mut self) {
self.1 = self.1.checked_add(1).expect("Cannot increment the index because it's too large");
}
}

/// An iterator over a [`LinkedChunk`] that traverses the chunk in backward
Expand Down
Loading
Loading