Improve usability of wrapped tables
This commit is contained in:
parent
538d15d2d0
commit
237b4a7ecb
@ -3,6 +3,7 @@ use crate::environment::Txn;
|
|||||||
use crate::wrapper::ByteWrapper;
|
use crate::wrapper::ByteWrapper;
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use libmdbx::{TransactionKind, RW};
|
use libmdbx::{TransactionKind, RW};
|
||||||
|
use std::borrow::Borrow;
|
||||||
|
|
||||||
pub struct WrappedTable<K, V> {
|
pub struct WrappedTable<K, V> {
|
||||||
pub raw: RawTable<[u8], [u8]>,
|
pub raw: RawTable<[u8], [u8]>,
|
||||||
@ -14,11 +15,11 @@ impl<K: ByteWrapper, V: ByteWrapper> WrappedTable<K, V> {
|
|||||||
pub fn get<'txn, TK: TransactionKind>(
|
pub fn get<'txn, TK: TransactionKind>(
|
||||||
&self,
|
&self,
|
||||||
txn: &Txn<'txn, TK>,
|
txn: &Txn<'txn, TK>,
|
||||||
k: impl AsRef<K::Item>,
|
k: impl Borrow<K::Item>,
|
||||||
) -> anyhow::Result<Option<V::Item>> {
|
) -> anyhow::Result<Option<V::Item>> {
|
||||||
let k_bytes = self
|
let k_bytes = self
|
||||||
.k_wrapper
|
.k_wrapper
|
||||||
.dump_to_db_bytes(k.as_ref())
|
.dump_to_db_bytes(k.borrow())
|
||||||
.context("whilst converting key to bytes")?;
|
.context("whilst converting key to bytes")?;
|
||||||
|
|
||||||
self.raw
|
self.raw
|
||||||
@ -30,16 +31,16 @@ impl<K: ByteWrapper, V: ByteWrapper> WrappedTable<K, V> {
|
|||||||
pub fn put<'txn>(
|
pub fn put<'txn>(
|
||||||
&self,
|
&self,
|
||||||
txn: &Txn<'txn, RW>,
|
txn: &Txn<'txn, RW>,
|
||||||
k: impl AsRef<K::Item>,
|
k: impl Borrow<K::Item>,
|
||||||
v: impl AsRef<V::Item>,
|
v: impl Borrow<V::Item>,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
let k_bytes = self
|
let k_bytes = self
|
||||||
.k_wrapper
|
.k_wrapper
|
||||||
.dump_to_db_bytes(k.as_ref())
|
.dump_to_db_bytes(k.borrow())
|
||||||
.context("whilst converting key to bytes")?;
|
.context("whilst converting key to bytes")?;
|
||||||
let v_bytes = self
|
let v_bytes = self
|
||||||
.v_wrapper
|
.v_wrapper
|
||||||
.dump_to_db_bytes(v.as_ref())
|
.dump_to_db_bytes(v.borrow())
|
||||||
.context("whilst converting value to bytes")?;
|
.context("whilst converting value to bytes")?;
|
||||||
|
|
||||||
self.raw.put(txn, k_bytes, v_bytes)?;
|
self.raw.put(txn, k_bytes, v_bytes)?;
|
||||||
@ -50,11 +51,11 @@ impl<K: ByteWrapper, V: ByteWrapper> WrappedTable<K, V> {
|
|||||||
pub fn delete<'txn>(
|
pub fn delete<'txn>(
|
||||||
&self,
|
&self,
|
||||||
txn: &Txn<'txn, RW>,
|
txn: &Txn<'txn, RW>,
|
||||||
k: impl AsRef<K::Item>,
|
k: impl Borrow<K::Item>,
|
||||||
) -> anyhow::Result<bool> {
|
) -> anyhow::Result<bool> {
|
||||||
let k_bytes = self
|
let k_bytes = self
|
||||||
.k_wrapper
|
.k_wrapper
|
||||||
.dump_to_db_bytes(k.as_ref())
|
.dump_to_db_bytes(k.borrow())
|
||||||
.context("whilst converting key to bytes")?;
|
.context("whilst converting key to bytes")?;
|
||||||
|
|
||||||
self.raw.delete(txn, k_bytes)
|
self.raw.delete(txn, k_bytes)
|
||||||
|
@ -17,6 +17,14 @@ pub struct SerdeBareWrapper<T> {
|
|||||||
phantom: PhantomData<T>,
|
phantom: PhantomData<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Default for SerdeBareWrapper<T> {
|
||||||
|
fn default() -> Self {
|
||||||
|
SerdeBareWrapper {
|
||||||
|
phantom: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: Serialize + DeserializeOwned> ByteWrapper for SerdeBareWrapper<T> {
|
impl<T: Serialize + DeserializeOwned> ByteWrapper for SerdeBareWrapper<T> {
|
||||||
type Item = T;
|
type Item = T;
|
||||||
|
|
||||||
@ -58,6 +66,40 @@ impl<T: ByteWrapper> ByteWrapper for CompressorWrapper<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> CompressorWrapper<T> {
|
||||||
|
pub fn new_with(inner: T) -> anyhow::Result<Self> {
|
||||||
|
let compressor = Compressor::new(13)?;
|
||||||
|
let decompressor = Decompressor::new()?;
|
||||||
|
|
||||||
|
Ok(CompressorWrapper {
|
||||||
|
inner,
|
||||||
|
compressor: Mutex::new(compressor),
|
||||||
|
decompressor: Mutex::new(decompressor),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new() -> anyhow::Result<Self>
|
||||||
|
where
|
||||||
|
T: Default,
|
||||||
|
{
|
||||||
|
let inner: T = Default::default();
|
||||||
|
|
||||||
|
Self::new_with(inner)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ByteWrapper for String {
|
||||||
|
type Item = String;
|
||||||
|
|
||||||
|
fn load_from_db_bytes(&self, bytes: &[u8]) -> anyhow::Result<Self::Item> {
|
||||||
|
Ok(std::str::from_utf8(bytes)?.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn dump_to_db_bytes(&self, item: &Self::Item) -> anyhow::Result<Vec<u8>> {
|
||||||
|
Ok(item.as_bytes().to_vec())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: ByteWrapper> ByteWrapper for Arc<T> {
|
impl<T: ByteWrapper> ByteWrapper for Arc<T> {
|
||||||
type Item = T::Item;
|
type Item = T::Item;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user