|
|
@ -12,12 +12,14 @@ |
|
|
|
|
|
|
|
namespace leveldb { |
|
|
|
|
|
|
|
static uint64_t PackSequenceAndTypeAndTtl(uint64_t seq, ValueType t, bool havettl) { |
|
|
|
static uint64_t PackSequenceAndTypeAndTtlAndLookup( |
|
|
|
uint64_t seq, ValueType t, bool havettl, bool islookup) { |
|
|
|
assert(seq <= kMaxSequenceNumber); |
|
|
|
assert(t <= kValueTypeForSeek); |
|
|
|
return (seq << 8) | (havettl << 1) | t; |
|
|
|
return (seq << 8) | (islookup << 2) | (havettl << 1) | t; |
|
|
|
} |
|
|
|
|
|
|
|
//下面有两个调这个函数的没改,也许也要修改标志位?
|
|
|
|
static uint64_t PackSequenceAndType(uint64_t seq, ValueType t) { |
|
|
|
assert(seq <= kMaxSequenceNumber); |
|
|
|
assert(t <= kValueTypeForSeek); |
|
|
@ -28,8 +30,8 @@ void AppendInternalKey(std::string* result, const ParsedInternalKey& key) { |
|
|
|
result->append(key.user_key.data(), key.user_key.size()); |
|
|
|
if(key.deadTime != 0) |
|
|
|
PutFixed64(result, key.deadTime); |
|
|
|
PutFixed64(result, PackSequenceAndTypeAndTtl( |
|
|
|
key.sequence, key.type, (key.deadTime != 0))); |
|
|
|
PutFixed64(result, PackSequenceAndTypeAndTtlAndLookup( |
|
|
|
key.sequence, key.type, (key.deadTime != 0), false)); |
|
|
|
} |
|
|
|
|
|
|
|
std::string ParsedInternalKey::DebugString() const { |
|
|
@ -58,17 +60,33 @@ int InternalKeyComparator::Compare(const Slice& akey, const Slice& bkey) const { |
|
|
|
// increasing user key (according to user-supplied comparator)
|
|
|
|
// decreasing sequence number
|
|
|
|
// decreasing type (though sequence# should be enough to disambiguate)
|
|
|
|
Slice user_akey = ExtractUserKey(akey); |
|
|
|
Slice user_bkey = ExtractUserKey(bkey); |
|
|
|
std::string a = user_akey.ToString(); |
|
|
|
std::string b = user_bkey.ToString(); |
|
|
|
//目前看调用时都是a为node, b为key,万一有不是的,逻辑还得补充
|
|
|
|
//for debug
|
|
|
|
// std::string a = ExtractUserKey(akey).ToString();
|
|
|
|
// std::string b = ExtractUserKey(bkey).ToString();
|
|
|
|
int r = user_comparator_->Compare(ExtractUserKey(akey), ExtractUserKey(bkey)); |
|
|
|
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) { |
|
|
|
const uint64_t atag = DecodeFixed64(akey.data() + akey.size() - 8); |
|
|
|
const uint64_t btag = DecodeFixed64(bkey.data() + bkey.size() - 8); |
|
|
|
|
|
|
|
const uint64_t aseq = atag >> 8; |
|
|
|
const uint64_t bseq = btag >> 8; |
|
|
|
if (aseq > bseq) { |
|
|
|
r = -1; |
|
|
|
} else if (anum < bnum) { |
|
|
|
return r; |
|
|
|
} |
|
|
|
|
|
|
|
//原本应该找到了,新加判断
|
|
|
|
if((btag & 0b100) && (atag & 0b10)){ //一个是查询键,另一个有ttl
|
|
|
|
const uint64_t atime = DecodeFixed64(akey.data() + akey.size() - 16); |
|
|
|
const uint64_t btime = DecodeFixed64(bkey.data() + bkey.size() - 16); |
|
|
|
if(atime <= btime){//过期了继续找
|
|
|
|
r = -1; |
|
|
|
return r; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (aseq < bseq) { |
|
|
|
r = +1; |
|
|
|
} |
|
|
|
} |
|
|
@ -127,9 +145,9 @@ bool InternalFilterPolicy::KeyMayMatch(const Slice& key, const Slice& f) const { |
|
|
|
return user_policy_->KeyMayMatch(ExtractUserKey(key), f); |
|
|
|
} |
|
|
|
|
|
|
|
LookupKey::LookupKey(const Slice& user_key, SequenceNumber s) { |
|
|
|
LookupKey::LookupKey(const Slice& user_key, SequenceNumber s, uint64_t nowTime) { |
|
|
|
size_t usize = user_key.size(); |
|
|
|
size_t needed = usize + 13; // A conservative estimate
|
|
|
|
size_t needed = usize + 21; // A conservative estimate
|
|
|
|
char* dst; |
|
|
|
if (needed <= sizeof(space_)) { |
|
|
|
dst = space_; |
|
|
@ -137,11 +155,13 @@ LookupKey::LookupKey(const Slice& user_key, SequenceNumber s) { |
|
|
|
dst = new char[needed]; |
|
|
|
} |
|
|
|
start_ = dst; |
|
|
|
dst = EncodeVarint32(dst, usize + 8); |
|
|
|
dst = EncodeVarint32(dst, usize + 16); |
|
|
|
kstart_ = dst; |
|
|
|
std::memcpy(dst, user_key.data(), usize); |
|
|
|
dst += usize; |
|
|
|
EncodeFixed64(dst, PackSequenceAndType(s, kValueTypeForSeek)); |
|
|
|
EncodeFixed64(dst, nowTime); |
|
|
|
dst += 8; |
|
|
|
EncodeFixed64(dst, PackSequenceAndTypeAndTtlAndLookup(s, kValueTypeForSeek, 0, true)); |
|
|
|
dst += 8; |
|
|
|
end_ = dst; |
|
|
|
} |
|
|
|