2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-10-16 23:59:46 +02:00
|
|
|
//
|
2011-03-18 23:37:00 +01:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
2014-01-27 22:53:22 +01:00
|
|
|
#include "db/dbformat.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2015-06-17 07:26:34 +02:00
|
|
|
#ifndef __STDC_FORMAT_MACROS
|
|
|
|
#define __STDC_FORMAT_MACROS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2011-03-18 23:37:00 +01:00
|
|
|
#include <stdio.h>
|
2017-04-06 04:02:00 +02:00
|
|
|
#include "monitoring/perf_context_imp.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
#include "port/port.h"
|
|
|
|
#include "util/coding.h"
|
2017-04-06 23:49:13 +02:00
|
|
|
#include "util/string_util.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2016-09-28 03:20:57 +02:00
|
|
|
// kValueTypeForSeek defines the ValueType that should be passed when
|
|
|
|
// constructing a ParsedInternalKey object for seeking to a particular
|
|
|
|
// sequence number (since we sort sequence numbers in decreasing order
|
|
|
|
// and the value type is embedded as the low 8 bits in the sequence
|
|
|
|
// number in internal keys, we need to use the highest-numbered
|
|
|
|
// ValueType, not the lowest).
|
2017-10-03 18:08:07 +02:00
|
|
|
const ValueType kValueTypeForSeek = kTypeBlobIndex;
|
2016-09-28 03:20:57 +02:00
|
|
|
const ValueType kValueTypeForSeekForPrev = kTypeDeletion;
|
|
|
|
|
2014-04-01 23:45:30 +02:00
|
|
|
uint64_t PackSequenceAndType(uint64_t seq, ValueType t) {
|
2011-03-18 23:37:00 +01:00
|
|
|
assert(seq <= kMaxSequenceNumber);
|
2016-08-16 17:16:04 +02:00
|
|
|
assert(IsExtendedValueType(t));
|
2011-03-18 23:37:00 +01:00
|
|
|
return (seq << 8) | t;
|
|
|
|
}
|
|
|
|
|
2015-05-29 23:36:35 +02:00
|
|
|
void UnPackSequenceAndType(uint64_t packed, uint64_t* seq, ValueType* t) {
|
|
|
|
*seq = packed >> 8;
|
|
|
|
*t = static_cast<ValueType>(packed & 0xff);
|
|
|
|
|
|
|
|
assert(*seq <= kMaxSequenceNumber);
|
2016-08-16 17:16:04 +02:00
|
|
|
assert(IsExtendedValueType(*t));
|
2015-05-29 23:36:35 +02:00
|
|
|
}
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
void AppendInternalKey(std::string* result, const ParsedInternalKey& key) {
|
|
|
|
result->append(key.user_key.data(), key.user_key.size());
|
|
|
|
PutFixed64(result, PackSequenceAndType(key.sequence, key.type));
|
|
|
|
}
|
|
|
|
|
2016-12-01 16:00:17 +01:00
|
|
|
void AppendInternalKeyFooter(std::string* result, SequenceNumber s,
|
|
|
|
ValueType t) {
|
|
|
|
PutFixed64(result, PackSequenceAndType(s, t));
|
|
|
|
}
|
|
|
|
|
2012-12-16 03:28:36 +01:00
|
|
|
std::string ParsedInternalKey::DebugString(bool hex) const {
|
2011-03-18 23:37:00 +01:00
|
|
|
char buf[50];
|
2017-03-13 18:24:52 +01:00
|
|
|
snprintf(buf, sizeof(buf), "' seq:%" PRIu64 ", type:%d", sequence,
|
2015-06-17 07:26:34 +02:00
|
|
|
static_cast<int>(type));
|
2011-03-18 23:37:00 +01:00
|
|
|
std::string result = "'";
|
2012-12-16 03:28:36 +01:00
|
|
|
result += user_key.ToString(hex);
|
2011-03-18 23:37:00 +01:00
|
|
|
result += buf;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-12-16 03:28:36 +01:00
|
|
|
std::string InternalKey::DebugString(bool hex) const {
|
2011-10-06 01:30:28 +02:00
|
|
|
std::string result;
|
|
|
|
ParsedInternalKey parsed;
|
|
|
|
if (ParseInternalKey(rep_, &parsed)) {
|
2012-12-16 03:28:36 +01:00
|
|
|
result = parsed.DebugString(hex);
|
2011-10-06 01:30:28 +02:00
|
|
|
} else {
|
|
|
|
result = "(bad)";
|
|
|
|
result.append(EscapeString(rep_));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
const char* InternalKeyComparator::Name() const {
|
2013-06-10 22:28:58 +02:00
|
|
|
return name_.c_str();
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int InternalKeyComparator::Compare(const Slice& akey, const Slice& bkey) const {
|
|
|
|
// Order by:
|
|
|
|
// increasing user key (according to user-supplied comparator)
|
|
|
|
// decreasing sequence number
|
|
|
|
// decreasing type (though sequence# should be enough to disambiguate)
|
|
|
|
int r = user_comparator_->Compare(ExtractUserKey(akey), ExtractUserKey(bkey));
|
2014-04-08 19:58:07 +02:00
|
|
|
PERF_COUNTER_ADD(user_key_comparison_count, 1);
|
2011-03-18 23:37:00 +01:00
|
|
|
if (r == 0) {
|
|
|
|
const uint64_t anum = DecodeFixed64(akey.data() + akey.size() - 8);
|
|
|
|
const uint64_t bnum = DecodeFixed64(bkey.data() + bkey.size() - 8);
|
|
|
|
if (anum > bnum) {
|
|
|
|
r = -1;
|
|
|
|
} else if (anum < bnum) {
|
|
|
|
r = +1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-01-27 22:53:22 +01:00
|
|
|
int InternalKeyComparator::Compare(const ParsedInternalKey& a,
|
|
|
|
const ParsedInternalKey& b) const {
|
|
|
|
// Order by:
|
|
|
|
// increasing user key (according to user-supplied comparator)
|
|
|
|
// decreasing sequence number
|
|
|
|
// decreasing type (though sequence# should be enough to disambiguate)
|
|
|
|
int r = user_comparator_->Compare(a.user_key, b.user_key);
|
2014-04-08 19:58:07 +02:00
|
|
|
PERF_COUNTER_ADD(user_key_comparison_count, 1);
|
2014-01-27 22:53:22 +01:00
|
|
|
if (r == 0) {
|
|
|
|
if (a.sequence > b.sequence) {
|
|
|
|
r = -1;
|
|
|
|
} else if (a.sequence < b.sequence) {
|
|
|
|
r = +1;
|
|
|
|
} else if (a.type > b.type) {
|
|
|
|
r = -1;
|
|
|
|
} else if (a.type < b.type) {
|
|
|
|
r = +1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
void InternalKeyComparator::FindShortestSeparator(
|
|
|
|
std::string* start,
|
|
|
|
const Slice& limit) const {
|
|
|
|
// Attempt to shorten the user portion of the key
|
|
|
|
Slice user_start = ExtractUserKey(*start);
|
|
|
|
Slice user_limit = ExtractUserKey(limit);
|
|
|
|
std::string tmp(user_start.data(), user_start.size());
|
|
|
|
user_comparator_->FindShortestSeparator(&tmp, user_limit);
|
2017-05-06 00:01:04 +02:00
|
|
|
if (tmp.size() <= user_start.size() &&
|
2011-11-14 18:06:16 +01:00
|
|
|
user_comparator_->Compare(user_start, tmp) < 0) {
|
|
|
|
// User key has become shorter physically, but larger logically.
|
|
|
|
// Tack on the earliest possible number to the shortened user key.
|
2011-03-18 23:37:00 +01:00
|
|
|
PutFixed64(&tmp, PackSequenceAndType(kMaxSequenceNumber,kValueTypeForSeek));
|
|
|
|
assert(this->Compare(*start, tmp) < 0);
|
|
|
|
assert(this->Compare(tmp, limit) < 0);
|
|
|
|
start->swap(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InternalKeyComparator::FindShortSuccessor(std::string* key) const {
|
|
|
|
Slice user_key = ExtractUserKey(*key);
|
|
|
|
std::string tmp(user_key.data(), user_key.size());
|
|
|
|
user_comparator_->FindShortSuccessor(&tmp);
|
2017-05-06 00:01:04 +02:00
|
|
|
if (tmp.size() <= user_key.size() &&
|
2011-11-14 18:06:16 +01:00
|
|
|
user_comparator_->Compare(user_key, tmp) < 0) {
|
|
|
|
// User key has become shorter physically, but larger logically.
|
|
|
|
// Tack on the earliest possible number to the shortened user key.
|
2011-03-18 23:37:00 +01:00
|
|
|
PutFixed64(&tmp, PackSequenceAndType(kMaxSequenceNumber,kValueTypeForSeek));
|
|
|
|
assert(this->Compare(*key, tmp) < 0);
|
|
|
|
key->swap(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-06 20:14:28 +01:00
|
|
|
LookupKey::LookupKey(const Slice& _user_key, SequenceNumber s) {
|
|
|
|
size_t usize = _user_key.size();
|
2011-06-22 04:36:45 +02:00
|
|
|
size_t needed = usize + 13; // A conservative estimate
|
|
|
|
char* dst;
|
|
|
|
if (needed <= sizeof(space_)) {
|
|
|
|
dst = space_;
|
|
|
|
} else {
|
|
|
|
dst = new char[needed];
|
|
|
|
}
|
|
|
|
start_ = dst;
|
2014-11-11 22:47:22 +01:00
|
|
|
// NOTE: We don't support users keys of more than 2GB :)
|
|
|
|
dst = EncodeVarint32(dst, static_cast<uint32_t>(usize + 8));
|
2011-06-22 04:36:45 +02:00
|
|
|
kstart_ = dst;
|
2014-11-06 20:14:28 +01:00
|
|
|
memcpy(dst, _user_key.data(), usize);
|
2011-06-22 04:36:45 +02:00
|
|
|
dst += usize;
|
|
|
|
EncodeFixed64(dst, PackSequenceAndType(s, kValueTypeForSeek));
|
|
|
|
dst += 8;
|
|
|
|
end_ = dst;
|
|
|
|
}
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|