typename some loose u64 ShortId's
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
0bc6fdd589
commit
f746be82c1
3 changed files with 37 additions and 33 deletions
|
@ -24,6 +24,7 @@ struct Services {
|
||||||
globals: Dep<globals::Service>,
|
globals: Dep<globals::Service>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type ShortStateHash = ShortId;
|
||||||
pub type ShortStateKey = ShortId;
|
pub type ShortStateKey = ShortId;
|
||||||
pub type ShortEventId = ShortId;
|
pub type ShortEventId = ShortId;
|
||||||
pub type ShortRoomId = ShortId;
|
pub type ShortRoomId = ShortId;
|
||||||
|
@ -50,7 +51,7 @@ impl crate::Service for Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[implement(Service)]
|
#[implement(Service)]
|
||||||
pub async fn get_or_create_shorteventid(&self, event_id: &EventId) -> u64 {
|
pub async fn get_or_create_shorteventid(&self, event_id: &EventId) -> ShortEventId {
|
||||||
const BUFSIZE: usize = size_of::<u64>();
|
const BUFSIZE: usize = size_of::<u64>();
|
||||||
|
|
||||||
if let Ok(shorteventid) = self
|
if let Ok(shorteventid) = self
|
||||||
|
@ -78,7 +79,7 @@ pub async fn get_or_create_shorteventid(&self, event_id: &EventId) -> u64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[implement(Service)]
|
#[implement(Service)]
|
||||||
pub async fn multi_get_or_create_shorteventid(&self, event_ids: &[&EventId]) -> Vec<u64> {
|
pub async fn multi_get_or_create_shorteventid(&self, event_ids: &[&EventId]) -> Vec<ShortEventId> {
|
||||||
self.db
|
self.db
|
||||||
.eventid_shorteventid
|
.eventid_shorteventid
|
||||||
.get_batch_blocking(event_ids.iter())
|
.get_batch_blocking(event_ids.iter())
|
||||||
|
@ -106,7 +107,7 @@ 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<u64> {
|
pub async fn get_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> Result<ShortStateKey> {
|
||||||
let key = (event_type, state_key);
|
let key = (event_type, state_key);
|
||||||
self.db
|
self.db
|
||||||
.statekey_shortstatekey
|
.statekey_shortstatekey
|
||||||
|
@ -116,8 +117,8 @@ pub async fn get_shortstatekey(&self, event_type: &StateEventType, state_key: &s
|
||||||
}
|
}
|
||||||
|
|
||||||
#[implement(Service)]
|
#[implement(Service)]
|
||||||
pub async fn get_or_create_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> u64 {
|
pub async fn get_or_create_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> ShortStateKey {
|
||||||
const BUFSIZE: usize = size_of::<u64>();
|
const BUFSIZE: usize = size_of::<ShortStateKey>();
|
||||||
|
|
||||||
let key = (event_type, state_key);
|
let key = (event_type, state_key);
|
||||||
if let Ok(shortstatekey) = self
|
if let Ok(shortstatekey) = self
|
||||||
|
@ -145,8 +146,8 @@ pub async fn get_or_create_shortstatekey(&self, event_type: &StateEventType, sta
|
||||||
}
|
}
|
||||||
|
|
||||||
#[implement(Service)]
|
#[implement(Service)]
|
||||||
pub async fn get_eventid_from_short(&self, shorteventid: u64) -> Result<Arc<EventId>> {
|
pub async fn get_eventid_from_short(&self, shorteventid: ShortEventId) -> Result<Arc<EventId>> {
|
||||||
const BUFSIZE: usize = size_of::<u64>();
|
const BUFSIZE: usize = size_of::<ShortEventId>();
|
||||||
|
|
||||||
self.db
|
self.db
|
||||||
.shorteventid_eventid
|
.shorteventid_eventid
|
||||||
|
@ -157,8 +158,8 @@ pub async fn get_eventid_from_short(&self, shorteventid: u64) -> Result<Arc<Even
|
||||||
}
|
}
|
||||||
|
|
||||||
#[implement(Service)]
|
#[implement(Service)]
|
||||||
pub async fn multi_get_eventid_from_short(&self, shorteventid: &[u64]) -> Vec<Result<Arc<EventId>>> {
|
pub async fn multi_get_eventid_from_short(&self, shorteventid: &[ShortEventId]) -> Vec<Result<Arc<EventId>>> {
|
||||||
const BUFSIZE: usize = size_of::<u64>();
|
const BUFSIZE: usize = size_of::<ShortEventId>();
|
||||||
|
|
||||||
let keys: Vec<[u8; BUFSIZE]> = shorteventid
|
let keys: Vec<[u8; BUFSIZE]> = shorteventid
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -174,8 +175,8 @@ pub async fn multi_get_eventid_from_short(&self, shorteventid: &[u64]) -> Vec<Re
|
||||||
}
|
}
|
||||||
|
|
||||||
#[implement(Service)]
|
#[implement(Service)]
|
||||||
pub async fn get_statekey_from_short(&self, shortstatekey: u64) -> Result<(StateEventType, String)> {
|
pub async fn get_statekey_from_short(&self, shortstatekey: ShortStateKey) -> Result<(StateEventType, String)> {
|
||||||
const BUFSIZE: usize = size_of::<u64>();
|
const BUFSIZE: usize = size_of::<ShortStateKey>();
|
||||||
|
|
||||||
self.db
|
self.db
|
||||||
.shortstatekey_statekey
|
.shortstatekey_statekey
|
||||||
|
@ -191,8 +192,8 @@ pub async fn get_statekey_from_short(&self, shortstatekey: u64) -> Result<(State
|
||||||
|
|
||||||
/// Returns (shortstatehash, already_existed)
|
/// Returns (shortstatehash, already_existed)
|
||||||
#[implement(Service)]
|
#[implement(Service)]
|
||||||
pub async fn get_or_create_shortstatehash(&self, state_hash: &[u8]) -> (u64, bool) {
|
pub async fn get_or_create_shortstatehash(&self, state_hash: &[u8]) -> (ShortStateHash, bool) {
|
||||||
const BUFSIZE: usize = size_of::<u64>();
|
const BUFSIZE: usize = size_of::<ShortStateHash>();
|
||||||
|
|
||||||
if let Ok(shortstatehash) = self
|
if let Ok(shortstatehash) = self
|
||||||
.db
|
.db
|
||||||
|
@ -215,19 +216,19 @@ pub async fn get_or_create_shortstatehash(&self, state_hash: &[u8]) -> (u64, boo
|
||||||
}
|
}
|
||||||
|
|
||||||
#[implement(Service)]
|
#[implement(Service)]
|
||||||
pub async fn get_shortroomid(&self, room_id: &RoomId) -> Result<u64> {
|
pub async fn get_shortroomid(&self, room_id: &RoomId) -> Result<ShortRoomId> {
|
||||||
self.db.roomid_shortroomid.get(room_id).await.deserialized()
|
self.db.roomid_shortroomid.get(room_id).await.deserialized()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[implement(Service)]
|
#[implement(Service)]
|
||||||
pub async fn get_or_create_shortroomid(&self, room_id: &RoomId) -> u64 {
|
pub async fn get_or_create_shortroomid(&self, room_id: &RoomId) -> ShortRoomId {
|
||||||
self.db
|
self.db
|
||||||
.roomid_shortroomid
|
.roomid_shortroomid
|
||||||
.get(room_id)
|
.get(room_id)
|
||||||
.await
|
.await
|
||||||
.deserialized()
|
.deserialized()
|
||||||
.unwrap_or_else(|_| {
|
.unwrap_or_else(|_| {
|
||||||
const BUFSIZE: usize = size_of::<u64>();
|
const BUFSIZE: usize = size_of::<ShortRoomId>();
|
||||||
|
|
||||||
let short = self.services.globals.next_count().unwrap();
|
let short = self.services.globals.next_count().unwrap();
|
||||||
debug_assert!(size_of_val(&short) == BUFSIZE, "buffer requirement changed");
|
debug_assert!(size_of_val(&short) == BUFSIZE, "buffer requirement changed");
|
||||||
|
|
|
@ -5,7 +5,7 @@ use database::{Deserialized, Map};
|
||||||
use futures::TryFutureExt;
|
use futures::TryFutureExt;
|
||||||
use ruma::{events::StateEventType, EventId, RoomId};
|
use ruma::{events::StateEventType, EventId, RoomId};
|
||||||
|
|
||||||
use crate::{rooms, Dep};
|
use crate::{rooms, rooms::short::ShortStateHash, Dep};
|
||||||
|
|
||||||
pub(super) struct Data {
|
pub(super) struct Data {
|
||||||
eventid_shorteventid: Arc<Map>,
|
eventid_shorteventid: Arc<Map>,
|
||||||
|
@ -36,7 +36,7 @@ impl Data {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused_qualifications)] // async traits
|
#[allow(unused_qualifications)] // async traits
|
||||||
pub(super) async fn state_full_ids(&self, shortstatehash: u64) -> Result<HashMap<u64, Arc<EventId>>> {
|
pub(super) async fn state_full_ids(&self, shortstatehash: ShortStateHash) -> Result<HashMap<u64, Arc<EventId>>> {
|
||||||
let full_state = self
|
let full_state = self
|
||||||
.services
|
.services
|
||||||
.state_compressor
|
.state_compressor
|
||||||
|
@ -69,7 +69,7 @@ impl Data {
|
||||||
|
|
||||||
#[allow(unused_qualifications)] // async traits
|
#[allow(unused_qualifications)] // async traits
|
||||||
pub(super) async fn state_full(
|
pub(super) async fn state_full(
|
||||||
&self, shortstatehash: u64,
|
&self, shortstatehash: ShortStateHash,
|
||||||
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
|
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
|
||||||
let full_state = self
|
let full_state = self
|
||||||
.services
|
.services
|
||||||
|
@ -107,7 +107,7 @@ impl Data {
|
||||||
/// Returns a single PDU from `room_id` with key (`event_type`,`state_key`).
|
/// Returns a single PDU from `room_id` with key (`event_type`,`state_key`).
|
||||||
#[allow(clippy::unused_self)]
|
#[allow(clippy::unused_self)]
|
||||||
pub(super) async fn state_get_id(
|
pub(super) async fn state_get_id(
|
||||||
&self, shortstatehash: u64, event_type: &StateEventType, state_key: &str,
|
&self, shortstatehash: ShortStateHash, event_type: &StateEventType, state_key: &str,
|
||||||
) -> Result<Arc<EventId>> {
|
) -> Result<Arc<EventId>> {
|
||||||
let shortstatekey = self
|
let shortstatekey = self
|
||||||
.services
|
.services
|
||||||
|
@ -147,7 +147,7 @@ impl Data {
|
||||||
|
|
||||||
/// Returns a single PDU from `room_id` with key (`event_type`,`state_key`).
|
/// Returns a single PDU from `room_id` with key (`event_type`,`state_key`).
|
||||||
pub(super) async fn state_get(
|
pub(super) async fn state_get(
|
||||||
&self, shortstatehash: u64, event_type: &StateEventType, state_key: &str,
|
&self, shortstatehash: ShortStateHash, event_type: &StateEventType, state_key: &str,
|
||||||
) -> Result<Arc<PduEvent>> {
|
) -> Result<Arc<PduEvent>> {
|
||||||
self.state_get_id(shortstatehash, event_type, state_key)
|
self.state_get_id(shortstatehash, event_type, state_key)
|
||||||
.and_then(|event_id| async move { self.services.timeline.get_pdu(&event_id).await })
|
.and_then(|event_id| async move { self.services.timeline.get_pdu(&event_id).await })
|
||||||
|
@ -155,7 +155,7 @@ impl Data {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the state hash for this pdu.
|
/// Returns the state hash for this pdu.
|
||||||
pub(super) async fn pdu_shortstatehash(&self, event_id: &EventId) -> Result<u64> {
|
pub(super) async fn pdu_shortstatehash(&self, event_id: &EventId) -> Result<ShortStateHash> {
|
||||||
self.eventid_shorteventid
|
self.eventid_shorteventid
|
||||||
.get(event_id)
|
.get(event_id)
|
||||||
.and_then(|shorteventid| self.shorteventid_shortstatehash.get(&shorteventid))
|
.and_then(|shorteventid| self.shorteventid_shortstatehash.get(&shorteventid))
|
||||||
|
|
|
@ -10,7 +10,11 @@ use database::Map;
|
||||||
use lru_cache::LruCache;
|
use lru_cache::LruCache;
|
||||||
use ruma::{EventId, RoomId};
|
use ruma::{EventId, RoomId};
|
||||||
|
|
||||||
use crate::{rooms, rooms::short::ShortId, Dep};
|
use crate::{
|
||||||
|
rooms,
|
||||||
|
rooms::short::{ShortStateHash, ShortStateKey},
|
||||||
|
Dep,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct Service {
|
pub struct Service {
|
||||||
pub stateinfo_cache: Mutex<StateInfoLruCache>,
|
pub stateinfo_cache: Mutex<StateInfoLruCache>,
|
||||||
|
@ -49,9 +53,8 @@ pub struct HashSetCompressStateEvent {
|
||||||
pub removed: Arc<HashSet<CompressedStateEvent>>,
|
pub removed: Arc<HashSet<CompressedStateEvent>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type ShortStateHash = ShortId;
|
|
||||||
pub(crate) type CompressedStateEvent = [u8; 2 * size_of::<u64>()];
|
pub(crate) type CompressedStateEvent = [u8; 2 * size_of::<u64>()];
|
||||||
type StateInfoLruCache = LruCache<u64, ShortStateInfoVec>;
|
type StateInfoLruCache = LruCache<ShortStateHash, ShortStateInfoVec>;
|
||||||
type ShortStateInfoVec = Vec<ShortStateInfo>;
|
type ShortStateInfoVec = Vec<ShortStateInfo>;
|
||||||
type ParentStatesVec = Vec<ShortStateInfo>;
|
type ParentStatesVec = Vec<ShortStateInfo>;
|
||||||
|
|
||||||
|
@ -86,7 +89,7 @@ impl crate::Service for Service {
|
||||||
impl Service {
|
impl Service {
|
||||||
/// Returns a stack with info on shortstatehash, full state, added diff and
|
/// Returns a stack with info on shortstatehash, full state, added diff and
|
||||||
/// removed diff for the selected shortstatehash and each parent layer.
|
/// removed diff for the selected shortstatehash and each parent layer.
|
||||||
pub async fn load_shortstatehash_info(&self, shortstatehash: u64) -> Result<ShortStateInfoVec> {
|
pub async fn load_shortstatehash_info(&self, shortstatehash: ShortStateHash) -> Result<ShortStateInfoVec> {
|
||||||
if let Some(r) = self
|
if let Some(r) = self
|
||||||
.stateinfo_cache
|
.stateinfo_cache
|
||||||
.lock()
|
.lock()
|
||||||
|
@ -141,7 +144,7 @@ impl Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn compress_state_event(&self, shortstatekey: u64, event_id: &EventId) -> CompressedStateEvent {
|
pub async fn compress_state_event(&self, shortstatekey: ShortStateKey, event_id: &EventId) -> CompressedStateEvent {
|
||||||
let mut v = shortstatekey.to_be_bytes().to_vec();
|
let mut v = shortstatekey.to_be_bytes().to_vec();
|
||||||
v.extend_from_slice(
|
v.extend_from_slice(
|
||||||
&self
|
&self
|
||||||
|
@ -159,7 +162,7 @@ impl Service {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub async fn parse_compressed_state_event(
|
pub async fn parse_compressed_state_event(
|
||||||
&self, compressed_event: &CompressedStateEvent,
|
&self, compressed_event: &CompressedStateEvent,
|
||||||
) -> Result<(u64, Arc<EventId>)> {
|
) -> Result<(ShortStateKey, Arc<EventId>)> {
|
||||||
use utils::u64_from_u8;
|
use utils::u64_from_u8;
|
||||||
|
|
||||||
let shortstatekey = u64_from_u8(&compressed_event[0..size_of::<u64>()]);
|
let shortstatekey = u64_from_u8(&compressed_event[0..size_of::<u64>()]);
|
||||||
|
@ -192,7 +195,7 @@ impl Service {
|
||||||
/// added diff and removed diff for each parent layer
|
/// added diff and removed diff for each parent layer
|
||||||
#[tracing::instrument(skip_all, level = "debug")]
|
#[tracing::instrument(skip_all, level = "debug")]
|
||||||
pub fn save_state_from_diff(
|
pub fn save_state_from_diff(
|
||||||
&self, shortstatehash: u64, statediffnew: Arc<HashSet<CompressedStateEvent>>,
|
&self, shortstatehash: ShortStateHash, statediffnew: Arc<HashSet<CompressedStateEvent>>,
|
||||||
statediffremoved: Arc<HashSet<CompressedStateEvent>>, diff_to_sibling: usize,
|
statediffremoved: Arc<HashSet<CompressedStateEvent>>, diff_to_sibling: usize,
|
||||||
mut parent_states: ParentStatesVec,
|
mut parent_states: ParentStatesVec,
|
||||||
) -> Result {
|
) -> Result {
|
||||||
|
@ -377,9 +380,9 @@ impl Service {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_statediff(&self, shortstatehash: u64) -> Result<StateDiff> {
|
async fn get_statediff(&self, shortstatehash: ShortStateHash) -> Result<StateDiff> {
|
||||||
const BUFSIZE: usize = size_of::<u64>();
|
const BUFSIZE: usize = size_of::<ShortStateHash>();
|
||||||
const STRIDE: usize = size_of::<u64>();
|
const STRIDE: usize = size_of::<ShortStateHash>();
|
||||||
|
|
||||||
let value = self
|
let value = self
|
||||||
.db
|
.db
|
||||||
|
@ -418,7 +421,7 @@ impl Service {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save_statediff(&self, shortstatehash: u64, diff: &StateDiff) {
|
fn save_statediff(&self, shortstatehash: ShortStateHash, diff: &StateDiff) {
|
||||||
let mut value = diff.parent.unwrap_or(0).to_be_bytes().to_vec();
|
let mut value = diff.parent.unwrap_or(0).to_be_bytes().to_vec();
|
||||||
for new in diff.added.iter() {
|
for new in diff.added.iter() {
|
||||||
value.extend_from_slice(&new[..]);
|
value.extend_from_slice(&new[..]);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue