Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
215a28d
Switch from `parity-rocksdb` to upstream `rust-rocksdb`
bkchr Dec 4, 2018
2c0468f
wip
ordian Oct 28, 2019
d445cde
wip
ordian Nov 1, 2019
2d42479
Merge branch 'master' into ao-rocksdb-switch-to-upstream2
ordian Nov 10, 2019
47a6e22
kvdb-rocksdb: working iterator
ordian Nov 10, 2019
81bce7c
kvdb-rocksdb: cleanup
ordian Nov 10, 2019
71b2af0
kvdb-rocksdb: more cleanup
ordian Nov 10, 2019
e5cd5cb
kvdb-rocksdb: use options from updated upstream
ordian Nov 12, 2019
c7640d1
kvdb-rocksdb: set bloom filter as recommended by tuning guide
ordian Nov 12, 2019
fb208ad
kvdb-rocksdb: fix build
ordian Nov 12, 2019
8a2442a
kvdb-rocksdb: set_level_compaction_dynamic_level_bytes
ordian Nov 12, 2019
fa16fb5
kvdb-rocksdb: switch to just published version
ordian Nov 14, 2019
3b31e4f
kvdb-rocksdb: preserve the old compression_per_level setting
ordian Nov 14, 2019
f595233
kvdb-rocksdb: add some iter module docs
ordian Nov 14, 2019
df8d614
kvdb-rocksdb: remove path on kvdb dependency temporarily
ordian Nov 19, 2019
a479485
kvdb-rocksdb: use only lz4 and snappy features
ordian Nov 19, 2019
1e1a865
Merge branch 'master' into ao-rocksdb-switch-to-upstream2
ordian Nov 22, 2019
3153cca
kvdb-rocksdb: support zstd compression as well
ordian Nov 22, 2019
ed372f4
Also add `kvdb` as path dependency
bkchr Nov 22, 2019
28de430
Apply suggestions from code review
ordian Nov 22, 2019
d978759
kvdb-rocksdb: fix build
ordian Nov 22, 2019
9adccb8
Merge branch 'master' into ao-rocksdb-switch-to-upstream2
ordian Nov 22, 2019
13b029c
kvdb-rocksdb: use open_cf_descriptors
ordian Nov 22, 2019
c88186f
kvdb-rocksdb: remove redundant .into()
ordian Nov 22, 2019
03a2ba0
Disable `zstd` again
bkchr Nov 22, 2019
407c604
kvdb-rocksdb: set block_size to 64 KB again
ordian Nov 26, 2019
1739d8a
kvdb-rocksdb: cargo fmt
ordian Nov 26, 2019
8c658ba
kvdb-rocksdb: set back block_size to 16 KB
ordian Nov 26, 2019
87d0aec
moar cargo fmt
ordian Nov 26, 2019
3a071af
Add tests for budget calculation
dvdplm Nov 26, 2019
cd343cc
Add test to check the rocksdb settings
dvdplm Nov 27, 2019
7eeaf7b
kvdb-rocksdb: do not account for default column memory budget
ordian Nov 27, 2019
bd5abf1
kvdb-rocksdb: please the CI
ordian Nov 27, 2019
f23574f
kvdb-rocksdb: remove lz4 feature as it has no effect for now
ordian Nov 27, 2019
8986947
Merge branch 'master' into ao-rocksdb-switch-to-upstream2
ordian Nov 27, 2019
6a64e36
Address review grumbles
dvdplm Nov 28, 2019
15af306
Merge branch 'ao-rocksdb-switch-to-upstream2' of github.com:paritytec…
dvdplm Nov 28, 2019
0217438
kvdb-rocksdb: add failing iter_from_prefix test
ordian Nov 28, 2019
e0d59e5
kvdb-rocksdb: add a workaround for the rocksdb prefix bug
ordian Nov 28, 2019
c4b5413
More prefix iter test
dvdplm Nov 28, 2019
adde7df
Merge branch 'ao-rocksdb-switch-to-upstream2' of github.com:paritytec…
dvdplm Nov 28, 2019
b40b416
Fix tests (hex, it's so hard)
dvdplm Nov 28, 2019
38876cf
whitespace
dvdplm Nov 28, 2019
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
5 changes: 3 additions & 2 deletions kvdb-rocksdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ edition = "2018"
elastic-array = "0.10.2"
fs-swap = "0.2.4"
interleaved-ordered = "0.1.1"
kvdb = { version = "0.1", path = "../kvdb" }
kvdb = { path = "../kvdb", version = "0.1" }
log = "0.4.8"
num_cpus = "1.10.1"
parking_lot = "0.9.0"
regex = "1.3.1"
parity-rocksdb = "0.5.1"
rocksdb = { version = "0.13", features = ["snappy"], default-features = false }
owning_ref = "0.4.0"

[dev-dependencies]
tempdir = "0.3.7"
Expand Down
128 changes: 128 additions & 0 deletions kvdb-rocksdb/src/iter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

//! This module contains an implementation of a RocksDB iterator
//! wrapped inside a `RwLock`. Since `RwLock` "owns" the inner data,
//! we're using `owning_ref` to work around the borrowing rules of Rust.

use crate::DBAndColumns;
use owning_ref::{OwningHandle, StableAddress};
use parking_lot::RwLockReadGuard;
use rocksdb::{DBIterator, IteratorMode};
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 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some documentation would be nice :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This trait was originally added in #120, we can remove it and all generics here, but it's good to have if we will consider merging something similar to #120.

Another idea I had is replace RwLock<Option<DBAndColumns>> with https://docs.rs/arc-swap/0.4.4/arc_swap/, but let's keep it as is for now.

type Iterator: Iterator<Item = KeyValuePair>;

/// Create an `Iterator` over the default DB column or over a `ColumnFamily` if a column number
/// is passed.
fn iter(&self, col: Option<u32>) -> Self::Iterator;
/// Create an `Iterator` over the default DB column or over a `ColumnFamily` if a column number
/// is passed. The iterator starts from the first key having the provided `prefix`.
fn iter_from_prefix(&self, col: Option<u32>, prefix: &[u8]) -> Self::Iterator;
}

impl<'a, T> ReadGuardedIterator<'a, <&'a T as IterationHandler>::Iterator, T>
where
&'a T: IterationHandler,
{
pub fn new(read_lock: RwLockReadGuard<'a, Option<T>>, col: Option<u32>) -> Self {
Self { inner: Self::new_inner(read_lock, |db| db.iter(col)) }
}

pub fn new_from_prefix(read_lock: RwLockReadGuard<'a, Option<T>>, col: Option<u32>, prefix: &[u8]) -> Self {
Self { inner: Self::new_inner(read_lock, |db| db.iter_from_prefix(col, prefix)) }
}

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>;

fn iter(&self, col: Option<u32>) -> Self::Iterator {
col.map_or_else(
|| self.db.iterator(IteratorMode::Start),
|c| {
self.db
.iterator_cf(self.get_cf(c as usize), IteratorMode::Start)
.expect("iterator params are valid; qed")
},
)
}

fn iter_from_prefix(&self, col: Option<u32>, prefix: &[u8]) -> Self::Iterator {
col.map_or_else(
|| self.db.prefix_iterator(prefix),
|c| self.db.prefix_iterator_cf(self.get_cf(c as usize), prefix).expect("iterator params are valid; qed"),
)
}
}
Loading