Skip to content

Commit

Permalink
feat(common): Implement RelationalLinkedChunk.
Browse files Browse the repository at this point in the history
A `RelationalLinkedChunk` is like a `LinkedChunk` but with a relational
layout, similar to what we would have in a database.

This is used by memory stores. The idea is to have a data layout that
is similar for memory stores and for relational database stores, to
represent a `LinkedChunk`.

This type is also designed to receive `Update`. Applying `Update`s
directly on a `LinkedChunk` is not ideal and particularly not trivial
as the `Update`s do _not_ match the internal data layout of the
`LinkedChunk`, they have been designed for storages, like a relational
database for example.

This type is not as performant as `LinkedChunk` (in terms of memory
layout, CPU caches etc.). It is only designed to be used in memory
stores, which are mostly used for test purposes or light usages of the
SDK.
  • Loading branch information
Hywan committed Nov 20, 2024
1 parent 295a3f2 commit 8c45f84
Show file tree
Hide file tree
Showing 2 changed files with 460 additions and 2 deletions.
15 changes: 13 additions & 2 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 @@ -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);

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);

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

0 comments on commit 8c45f84

Please sign in to comment.