From 75ca13875c0847ef89805274677fbfb7375a7a95 Mon Sep 17 00:00:00 2001 From: JiYou Date: Thu, 27 Sep 2018 10:33:04 -0700 Subject: [PATCH] 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 Pull Request resolved: https://github.com/facebook/rocksdb/pull/4372 Differential Revision: D9919677 Pulled By: ajkr fbshipit-source-id: f74aaa30e2f80e410e299c5a5bca4eaf2a7a26de --- db/compacted_db_impl.cc | 20 +++++--------------- db/forward_iterator.cc | 22 +++++++--------------- db/version_set.cc | 20 ++++++-------------- 3 files changed, 18 insertions(+), 44 deletions(-) diff --git a/db/compacted_db_impl.cc b/db/compacted_db_impl.cc index 823276c4d..1c315b63b 100644 --- a/db/compacted_db_impl.cc +++ b/db/compacted_db_impl.cc @@ -25,22 +25,12 @@ CompactedDBImpl::~CompactedDBImpl() { } size_t CompactedDBImpl::FindFile(const Slice& key) { - size_t left = 0; size_t right = files_.num_files - 1; - while (left < right) { - size_t mid = (left + right) >> 1; - const FdWithKeyRange& f = files_.files[mid]; - if (user_comparator_->Compare(ExtractUserKey(f.largest_key), key) < 0) { - // Key at "mid.largest" is < "target". Therefore all - // 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; + auto cmp = [&](const FdWithKeyRange& f, const Slice& k) -> bool { + return user_comparator_->Compare(ExtractUserKey(f.largest_key), k) < 0; + }; + return static_cast(std::lower_bound(files_.files, + files_.files + right, key, cmp) - files_.files); } Status CompactedDBImpl::Get(const ReadOptions& options, ColumnFamilyHandle*, diff --git a/db/forward_iterator.cc b/db/forward_iterator.cc index 42395cc46..01afac79e 100644 --- a/db/forward_iterator.cc +++ b/db/forward_iterator.cc @@ -916,21 +916,13 @@ bool ForwardIterator::TEST_CheckDeletedIters(int* pdeleted_iters, uint32_t ForwardIterator::FindFileInRange( const std::vector& files, const Slice& internal_key, uint32_t left, uint32_t right) { - while (left < right) { - uint32_t mid = (left + right) / 2; - const FileMetaData* f = files[mid]; - if (cfd_->internal_comparator().InternalKeyComparator::Compare( - f->largest.Encode(), internal_key) < 0) { - // Key at "mid.largest" is < "target". Therefore all - // 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; + auto cmp = [&](const FileMetaData* f, const Slice& key) -> bool { + return cfd_->internal_comparator().InternalKeyComparator::Compare( + f->largest.Encode(), key) < 0; + }; + const auto &b = files.begin(); + return static_cast(std::lower_bound(b + left, + b + right, internal_key, cmp) - b); } void ForwardIterator::DeleteIterator(InternalIterator* iter, bool is_arena) { diff --git a/db/version_set.cc b/db/version_set.cc index 704dabaf8..cb950cad2 100644 --- a/db/version_set.cc +++ b/db/version_set.cc @@ -62,20 +62,12 @@ int FindFileInRange(const InternalKeyComparator& icmp, const Slice& key, uint32_t left, uint32_t right) { - while (left < right) { - uint32_t mid = (left + right) / 2; - const FdWithKeyRange& f = file_level.files[mid]; - if (icmp.InternalKeyComparator::Compare(f.largest_key, key) < 0) { - // Key at "mid.largest" is < "target". Therefore all - // 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; + auto cmp = [&](const FdWithKeyRange& f, const Slice& k) -> bool { + return icmp.InternalKeyComparator::Compare(f.largest_key, k) < 0; + }; + const auto &b = file_level.files; + return static_cast(std::lower_bound(b + left, + b + right, key, cmp) - b); } Status OverlapWithIterator(const Comparator* ucmp,