optimize get w/ zero-copy ref handle

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-02 05:56:10 +00:00
parent 0522fe7d92
commit ee64fb149c
11 changed files with 46 additions and 16 deletions

25
src/database/handle.rs Normal file
View file

@ -0,0 +1,25 @@
use std::ops::Deref;
use rocksdb::DBPinnableSlice;
pub struct Handle<'a> {
val: DBPinnableSlice<'a>,
}
impl<'a> From<DBPinnableSlice<'a>> for Handle<'a> {
fn from(val: DBPinnableSlice<'a>) -> Self {
Self {
val,
}
}
}
impl Deref for Handle<'_> {
type Target = [u8];
fn deref(&self) -> &Self::Target { &self.val }
}
impl AsRef<[u8]> for Handle<'_> {
fn as_ref(&self) -> &[u8] { &self.val }
}

View file

@ -5,7 +5,7 @@ use rocksdb::{
AsColumnFamilyRef, ColumnFamily, Direction, IteratorMode, ReadOptions, WriteBatchWithTransaction, WriteOptions,
};
use crate::{or_else, result, watchers::Watchers, Engine, Iter};
use crate::{or_else, result, watchers::Watchers, Engine, Handle, Iter};
pub struct Map {
name: String,
@ -32,11 +32,11 @@ impl Map {
}))
}
pub fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
pub fn get(&self, key: &[u8]) -> Result<Option<Handle<'_>>> {
let read_options = &self.read_options;
let res = self.db.db.get_cf_opt(&self.cf(), key, read_options);
let res = self.db.db.get_pinned_cf_opt(&self.cf(), key, read_options);
result(res)
Ok(result(res)?.map(Handle::from))
}
pub fn multi_get(&self, keys: &[&[u8]]) -> Result<Vec<Option<Vec<u8>>>> {

View file

@ -1,6 +1,7 @@
mod cork;
mod database;
mod engine;
mod handle;
mod iter;
mod map;
pub mod maps;
@ -13,6 +14,7 @@ extern crate rust_rocksdb as rocksdb;
pub use database::Database;
pub(crate) use engine::Engine;
pub use handle::Handle;
pub use iter::Iter;
pub use map::Map;
pub(crate) use util::{or_else, result};