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

Add custom storage #315

Merged
merged 3 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
10 changes: 10 additions & 0 deletions matrix_sdk/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,16 @@ impl Client {
self.base_client.store()
}

/// Store a value in the custom tree on the store
pub async fn store_save_value(&self, key: &[u8], value: Vec<u8>) -> Result<Option<Vec<u8>>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you noticed, you can get the Store from the client, so these methods are a bit redundant. I would say let's try to keep the Client slim, it is a bit an impossible task to do so considering the API surface of Matrix but removing this certainly would help with that.

Ok(self.store().set_custom_value(key, value).await?)
}

/// Get a value in the custom tree in the store
pub async fn store_get_value(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
Ok(self.store().get_custom_value(key).await?)
}

/// Sets the mxc avatar url of the client's owner. The avatar gets unset if
/// `url` is `None`.
pub async fn set_avatar_url(&self, url: Option<&MxcUri>) -> Result<()> {
Expand Down
18 changes: 18 additions & 0 deletions matrix_sdk_base/src/store/memory_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub struct MemoryStore {
room_event_receipts:
Arc<DashMap<RoomId, DashMap<String, DashMap<EventId, DashMap<UserId, Receipt>>>>>,
media: Arc<Mutex<LruCache<String, Vec<u8>>>>,
custom: Arc<DashMap<Vec<u8>, Vec<u8>>>,
}

impl MemoryStore {
Expand All @@ -90,6 +91,7 @@ impl MemoryStore {
room_user_receipts: DashMap::new().into(),
room_event_receipts: DashMap::new().into(),
media: Arc::new(Mutex::new(LruCache::new(100))),
custom: DashMap::new().into(),
}
}

Expand Down Expand Up @@ -407,6 +409,14 @@ impl MemoryStore {
.unwrap_or_else(Vec::new))
}

async fn get_custom_value(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
Ok(self.custom.get(key).map(|e| e.value().clone()))
}

async fn set_custom_value(&self, key: &[u8], value: Vec<u8>) -> Result<Option<Vec<u8>>> {
Ok(self.custom.insert(key.to_vec(), value))
}

async fn add_media_content(&self, request: &MediaRequest, data: Vec<u8>) -> Result<()> {
self.media.lock().await.put(request.unique_key(), data);

Expand Down Expand Up @@ -563,6 +573,14 @@ impl StateStore for MemoryStore {
self.get_event_room_receipt_events(room_id, receipt_type, event_id).await
}

async fn get_custom_value(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
self.get_custom_value(key).await
}

async fn set_custom_value(&self, key: &[u8], value: Vec<u8>) -> Result<Option<Vec<u8>>> {
self.set_custom_value(key, value).await
}

async fn add_media_content(&self, request: &MediaRequest, data: Vec<u8>) -> Result<()> {
self.add_media_content(request, data).await
}
Expand Down
16 changes: 16 additions & 0 deletions matrix_sdk_base/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,22 @@ pub trait StateStore: AsyncTraitDeps {
event_id: &EventId,
) -> Result<Vec<(UserId, Receipt)>>;

/// Get arbitrary data from the custom store
///
/// # Arguments
///
/// * `key` - The key to fetch data for
async fn get_custom_value(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;

/// Put arbitrary data into the custom store
///
/// # Arguments
///
/// * `key` - The key to insert data into
///
/// * `value` - The value to insert
async fn set_custom_value(&self, key: &[u8], value: Vec<u8>) -> Result<Option<Vec<u8>>>;

/// Add a media file's content in the media store.
///
/// # Arguments
Expand Down
20 changes: 20 additions & 0 deletions matrix_sdk_base/src/store/sled_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ pub struct SledStore {
room_user_receipts: Tree,
room_event_receipts: Tree,
media: Tree,
custom: Tree,
}

impl std::fmt::Debug for SledStore {
Expand Down Expand Up @@ -226,6 +227,8 @@ impl SledStore {

let media = db.open_tree("media")?;

let custom = db.open_tree("custom")?;

Ok(Self {
path,
inner: db,
Expand All @@ -247,6 +250,7 @@ impl SledStore {
room_user_receipts,
room_event_receipts,
media,
custom,
})
}

Expand Down Expand Up @@ -762,6 +766,14 @@ impl SledStore {
.map(|m| m.to_vec()))
}

async fn get_custom_value(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
Ok(self.custom.get(key)?.map(|v| v.to_vec()))
}

async fn set_custom_value(&self, key: &[u8], value: Vec<u8>) -> Result<Option<Vec<u8>>> {
Ok(self.custom.insert(key, value)?.map(|v| v.to_vec()))
}

async fn remove_media_content(&self, request: &MediaRequest) -> Result<()> {
self.media.remove(
(request.media_type.unique_key().as_str(), request.format.unique_key().as_str())
Expand Down Expand Up @@ -899,6 +911,14 @@ impl StateStore for SledStore {
self.get_event_room_receipt_events(room_id, receipt_type, event_id).await
}

async fn get_custom_value(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
self.get_custom_value(key).await
}

async fn set_custom_value(&self, key: &[u8], value: Vec<u8>) -> Result<Option<Vec<u8>>> {
self.set_custom_value(key, value).await
}

async fn add_media_content(&self, request: &MediaRequest, data: Vec<u8>) -> Result<()> {
self.add_media_content(request, data).await
}
Expand Down