FindFile: use std::lower_bound reduce the repeated code. (#4372)

Summary:
`FindFile()` and  `FindFileInRange()` actually works as the same
of `std::lower_bound()`. Use `std::lower_bound()` to reduce the
repeated code.

- change `FindFile()` and `FindFileInRange()` to use `std::lower_bound()`

Signed-off-by: JiYou <jiyou09@gmail.com>
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4372

Differential Revision: D9919677

Pulled By: ajkr

fbshipit-source-id: f74aaa30e2f80e410e299c5a5bca4eaf2a7a26de
main
JiYou 6 years ago committed by Facebook Github Bot
parent b1dad4cfcc
commit 75ca13875c
  1. 20
      db/compacted_db_impl.cc
  2. 22
      db/forward_iterator.cc
  3. 20
      db/version_set.cc

@ -25,22 +25,12 @@ CompactedDBImpl::~CompactedDBImpl() {
} }
size_t CompactedDBImpl::FindFile(const Slice& key) { size_t CompactedDBImpl::FindFile(const Slice& key) {
size_t left = 0;
size_t right = files_.num_files - 1; size_t right = files_.num_files - 1;
while (left < right) { auto cmp = [&](const FdWithKeyRange& f, const Slice& k) -> bool {
size_t mid = (left + right) >> 1; return user_comparator_->Compare(ExtractUserKey(f.largest_key), k) < 0;
const FdWithKeyRange& f = files_.files[mid]; };
if (user_comparator_->Compare(ExtractUserKey(f.largest_key), key) < 0) { return static_cast<size_t>(std::lower_bound(files_.files,
// Key at "mid.largest" is < "target". Therefore all files_.files + right, key, cmp) - files_.files);
// files at or before "mid" are uninteresting.
left = mid + 1;
} else {
// Key at "mid.largest" is >= "target". Therefore all files
// after "mid" are uninteresting.
right = mid;
}
}
return right;
} }
Status CompactedDBImpl::Get(const ReadOptions& options, ColumnFamilyHandle*, Status CompactedDBImpl::Get(const ReadOptions& options, ColumnFamilyHandle*,

@ -916,21 +916,13 @@ bool ForwardIterator::TEST_CheckDeletedIters(int* pdeleted_iters,
uint32_t ForwardIterator::FindFileInRange( uint32_t ForwardIterator::FindFileInRange(
const std::vector<FileMetaData*>& files, const Slice& internal_key, const std::vector<FileMetaData*>& files, const Slice& internal_key,
uint32_t left, uint32_t right) { uint32_t left, uint32_t right) {
while (left < right) { auto cmp = [&](const FileMetaData* f, const Slice& key) -> bool {
uint32_t mid = (left + right) / 2; return cfd_->internal_comparator().InternalKeyComparator::Compare(
const FileMetaData* f = files[mid]; f->largest.Encode(), key) < 0;
if (cfd_->internal_comparator().InternalKeyComparator::Compare( };
f->largest.Encode(), internal_key) < 0) { const auto &b = files.begin();
// Key at "mid.largest" is < "target". Therefore all return static_cast<uint32_t>(std::lower_bound(b + left,
// files at or before "mid" are uninteresting. b + right, internal_key, cmp) - b);
left = mid + 1;
} else {
// Key at "mid.largest" is >= "target". Therefore all files
// after "mid" are uninteresting.
right = mid;
}
}
return right;
} }
void ForwardIterator::DeleteIterator(InternalIterator* iter, bool is_arena) { void ForwardIterator::DeleteIterator(InternalIterator* iter, bool is_arena) {

@ -62,20 +62,12 @@ int FindFileInRange(const InternalKeyComparator& icmp,
const Slice& key, const Slice& key,
uint32_t left, uint32_t left,
uint32_t right) { uint32_t right) {
while (left < right) { auto cmp = [&](const FdWithKeyRange& f, const Slice& k) -> bool {
uint32_t mid = (left + right) / 2; return icmp.InternalKeyComparator::Compare(f.largest_key, k) < 0;
const FdWithKeyRange& f = file_level.files[mid]; };
if (icmp.InternalKeyComparator::Compare(f.largest_key, key) < 0) { const auto &b = file_level.files;
// Key at "mid.largest" is < "target". Therefore all return static_cast<int>(std::lower_bound(b + left,
// files at or before "mid" are uninteresting. b + right, key, cmp) - b);
left = mid + 1;
} else {
// Key at "mid.largest" is >= "target". Therefore all files
// after "mid" are uninteresting.
right = mid;
}
}
return right;
} }
Status OverlapWithIterator(const Comparator* ucmp, Status OverlapWithIterator(const Comparator* ucmp,

Loading…
Cancel
Save