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
|
|
|
|
2024-09-29 12:49:24 +00:00
|
|
|
use arrayvec::ArrayVec;
|
2024-12-03 10:42:52 +00:00
|
|
|
use conduit::{err, implement, utils::result::MapExpect, Err, Result};
|
|
|
|
|
use futures::{future, Future, FutureExt};
|
2024-09-29 07:37:43 +00:00
|
|
|
use serde::Serialize;
|
|
|
|
|
|
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
|
|
|
|
2024-09-29 12:49:24 +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)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 12:49:24 +00:00
|
|
|
/// 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]
|
2024-11-27 06:28:32 +00:00
|
|
|
pub fn aqry<const MAX: usize, K>(self: &Arc<Self>, key: &K) -> impl Future<Output = Result<Handle<'_>>> + Send
|
2024-09-29 12:49:24 +00:00
|
|
|
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)]
|
2024-11-25 06:26:34 +00:00
|
|
|
#[tracing::instrument(skip(self, buf), level = "trace")]
|
2024-11-27 06:28:32 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 12:49:24 +00:00
|
|
|
/// 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
|
|
|
{
|
2024-12-02 08:51:59 +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-02 08:51:59 +00:00
|
|
|
return future::ready(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");
|
2024-12-02 08:51:59 +00:00
|
|
|
let cmd = Get {
|
2024-11-27 06:28:32 +00:00
|
|
|
map: self.clone(),
|
2024-11-28 05:54:34 +00:00
|
|
|
key: key.as_ref().into(),
|
2024-11-27 06:28:32 +00:00
|
|
|
res: None,
|
2024-12-02 08:51:59 +00:00
|
|
|
};
|
2024-11-27 06:28:32 +00:00
|
|
|
|
2024-12-02 08:51:59 +00:00
|
|
|
self.db.pool.execute_get(cmd).boxed()
|
2024-09-29 07:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
{
|
2024-12-03 10:42:52 +00:00
|
|
|
self.db
|
2024-11-27 06:28:32 +00:00
|
|
|
.db
|
2024-12-03 10:42:52 +00:00
|
|
|
.get_pinned_cf_opt(&self.cf(), key, &self.read_options)
|
|
|
|
|
.map_err(map_err)?
|
|
|
|
|
.map(Handle::from)
|
|
|
|
|
.ok_or(err!(Request(NotFound("Not found in database"))))
|
2024-11-27 06:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Fetch a value from the cache without I/O.
|
|
|
|
|
#[implement(super::Map)]
|
2024-11-30 03:16:57 +00:00
|
|
|
#[tracing::instrument(skip(self, key), name = "cache", level = "trace")]
|
2024-11-27 06:28:32 +00:00
|
|
|
pub(crate) fn get_cached<K>(&self, key: &K) -> Result<Option<Handle<'_>>>
|
|
|
|
|
where
|
|
|
|
|
K: AsRef<[u8]> + Debug + ?Sized,
|
|
|
|
|
{
|
|
|
|
|
let res = self
|
|
|
|
|
.db
|
|
|
|
|
.db
|
|
|
|
|
.get_pinned_cf_opt(&self.cf(), key, &self.cache_read_options);
|
|
|
|
|
|
|
|
|
|
match res {
|
|
|
|
|
// cache hit; not found
|
|
|
|
|
Ok(None) => Err!(Request(NotFound("Not found in database"))),
|
|
|
|
|
|
|
|
|
|
// cache hit; value found
|
|
|
|
|
Ok(Some(res)) => Ok(Some(Handle::from(res))),
|
|
|
|
|
|
|
|
|
|
// cache miss; unknown
|
|
|
|
|
Err(e) if is_incomplete(&e) => Ok(None),
|
|
|
|
|
|
|
|
|
|
// some other error occurred
|
|
|
|
|
Err(e) => or_else(e),
|
|
|
|
|
}
|
|
|
|
|
}
|