mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-24 17:37:34 +00:00
style: Eliminate implement database
This commit is contained in:
25 files changed
+1480
-1520
No files matched your search
+62
-64
@@ -1,6 +1,6 @@
|
||||
use std::{convert::AsRef, fmt::Debug, sync::Arc};
|
||||
|
||||
use conduwuit::{Err, Result, err, implement, utils::result::MapExpect};
|
||||
use conduwuit::{Err, Result, err, utils::result::MapExpect};
|
||||
use futures::{Future, FutureExt, TryFutureExt, future::ready};
|
||||
use rocksdb::{DBPinnableSlice, ReadOptions};
|
||||
use tokio::task;
|
||||
@@ -10,74 +10,72 @@ use crate::{
|
||||
util::{is_incomplete, map_err, or_else},
|
||||
};
|
||||
|
||||
/// Fetch a value from the database into cache, returning a reference-handle
|
||||
/// asynchronously. The key is referenced directly to perform the query.
|
||||
#[implement(super::Map)]
|
||||
#[tracing::instrument(skip(self, key), fields(%self), level = "trace")]
|
||||
pub fn get<K>(
|
||||
self: &Arc<Self>,
|
||||
key: &K,
|
||||
) -> impl Future<Output = Result<Handle<'_>>> + Send + use<'_, K>
|
||||
where
|
||||
K: AsRef<[u8]> + Debug + ?Sized,
|
||||
{
|
||||
use crate::pool::Get;
|
||||
impl super::Map {
|
||||
/// Fetch a value from the database into cache, returning a reference-handle
|
||||
/// asynchronously. The key is referenced directly to perform the query.
|
||||
#[tracing::instrument(skip(self, key), fields(%self), level = "trace")]
|
||||
pub fn get<K>(
|
||||
self: &Arc<Self>,
|
||||
key: &K,
|
||||
) -> impl Future<Output = Result<Handle<'_>>> + Send + use<'_, K>
|
||||
where
|
||||
K: AsRef<[u8]> + Debug + ?Sized,
|
||||
{
|
||||
use crate::pool::Get;
|
||||
|
||||
let cached = self.get_cached(key);
|
||||
if matches!(cached, Err(_) | Ok(Some(_))) {
|
||||
return task::consume_budget()
|
||||
.map(move |()| cached.map_expect("data found in cache"))
|
||||
.boxed();
|
||||
let cached = self.get_cached(key);
|
||||
if matches!(cached, Err(_) | Ok(Some(_))) {
|
||||
return task::consume_budget()
|
||||
.map(move |()| cached.map_expect("data found in cache"))
|
||||
.boxed();
|
||||
}
|
||||
|
||||
debug_assert!(matches!(cached, Ok(None)), "expected status Incomplete");
|
||||
let cmd = Get {
|
||||
map: self.clone(),
|
||||
key: [key.as_ref().into()].into(),
|
||||
res: None,
|
||||
};
|
||||
|
||||
self.db
|
||||
.pool
|
||||
.execute_get(cmd)
|
||||
.and_then(|mut res| ready(res.remove(0)))
|
||||
.boxed()
|
||||
}
|
||||
|
||||
debug_assert!(matches!(cached, Ok(None)), "expected status Incomplete");
|
||||
let cmd = Get {
|
||||
map: self.clone(),
|
||||
key: [key.as_ref().into()].into(),
|
||||
res: None,
|
||||
};
|
||||
/// Fetch a value from the cache without I/O.
|
||||
#[tracing::instrument(skip(self, key), name = "cache", level = "trace")]
|
||||
pub(crate) fn get_cached<K>(&self, key: &K) -> Result<Option<Handle<'_>>>
|
||||
where
|
||||
K: AsRef<[u8]> + Debug + ?Sized,
|
||||
{
|
||||
let res = self.get_blocking_opts(key, &self.cache_read_options);
|
||||
cached_handle_from(res)
|
||||
}
|
||||
|
||||
self.db
|
||||
.pool
|
||||
.execute_get(cmd)
|
||||
.and_then(|mut res| ready(res.remove(0)))
|
||||
.boxed()
|
||||
}
|
||||
/// Fetch a value from the database into cache, returning a
|
||||
/// reference-handle. The key is referenced directly to perform the query.
|
||||
/// This is a thread- blocking call.
|
||||
#[tracing::instrument(skip(self, key), name = "blocking", level = "trace")]
|
||||
pub fn get_blocking<K>(&self, key: &K) -> Result<Handle<'_>>
|
||||
where
|
||||
K: AsRef<[u8]> + ?Sized,
|
||||
{
|
||||
let res = self.get_blocking_opts(key, &self.read_options);
|
||||
handle_from(res)
|
||||
}
|
||||
|
||||
/// Fetch a value from the cache without I/O.
|
||||
#[implement(super::Map)]
|
||||
#[tracing::instrument(skip(self, key), name = "cache", level = "trace")]
|
||||
pub(crate) fn get_cached<K>(&self, key: &K) -> Result<Option<Handle<'_>>>
|
||||
where
|
||||
K: AsRef<[u8]> + Debug + ?Sized,
|
||||
{
|
||||
let res = self.get_blocking_opts(key, &self.cache_read_options);
|
||||
cached_handle_from(res)
|
||||
}
|
||||
|
||||
/// Fetch a value from the database into cache, returning a reference-handle.
|
||||
/// The key is referenced directly to perform the query. This is a thread-
|
||||
/// blocking call.
|
||||
#[implement(super::Map)]
|
||||
#[tracing::instrument(skip(self, key), name = "blocking", level = "trace")]
|
||||
pub fn get_blocking<K>(&self, key: &K) -> Result<Handle<'_>>
|
||||
where
|
||||
K: AsRef<[u8]> + ?Sized,
|
||||
{
|
||||
let res = self.get_blocking_opts(key, &self.read_options);
|
||||
handle_from(res)
|
||||
}
|
||||
|
||||
#[implement(super::Map)]
|
||||
fn get_blocking_opts<K>(
|
||||
&self,
|
||||
key: &K,
|
||||
read_options: &ReadOptions,
|
||||
) -> Result<Option<DBPinnableSlice<'_>>, rocksdb::Error>
|
||||
where
|
||||
K: AsRef<[u8]> + ?Sized,
|
||||
{
|
||||
self.db.db.get_pinned_cf_opt(&self.cf(), key, read_options)
|
||||
fn get_blocking_opts<K>(
|
||||
&self,
|
||||
key: &K,
|
||||
read_options: &ReadOptions,
|
||||
) -> Result<Option<DBPinnableSlice<'_>>, rocksdb::Error>
|
||||
where
|
||||
K: AsRef<[u8]> + ?Sized,
|
||||
{
|
||||
self.db.db.get_pinned_cf_opt(&self.cf(), key, read_options)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
||||
Reference in new issue
Block a user