refactor multi-get to handle result type

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-01 22:37:01 +00:00 committed by strawberry
parent 26dcab272d
commit ab06701ed0
3 changed files with 33 additions and 59 deletions

View file

@ -3,10 +3,6 @@ use serde::Deserialize;
use crate::de;
pub(crate) type OwnedKeyVal = (Vec<u8>, Vec<u8>);
pub(crate) type OwnedKey = Vec<u8>;
pub(crate) type OwnedVal = Vec<u8>;
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;
@ -72,10 +68,6 @@ where
de::from_slice::<V>(val)
}
#[inline]
#[must_use]
pub fn to_owned(kv: KeyVal<'_>) -> OwnedKeyVal { (kv.0.to_owned(), kv.1.to_owned()) }
#[inline]
pub fn key<K, V>(kv: KeyVal<'_, K, V>) -> Key<'_, K> { kv.0 }

View file

@ -3,14 +3,12 @@ use std::{convert::AsRef, fmt::Debug, future::Future, io::Write};
use arrayvec::ArrayVec;
use conduit::{err, implement, Result};
use futures::future::ready;
use rocksdb::DBPinnableSlice;
use serde::Serialize;
use crate::{
keyval::{OwnedKey, OwnedVal},
ser,
util::{map_err, or_else},
Handle,
};
use crate::{ser, util, Handle};
type RocksdbResult<'a> = Result<Option<DBPinnableSlice<'a>>, 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
@ -68,17 +66,17 @@ pub fn get_blocking<K>(&self, key: &K) -> Result<Handle<'_>>
where
K: AsRef<[u8]> + ?Sized + Debug,
{
self.db
let res = self
.db
.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"))))
.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_blocking<'a, I, K>(&self, keys: I) -> Vec<Option<OwnedVal>>
pub fn get_batch_blocking<'a, I, K>(&self, keys: I) -> Vec<Result<Handle<'_>>>
where
I: Iterator<Item = &'a K> + ExactSizeIterator + Send + Debug,
K: AsRef<[u8]> + Sized + Debug + 'a,
@ -87,19 +85,18 @@ where
// comparator**.
const SORTED: bool = false;
let mut ret: Vec<Option<OwnedKey>> = Vec::with_capacity(keys.len());
let read_options = &self.read_options;
for res in self
.db
self.db
.db
.batched_multi_get_cf_opt(&self.cf(), keys, SORTED, read_options)
{
match res {
Ok(Some(res)) => ret.push(Some((*res).to_vec())),
Ok(None) => ret.push(None),
Err(e) => or_else(e).expect("database multiget error"),
}
}
ret
.into_iter()
.map(into_result_handle)
.collect()
}
fn into_result_handle(result: RocksdbResult<'_>) -> Result<Handle<'_>> {
result
.map_err(util::map_err)?
.map(Handle::from)
.ok_or(err!(Request(NotFound("Not found in database"))))
}