From e003d3864c8ebb89e83e7a463562365e9ba1acc5 Mon Sep 17 00:00:00 2001 From: Igor Canadi Date: Thu, 23 Apr 2015 18:08:37 -0700 Subject: [PATCH] Abstract out SetMaxPossibleForUserKey() and SetMinPossibleForUserKey Summary: Based on feedback from D37083. Are all of these correct? In some spaces it seems like we're doing SetMaxPossibleForUserKey() although we want the smallest possible internal key for user key. Test Plan: make check Reviewers: sdong Reviewed By: sdong Subscribers: dhruba, leveldb Differential Revision: https://reviews.facebook.net/D37341 --- db/db_impl.cc | 9 +++++---- db/db_impl_experimental.cc | 4 ++-- db/dbformat.h | 14 ++++++++++++++ db/version_set.cc | 6 ++++-- util/ldb_cmd.cc | 3 ++- util/sst_dump_tool.cc | 3 ++- 6 files changed, 29 insertions(+), 10 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index 2b073aae7..1461e7d22 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -1686,7 +1686,7 @@ Status DBImpl::RunManualCompaction(ColumnFamilyData* cfd, int input_level, cfd->ioptions()->compaction_style == kCompactionStyleFIFO) { manual.begin = nullptr; } else { - begin_storage = InternalKey(*begin, kMaxSequenceNumber, kValueTypeForSeek); + begin_storage.SetMaxPossibleForUserKey(*begin); manual.begin = &begin_storage; } if (end == nullptr || @@ -1694,7 +1694,7 @@ Status DBImpl::RunManualCompaction(ColumnFamilyData* cfd, int input_level, cfd->ioptions()->compaction_style == kCompactionStyleFIFO) { manual.end = nullptr; } else { - end_storage = InternalKey(*end, 0, static_cast(0)); + end_storage.SetMinPossibleForUserKey(*end); manual.end = &end_storage; } @@ -3555,8 +3555,9 @@ void DBImpl::GetApproximateSizes(ColumnFamilyHandle* column_family, for (int i = 0; i < n; i++) { // Convert user_key into a corresponding internal key. - InternalKey k1(range[i].start, kMaxSequenceNumber, kValueTypeForSeek); - InternalKey k2(range[i].limit, kMaxSequenceNumber, kValueTypeForSeek); + InternalKey k1, k2; + k1.SetMaxPossibleForUserKey(range[i].start); + k2.SetMaxPossibleForUserKey(range[i].limit); uint64_t start = versions_->ApproximateOffsetOf(v, k1); uint64_t limit = versions_->ApproximateOffsetOf(v, k2); sizes[i] = (limit >= start ? limit - start : 0); diff --git a/db/db_impl_experimental.cc b/db/db_impl_experimental.cc index e8d5b1561..f2c70740e 100644 --- a/db/db_impl_experimental.cc +++ b/db/db_impl_experimental.cc @@ -30,10 +30,10 @@ Status DBImpl::SuggestCompactRange(ColumnFamilyHandle* column_family, auto cfd = cfh->cfd(); InternalKey start_key, end_key; if (begin != nullptr) { - start_key = InternalKey(*begin, kMaxSequenceNumber, kValueTypeForSeek); + start_key.SetMaxPossibleForUserKey(*begin); } if (end != nullptr) { - end_key = InternalKey(*end, 0, static_cast(0)); + end_key.SetMinPossibleForUserKey(*end); } { InstrumentedMutexLock l(&mutex_); diff --git a/db/dbformat.h b/db/dbformat.h index 58b3aa386..f15a8c059 100644 --- a/db/dbformat.h +++ b/db/dbformat.h @@ -135,6 +135,20 @@ class InternalKey { AppendInternalKey(&rep_, ParsedInternalKey(_user_key, s, t)); } + // sets the internal key to be bigger or equal to all internal keys with this + // user key + void SetMaxPossibleForUserKey(const Slice& _user_key) { + AppendInternalKey(&rep_, ParsedInternalKey(_user_key, kMaxSequenceNumber, + kValueTypeForSeek)); + } + + // sets the internal key to be smaller or equal to all internal keys with this + // user key + void SetMinPossibleForUserKey(const Slice& _user_key) { + AppendInternalKey( + &rep_, ParsedInternalKey(_user_key, 0, static_cast(0))); + } + bool Valid() const { ParsedInternalKey parsed; return ParseInternalKey(Slice(rep_), &parsed); diff --git a/db/version_set.cc b/db/version_set.cc index aa8256a58..a623e51ae 100644 --- a/db/version_set.cc +++ b/db/version_set.cc @@ -397,7 +397,8 @@ bool SomeFileOverlapsRange( uint32_t index = 0; if (smallest_user_key != nullptr) { // Find the earliest possible internal key for smallest_user_key - InternalKey small(*smallest_user_key, kMaxSequenceNumber,kValueTypeForSeek); + InternalKey small; + small.SetMaxPossibleForUserKey(*smallest_user_key); index = FindFile(icmp, file_level, small.Encode()); } @@ -1230,7 +1231,8 @@ int VersionStorageInfo::PickLevelForMemTableOutput( if (!OverlapInLevel(0, &smallest_user_key, &largest_user_key)) { // Push to next level if there is no overlap in next level, // and the #bytes overlapping in the level after that are limited. - InternalKey start(smallest_user_key, kMaxSequenceNumber, kValueTypeForSeek); + InternalKey start; + start.SetMaxPossibleForUserKey(smallest_user_key); InternalKey limit(largest_user_key, 0, static_cast(0)); std::vector overlaps; while (mutable_cf_options.max_mem_compaction_level > 0 && diff --git a/util/ldb_cmd.cc b/util/ldb_cmd.cc index b73ec3666..0ea2fd4e0 100644 --- a/util/ldb_cmd.cc +++ b/util/ldb_cmd.cc @@ -796,7 +796,8 @@ void InternalDumpCommand::DoCommand() { } if (has_from_) { - InternalKey ikey(from_, kMaxSequenceNumber, kValueTypeForSeek); + InternalKey ikey; + ikey.SetMaxPossibleForUserKey(from_); iter->Seek(ikey.Encode()); } else { iter->SeekToFirst(); diff --git a/util/sst_dump_tool.cc b/util/sst_dump_tool.cc index d65feb220..04486da83 100644 --- a/util/sst_dump_tool.cc +++ b/util/sst_dump_tool.cc @@ -187,7 +187,8 @@ Status SstFileReader::ReadSequential(bool print_kv, false)); uint64_t i = 0; if (has_from) { - InternalKey ikey(from_key, kMaxSequenceNumber, kValueTypeForSeek); + InternalKey ikey; + ikey.SetMaxPossibleForUserKey(from_key); iter->Seek(ikey.Encode()); } else { iter->SeekToFirst();