Files
continuwuity/src/database/map/get.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

158 lines
4.3 KiB
Rust
Raw Normal View History

2024-11-27 06:28:32 +00:00
use std::{convert::AsRef, fmt::Debug, io::Write, sync::Arc};
2024-09-29 07:37:43 +00:00
use arrayvec::ArrayVec;
2024-12-14 21:58:01 -05:00
use conduwuit::{err, implement, utils::result::MapExpect, Err, Result};
2025-01-01 06:08:20 +00:00
use futures::{future::ready, Future, FutureExt, TryFutureExt};
use rocksdb::{DBPinnableSlice, ReadOptions};
2024-09-29 07:37:43 +00:00
use serde::Serialize;
2024-12-08 05:45:15 +00:00
use tokio::task;
2024-09-29 07:37:43 +00:00
2024-11-27 06:28:32 +00:00
use crate::{
2024-11-28 05:54:34 +00:00
keyval::KeyBuf,
2024-11-27 06:28:32 +00:00
ser,
util::{is_incomplete, map_err, or_else},
Handle,
};
2024-10-01 22:37:01 +00:00
/// Fetch a value from the database into cache, returning a reference-handle
/// asynchronously. The key is serialized into an allocated buffer to perform
/// the query.
2024-09-29 07:37:43 +00:00
#[implement(super::Map)]
2024-11-28 05:54:34 +00:00
#[inline]
2024-11-27 06:28:32 +00:00
pub fn qry<K>(self: &Arc<Self>, key: &K) -> impl Future<Output = Result<Handle<'_>>> + Send
2024-09-29 07:37:43 +00:00
where
K: Serialize + ?Sized + Debug,
{
2024-11-28 05:54:34 +00:00
let mut buf = KeyBuf::new();
2024-09-29 07:37:43 +00:00
self.bqry(key, &mut buf)
}
/// Fetch a value from the database into cache, returning a reference-handle
/// 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)]
2024-11-28 05:54:34 +00:00
#[inline]
pub fn aqry<const MAX: usize, K>(
self: &Arc<Self>,
key: &K,
) -> impl Future<Output = Result<Handle<'_>>> + Send
where
K: Serialize + ?Sized + Debug,
{
let mut buf = ArrayVec::<u8, MAX>::new();
self.bqry(key, &mut buf)
}
/// Fetch a value from the database into cache, returning a reference-handle
/// asynchronously. The key is serialized into a user-supplied Writer.
2024-09-29 07:37:43 +00:00
#[implement(super::Map)]
#[tracing::instrument(skip(self, buf), level = "trace")]
pub fn bqry<K, B>(
self: &Arc<Self>,
key: &K,
buf: &mut B,
) -> impl Future<Output = Result<Handle<'_>>> + Send
2024-09-29 07:37:43 +00:00
where
K: Serialize + ?Sized + Debug,
B: Write + AsRef<[u8]>,
{
let key = ser::serialize(buf, key).expect("failed to serialize query key");
self.get(key)
}
/// Fetch a value from the database into cache, returning a reference-handle
/// asynchronously. The key is referenced directly to perform the query.
2024-09-29 07:37:43 +00:00
#[implement(super::Map)]
2024-11-15 03:44:04 +00:00
#[tracing::instrument(skip(self, key), fields(%self), level = "trace")]
2024-11-27 06:28:32 +00:00
pub fn get<K>(self: &Arc<Self>, key: &K) -> impl Future<Output = Result<Handle<'_>>> + Send
2024-09-29 07:37:43 +00:00
where
2024-11-27 06:28:32 +00:00
K: AsRef<[u8]> + Debug + ?Sized,
2024-09-29 07:37:43 +00:00
{
use crate::pool::Get;
2024-09-29 07:37:43 +00:00
2024-11-27 06:28:32 +00:00
let cached = self.get_cached(key);
if matches!(cached, Err(_) | Ok(Some(_))) {
2024-12-08 05:45:15 +00:00
return task::consume_budget()
.map(move |()| cached.map_expect("data found in cache"))
.boxed();
2024-11-27 06:28:32 +00:00
}
2024-10-01 22:37:01 +00:00
2024-11-27 06:28:32 +00:00
debug_assert!(matches!(cached, Ok(None)), "expected status Incomplete");
let cmd = Get {
2024-11-27 06:28:32 +00:00
map: self.clone(),
2025-01-01 06:08:20 +00:00
key: [key.as_ref().into()].into(),
2024-11-27 06:28:32 +00:00
res: None,
};
2024-11-27 06:28:32 +00:00
2025-01-01 06:08:20 +00:00
self.db
.pool
.execute_get(cmd)
.and_then(|mut res| ready(res.remove(0)))
.boxed()
2024-09-29 07:37:43 +00:00
}
/// 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)
}
2024-11-27 06:28:32 +00:00
/// 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)]
2024-11-30 03:16:57 +00:00
#[tracing::instrument(skip(self, key), name = "blocking", level = "trace")]
2024-11-27 06:28:32 +00:00
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)
2024-11-27 06:28:32 +00:00
}
#[implement(super::Map)]
fn get_blocking_opts<K>(
&self,
key: &K,
read_options: &ReadOptions,
) -> Result<Option<DBPinnableSlice<'_>>, rocksdb::Error>
2024-11-27 06:28:32 +00:00
where
K: AsRef<[u8]> + ?Sized,
2024-11-27 06:28:32 +00:00
{
self.db.db.get_pinned_cf_opt(&self.cf(), key, read_options)
}
2024-11-27 06:28:32 +00:00
#[inline]
pub(super) fn handle_from(
result: Result<Option<DBPinnableSlice<'_>>, rocksdb::Error>,
) -> Result<Handle<'_>> {
result
.map_err(map_err)?
.map(Handle::from)
.ok_or(err!(Request(NotFound("Not found in database"))))
}
#[inline]
pub(super) fn cached_handle_from(
result: Result<Option<DBPinnableSlice<'_>>, rocksdb::Error>,
) -> Result<Option<Handle<'_>>> {
match result {
2024-11-27 06:28:32 +00:00
// cache hit; not found
| Ok(None) => Err!(Request(NotFound("Not found in database"))),
2024-11-27 06:28:32 +00:00
// cache hit; value found
| Ok(Some(result)) => Ok(Some(Handle::from(result))),
2024-11-27 06:28:32 +00:00
// cache miss; unknown
| Err(error) if is_incomplete(&error) => Ok(None),
2024-11-27 06:28:32 +00:00
// some other error occurred
| Err(error) => or_else(error),
2024-11-27 06:28:32 +00:00
}
}