add standalone getters for shortid service

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-11-07 03:59:08 +00:00
parent 27966221f1
commit 13ef6dcbcf

View file

@ -52,13 +52,7 @@ impl crate::Service for Service {
pub async fn get_or_create_shorteventid(&self, event_id: &EventId) -> ShortEventId { pub async fn get_or_create_shorteventid(&self, event_id: &EventId) -> ShortEventId {
const BUFSIZE: usize = size_of::<ShortEventId>(); const BUFSIZE: usize = size_of::<ShortEventId>();
if let Ok(shorteventid) = self if let Ok(shorteventid) = self.get_shorteventid(event_id).await {
.db
.eventid_shorteventid
.get(event_id)
.await
.deserialized()
{
return shorteventid; return shorteventid;
} }
@ -105,11 +99,10 @@ pub async fn multi_get_or_create_shorteventid(&self, event_ids: &[&EventId]) ->
} }
#[implement(Service)] #[implement(Service)]
pub async fn get_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> Result<ShortStateKey> { pub async fn get_shorteventid(&self, event_id: &EventId) -> Result<ShortEventId> {
let key = (event_type, state_key);
self.db self.db
.statekey_shortstatekey .eventid_shorteventid
.qry(&key) .get(event_id)
.await .await
.deserialized() .deserialized()
} }
@ -118,17 +111,11 @@ pub async fn get_shortstatekey(&self, event_type: &StateEventType, state_key: &s
pub async fn get_or_create_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> ShortStateKey { pub async fn get_or_create_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> ShortStateKey {
const BUFSIZE: usize = size_of::<ShortStateKey>(); const BUFSIZE: usize = size_of::<ShortStateKey>();
let key = (event_type, state_key); if let Ok(shortstatekey) = self.get_shortstatekey(event_type, state_key).await {
if let Ok(shortstatekey) = self
.db
.statekey_shortstatekey
.qry(&key)
.await
.deserialized()
{
return shortstatekey; return shortstatekey;
} }
let key = (event_type, state_key);
let shortstatekey = self.services.globals.next_count().unwrap(); let shortstatekey = self.services.globals.next_count().unwrap();
debug_assert!(size_of_val(&shortstatekey) == BUFSIZE, "buffer requirement changed"); debug_assert!(size_of_val(&shortstatekey) == BUFSIZE, "buffer requirement changed");
@ -143,6 +130,16 @@ pub async fn get_or_create_shortstatekey(&self, event_type: &StateEventType, sta
shortstatekey shortstatekey
} }
#[implement(Service)]
pub async fn get_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> Result<ShortStateKey> {
let key = (event_type, state_key);
self.db
.statekey_shortstatekey
.qry(&key)
.await
.deserialized()
}
#[implement(Service)] #[implement(Service)]
pub async fn get_eventid_from_short(&self, shorteventid: ShortEventId) -> Result<Arc<EventId>> { pub async fn get_eventid_from_short(&self, shorteventid: ShortEventId) -> Result<Arc<EventId>> {
const BUFSIZE: usize = size_of::<ShortEventId>(); const BUFSIZE: usize = size_of::<ShortEventId>();