mirror of
https://github.com/girlbossceo/conduwuit.git
synced 2025-03-14 18:55:37 +00:00
use smallvec for db query buffering
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
76c75cc05a
commit
3ad6aa59f9
23 changed files with 173 additions and 98 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -765,6 +765,7 @@ dependencies = [
|
|||
"rust-rocksdb-uwu",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"smallvec",
|
||||
"tokio",
|
||||
"tracing",
|
||||
]
|
||||
|
@ -3956,6 +3957,9 @@ name = "smallvec"
|
|||
version = "1.13.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "socket2"
|
||||
|
|
11
Cargo.toml
11
Cargo.toml
|
@ -27,7 +27,16 @@ name = "conduit"
|
|||
|
||||
[workspace.dependencies.arrayvec]
|
||||
version = "0.7.4"
|
||||
features = ["std", "serde"]
|
||||
features = ["serde"]
|
||||
|
||||
[workspace.dependencies.smallvec]
|
||||
version = "1.13.2"
|
||||
features = [
|
||||
"const_generics",
|
||||
"const_new",
|
||||
"serde",
|
||||
"write",
|
||||
]
|
||||
|
||||
[workspace.dependencies.const-str]
|
||||
version = "0.5.7"
|
||||
|
|
|
@ -44,6 +44,7 @@ log.workspace = true
|
|||
rust-rocksdb.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
smallvec.workspace = true
|
||||
tokio.workspace = true
|
||||
tracing.workspace = true
|
||||
|
||||
|
|
|
@ -58,6 +58,7 @@ impl<'a> Deserialized for Result<&'a Handle<'a>> {
|
|||
}
|
||||
|
||||
impl<'a> Deserialized for &'a Handle<'a> {
|
||||
#[inline]
|
||||
fn map_de<T, U, F>(self, f: F) -> Result<U>
|
||||
where
|
||||
F: FnOnce(T) -> U,
|
||||
|
|
|
@ -1,13 +1,42 @@
|
|||
use conduit::Result;
|
||||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use smallvec::SmallVec;
|
||||
|
||||
use crate::de;
|
||||
use crate::{de, ser};
|
||||
|
||||
pub type KeyVal<'a, K = &'a Slice, V = &'a Slice> = (Key<'a, K>, Val<'a, V>);
|
||||
pub type Key<'a, T = &'a Slice> = T;
|
||||
pub type Val<'a, T = &'a Slice> = T;
|
||||
|
||||
pub type Slice = [u8];
|
||||
pub type KeyBuf = KeyBuffer;
|
||||
pub type ValBuf = ValBuffer;
|
||||
|
||||
pub type KeyBuffer<const CAP: usize = KEY_STACK_CAP> = Buffer<CAP>;
|
||||
pub type ValBuffer<const CAP: usize = VAL_STACK_CAP> = Buffer<CAP>;
|
||||
pub type Buffer<const CAP: usize = DEF_STACK_CAP> = SmallVec<[Byte; CAP]>;
|
||||
|
||||
pub type Slice = [Byte];
|
||||
pub type Byte = u8;
|
||||
|
||||
pub const KEY_STACK_CAP: usize = 128;
|
||||
pub const VAL_STACK_CAP: usize = 512;
|
||||
pub const DEF_STACK_CAP: usize = KEY_STACK_CAP;
|
||||
|
||||
#[inline]
|
||||
pub fn serialize_key<T>(val: T) -> Result<KeyBuf>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
ser::serialize_to::<KeyBuf, _>(val)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn serialize_val<T>(val: T) -> Result<ValBuf>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
ser::serialize_to::<ValBuf, _>(val)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn _expect_deserialize<'a, K, V>(kv: Result<KeyVal<'a>>) -> KeyVal<'a, K, V>
|
||||
|
|
|
@ -9,23 +9,25 @@ use conduit::{
|
|||
use futures::FutureExt;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::ser;
|
||||
use crate::{keyval::KeyBuf, ser};
|
||||
|
||||
/// Returns true if the map contains the key.
|
||||
/// - key is serialized into allocated buffer
|
||||
/// - harder errors may not be reported
|
||||
#[inline]
|
||||
#[implement(super::Map)]
|
||||
pub fn contains<K>(self: &Arc<Self>, key: &K) -> impl Future<Output = bool> + Send + '_
|
||||
where
|
||||
K: Serialize + ?Sized + Debug,
|
||||
{
|
||||
let mut buf = Vec::<u8>::with_capacity(64);
|
||||
let mut buf = KeyBuf::new();
|
||||
self.bcontains(key, &mut buf)
|
||||
}
|
||||
|
||||
/// Returns true if the map contains the key.
|
||||
/// - key is serialized into stack-buffer
|
||||
/// - harder errors will panic
|
||||
#[inline]
|
||||
#[implement(super::Map)]
|
||||
pub fn acontains<const MAX: usize, K>(self: &Arc<Self>, key: &K) -> impl Future<Output = bool> + Send + '_
|
||||
where
|
||||
|
@ -51,6 +53,7 @@ where
|
|||
|
||||
/// Returns Ok if the map contains the key.
|
||||
/// - key is raw
|
||||
#[inline]
|
||||
#[implement(super::Map)]
|
||||
pub fn exists<'a, K>(self: &'a Arc<Self>, key: &K) -> impl Future<Output = Result> + Send + 'a
|
||||
where
|
||||
|
|
|
@ -7,6 +7,7 @@ use rocksdb::DBPinnableSlice;
|
|||
use serde::Serialize;
|
||||
|
||||
use crate::{
|
||||
keyval::KeyBuf,
|
||||
ser,
|
||||
util::{is_incomplete, map_err, or_else},
|
||||
Handle,
|
||||
|
@ -18,11 +19,12 @@ type RocksdbResult<'a> = Result<Option<DBPinnableSlice<'a>>, rocksdb::Error>;
|
|||
/// asynchronously. The key is serialized into an allocated buffer to perform
|
||||
/// the query.
|
||||
#[implement(super::Map)]
|
||||
#[inline]
|
||||
pub fn qry<K>(self: &Arc<Self>, key: &K) -> impl Future<Output = Result<Handle<'_>>> + Send
|
||||
where
|
||||
K: Serialize + ?Sized + Debug,
|
||||
{
|
||||
let mut buf = Vec::<u8>::with_capacity(64);
|
||||
let mut buf = KeyBuf::new();
|
||||
self.bqry(key, &mut buf)
|
||||
}
|
||||
|
||||
|
@ -30,6 +32,7 @@ where
|
|||
/// asynchronously. The key is serialized into a fixed-sized buffer to perform
|
||||
/// the query. The maximum size is supplied as const generic parameter.
|
||||
#[implement(super::Map)]
|
||||
#[inline]
|
||||
pub fn aqry<const MAX: usize, K>(self: &Arc<Self>, key: &K) -> impl Future<Output = Result<Handle<'_>>> + Send
|
||||
where
|
||||
K: Serialize + ?Sized + Debug,
|
||||
|
@ -69,11 +72,8 @@ where
|
|||
debug_assert!(matches!(cached, Ok(None)), "expected status Incomplete");
|
||||
let cmd = Cmd::Get(Get {
|
||||
map: self.clone(),
|
||||
key: key.as_ref().into(),
|
||||
res: None,
|
||||
key: key
|
||||
.as_ref()
|
||||
.try_into()
|
||||
.expect("failed to copy key into buffer"),
|
||||
});
|
||||
|
||||
self.db.pool.execute(cmd).boxed()
|
||||
|
|
|
@ -10,20 +10,25 @@ use conduit::implement;
|
|||
use rocksdb::WriteBatchWithTransaction;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{ser, util::or_else};
|
||||
use crate::{
|
||||
keyval::{KeyBuf, ValBuf},
|
||||
ser,
|
||||
util::or_else,
|
||||
};
|
||||
|
||||
/// Insert Key/Value
|
||||
///
|
||||
/// - Key is serialized
|
||||
/// - Val is serialized
|
||||
#[implement(super::Map)]
|
||||
#[inline]
|
||||
pub fn put<K, V>(&self, key: K, val: V)
|
||||
where
|
||||
K: Serialize + Debug,
|
||||
V: Serialize,
|
||||
{
|
||||
let mut key_buf = Vec::new();
|
||||
let mut val_buf = Vec::new();
|
||||
let mut key_buf = KeyBuf::new();
|
||||
let mut val_buf = ValBuf::new();
|
||||
self.bput(key, val, (&mut key_buf, &mut val_buf));
|
||||
}
|
||||
|
||||
|
@ -32,12 +37,13 @@ where
|
|||
/// - Key is serialized
|
||||
/// - Val is raw
|
||||
#[implement(super::Map)]
|
||||
#[inline]
|
||||
pub fn put_raw<K, V>(&self, key: K, val: V)
|
||||
where
|
||||
K: Serialize + Debug,
|
||||
V: AsRef<[u8]>,
|
||||
{
|
||||
let mut key_buf = Vec::new();
|
||||
let mut key_buf = KeyBuf::new();
|
||||
self.bput_raw(key, val, &mut key_buf);
|
||||
}
|
||||
|
||||
|
@ -46,12 +52,13 @@ where
|
|||
/// - Key is raw
|
||||
/// - Val is serialized
|
||||
#[implement(super::Map)]
|
||||
#[inline]
|
||||
pub fn raw_put<K, V>(&self, key: K, val: V)
|
||||
where
|
||||
K: AsRef<[u8]>,
|
||||
V: Serialize,
|
||||
{
|
||||
let mut val_buf = Vec::new();
|
||||
let mut val_buf = ValBuf::new();
|
||||
self.raw_bput(key, val, &mut val_buf);
|
||||
}
|
||||
|
||||
|
@ -60,12 +67,13 @@ where
|
|||
/// - Key is serialized
|
||||
/// - Val is serialized to stack-buffer
|
||||
#[implement(super::Map)]
|
||||
#[inline]
|
||||
pub fn put_aput<const VMAX: usize, K, V>(&self, key: K, val: V)
|
||||
where
|
||||
K: Serialize + Debug,
|
||||
V: Serialize,
|
||||
{
|
||||
let mut key_buf = Vec::new();
|
||||
let mut key_buf = KeyBuf::new();
|
||||
let mut val_buf = ArrayVec::<u8, VMAX>::new();
|
||||
self.bput(key, val, (&mut key_buf, &mut val_buf));
|
||||
}
|
||||
|
@ -75,13 +83,14 @@ where
|
|||
/// - Key is serialized to stack-buffer
|
||||
/// - Val is serialized
|
||||
#[implement(super::Map)]
|
||||
#[inline]
|
||||
pub fn aput_put<const KMAX: usize, K, V>(&self, key: K, val: V)
|
||||
where
|
||||
K: Serialize + Debug,
|
||||
V: Serialize,
|
||||
{
|
||||
let mut key_buf = ArrayVec::<u8, KMAX>::new();
|
||||
let mut val_buf = Vec::new();
|
||||
let mut val_buf = ValBuf::new();
|
||||
self.bput(key, val, (&mut key_buf, &mut val_buf));
|
||||
}
|
||||
|
||||
|
@ -90,6 +99,7 @@ where
|
|||
/// - Key is serialized to stack-buffer
|
||||
/// - Val is serialized to stack-buffer
|
||||
#[implement(super::Map)]
|
||||
#[inline]
|
||||
pub fn aput<const KMAX: usize, const VMAX: usize, K, V>(&self, key: K, val: V)
|
||||
where
|
||||
K: Serialize + Debug,
|
||||
|
@ -105,6 +115,7 @@ where
|
|||
/// - Key is serialized to stack-buffer
|
||||
/// - Val is raw
|
||||
#[implement(super::Map)]
|
||||
#[inline]
|
||||
pub fn aput_raw<const KMAX: usize, K, V>(&self, key: K, val: V)
|
||||
where
|
||||
K: Serialize + Debug,
|
||||
|
@ -119,6 +130,7 @@ where
|
|||
/// - Key is raw
|
||||
/// - Val is serialized to stack-buffer
|
||||
#[implement(super::Map)]
|
||||
#[inline]
|
||||
pub fn raw_aput<const VMAX: usize, K, V>(&self, key: K, val: V)
|
||||
where
|
||||
K: AsRef<[u8]>,
|
||||
|
|
|
@ -4,7 +4,10 @@ use conduit::{implement, Result};
|
|||
use futures::{Stream, StreamExt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{keyval, keyval::Key, ser, stream};
|
||||
use crate::{
|
||||
keyval::{result_deserialize_key, serialize_key, Key},
|
||||
stream,
|
||||
};
|
||||
|
||||
#[implement(super::Map)]
|
||||
pub fn keys_from<'a, K, P>(&'a self, from: &P) -> impl Stream<Item = Result<Key<'_, K>>> + Send
|
||||
|
@ -12,8 +15,7 @@ where
|
|||
P: Serialize + ?Sized + Debug,
|
||||
K: Deserialize<'a> + Send,
|
||||
{
|
||||
self.keys_from_raw(from)
|
||||
.map(keyval::result_deserialize_key::<K>)
|
||||
self.keys_from_raw(from).map(result_deserialize_key::<K>)
|
||||
}
|
||||
|
||||
#[implement(super::Map)]
|
||||
|
@ -22,7 +24,7 @@ pub fn keys_from_raw<P>(&self, from: &P) -> impl Stream<Item = Result<Key<'_>>>
|
|||
where
|
||||
P: Serialize + ?Sized + Debug,
|
||||
{
|
||||
let key = ser::serialize_to_vec(from).expect("failed to serialize query key");
|
||||
let key = serialize_key(from).expect("failed to serialize query key");
|
||||
self.raw_keys_from(&key)
|
||||
}
|
||||
|
||||
|
@ -32,8 +34,7 @@ where
|
|||
P: AsRef<[u8]> + ?Sized + Debug + Sync,
|
||||
K: Deserialize<'a> + Send,
|
||||
{
|
||||
self.raw_keys_from(from)
|
||||
.map(keyval::result_deserialize_key::<K>)
|
||||
self.raw_keys_from(from).map(result_deserialize_key::<K>)
|
||||
}
|
||||
|
||||
#[implement(super::Map)]
|
||||
|
|
|
@ -8,7 +8,7 @@ use futures::{
|
|||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{keyval, keyval::Key, ser};
|
||||
use crate::keyval::{result_deserialize_key, serialize_key, Key};
|
||||
|
||||
#[implement(super::Map)]
|
||||
pub fn keys_prefix<'a, K, P>(&'a self, prefix: &P) -> impl Stream<Item = Result<Key<'_, K>>> + Send
|
||||
|
@ -17,7 +17,7 @@ where
|
|||
K: Deserialize<'a> + Send,
|
||||
{
|
||||
self.keys_prefix_raw(prefix)
|
||||
.map(keyval::result_deserialize_key::<K>)
|
||||
.map(result_deserialize_key::<K>)
|
||||
}
|
||||
|
||||
#[implement(super::Map)]
|
||||
|
@ -26,7 +26,7 @@ pub fn keys_prefix_raw<P>(&self, prefix: &P) -> impl Stream<Item = Result<Key<'_
|
|||
where
|
||||
P: Serialize + ?Sized + Debug,
|
||||
{
|
||||
let key = ser::serialize_to_vec(prefix).expect("failed to serialize query key");
|
||||
let key = serialize_key(prefix).expect("failed to serialize query key");
|
||||
self.raw_keys_from(&key)
|
||||
.try_take_while(move |k: &Key<'_>| future::ok(k.starts_with(&key)))
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ where
|
|||
K: Deserialize<'a> + Send + 'a,
|
||||
{
|
||||
self.raw_keys_prefix(prefix)
|
||||
.map(keyval::result_deserialize_key::<K>)
|
||||
.map(result_deserialize_key::<K>)
|
||||
}
|
||||
|
||||
#[implement(super::Map)]
|
||||
|
|
|
@ -4,18 +4,20 @@ use arrayvec::ArrayVec;
|
|||
use conduit::implement;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{ser, util::or_else};
|
||||
use crate::{keyval::KeyBuf, ser, util::or_else};
|
||||
|
||||
#[implement(super::Map)]
|
||||
#[inline]
|
||||
pub fn del<K>(&self, key: K)
|
||||
where
|
||||
K: Serialize + Debug,
|
||||
{
|
||||
let mut buf = Vec::<u8>::with_capacity(64);
|
||||
let mut buf = KeyBuf::new();
|
||||
self.bdel(key, &mut buf);
|
||||
}
|
||||
|
||||
#[implement(super::Map)]
|
||||
#[inline]
|
||||
pub fn adel<const MAX: usize, K>(&self, key: K)
|
||||
where
|
||||
K: Serialize + Debug,
|
||||
|
|
|
@ -4,7 +4,10 @@ use conduit::{implement, Result};
|
|||
use futures::{Stream, StreamExt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{keyval, keyval::Key, ser, stream};
|
||||
use crate::{
|
||||
keyval::{result_deserialize_key, serialize_key, Key},
|
||||
stream,
|
||||
};
|
||||
|
||||
#[implement(super::Map)]
|
||||
pub fn rev_keys_from<'a, K, P>(&'a self, from: &P) -> impl Stream<Item = Result<Key<'_, K>>> + Send
|
||||
|
@ -13,7 +16,7 @@ where
|
|||
K: Deserialize<'a> + Send,
|
||||
{
|
||||
self.rev_keys_from_raw(from)
|
||||
.map(keyval::result_deserialize_key::<K>)
|
||||
.map(result_deserialize_key::<K>)
|
||||
}
|
||||
|
||||
#[implement(super::Map)]
|
||||
|
@ -22,7 +25,7 @@ pub fn rev_keys_from_raw<P>(&self, from: &P) -> impl Stream<Item = Result<Key<'_
|
|||
where
|
||||
P: Serialize + ?Sized + Debug,
|
||||
{
|
||||
let key = ser::serialize_to_vec(from).expect("failed to serialize query key");
|
||||
let key = serialize_key(from).expect("failed to serialize query key");
|
||||
self.rev_raw_keys_from(&key)
|
||||
}
|
||||
|
||||
|
@ -33,7 +36,7 @@ where
|
|||
K: Deserialize<'a> + Send,
|
||||
{
|
||||
self.rev_raw_keys_from(from)
|
||||
.map(keyval::result_deserialize_key::<K>)
|
||||
.map(result_deserialize_key::<K>)
|
||||
}
|
||||
|
||||
#[implement(super::Map)]
|
||||
|
|
|
@ -8,7 +8,7 @@ use futures::{
|
|||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{keyval, keyval::Key, ser};
|
||||
use crate::keyval::{result_deserialize_key, serialize_key, Key};
|
||||
|
||||
#[implement(super::Map)]
|
||||
pub fn rev_keys_prefix<'a, K, P>(&'a self, prefix: &P) -> impl Stream<Item = Result<Key<'_, K>>> + Send
|
||||
|
@ -17,7 +17,7 @@ where
|
|||
K: Deserialize<'a> + Send,
|
||||
{
|
||||
self.rev_keys_prefix_raw(prefix)
|
||||
.map(keyval::result_deserialize_key::<K>)
|
||||
.map(result_deserialize_key::<K>)
|
||||
}
|
||||
|
||||
#[implement(super::Map)]
|
||||
|
@ -26,7 +26,7 @@ pub fn rev_keys_prefix_raw<P>(&self, prefix: &P) -> impl Stream<Item = Result<Ke
|
|||
where
|
||||
P: Serialize + ?Sized + Debug,
|
||||
{
|
||||
let key = ser::serialize_to_vec(prefix).expect("failed to serialize query key");
|
||||
let key = serialize_key(prefix).expect("failed to serialize query key");
|
||||
self.rev_raw_keys_from(&key)
|
||||
.try_take_while(move |k: &Key<'_>| future::ok(k.starts_with(&key)))
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ where
|
|||
K: Deserialize<'a> + Send + 'a,
|
||||
{
|
||||
self.rev_raw_keys_prefix(prefix)
|
||||
.map(keyval::result_deserialize_key::<K>)
|
||||
.map(result_deserialize_key::<K>)
|
||||
}
|
||||
|
||||
#[implement(super::Map)]
|
||||
|
|
|
@ -4,7 +4,10 @@ use conduit::{implement, Result};
|
|||
use futures::stream::{Stream, StreamExt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{keyval, keyval::KeyVal, ser, stream};
|
||||
use crate::{
|
||||
keyval::{result_deserialize, serialize_key, KeyVal},
|
||||
stream,
|
||||
};
|
||||
|
||||
/// Iterate key-value entries in the map starting from upper-bound.
|
||||
///
|
||||
|
@ -18,7 +21,7 @@ where
|
|||
V: Deserialize<'a> + Send,
|
||||
{
|
||||
self.rev_stream_from_raw(from)
|
||||
.map(keyval::result_deserialize::<K, V>)
|
||||
.map(result_deserialize::<K, V>)
|
||||
}
|
||||
|
||||
/// Iterate key-value entries in the map starting from upper-bound.
|
||||
|
@ -31,7 +34,7 @@ pub fn rev_stream_from_raw<P>(&self, from: &P) -> impl Stream<Item = Result<KeyV
|
|||
where
|
||||
P: Serialize + ?Sized + Debug,
|
||||
{
|
||||
let key = ser::serialize_to_vec(from).expect("failed to serialize query key");
|
||||
let key = serialize_key(from).expect("failed to serialize query key");
|
||||
self.rev_raw_stream_from(&key)
|
||||
}
|
||||
|
||||
|
@ -47,7 +50,7 @@ where
|
|||
V: Deserialize<'a> + Send,
|
||||
{
|
||||
self.rev_raw_stream_from(from)
|
||||
.map(keyval::result_deserialize::<K, V>)
|
||||
.map(result_deserialize::<K, V>)
|
||||
}
|
||||
|
||||
/// Iterate key-value entries in the map starting from upper-bound.
|
||||
|
|
|
@ -8,7 +8,7 @@ use futures::{
|
|||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{keyval, keyval::KeyVal, ser};
|
||||
use crate::keyval::{result_deserialize, serialize_key, KeyVal};
|
||||
|
||||
/// Iterate key-value entries in the map where the key matches a prefix.
|
||||
///
|
||||
|
@ -22,7 +22,7 @@ where
|
|||
V: Deserialize<'a> + Send,
|
||||
{
|
||||
self.rev_stream_prefix_raw(prefix)
|
||||
.map(keyval::result_deserialize::<K, V>)
|
||||
.map(result_deserialize::<K, V>)
|
||||
}
|
||||
|
||||
/// Iterate key-value entries in the map where the key matches a prefix.
|
||||
|
@ -35,7 +35,7 @@ pub fn rev_stream_prefix_raw<P>(&self, prefix: &P) -> impl Stream<Item = Result<
|
|||
where
|
||||
P: Serialize + ?Sized + Debug,
|
||||
{
|
||||
let key = ser::serialize_to_vec(prefix).expect("failed to serialize query key");
|
||||
let key = serialize_key(prefix).expect("failed to serialize query key");
|
||||
self.rev_raw_stream_from(&key)
|
||||
.try_take_while(move |(k, _): &KeyVal<'_>| future::ok(k.starts_with(&key)))
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ where
|
|||
V: Deserialize<'a> + Send + 'a,
|
||||
{
|
||||
self.rev_raw_stream_prefix(prefix)
|
||||
.map(keyval::result_deserialize::<K, V>)
|
||||
.map(result_deserialize::<K, V>)
|
||||
}
|
||||
|
||||
/// Iterate key-value entries in the map where the key matches a prefix.
|
||||
|
|
|
@ -4,7 +4,10 @@ use conduit::{implement, Result};
|
|||
use futures::stream::{Stream, StreamExt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{keyval, keyval::KeyVal, ser, stream};
|
||||
use crate::{
|
||||
keyval::{result_deserialize, serialize_key, KeyVal},
|
||||
stream,
|
||||
};
|
||||
|
||||
/// Iterate key-value entries in the map starting from lower-bound.
|
||||
///
|
||||
|
@ -17,8 +20,7 @@ where
|
|||
K: Deserialize<'a> + Send,
|
||||
V: Deserialize<'a> + Send,
|
||||
{
|
||||
self.stream_from_raw(from)
|
||||
.map(keyval::result_deserialize::<K, V>)
|
||||
self.stream_from_raw(from).map(result_deserialize::<K, V>)
|
||||
}
|
||||
|
||||
/// Iterate key-value entries in the map starting from lower-bound.
|
||||
|
@ -31,7 +33,7 @@ pub fn stream_from_raw<P>(&self, from: &P) -> impl Stream<Item = Result<KeyVal<'
|
|||
where
|
||||
P: Serialize + ?Sized + Debug,
|
||||
{
|
||||
let key = ser::serialize_to_vec(from).expect("failed to serialize query key");
|
||||
let key = serialize_key(from).expect("failed to serialize query key");
|
||||
self.raw_stream_from(&key)
|
||||
}
|
||||
|
||||
|
@ -46,8 +48,7 @@ where
|
|||
K: Deserialize<'a> + Send,
|
||||
V: Deserialize<'a> + Send,
|
||||
{
|
||||
self.raw_stream_from(from)
|
||||
.map(keyval::result_deserialize::<K, V>)
|
||||
self.raw_stream_from(from).map(result_deserialize::<K, V>)
|
||||
}
|
||||
|
||||
/// Iterate key-value entries in the map starting from lower-bound.
|
||||
|
|
|
@ -8,7 +8,7 @@ use futures::{
|
|||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{keyval, keyval::KeyVal, ser};
|
||||
use crate::keyval::{result_deserialize, serialize_key, KeyVal};
|
||||
|
||||
/// Iterate key-value entries in the map where the key matches a prefix.
|
||||
///
|
||||
|
@ -22,7 +22,7 @@ where
|
|||
V: Deserialize<'a> + Send,
|
||||
{
|
||||
self.stream_prefix_raw(prefix)
|
||||
.map(keyval::result_deserialize::<K, V>)
|
||||
.map(result_deserialize::<K, V>)
|
||||
}
|
||||
|
||||
/// Iterate key-value entries in the map where the key matches a prefix.
|
||||
|
@ -35,7 +35,7 @@ pub fn stream_prefix_raw<P>(&self, prefix: &P) -> impl Stream<Item = Result<KeyV
|
|||
where
|
||||
P: Serialize + ?Sized + Debug,
|
||||
{
|
||||
let key = ser::serialize_to_vec(prefix).expect("failed to serialize query key");
|
||||
let key = serialize_key(prefix).expect("failed to serialize query key");
|
||||
self.raw_stream_from(&key)
|
||||
.try_take_while(move |(k, _): &KeyVal<'_>| future::ok(k.starts_with(&key)))
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ where
|
|||
V: Deserialize<'a> + Send + 'a,
|
||||
{
|
||||
self.raw_stream_prefix(prefix)
|
||||
.map(keyval::result_deserialize::<K, V>)
|
||||
.map(result_deserialize::<K, V>)
|
||||
}
|
||||
|
||||
/// Iterate key-value entries in the map where the key matches a prefix.
|
||||
|
|
|
@ -28,9 +28,9 @@ pub use self::{
|
|||
de::{Ignore, IgnoreAll},
|
||||
deserialized::Deserialized,
|
||||
handle::Handle,
|
||||
keyval::{KeyVal, Slice},
|
||||
keyval::{serialize_key, serialize_val, KeyVal, Slice},
|
||||
map::Map,
|
||||
ser::{serialize, serialize_to_array, serialize_to_vec, Interfix, Json, Separator, SEP},
|
||||
ser::{serialize, serialize_to, serialize_to_vec, Interfix, Json, Separator, SEP},
|
||||
};
|
||||
|
||||
conduit::mod_ctor! {}
|
||||
|
|
|
@ -5,12 +5,11 @@ use std::{
|
|||
thread::JoinHandle,
|
||||
};
|
||||
|
||||
use arrayvec::ArrayVec;
|
||||
use async_channel::{bounded, Receiver, Sender};
|
||||
use conduit::{debug, defer, err, implement, Result};
|
||||
use futures::channel::oneshot;
|
||||
|
||||
use crate::{Handle, Map};
|
||||
use crate::{keyval::KeyBuf, Handle, Map};
|
||||
|
||||
pub(crate) struct Pool {
|
||||
workers: Mutex<Vec<JoinHandle<()>>>,
|
||||
|
@ -27,7 +26,6 @@ pub(crate) struct Opts {
|
|||
const WORKER_THREAD_NAME: &str = "conduwuit:db";
|
||||
const DEFAULT_QUEUE_SIZE: usize = 1024;
|
||||
const DEFAULT_WORKER_NUM: usize = 32;
|
||||
const KEY_MAX_BYTES: usize = 384;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum Cmd {
|
||||
|
@ -37,10 +35,12 @@ pub(crate) enum Cmd {
|
|||
#[derive(Debug)]
|
||||
pub(crate) struct Get {
|
||||
pub(crate) map: Arc<Map>,
|
||||
pub(crate) key: ArrayVec<u8, KEY_MAX_BYTES>,
|
||||
pub(crate) res: Option<oneshot::Sender<Result<Handle<'static>>>>,
|
||||
pub(crate) key: KeyBuf,
|
||||
pub(crate) res: Option<ResultSender>,
|
||||
}
|
||||
|
||||
type ResultSender = oneshot::Sender<Result<Handle<'static>>>;
|
||||
|
||||
#[implement(Pool)]
|
||||
pub(crate) fn new(opts: &Opts) -> Result<Arc<Self>> {
|
||||
let queue_size = opts.queue_size.unwrap_or(DEFAULT_QUEUE_SIZE);
|
||||
|
@ -92,7 +92,6 @@ pub(crate) fn close(self: &Arc<Self>) {
|
|||
receivers = %self.send.receiver_count(),
|
||||
"Closing pool channel"
|
||||
);
|
||||
|
||||
let closing = self.send.close();
|
||||
debug_assert!(closing, "channel is not closing");
|
||||
|
||||
|
@ -116,11 +115,7 @@ pub(crate) fn close(self: &Arc<Self>) {
|
|||
#[tracing::instrument(skip(self, cmd), level = "trace")]
|
||||
pub(crate) async fn execute(&self, mut cmd: Cmd) -> Result<Handle<'_>> {
|
||||
let (send, recv) = oneshot::channel();
|
||||
match &mut cmd {
|
||||
Cmd::Get(ref mut cmd) => {
|
||||
_ = cmd.res.insert(send);
|
||||
},
|
||||
};
|
||||
Self::prepare(&mut cmd, send);
|
||||
|
||||
self.send
|
||||
.send(cmd)
|
||||
|
@ -132,6 +127,15 @@ pub(crate) async fn execute(&self, mut cmd: Cmd) -> Result<Handle<'_>> {
|
|||
.map_err(|e| err!(error!("recv failed {e:?}")))?
|
||||
}
|
||||
|
||||
#[implement(Pool)]
|
||||
fn prepare(cmd: &mut Cmd, send: ResultSender) {
|
||||
match cmd {
|
||||
Cmd::Get(ref mut cmd) => {
|
||||
_ = cmd.res.insert(send);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
#[implement(Pool)]
|
||||
#[tracing::instrument(skip(self))]
|
||||
fn worker(self: Arc<Self>, id: usize) {
|
||||
|
@ -157,25 +161,35 @@ fn handle(&self, id: usize, cmd: &mut Cmd) {
|
|||
#[implement(Pool)]
|
||||
#[tracing::instrument(skip(self, cmd), fields(%cmd.map), level = "trace")]
|
||||
fn handle_get(&self, id: usize, cmd: &mut Get) {
|
||||
debug_assert!(!cmd.key.is_empty(), "querying for empty key");
|
||||
|
||||
// Obtain the result channel.
|
||||
let chan = cmd.res.take().expect("missing result channel");
|
||||
|
||||
// If the future was dropped while the command was queued then we can bail
|
||||
// without any query. This makes it more efficient to use select() variants and
|
||||
// pessimistic parallel queries.
|
||||
// It is worth checking if the future was dropped while the command was queued
|
||||
// so we can bail without paying for any query.
|
||||
if chan.is_canceled() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Perform the actual database query. We reuse our database::Map interface but
|
||||
// limited to the blocking calls, rather than creating another surface directly
|
||||
// with rocksdb here.
|
||||
let result = cmd.map.get_blocking(&cmd.key);
|
||||
let _sent = chan.send(into_send_result(result)).is_ok();
|
||||
|
||||
// Send the result back to the submitter.
|
||||
let chan_result = chan.send(into_send_result(result));
|
||||
|
||||
// If the future was dropped during the query this will fail acceptably.
|
||||
let _chan_sent = chan_result.is_ok();
|
||||
}
|
||||
|
||||
fn into_send_result(result: Result<Handle<'_>>) -> Result<Handle<'static>> {
|
||||
// SAFETY: Necessary to send the Handle (rust_rocksdb::PinnableSlice) through
|
||||
// the channel. The lifetime on the handle is a device by rust-rocksdb to
|
||||
// associate a database lifetime with its assets, not a function of rocksdb or
|
||||
// the asset. The Handle must be dropped before the database is dropped. The
|
||||
// handle must pass through recv_handle() on the other end of the channel.
|
||||
// associate a database lifetime with its assets. The Handle must be dropped
|
||||
// before the database is dropped. The handle must pass through recv_handle() on
|
||||
// the other end of the channel.
|
||||
unsafe { std::mem::transmute(result) }
|
||||
}
|
||||
|
||||
|
|
|
@ -1,28 +1,20 @@
|
|||
use std::io::Write;
|
||||
|
||||
use arrayvec::ArrayVec;
|
||||
use conduit::{debug::type_name, err, result::DebugInspect, utils::exchange, Error, Result};
|
||||
use serde::{ser, Serialize};
|
||||
|
||||
use crate::util::unhandled;
|
||||
|
||||
#[inline]
|
||||
pub fn serialize_to_array<const MAX: usize, T>(val: T) -> Result<ArrayVec<u8, MAX>>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let mut buf = ArrayVec::<u8, MAX>::new();
|
||||
serialize(&mut buf, val)?;
|
||||
|
||||
Ok(buf)
|
||||
}
|
||||
pub fn serialize_to_vec<T: Serialize>(val: T) -> Result<Vec<u8>> { serialize_to::<Vec<u8>, T>(val) }
|
||||
|
||||
#[inline]
|
||||
pub fn serialize_to_vec<T>(val: T) -> Result<Vec<u8>>
|
||||
pub fn serialize_to<B, T>(val: T) -> Result<B>
|
||||
where
|
||||
B: Default + Write + AsRef<[u8]>,
|
||||
T: Serialize,
|
||||
{
|
||||
let mut buf = Vec::with_capacity(64);
|
||||
let mut buf = B::default();
|
||||
serialize(&mut buf, val)?;
|
||||
|
||||
Ok(buf)
|
||||
|
|
|
@ -116,7 +116,7 @@ pub fn changes_since<'a>(
|
|||
&'a self, room_id: Option<&'a RoomId>, user_id: &'a UserId, since: u64,
|
||||
) -> impl Stream<Item = AnyRawAccountDataEvent> + Send + 'a {
|
||||
let prefix = (room_id, user_id, Interfix);
|
||||
let prefix = database::serialize_to_vec(prefix).expect("failed to serialize prefix");
|
||||
let prefix = database::serialize_key(prefix).expect("failed to serialize prefix");
|
||||
|
||||
// Skip the data that's exactly at since, because we sent that last time
|
||||
let first_possible = (room_id, user_id, since.saturating_add(1));
|
||||
|
|
|
@ -39,14 +39,14 @@ impl Data {
|
|||
) -> Result<Vec<u8>> {
|
||||
let dim: &[u32] = &[dim.width, dim.height];
|
||||
let key = (mxc, dim, content_disposition, content_type);
|
||||
let key = database::serialize_to_vec(key)?;
|
||||
let key = database::serialize_key(key)?;
|
||||
self.mediaid_file.insert(&key, []);
|
||||
if let Some(user) = user {
|
||||
let key = (mxc, user);
|
||||
self.mediaid_user.put_raw(key, user);
|
||||
}
|
||||
|
||||
Ok(key)
|
||||
Ok(key.to_vec())
|
||||
}
|
||||
|
||||
pub(super) async fn delete_file_mxc(&self, mxc: &Mxc<'_>) {
|
||||
|
|
|
@ -9,7 +9,7 @@ use conduit::{
|
|||
utils::{stream::TryIgnore, ReadyExt, StreamTools},
|
||||
warn, Result,
|
||||
};
|
||||
use database::{serialize_to_vec, Deserialized, Ignore, Interfix, Json, Map};
|
||||
use database::{serialize_key, Deserialized, Ignore, Interfix, Json, Map};
|
||||
use futures::{future::join4, pin_mut, stream::iter, Stream, StreamExt};
|
||||
use itertools::Itertools;
|
||||
use ruma::{
|
||||
|
@ -289,10 +289,10 @@ impl Service {
|
|||
#[tracing::instrument(skip(self), level = "debug")]
|
||||
pub fn mark_as_joined(&self, user_id: &UserId, room_id: &RoomId) {
|
||||
let userroom_id = (user_id, room_id);
|
||||
let userroom_id = serialize_to_vec(userroom_id).expect("failed to serialize userroom_id");
|
||||
let userroom_id = serialize_key(userroom_id).expect("failed to serialize userroom_id");
|
||||
|
||||
let roomuser_id = (room_id, user_id);
|
||||
let roomuser_id = serialize_to_vec(roomuser_id).expect("failed to serialize roomuser_id");
|
||||
let roomuser_id = serialize_key(roomuser_id).expect("failed to serialize roomuser_id");
|
||||
|
||||
self.db.userroomid_joined.insert(&userroom_id, []);
|
||||
self.db.roomuserid_joined.insert(&roomuser_id, []);
|
||||
|
@ -312,10 +312,10 @@ impl Service {
|
|||
#[tracing::instrument(skip(self), level = "debug")]
|
||||
pub fn mark_as_left(&self, user_id: &UserId, room_id: &RoomId) {
|
||||
let userroom_id = (user_id, room_id);
|
||||
let userroom_id = serialize_to_vec(userroom_id).expect("failed to serialize userroom_id");
|
||||
let userroom_id = serialize_key(userroom_id).expect("failed to serialize userroom_id");
|
||||
|
||||
let roomuser_id = (room_id, user_id);
|
||||
let roomuser_id = serialize_to_vec(roomuser_id).expect("failed to serialize roomuser_id");
|
||||
let roomuser_id = serialize_key(roomuser_id).expect("failed to serialize roomuser_id");
|
||||
|
||||
// (timo) TODO
|
||||
let leftstate = Vec::<Raw<AnySyncStateEvent>>::new();
|
||||
|
@ -716,10 +716,10 @@ impl Service {
|
|||
invite_via: Option<Vec<OwnedServerName>>,
|
||||
) {
|
||||
let roomuser_id = (room_id, user_id);
|
||||
let roomuser_id = serialize_to_vec(roomuser_id).expect("failed to serialize roomuser_id");
|
||||
let roomuser_id = serialize_key(roomuser_id).expect("failed to serialize roomuser_id");
|
||||
|
||||
let userroom_id = (user_id, room_id);
|
||||
let userroom_id = serialize_to_vec(userroom_id).expect("failed to serialize userroom_id");
|
||||
let userroom_id = serialize_key(userroom_id).expect("failed to serialize userroom_id");
|
||||
|
||||
self.db
|
||||
.userroomid_invitestate
|
||||
|
|
Loading…
Add table
Reference in a new issue