From 237b4a7ecb3c359cbfb7f0b0beafc10431745a50 Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Fri, 25 Mar 2022 19:23:11 +0000 Subject: [PATCH] Improve usability of wrapped tables --- src/database/wrapped.rs | 17 +++++++++-------- src/wrapper.rs | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/database/wrapped.rs b/src/database/wrapped.rs index 53f6895..5c38f25 100644 --- a/src/database/wrapped.rs +++ b/src/database/wrapped.rs @@ -3,6 +3,7 @@ use crate::environment::Txn; use crate::wrapper::ByteWrapper; use anyhow::Context; use libmdbx::{TransactionKind, RW}; +use std::borrow::Borrow; pub struct WrappedTable { pub raw: RawTable<[u8], [u8]>, @@ -14,11 +15,11 @@ impl WrappedTable { pub fn get<'txn, TK: TransactionKind>( &self, txn: &Txn<'txn, TK>, - k: impl AsRef, + k: impl Borrow, ) -> anyhow::Result> { let k_bytes = self .k_wrapper - .dump_to_db_bytes(k.as_ref()) + .dump_to_db_bytes(k.borrow()) .context("whilst converting key to bytes")?; self.raw @@ -30,16 +31,16 @@ impl WrappedTable { pub fn put<'txn>( &self, txn: &Txn<'txn, RW>, - k: impl AsRef, - v: impl AsRef, + k: impl Borrow, + v: impl Borrow, ) -> anyhow::Result<()> { let k_bytes = self .k_wrapper - .dump_to_db_bytes(k.as_ref()) + .dump_to_db_bytes(k.borrow()) .context("whilst converting key to bytes")?; let v_bytes = self .v_wrapper - .dump_to_db_bytes(v.as_ref()) + .dump_to_db_bytes(v.borrow()) .context("whilst converting value to bytes")?; self.raw.put(txn, k_bytes, v_bytes)?; @@ -50,11 +51,11 @@ impl WrappedTable { pub fn delete<'txn>( &self, txn: &Txn<'txn, RW>, - k: impl AsRef, + k: impl Borrow, ) -> anyhow::Result { let k_bytes = self .k_wrapper - .dump_to_db_bytes(k.as_ref()) + .dump_to_db_bytes(k.borrow()) .context("whilst converting key to bytes")?; self.raw.delete(txn, k_bytes) diff --git a/src/wrapper.rs b/src/wrapper.rs index 337a8c8..9d98f39 100644 --- a/src/wrapper.rs +++ b/src/wrapper.rs @@ -17,6 +17,14 @@ pub struct SerdeBareWrapper { phantom: PhantomData, } +impl Default for SerdeBareWrapper { + fn default() -> Self { + SerdeBareWrapper { + phantom: Default::default(), + } + } +} + impl ByteWrapper for SerdeBareWrapper { type Item = T; @@ -58,6 +66,40 @@ impl ByteWrapper for CompressorWrapper { } } +impl CompressorWrapper { + pub fn new_with(inner: T) -> anyhow::Result { + 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 + 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 { + Ok(std::str::from_utf8(bytes)?.to_string()) + } + + fn dump_to_db_bytes(&self, item: &Self::Item) -> anyhow::Result> { + Ok(item.as_bytes().to_vec()) + } +} + impl ByteWrapper for Arc { type Item = T::Item;