use std::{convert::AsRef, fmt::Debug, future::Future, io::Write}; use arrayvec::ArrayVec; use conduit::{err, implement, utils::IterStream, Result}; use futures::{FutureExt, Stream}; use rocksdb::DBPinnableSlice; use serde::Serialize; use tokio::task; use crate::{ser, util, Handle}; type RocksdbResult<'a> = Result>, rocksdb::Error>; /// 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. #[implement(super::Map)] pub fn qry(&self, key: &K) -> impl Future>> + Send where K: Serialize + ?Sized + Debug, { let mut buf = Vec::::with_capacity(64); 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)] pub fn aqry(&self, key: &K) -> impl Future>> + Send where K: Serialize + ?Sized + Debug, { let mut buf = ArrayVec::::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. #[implement(super::Map)] #[tracing::instrument(skip(self, buf), level = "trace")] pub fn bqry(&self, key: &K, buf: &mut B) -> impl Future>> + Send 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. #[implement(super::Map)] #[tracing::instrument(skip(self, key), fields(%self), level = "trace")] pub fn get(&self, key: &K) -> impl Future>> + Send where K: AsRef<[u8]> + ?Sized + Debug, { let result = self.get_blocking(key); task::consume_budget().map(move |()| result) } /// 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)] pub fn get_blocking(&self, key: &K) -> Result> where K: AsRef<[u8]> + ?Sized, { let res = self .db .db .get_pinned_cf_opt(&self.cf(), key, &self.read_options); into_result_handle(res) } #[implement(super::Map)] #[tracing::instrument(skip(self, keys), fields(%self), level = "trace")] pub fn get_batch<'a, I, K>(&self, keys: I) -> impl Stream>> where I: Iterator + ExactSizeIterator + Debug + Send, K: AsRef<[u8]> + Debug + Send + ?Sized + Sync + 'a, { self.get_batch_blocking(keys).stream() } #[implement(super::Map)] pub fn get_batch_blocking<'a, I, K>(&self, keys: I) -> impl Iterator>> where I: Iterator + ExactSizeIterator + Debug + Send, K: AsRef<[u8]> + Debug + Send + ?Sized + Sync + 'a, { // Optimization can be `true` if key vector is pre-sorted **by the column // comparator**. const SORTED: bool = false; let read_options = &self.read_options; self.db .db .batched_multi_get_cf_opt(&self.cf(), keys, SORTED, read_options) .into_iter() .map(into_result_handle) } fn into_result_handle(result: RocksdbResult<'_>) -> Result> { result .map_err(util::map_err)? .map(Handle::from) .ok_or(err!(Request(NotFound("Not found in database")))) }