Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rocksdb: remove and simplify a bunch of stuff #662

Merged
merged 3 commits into from
Aug 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions kvdb-memorydb/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog].
[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

## [Unreleased]
### Breaking
- Updated `kvdb` to 0.12. [662](https://github.com/paritytech/parity-common/pull/662)

## [0.11.0] - 2022-02-04
### Breaking
Expand Down
4 changes: 0 additions & 4 deletions kvdb-memorydb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ impl KeyValueDB for InMemory {
None => Box::new(None.into_iter()),
}
}

fn restore(&self, _new_db: &str) -> io::Result<()> {
Err(io::Error::new(io::ErrorKind::Other, "Attempted to restore in-memory database"))
}
}

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions kvdb-rocksdb/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog].
[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

## [Unreleased]
- Removed `owning_ref` from dependencies :tada:. [662](https://github.com/paritytech/parity-common/pull/662)
### Breaking
- Update `kvdb` to 0.12. [662](https://github.com/paritytech/parity-common/pull/662)
- `add_column` and `remove_last_column` now require `&mut self`
cheme marked this conversation as resolved.
Show resolved Hide resolved

## [0.15.2] - 2022-03-20
- Disable `jemalloc` feature for `rocksdb` where it is not working. [#633](https://github.com/paritytech/parity-common/pull/633)
Expand Down
2 changes: 0 additions & 2 deletions kvdb-rocksdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ harness = false

[dependencies]
smallvec = "1.0.0"
fs-swap = "0.2.6"
kvdb = { path = "../kvdb", version = "0.11" }
log = "0.4.8"
num_cpus = "1.10.1"
parking_lot = "0.12.0"
regex = "1.3.1"
owning_ref = "0.4.0"
parity-util-mem = { path = "../parity-util-mem", version = "0.11", default-features = false, features = ["std", "smallvec"] }

# OpenBSD and MSVC are unteested and shouldn't enable jemalloc:
Expand Down
80 changes: 0 additions & 80 deletions kvdb-rocksdb/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,11 @@
//! See https://github.com/facebook/rocksdb/wiki/Prefix-Seek-API-Changes for details.

use crate::DBAndColumns;
use owning_ref::{OwningHandle, StableAddress};
use parking_lot::RwLockReadGuard;
use rocksdb::{DBIterator, Direction, IteratorMode, ReadOptions};
use std::ops::{Deref, DerefMut};

/// A tuple holding key and value data, used as the iterator item type.
pub type KeyValuePair = (Box<[u8]>, Box<[u8]>);

/// Iterator with built-in synchronization.
pub struct ReadGuardedIterator<'a, I, T> {
inner: OwningHandle<UnsafeStableAddress<'a, Option<T>>, DerefWrapper<Option<I>>>,
}

// We can't implement `StableAddress` for a `RwLockReadGuard`
// directly due to orphan rules.
#[repr(transparent)]
struct UnsafeStableAddress<'a, T>(RwLockReadGuard<'a, T>);

impl<'a, T> Deref for UnsafeStableAddress<'a, T> {
type Target = T;

fn deref(&self) -> &Self::Target {
self.0.deref()
}
}

// RwLockReadGuard dereferences to a stable address; qed
unsafe impl<'a, T> StableAddress for UnsafeStableAddress<'a, T> {}

struct DerefWrapper<T>(T);

impl<T> Deref for DerefWrapper<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T> DerefMut for DerefWrapper<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<'a, I: Iterator, T> Iterator for ReadGuardedIterator<'a, I, T> {
type Item = I::Item;

fn next(&mut self) -> Option<Self::Item> {
self.inner.deref_mut().as_mut().and_then(|iter| iter.next())
}
}

/// Instantiate iterators yielding `KeyValuePair`s.
pub trait IterationHandler {
type Iterator: Iterator<Item = KeyValuePair>;
Expand All @@ -84,38 +36,6 @@ pub trait IterationHandler {
fn iter_with_prefix(&self, col: u32, prefix: &[u8], read_opts: ReadOptions) -> Self::Iterator;
}

impl<'a, T> ReadGuardedIterator<'a, <&'a T as IterationHandler>::Iterator, T>
where
&'a T: IterationHandler,
{
/// Creates a new `ReadGuardedIterator` that maps `RwLock<RocksDB>` to `RwLock<DBIterator>`,
/// where `DBIterator` iterates over all keys.
pub fn new(read_lock: RwLockReadGuard<'a, Option<T>>, col: u32, read_opts: ReadOptions) -> Self {
Self { inner: Self::new_inner(read_lock, |db| db.iter(col, read_opts)) }
}

/// Creates a new `ReadGuardedIterator` that maps `RwLock<RocksDB>` to `RwLock<DBIterator>`,
/// where `DBIterator` iterates over keys which start with the given prefix.
pub fn new_with_prefix(
read_lock: RwLockReadGuard<'a, Option<T>>,
col: u32,
prefix: &[u8],
read_opts: ReadOptions,
) -> Self {
Self { inner: Self::new_inner(read_lock, |db| db.iter_with_prefix(col, prefix, read_opts)) }
}

fn new_inner(
rlock: RwLockReadGuard<'a, Option<T>>,
f: impl FnOnce(&'a T) -> <&'a T as IterationHandler>::Iterator,
) -> OwningHandle<UnsafeStableAddress<'a, Option<T>>, DerefWrapper<Option<<&'a T as IterationHandler>::Iterator>>> {
OwningHandle::new_with_fn(UnsafeStableAddress(rlock), move |rlock| {
let rlock = unsafe { rlock.as_ref().expect("initialized as non-null; qed") };
DerefWrapper(rlock.as_ref().map(f))
})
}
}

impl<'a> IterationHandler for &'a DBAndColumns {
type Iterator = DBIterator<'a>;

Expand Down
Loading