Safe choice: return Vec<u8> and not some fancy zero-copy type
This commit is contained in:
parent
4539a6c229
commit
7f2cf0b809
9 changed files with 49 additions and 205 deletions
|
@ -555,11 +555,7 @@ impl BlockManager {
|
||||||
// - Ok(false) -> no block was processed, but we are ready for the next iteration
|
// - Ok(false) -> no block was processed, but we are ready for the next iteration
|
||||||
// - Err(_) -> a Sled error occurred when reading/writing from resync_queue/resync_errors
|
// - Err(_) -> a Sled error occurred when reading/writing from resync_queue/resync_errors
|
||||||
async fn resync_iter(&self, must_exit: &mut watch::Receiver<bool>) -> Result<bool, db::Error> {
|
async fn resync_iter(&self, must_exit: &mut watch::Receiver<bool>) -> Result<bool, db::Error> {
|
||||||
let next = self
|
if let Some((time_bytes, hash_bytes)) = self.resync_queue.first()? {
|
||||||
.resync_queue
|
|
||||||
.first()?
|
|
||||||
.map(|(k, v)| (k.into_vec(), v.into_vec()));
|
|
||||||
if let Some((time_bytes, hash_bytes)) = next {
|
|
||||||
let time_msec = u64::from_be_bytes(time_bytes[0..8].try_into().unwrap());
|
let time_msec = u64::from_be_bytes(time_bytes[0..8].try_into().unwrap());
|
||||||
let now = now_msec();
|
let now = now_msec();
|
||||||
|
|
||||||
|
@ -567,7 +563,7 @@ impl BlockManager {
|
||||||
let hash = Hash::try_from(&hash_bytes[..]).unwrap();
|
let hash = Hash::try_from(&hash_bytes[..]).unwrap();
|
||||||
|
|
||||||
if let Some(ec) = self.resync_errors.get(hash.as_slice())? {
|
if let Some(ec) = self.resync_errors.get(hash.as_slice())? {
|
||||||
let ec = ErrorCounter::decode(ec);
|
let ec = ErrorCounter::decode(&ec);
|
||||||
if now < ec.next_try() {
|
if now < ec.next_try() {
|
||||||
// if next retry after an error is not yet,
|
// if next retry after an error is not yet,
|
||||||
// don't do resync and return early, but still
|
// don't do resync and return early, but still
|
||||||
|
@ -608,7 +604,7 @@ impl BlockManager {
|
||||||
warn!("Error when resyncing {:?}: {}", hash, e);
|
warn!("Error when resyncing {:?}: {}", hash, e);
|
||||||
|
|
||||||
let err_counter = match self.resync_errors.get(hash.as_slice())? {
|
let err_counter = match self.resync_errors.get(hash.as_slice())? {
|
||||||
Some(ec) => ErrorCounter::decode(ec).add1(now + 1),
|
Some(ec) => ErrorCounter::decode(&ec).add1(now + 1),
|
||||||
None => ErrorCounter::new(now + 1),
|
None => ErrorCounter::new(now + 1),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -972,7 +968,7 @@ impl ErrorCounter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decode(data: db::Value<'_>) -> Self {
|
fn decode(data: &db::Value) -> Self {
|
||||||
Self {
|
Self {
|
||||||
errors: u64::from_be_bytes(data[0..8].try_into().unwrap()),
|
errors: u64::from_be_bytes(data[0..8].try_into().unwrap()),
|
||||||
last_try: u64::from_be_bytes(data[8..16].try_into().unwrap()),
|
last_try: u64::from_be_bytes(data[8..16].try_into().unwrap()),
|
||||||
|
|
101
src/db/lib.rs
101
src/db/lib.rs
|
@ -21,93 +21,8 @@ pub struct Transaction<'a>(pub(crate) &'a mut dyn ITx);
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Tree(pub(crate) Arc<dyn IDb>, pub(crate) usize);
|
pub struct Tree(pub(crate) Arc<dyn IDb>, pub(crate) usize);
|
||||||
|
|
||||||
pub type ValueIter<'a> = Box<dyn std::iter::Iterator<Item = Result<(Value<'a>, Value<'a>)>> + 'a>;
|
pub type Value = Vec<u8>;
|
||||||
|
pub type ValueIter<'a> = Box<dyn std::iter::Iterator<Item = Result<(Value, Value)>> + 'a>;
|
||||||
// ----
|
|
||||||
|
|
||||||
pub struct Value<'a>(pub(crate) Box<dyn IValue<'a> + 'a>);
|
|
||||||
|
|
||||||
pub trait IValue<'a>: AsRef<[u8]> + core::borrow::Borrow<[u8]> {
|
|
||||||
fn take_maybe(&mut self) -> Vec<u8>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Value<'a> {
|
|
||||||
#[inline]
|
|
||||||
pub fn into_vec(mut self) -> Vec<u8> {
|
|
||||||
self.0.take_maybe()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> AsRef<[u8]> for Value<'a> {
|
|
||||||
#[inline]
|
|
||||||
fn as_ref(&self) -> &[u8] {
|
|
||||||
self.0.as_ref().as_ref()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> std::borrow::Borrow<[u8]> for Value<'a> {
|
|
||||||
#[inline]
|
|
||||||
fn borrow(&self) -> &[u8] {
|
|
||||||
self.0.as_ref().borrow()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> core::ops::Deref for Value<'a> {
|
|
||||||
type Target = [u8];
|
|
||||||
#[inline]
|
|
||||||
fn deref(&self) -> &[u8] {
|
|
||||||
self.0.as_ref().as_ref()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, T> PartialEq<T> for Value<'a>
|
|
||||||
where
|
|
||||||
T: AsRef<[u8]>,
|
|
||||||
{
|
|
||||||
fn eq(&self, other: &T) -> bool {
|
|
||||||
self.as_ref() == other.as_ref()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> std::fmt::Debug for Value<'a> {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
for line in hexdump::hexdump_iter(self.as_ref()) {
|
|
||||||
f.write_str(&line)?;
|
|
||||||
f.write_str("\n")?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> IValue<'a> for Vec<u8> {
|
|
||||||
fn take_maybe(&mut self) -> Vec<u8> {
|
|
||||||
std::mem::take(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<Value<'a>> for Vec<u8> {
|
|
||||||
fn from(v: Value<'a>) -> Vec<u8> {
|
|
||||||
v.into_vec()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<Vec<u8>> for Value<'a> {
|
|
||||||
fn from(v: Vec<u8>) -> Value<'a> {
|
|
||||||
Value(Box::new(v))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&'a [u8]> for Value<'a> {
|
|
||||||
fn from(v: &'a [u8]) -> Value<'a> {
|
|
||||||
Value(Box::new(v))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> IValue<'a> for &'a [u8] {
|
|
||||||
fn take_maybe(&mut self) -> Vec<u8> {
|
|
||||||
self.to_vec()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----
|
// ----
|
||||||
|
|
||||||
|
@ -228,17 +143,17 @@ impl Tree {
|
||||||
Db(self.0.clone())
|
Db(self.0.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get<T: AsRef<[u8]>>(&self, key: T) -> Result<Option<Value<'_>>> {
|
pub fn get<T: AsRef<[u8]>>(&self, key: T) -> Result<Option<Value>> {
|
||||||
self.0.get(self.1, key.as_ref())
|
self.0.get(self.1, key.as_ref())
|
||||||
}
|
}
|
||||||
pub fn len(&self) -> Result<usize> {
|
pub fn len(&self) -> Result<usize> {
|
||||||
self.0.len(self.1)
|
self.0.len(self.1)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn first(&self) -> Result<Option<(Value<'_>, Value<'_>)>> {
|
pub fn first(&self) -> Result<Option<(Value, Value)>> {
|
||||||
self.iter()?.next().transpose()
|
self.iter()?.next().transpose()
|
||||||
}
|
}
|
||||||
pub fn get_gt<T: AsRef<[u8]>>(&self, from: T) -> Result<Option<(Value<'_>, Value<'_>)>> {
|
pub fn get_gt<T: AsRef<[u8]>>(&self, from: T) -> Result<Option<(Value, Value)>> {
|
||||||
self.range((Bound::Excluded(from), Bound::Unbounded))?
|
self.range((Bound::Excluded(from), Bound::Unbounded))?
|
||||||
.next()
|
.next()
|
||||||
.transpose()
|
.transpose()
|
||||||
|
@ -280,7 +195,7 @@ impl Tree {
|
||||||
|
|
||||||
#[allow(clippy::len_without_is_empty)]
|
#[allow(clippy::len_without_is_empty)]
|
||||||
impl<'a> Transaction<'a> {
|
impl<'a> Transaction<'a> {
|
||||||
pub fn get<T: AsRef<[u8]>>(&self, tree: &Tree, key: T) -> Result<Option<Value<'_>>> {
|
pub fn get<T: AsRef<[u8]>>(&self, tree: &Tree, key: T) -> Result<Option<Value>> {
|
||||||
self.0.get(tree.1, key.as_ref())
|
self.0.get(tree.1, key.as_ref())
|
||||||
}
|
}
|
||||||
pub fn len(&self, tree: &Tree) -> Result<usize> {
|
pub fn len(&self, tree: &Tree) -> Result<usize> {
|
||||||
|
@ -342,7 +257,7 @@ pub(crate) trait IDb: Send + Sync {
|
||||||
fn open_tree(&self, name: &str) -> Result<usize>;
|
fn open_tree(&self, name: &str) -> Result<usize>;
|
||||||
fn list_trees(&self) -> Result<Vec<String>>;
|
fn list_trees(&self) -> Result<Vec<String>>;
|
||||||
|
|
||||||
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value<'_>>>;
|
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value>>;
|
||||||
fn len(&self, tree: usize) -> Result<usize>;
|
fn len(&self, tree: usize) -> Result<usize>;
|
||||||
|
|
||||||
fn insert(&self, tree: usize, key: &[u8], value: &[u8]) -> Result<()>;
|
fn insert(&self, tree: usize, key: &[u8], value: &[u8]) -> Result<()>;
|
||||||
|
@ -368,7 +283,7 @@ pub(crate) trait IDb: Send + Sync {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) trait ITx {
|
pub(crate) trait ITx {
|
||||||
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value<'_>>>;
|
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value>>;
|
||||||
fn len(&self, tree: usize) -> Result<usize>;
|
fn len(&self, tree: usize) -> Result<usize>;
|
||||||
|
|
||||||
fn insert(&mut self, tree: usize, key: &[u8], value: &[u8]) -> Result<()>;
|
fn insert(&mut self, tree: usize, key: &[u8], value: &[u8]) -> Result<()>;
|
||||||
|
|
|
@ -11,7 +11,7 @@ use heed::types::ByteSlice;
|
||||||
use heed::{BytesDecode, Env, RoTxn, RwTxn, UntypedDatabase as Database};
|
use heed::{BytesDecode, Env, RoTxn, RwTxn, UntypedDatabase as Database};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Db, Error, IDb, ITx, ITxFn, IValue, Result, TxError, TxFnResult, TxResult, Value, ValueIter,
|
Db, Error, IDb, ITx, ITxFn, Result, TxError, TxFnResult, TxResult, Value, ValueIter,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use heed;
|
pub use heed;
|
||||||
|
@ -101,28 +101,15 @@ impl IDb for LmdbDb {
|
||||||
|
|
||||||
// ----
|
// ----
|
||||||
|
|
||||||
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value<'_>>> {
|
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value>> {
|
||||||
let tree = self.get_tree(tree)?;
|
let tree = self.get_tree(tree)?;
|
||||||
|
|
||||||
let res = TxAndValue {
|
let tx = self.db.read_txn()?;
|
||||||
tx: self.db.read_txn()?,
|
let val = tree.get(&tx, &key)?;
|
||||||
value: NonNull::dangling(),
|
match val {
|
||||||
_pin: PhantomPinned,
|
None => Ok(None),
|
||||||
};
|
Some(v) => Ok(Some(v.to_vec())),
|
||||||
let mut boxed = Box::pin(res);
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
let tx = NonNull::from(&boxed.tx);
|
|
||||||
let val = match tree.get(tx.as_ref(), &key)? {
|
|
||||||
None => return Ok(None),
|
|
||||||
Some(v) => v,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut_ref: Pin<&mut TxAndValue<'_>> = Pin::as_mut(&mut boxed);
|
|
||||||
Pin::get_unchecked_mut(mut_ref).value = NonNull::from(&val);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Some(Value(Box::new(TxAndValuePin(boxed)))))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove(&self, tree: usize, key: &[u8]) -> Result<bool> {
|
fn remove(&self, tree: usize, key: &[u8]) -> Result<bool> {
|
||||||
|
@ -227,13 +214,10 @@ impl<'a, 'db> LmdbTx<'a, 'db> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'db> ITx for LmdbTx<'a, 'db> {
|
impl<'a, 'db> ITx for LmdbTx<'a, 'db> {
|
||||||
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value<'_>>> {
|
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value>> {
|
||||||
let tree = self.get_tree(tree)?;
|
let tree = self.get_tree(tree)?;
|
||||||
match tree.get(&self.tx, &key)? {
|
match tree.get(&self.tx, &key)? {
|
||||||
Some(v) => {
|
Some(v) => Ok(Some(v.to_vec())),
|
||||||
let v: &'_ [u8] = v;
|
|
||||||
Ok(Some(v.into()))
|
|
||||||
}
|
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -279,34 +263,6 @@ impl<'a, 'db> ITx for LmdbTx<'a, 'db> {
|
||||||
|
|
||||||
// ----
|
// ----
|
||||||
|
|
||||||
struct TxAndValue<'a> {
|
|
||||||
tx: RoTxn<'a>,
|
|
||||||
value: NonNull<&'a [u8]>,
|
|
||||||
_pin: PhantomPinned,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct TxAndValuePin<'a>(Pin<Box<TxAndValue<'a>>>);
|
|
||||||
|
|
||||||
impl<'a> IValue<'a> for TxAndValuePin<'a> {
|
|
||||||
fn take_maybe(&mut self) -> Vec<u8> {
|
|
||||||
self.as_ref().to_vec()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> AsRef<[u8]> for TxAndValuePin<'a> {
|
|
||||||
fn as_ref(&self) -> &[u8] {
|
|
||||||
unsafe { self.0.value.as_ref() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> std::borrow::Borrow<[u8]> for TxAndValuePin<'a> {
|
|
||||||
fn borrow(&self) -> &[u8] {
|
|
||||||
self.as_ref()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----
|
|
||||||
|
|
||||||
type IteratorItem<'a> = heed::Result<(
|
type IteratorItem<'a> = heed::Result<(
|
||||||
<ByteSlice as BytesDecode<'a>>::DItem,
|
<ByteSlice as BytesDecode<'a>>::DItem,
|
||||||
<ByteSlice as BytesDecode<'a>>::DItem,
|
<ByteSlice as BytesDecode<'a>>::DItem,
|
||||||
|
@ -365,7 +321,7 @@ impl<'a, I> Iterator for TxAndIteratorPin<'a, I>
|
||||||
where
|
where
|
||||||
I: Iterator<Item = IteratorItem<'a>> + 'a,
|
I: Iterator<Item = IteratorItem<'a>> + 'a,
|
||||||
{
|
{
|
||||||
type Item = Result<(Value<'a>, Value<'a>)>;
|
type Item = Result<(Value, Value)>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let iter_ref = unsafe {
|
let iter_ref = unsafe {
|
||||||
|
@ -375,11 +331,7 @@ where
|
||||||
match iter_ref.unwrap().next() {
|
match iter_ref.unwrap().next() {
|
||||||
None => None,
|
None => None,
|
||||||
Some(Err(e)) => Some(Err(e.into())),
|
Some(Err(e)) => Some(Err(e.into())),
|
||||||
Some(Ok((k, v))) => {
|
Some(Ok((k, v))) => Some(Ok((k.to_vec(), v.to_vec()))),
|
||||||
let k: &'a [u8] = k;
|
|
||||||
let v: &'a [u8] = v;
|
|
||||||
Some(Ok((k.into(), v.into())))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ use sled::transaction::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Db, Error, IDb, ITx, ITxFn, IValue, Result, TxError, TxFnResult, TxResult, Value, ValueIter,
|
Db, Error, IDb, ITx, ITxFn, Result, TxError, TxFnResult, TxResult, Value, ValueIter,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use sled;
|
pub use sled;
|
||||||
|
@ -23,26 +23,6 @@ impl From<sled::Error> for Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- val
|
|
||||||
|
|
||||||
impl<'a> IValue<'a> for sled::IVec {
|
|
||||||
fn take_maybe(&mut self) -> Vec<u8> {
|
|
||||||
self.to_vec()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<Value<'a>> for sled::IVec {
|
|
||||||
fn from(v: Value<'a>) -> sled::IVec {
|
|
||||||
sled::IVec::from(v.into_vec())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<sled::IVec> for Value<'a> {
|
|
||||||
fn from(v: sled::IVec) -> Value<'a> {
|
|
||||||
Value(Box::new(v))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// -- db
|
// -- db
|
||||||
|
|
||||||
pub struct SledDb {
|
pub struct SledDb {
|
||||||
|
@ -99,9 +79,10 @@ impl IDb for SledDb {
|
||||||
|
|
||||||
// ----
|
// ----
|
||||||
|
|
||||||
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value<'_>>> {
|
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value>> {
|
||||||
let tree = self.get_tree(tree)?;
|
let tree = self.get_tree(tree)?;
|
||||||
Ok(tree.get(key)?.map(From::from))
|
let val = tree.get(key)?;
|
||||||
|
Ok(val.map(|x| x.to_vec()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove(&self, tree: usize, key: &[u8]) -> Result<bool> {
|
fn remove(&self, tree: usize, key: &[u8]) -> Result<bool> {
|
||||||
|
@ -123,14 +104,14 @@ impl IDb for SledDb {
|
||||||
fn iter(&self, tree: usize) -> Result<ValueIter<'_>> {
|
fn iter(&self, tree: usize) -> Result<ValueIter<'_>> {
|
||||||
let tree = self.get_tree(tree)?;
|
let tree = self.get_tree(tree)?;
|
||||||
Ok(Box::new(tree.iter().map(|v| {
|
Ok(Box::new(tree.iter().map(|v| {
|
||||||
v.map(|(x, y)| (x.into(), y.into())).map_err(Into::into)
|
v.map(|(x, y)| (x.to_vec(), y.to_vec())).map_err(Into::into)
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iter_rev(&self, tree: usize) -> Result<ValueIter<'_>> {
|
fn iter_rev(&self, tree: usize) -> Result<ValueIter<'_>> {
|
||||||
let tree = self.get_tree(tree)?;
|
let tree = self.get_tree(tree)?;
|
||||||
Ok(Box::new(tree.iter().rev().map(|v| {
|
Ok(Box::new(tree.iter().rev().map(|v| {
|
||||||
v.map(|(x, y)| (x.into(), y.into())).map_err(Into::into)
|
v.map(|(x, y)| (x.to_vec(), y.to_vec())).map_err(Into::into)
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,7 +123,7 @@ impl IDb for SledDb {
|
||||||
) -> Result<ValueIter<'_>> {
|
) -> Result<ValueIter<'_>> {
|
||||||
let tree = self.get_tree(tree)?;
|
let tree = self.get_tree(tree)?;
|
||||||
Ok(Box::new(tree.range::<&'r [u8], _>((low, high)).map(|v| {
|
Ok(Box::new(tree.range::<&'r [u8], _>((low, high)).map(|v| {
|
||||||
v.map(|(x, y)| (x.into(), y.into())).map_err(Into::into)
|
v.map(|(x, y)| (x.to_vec(), y.to_vec())).map_err(Into::into)
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
fn range_rev<'r>(
|
fn range_rev<'r>(
|
||||||
|
@ -153,7 +134,7 @@ impl IDb for SledDb {
|
||||||
) -> Result<ValueIter<'_>> {
|
) -> Result<ValueIter<'_>> {
|
||||||
let tree = self.get_tree(tree)?;
|
let tree = self.get_tree(tree)?;
|
||||||
Ok(Box::new(tree.range::<&'r [u8], _>((low, high)).rev().map(
|
Ok(Box::new(tree.range::<&'r [u8], _>((low, high)).rev().map(
|
||||||
|v| v.map(|(x, y)| (x.into(), y.into())).map_err(Into::into),
|
|v| v.map(|(x, y)| (x.to_vec(), y.to_vec())).map_err(Into::into),
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -218,10 +199,10 @@ impl<'a> SledTx<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ITx for SledTx<'a> {
|
impl<'a> ITx for SledTx<'a> {
|
||||||
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value<'_>>> {
|
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value>> {
|
||||||
let tree = self.get_tree(tree)?;
|
let tree = self.get_tree(tree)?;
|
||||||
let tmp = self.save_error(tree.get(key))?;
|
let tmp = self.save_error(tree.get(key))?;
|
||||||
Ok(tmp.map(From::from))
|
Ok(tmp.map(|x| x.to_vec()))
|
||||||
}
|
}
|
||||||
fn len(&self, _tree: usize) -> Result<usize> {
|
fn len(&self, _tree: usize) -> Result<usize> {
|
||||||
unimplemented!(".len() in transaction not supported with Sled backend")
|
unimplemented!(".len() in transaction not supported with Sled backend")
|
||||||
|
|
|
@ -105,7 +105,7 @@ impl IDb for SqliteDb {
|
||||||
|
|
||||||
// ----
|
// ----
|
||||||
|
|
||||||
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value<'_>>> {
|
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value>> {
|
||||||
trace!("get {}: lock db", tree);
|
trace!("get {}: lock db", tree);
|
||||||
let this = self.0.lock().unwrap();
|
let this = self.0.lock().unwrap();
|
||||||
trace!("get {}: lock acquired", tree);
|
trace!("get {}: lock acquired", tree);
|
||||||
|
@ -118,7 +118,7 @@ impl IDb for SqliteDb {
|
||||||
let mut res_iter = stmt.query([key])?;
|
let mut res_iter = stmt.query([key])?;
|
||||||
match res_iter.next()? {
|
match res_iter.next()? {
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
Some(v) => Ok(Some(v.get::<_, Vec<u8>>(0)?.into())),
|
Some(v) => Ok(Some(v.get::<_, Vec<u8>>(0)?)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -279,7 +279,7 @@ impl<'a> SqliteTx<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ITx for SqliteTx<'a> {
|
impl<'a> ITx for SqliteTx<'a> {
|
||||||
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value<'_>>> {
|
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value>> {
|
||||||
let tree = self.get_tree(tree)?;
|
let tree = self.get_tree(tree)?;
|
||||||
let mut stmt = self
|
let mut stmt = self
|
||||||
.tx
|
.tx
|
||||||
|
@ -287,7 +287,7 @@ impl<'a> ITx for SqliteTx<'a> {
|
||||||
let mut res_iter = stmt.query([key])?;
|
let mut res_iter = stmt.query([key])?;
|
||||||
match res_iter.next()? {
|
match res_iter.next()? {
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
Some(v) => Ok(Some(v.get::<_, Vec<u8>>(0)?.into())),
|
Some(v) => Ok(Some(v.get::<_, Vec<u8>>(0)?)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn len(&self, tree: usize) -> Result<usize> {
|
fn len(&self, tree: usize) -> Result<usize> {
|
||||||
|
@ -394,7 +394,7 @@ impl<'a> Drop for DbValueIterator<'a> {
|
||||||
struct DbValueIteratorPin<'a>(Pin<Box<DbValueIterator<'a>>>);
|
struct DbValueIteratorPin<'a>(Pin<Box<DbValueIterator<'a>>>);
|
||||||
|
|
||||||
impl<'a> Iterator for DbValueIteratorPin<'a> {
|
impl<'a> Iterator for DbValueIteratorPin<'a> {
|
||||||
type Item = Result<(Value<'a>, Value<'a>)>;
|
type Item = Result<(Value, Value)>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let next = unsafe {
|
let next = unsafe {
|
||||||
|
@ -414,7 +414,7 @@ impl<'a> Iterator for DbValueIteratorPin<'a> {
|
||||||
Err(e) => return Some(Err(e.into())),
|
Err(e) => return Some(Err(e.into())),
|
||||||
Ok(y) => y,
|
Ok(y) => y,
|
||||||
};
|
};
|
||||||
Some(Ok((k.into(), v.into())))
|
Some(Ok((k, v)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -72,8 +72,8 @@ impl Repair {
|
||||||
Some(pair) => pair,
|
Some(pair) => pair,
|
||||||
None => break,
|
None => break,
|
||||||
};
|
};
|
||||||
pos = k.into_vec();
|
pos = k;
|
||||||
v.into_vec()
|
v
|
||||||
};
|
};
|
||||||
|
|
||||||
i += 1;
|
i += 1;
|
||||||
|
@ -124,8 +124,8 @@ impl Repair {
|
||||||
Some(pair) => pair,
|
Some(pair) => pair,
|
||||||
None => break,
|
None => break,
|
||||||
};
|
};
|
||||||
pos = k.into_vec();
|
pos = k;
|
||||||
v.into_vec()
|
v
|
||||||
};
|
};
|
||||||
|
|
||||||
i += 1;
|
i += 1;
|
||||||
|
|
|
@ -138,7 +138,7 @@ where
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if keep {
|
if keep {
|
||||||
ret.push(Arc::new(ByteBuf::from(value.as_ref())));
|
ret.push(Arc::new(ByteBuf::from(value)));
|
||||||
}
|
}
|
||||||
if ret.len() >= limit {
|
if ret.len() >= limit {
|
||||||
break;
|
break;
|
||||||
|
@ -271,7 +271,7 @@ where
|
||||||
) -> Result<bool, Error> {
|
) -> Result<bool, Error> {
|
||||||
let removed = self.store.db().transaction(|mut tx| {
|
let removed = self.store.db().transaction(|mut tx| {
|
||||||
let remove_v = match tx.get(&self.store, k)? {
|
let remove_v = match tx.get(&self.store, k)? {
|
||||||
Some(cur_v) if blake2sum(&cur_v[..]) == vhash => Some(cur_v.into_vec()),
|
Some(cur_v) if blake2sum(&cur_v[..]) == vhash => Some(cur_v),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
if remove_v.is_some() {
|
if remove_v.is_some() {
|
||||||
|
|
|
@ -292,7 +292,7 @@ where
|
||||||
k: &MerkleNodeKey,
|
k: &MerkleNodeKey,
|
||||||
) -> db::TxResult<MerkleNode, Error> {
|
) -> db::TxResult<MerkleNode, Error> {
|
||||||
let ent = tx.get(&self.data.merkle_tree, k.encode())?;
|
let ent = tx.get(&self.data.merkle_tree, k.encode())?;
|
||||||
MerkleNode::decode_opt(ent).map_err(db::TxError::Abort)
|
MerkleNode::decode_opt(&ent).map_err(db::TxError::Abort)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_node_txn(
|
fn put_node_txn(
|
||||||
|
@ -316,7 +316,7 @@ where
|
||||||
// Access a node in the Merkle tree, used by the sync protocol
|
// Access a node in the Merkle tree, used by the sync protocol
|
||||||
pub(crate) fn read_node(&self, k: &MerkleNodeKey) -> Result<MerkleNode, Error> {
|
pub(crate) fn read_node(&self, k: &MerkleNodeKey) -> Result<MerkleNode, Error> {
|
||||||
let ent = self.data.merkle_tree.get(k.encode())?;
|
let ent = self.data.merkle_tree.get(k.encode())?;
|
||||||
MerkleNode::decode_opt(ent)
|
MerkleNode::decode_opt(&ent)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn merkle_tree_len(&self) -> usize {
|
pub fn merkle_tree_len(&self) -> usize {
|
||||||
|
@ -351,7 +351,7 @@ impl MerkleNodeKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MerkleNode {
|
impl MerkleNode {
|
||||||
fn decode_opt(ent: Option<db::Value<'_>>) -> Result<Self, Error> {
|
fn decode_opt(ent: &Option<db::Value>) -> Result<Self, Error> {
|
||||||
match ent {
|
match ent {
|
||||||
None => Ok(MerkleNode::Empty),
|
None => Ok(MerkleNode::Empty),
|
||||||
Some(v) => Ok(rmp_serde::decode::from_slice::<MerkleNode>(&v[..])?),
|
Some(v) => Ok(rmp_serde::decode::from_slice::<MerkleNode>(&v[..])?),
|
||||||
|
|
|
@ -260,7 +260,7 @@ where
|
||||||
|
|
||||||
for item in self.data.store.range(begin.to_vec()..end.to_vec())? {
|
for item in self.data.store.range(begin.to_vec()..end.to_vec())? {
|
||||||
let (key, value) = item?;
|
let (key, value) = item?;
|
||||||
items.push((key.to_vec(), Arc::new(ByteBuf::from(value.as_ref()))));
|
items.push((key.to_vec(), Arc::new(ByteBuf::from(value))));
|
||||||
|
|
||||||
if items.len() >= 1024 {
|
if items.len() >= 1024 {
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in a new issue