Add custom storage

Add an additional tree to the Store where custom api consumer data may
be stored.
master
Amanda Graven 2021-08-25 11:39:43 +02:00
parent 623408913c
commit 5be7bb980d
No known key found for this signature in database
GPG Key ID: 45C461CDC9286390
4 changed files with 64 additions and 0 deletions

View File

@ -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>>> {
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<()> {

View File

@ -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 {
@ -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(),
}
}
@ -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);
@ -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
}

View File

@ -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

View File

@ -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 {
@ -226,6 +227,8 @@ impl SledStore {
let media = db.open_tree("media")?;
let custom = db.open_tree("custom")?;
Ok(Self {
path,
inner: db,
@ -247,6 +250,7 @@ impl SledStore {
room_user_receipts,
room_event_receipts,
media,
custom,
})
}
@ -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())
@ -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
}