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

kvdb-rocksdb: optimize and rename iter_from_prefix #365

Merged
merged 7 commits into from
Apr 15, 2020

Conversation

ordian
Copy link
Member

@ordian ordian commented Mar 27, 2020

We wanted to rename iter_from_prefix for a long time as it was a source of confusion. Hopefully, the new name iter_with_prefix is less confusing :) Another alternative is iter_by_prefix.

Also for RocksDB impl we're setting upper bound to optimize the prefix iteration.

@ordian ordian requested review from dvdplm and cheme and removed request for dvdplm March 27, 2020 21:12
@ordian ordian requested a review from dvdplm March 27, 2020 21:12
Copy link
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

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

neat.

@@ -28,6 +27,11 @@ 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 store the upper bound here
// to make sure it lives at least as long as the iterator.
// See https://github.com/rust-rocksdb/rust-rocksdb/pull/309.
Copy link
Contributor

Choose a reason for hiding this comment

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

kvdb/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@cheme cheme left a comment

Choose a reason for hiding this comment

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

Looks good, about the naming I a was also thinking 'iter_on_prefix', but in substrate we got function such as for_keys_with_prefix so with is clearly the best choice for me.

read_lock: RwLockReadGuard<'a, Option<T>>,
col: u32,
prefix: &[u8],
upper_bound: Box<[u8]>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Small nit, I would have put the calculation to end_prefix (or the call to a function doing the calculation) inside this function to avoid this additional parameter in the api.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree the current internal API is cumbersome, but we also pass here &ReadOptions, so in order to move the end_prefix calculation there, a bigger refactoring is needed. I'll leave it as is for now.

@ordian
Copy link
Member Author

ordian commented Mar 28, 2020

The upper_bound doesn't actually seem to work if I remove starts_with here

optional.into_iter().flat_map(identity).take_while(move |(k, _)| k.starts_with(prefix))

Putting on ice till I figure this out.

Comment on lines +521 to +522
// rocksdb doesn't work with an empty upper bound
if !end_prefix.is_empty() {
Copy link
Member Author

Choose a reason for hiding this comment

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

cc @cheme, we might want to re-review prefix deletion code as rocksdb doesn't iterate at all if the upper bound is empty as it matches all keys

Copy link
Collaborator

Choose a reason for hiding this comment

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

I should have remember that, there was some similar things in the deletion prefix pr, let me check back:

if prefix.len() > 0 {

Is there a case where we got a empty upper bound and the start bound is not empty, ok [255] or [255, 255] ... So yes the test should be rewrite on the result of end prefix instead of the prefix 👍 I am doing a quick PR.

* master:
  keccak-hash: fix bench and add one for range (#372)
  [parity-crypto] Release 0.6.1 (#373)
  keccak-hash: bump version to 0.5.1 (#371)
  keccak-hash: add keccak256_range and keccak512_range functions (#370)
  Allow pubkey recovery for all-zero messages (#369)
@ordian ordian merged commit a52fbeb into master Apr 15, 2020
@ordian ordian deleted the ao-prefix-iter-optimized branch April 15, 2020 08:45
ordian added a commit that referenced this pull request Apr 22, 2020
* master:
  kvdb-rocksdb: optimize and rename iter_from_prefix  (#365)
  bump parity-util-mem (#376)
  parity-util-mem: fix for windows (#375)
  keccak-hash: fix bench and add one for range (#372)
  [parity-crypto] Release 0.6.1 (#373)
  keccak-hash: bump version to 0.5.1 (#371)
  keccak-hash: add keccak256_range and keccak512_range functions (#370)
  Allow pubkey recovery for all-zero messages (#369)
  Delete by prefix operator in kvdb (#360)
  kvdb: no overlay (#313)
  Ban duplicates of parity-uil-mem from being linked into the same program (#363)
  Use correct license ID (#362)
  Memtest example for Rocksdb (#349)
  Prep for release (#361)
  parity-util-mem: prepare release for 0.5.2 (#359)
  travis: test parity-util-mem on android (#358)
  parity-util-mem: update mimalloc feature (#352)
  kvdb: remove parity-bytes dependency (#351)
  parity-util-mem: use malloc for usable_size on android (#355)
  CI: troubleshoot macOS build (#356)
ordian added a commit that referenced this pull request May 5, 2020
* master: (56 commits)
  primitive-types: add no_std support for serde feature (#385)
  Add Rocksdb Secondary Instance Api (#384)
  kvdb-rocksdb: update rocksdb to 0.14 (#379)
  prepare releases for a few crates (#382)
  uint: fix UB in uint::from_big_endian (#381)
  Fix limit prefix delete case (#368)
  Add arbitrary trait implementation (#378)
  kvdb-rocksdb: optimize and rename iter_from_prefix  (#365)
  bump parity-util-mem (#376)
  parity-util-mem: fix for windows (#375)
  keccak-hash: fix bench and add one for range (#372)
  [parity-crypto] Release 0.6.1 (#373)
  keccak-hash: bump version to 0.5.1 (#371)
  keccak-hash: add keccak256_range and keccak512_range functions (#370)
  Allow pubkey recovery for all-zero messages (#369)
  Delete by prefix operator in kvdb (#360)
  kvdb: no overlay (#313)
  Ban duplicates of parity-uil-mem from being linked into the same program (#363)
  Use correct license ID (#362)
  Memtest example for Rocksdb (#349)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants