2022-09-06 23:15:09 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
2024-10-01 22:37:01 +00:00
|
|
|
use conduit::{err, implement, utils, Result};
|
2024-09-29 13:13:09 +00:00
|
|
|
use database::{Deserialized, Map};
|
2022-10-05 20:34:31 +02:00
|
|
|
use ruma::{events::StateEventType, EventId, RoomId};
|
2021-08-11 19:15:38 +02:00
|
|
|
|
2024-09-29 13:13:09 +00:00
|
|
|
use crate::{globals, Dep};
|
2024-07-18 06:37:47 +00:00
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
pub struct Service {
|
2024-05-27 03:17:20 +00:00
|
|
|
db: Data,
|
2024-09-29 13:13:09 +00:00
|
|
|
services: Services,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Data {
|
|
|
|
|
eventid_shorteventid: Arc<Map>,
|
|
|
|
|
shorteventid_eventid: Arc<Map>,
|
|
|
|
|
statekey_shortstatekey: Arc<Map>,
|
|
|
|
|
shortstatekey_statekey: Arc<Map>,
|
|
|
|
|
roomid_shortroomid: Arc<Map>,
|
|
|
|
|
statehash_shortstatehash: Arc<Map>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Services {
|
|
|
|
|
globals: Dep<globals::Service>,
|
2022-08-07 19:42:22 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-04 03:26:19 +00:00
|
|
|
impl crate::Service for Service {
|
|
|
|
|
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
|
|
|
|
|
Ok(Arc::new(Self {
|
2024-09-29 13:13:09 +00:00
|
|
|
db: Data {
|
|
|
|
|
eventid_shorteventid: args.db["eventid_shorteventid"].clone(),
|
|
|
|
|
shorteventid_eventid: args.db["shorteventid_eventid"].clone(),
|
|
|
|
|
statekey_shortstatekey: args.db["statekey_shortstatekey"].clone(),
|
|
|
|
|
shortstatekey_statekey: args.db["shortstatekey_statekey"].clone(),
|
|
|
|
|
roomid_shortroomid: args.db["roomid_shortroomid"].clone(),
|
|
|
|
|
statehash_shortstatehash: args.db["statehash_shortstatehash"].clone(),
|
|
|
|
|
},
|
|
|
|
|
services: Services {
|
|
|
|
|
globals: args.depend::<globals::Service>("globals"),
|
|
|
|
|
},
|
2024-07-04 03:26:19 +00:00
|
|
|
}))
|
2024-05-27 03:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-04 03:26:19 +00:00
|
|
|
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 13:13:09 +00:00
|
|
|
#[implement(Service)]
|
|
|
|
|
pub async fn get_or_create_shorteventid(&self, event_id: &EventId) -> u64 {
|
|
|
|
|
if let Ok(shorteventid) = self
|
|
|
|
|
.db
|
|
|
|
|
.eventid_shorteventid
|
|
|
|
|
.get(event_id)
|
|
|
|
|
.await
|
|
|
|
|
.deserialized()
|
|
|
|
|
{
|
|
|
|
|
return shorteventid;
|
2021-08-11 19:15:38 +02:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-09-29 13:13:09 +00:00
|
|
|
let shorteventid = self.services.globals.next_count().unwrap();
|
|
|
|
|
self.db
|
|
|
|
|
.eventid_shorteventid
|
|
|
|
|
.insert(event_id.as_bytes(), &shorteventid.to_be_bytes());
|
|
|
|
|
self.db
|
|
|
|
|
.shorteventid_eventid
|
|
|
|
|
.insert(&shorteventid.to_be_bytes(), event_id.as_bytes());
|
2024-04-10 12:21:23 -07:00
|
|
|
|
2024-09-29 13:13:09 +00:00
|
|
|
shorteventid
|
|
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-09-29 13:13:09 +00:00
|
|
|
#[implement(Service)]
|
|
|
|
|
pub async fn multi_get_or_create_shorteventid(&self, event_ids: &[&EventId]) -> Vec<u64> {
|
2024-10-01 22:37:01 +00:00
|
|
|
self.db
|
2024-09-29 13:13:09 +00:00
|
|
|
.eventid_shorteventid
|
2024-10-01 22:37:01 +00:00
|
|
|
.get_batch_blocking(event_ids.iter())
|
|
|
|
|
.into_iter()
|
2024-09-29 13:13:09 +00:00
|
|
|
.enumerate()
|
2024-10-01 22:37:01 +00:00
|
|
|
.map(|(i, result)| match result {
|
|
|
|
|
Ok(ref short) => utils::u64_from_u8(short),
|
|
|
|
|
Err(_) => {
|
2024-09-29 13:13:09 +00:00
|
|
|
let short = self.services.globals.next_count().unwrap();
|
|
|
|
|
self.db
|
|
|
|
|
.eventid_shorteventid
|
2024-10-01 22:37:01 +00:00
|
|
|
.insert(event_ids[i], &short.to_be_bytes());
|
2024-09-29 13:13:09 +00:00
|
|
|
self.db
|
|
|
|
|
.shorteventid_eventid
|
2024-10-01 22:37:01 +00:00
|
|
|
.insert(&short.to_be_bytes(), event_ids[i]);
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-10-01 22:37:01 +00:00
|
|
|
short
|
2024-09-29 13:13:09 +00:00
|
|
|
},
|
2024-10-01 22:37:01 +00:00
|
|
|
})
|
|
|
|
|
.collect()
|
2024-09-29 13:13:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[implement(Service)]
|
|
|
|
|
pub async fn get_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> Result<u64> {
|
|
|
|
|
let key = (event_type, state_key);
|
|
|
|
|
self.db
|
|
|
|
|
.statekey_shortstatekey
|
|
|
|
|
.qry(&key)
|
|
|
|
|
.await
|
|
|
|
|
.deserialized()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[implement(Service)]
|
|
|
|
|
pub async fn get_or_create_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> u64 {
|
|
|
|
|
let key = (event_type.to_string(), state_key);
|
|
|
|
|
if let Ok(shortstatekey) = self
|
|
|
|
|
.db
|
|
|
|
|
.statekey_shortstatekey
|
|
|
|
|
.qry(&key)
|
|
|
|
|
.await
|
|
|
|
|
.deserialized()
|
|
|
|
|
{
|
|
|
|
|
return shortstatekey;
|
2021-03-26 11:10:45 +01:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-09-29 13:13:09 +00:00
|
|
|
let mut key = event_type.to_string().as_bytes().to_vec();
|
|
|
|
|
key.push(0xFF);
|
|
|
|
|
key.extend_from_slice(state_key.as_bytes());
|
|
|
|
|
|
|
|
|
|
let shortstatekey = self.services.globals.next_count().unwrap();
|
|
|
|
|
self.db
|
|
|
|
|
.statekey_shortstatekey
|
|
|
|
|
.insert(&key, &shortstatekey.to_be_bytes());
|
|
|
|
|
self.db
|
|
|
|
|
.shortstatekey_statekey
|
|
|
|
|
.insert(&shortstatekey.to_be_bytes(), &key);
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-09-29 13:13:09 +00:00
|
|
|
shortstatekey
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[implement(Service)]
|
|
|
|
|
pub async fn get_eventid_from_short(&self, shorteventid: u64) -> Result<Arc<EventId>> {
|
|
|
|
|
const BUFSIZE: usize = size_of::<u64>();
|
|
|
|
|
|
|
|
|
|
self.db
|
|
|
|
|
.shorteventid_eventid
|
|
|
|
|
.aqry::<BUFSIZE, _>(&shorteventid)
|
|
|
|
|
.await
|
|
|
|
|
.deserialized()
|
|
|
|
|
.map_err(|e| err!(Database("Failed to find EventId from short {shorteventid:?}: {e:?}")))
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 23:19:47 +00:00
|
|
|
#[implement(Service)]
|
|
|
|
|
pub async fn multi_get_eventid_from_short(&self, shorteventid: &[u64]) -> Vec<Result<Arc<EventId>>> {
|
|
|
|
|
const BUFSIZE: usize = size_of::<u64>();
|
|
|
|
|
|
|
|
|
|
let keys: Vec<[u8; BUFSIZE]> = shorteventid
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|short| short.to_be_bytes())
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
self.db
|
|
|
|
|
.shorteventid_eventid
|
|
|
|
|
.get_batch_blocking(keys.iter())
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(Deserialized::deserialized)
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 13:13:09 +00:00
|
|
|
#[implement(Service)]
|
|
|
|
|
pub async fn get_statekey_from_short(&self, shortstatekey: u64) -> Result<(StateEventType, String)> {
|
|
|
|
|
const BUFSIZE: usize = size_of::<u64>();
|
|
|
|
|
|
|
|
|
|
self.db
|
|
|
|
|
.shortstatekey_statekey
|
|
|
|
|
.aqry::<BUFSIZE, _>(&shortstatekey)
|
|
|
|
|
.await
|
|
|
|
|
.deserialized()
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
err!(Database(
|
|
|
|
|
"Failed to find (StateEventType, state_key) from short {shortstatekey:?}: {e:?}"
|
|
|
|
|
))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns (shortstatehash, already_existed)
|
|
|
|
|
#[implement(Service)]
|
|
|
|
|
pub async fn get_or_create_shortstatehash(&self, state_hash: &[u8]) -> (u64, bool) {
|
|
|
|
|
if let Ok(shortstatehash) = self
|
|
|
|
|
.db
|
|
|
|
|
.statehash_shortstatehash
|
|
|
|
|
.get(state_hash)
|
|
|
|
|
.await
|
|
|
|
|
.deserialized()
|
|
|
|
|
{
|
|
|
|
|
return (shortstatehash, true);
|
2022-06-25 16:12:23 +02:00
|
|
|
}
|
2024-09-29 13:13:09 +00:00
|
|
|
|
|
|
|
|
let shortstatehash = self.services.globals.next_count().unwrap();
|
|
|
|
|
self.db
|
|
|
|
|
.statehash_shortstatehash
|
|
|
|
|
.insert(state_hash, &shortstatehash.to_be_bytes());
|
|
|
|
|
|
|
|
|
|
(shortstatehash, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[implement(Service)]
|
|
|
|
|
pub async fn get_shortroomid(&self, room_id: &RoomId) -> Result<u64> {
|
2024-10-09 03:37:13 +00:00
|
|
|
self.db.roomid_shortroomid.get(room_id).await.deserialized()
|
2024-09-29 13:13:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[implement(Service)]
|
|
|
|
|
pub async fn get_or_create_shortroomid(&self, room_id: &RoomId) -> u64 {
|
|
|
|
|
self.db
|
|
|
|
|
.roomid_shortroomid
|
|
|
|
|
.get(room_id)
|
|
|
|
|
.await
|
|
|
|
|
.deserialized()
|
|
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
|
let short = self.services.globals.next_count().unwrap();
|
|
|
|
|
self.db
|
|
|
|
|
.roomid_shortroomid
|
|
|
|
|
.insert(room_id.as_bytes(), &short.to_be_bytes());
|
|
|
|
|
short
|
|
|
|
|
})
|
2022-08-07 19:42:22 +02:00
|
|
|
}
|