Skip to content

Commit 99a17fd

Browse files
committed
feat(common): Implement RelationalLinkedChunk.
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.
1 parent 295a3f2 commit 99a17fd

File tree

2 files changed

+463
-2
lines changed

2 files changed

+463
-2
lines changed

crates/matrix-sdk-common/src/linked_chunk/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ macro_rules! assert_items_eq {
9393
}
9494

9595
mod as_vector;
96+
pub mod relational;
9697
mod updates;
9798

9899
use std::{
@@ -933,7 +934,7 @@ impl ChunkIdentifierGenerator {
933934
/// Learn more with [`ChunkIdentifierGenerator`].
934935
#[derive(Copy, Clone, Debug, PartialEq)]
935936
#[repr(transparent)]
936-
pub struct ChunkIdentifier(u64);
937+
pub struct ChunkIdentifier(pub(super) u64);
937938

938939
impl PartialEq<u64> for ChunkIdentifier {
939940
fn eq(&self, other: &u64) -> bool {
@@ -945,7 +946,7 @@ impl PartialEq<u64> for ChunkIdentifier {
945946
///
946947
/// It's a pair of a chunk position and an item index.
947948
#[derive(Copy, Clone, Debug, PartialEq)]
948-
pub struct Position(ChunkIdentifier, usize);
949+
pub struct Position(pub(super) ChunkIdentifier, pub(super) usize);
949950

950951
impl Position {
951952
/// Get the chunk identifier of the item.
@@ -966,6 +967,16 @@ impl Position {
966967
pub fn decrement_index(&mut self) {
967968
self.1 = self.1.checked_sub(1).expect("Cannot decrement the index because it's already 0");
968969
}
970+
971+
/// Increment the index part (see [`Self::index`]), i.e. add 1.
972+
///
973+
/// # Panic
974+
///
975+
/// This method will panic if it will overflow, i.e. if the index is larger
976+
/// than `usize::MAX`.
977+
pub fn increment_index(&mut self) {
978+
self.1 = self.1.checked_add(1).expect("Cannot increment the index because it's too large");
979+
}
969980
}
970981

971982
/// An iterator over a [`LinkedChunk`] that traverses the chunk in backward

0 commit comments

Comments
 (0)