2013-10-16 23:59:46 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
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.
|
|
|
|
|
2013-10-05 07:32:05 +02:00
|
|
|
#pragma once
|
2011-03-18 23:37:00 +01:00
|
|
|
#include <stdio.h>
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/comparator.h"
|
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/filter_policy.h"
|
|
|
|
#include "rocksdb/slice.h"
|
2014-04-10 23:19:43 +02:00
|
|
|
#include "rocksdb/slice_transform.h"
|
2013-10-29 01:54:09 +01:00
|
|
|
#include "rocksdb/table.h"
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/types.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
#include "util/coding.h"
|
|
|
|
#include "util/logging.h"
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
class InternalKey;
|
|
|
|
|
|
|
|
// Value types encoded as the last component of internal keys.
|
|
|
|
// DO NOT CHANGE THESE ENUM VALUES: they are embedded in the on-disk
|
|
|
|
// data structures.
|
2014-01-27 22:53:22 +01:00
|
|
|
// The highest bit of the value type needs to be reserved to SST tables
|
|
|
|
// for them to do more flexible encoding.
|
|
|
|
enum ValueType : unsigned char {
|
2011-03-18 23:37:00 +01:00
|
|
|
kTypeDeletion = 0x0,
|
2013-03-21 23:59:47 +01:00
|
|
|
kTypeValue = 0x1,
|
2013-08-15 01:32:46 +02:00
|
|
|
kTypeMerge = 0x2,
|
2014-01-07 23:41:42 +01:00
|
|
|
kTypeLogData = 0x3,
|
|
|
|
kTypeColumnFamilyDeletion = 0x4,
|
|
|
|
kTypeColumnFamilyValue = 0x5,
|
|
|
|
kTypeColumnFamilyMerge = 0x6,
|
2014-01-27 22:53:22 +01:00
|
|
|
kMaxValue = 0x7F
|
2011-03-18 23:37:00 +01:00
|
|
|
};
|
2014-01-27 22:53:22 +01:00
|
|
|
|
2011-03-18 23:37:00 +01: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).
|
2013-03-21 23:59:47 +01:00
|
|
|
static const ValueType kValueTypeForSeek = kTypeMerge;
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
// We leave eight bits empty at the bottom so a type and sequence#
|
|
|
|
// can be packed together into 64-bits.
|
|
|
|
static const SequenceNumber kMaxSequenceNumber =
|
|
|
|
((0x1ull << 56) - 1);
|
|
|
|
|
|
|
|
struct ParsedInternalKey {
|
|
|
|
Slice user_key;
|
|
|
|
SequenceNumber sequence;
|
|
|
|
ValueType type;
|
|
|
|
|
|
|
|
ParsedInternalKey() { } // Intentionally left uninitialized (for speed)
|
|
|
|
ParsedInternalKey(const Slice& u, const SequenceNumber& seq, ValueType t)
|
|
|
|
: user_key(u), sequence(seq), type(t) { }
|
2012-12-16 03:28:36 +01:00
|
|
|
std::string DebugString(bool hex = false) const;
|
2011-03-18 23:37:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Return the length of the encoding of "key".
|
|
|
|
inline size_t InternalKeyEncodingLength(const ParsedInternalKey& key) {
|
|
|
|
return key.user_key.size() + 8;
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:45:30 +02:00
|
|
|
extern uint64_t PackSequenceAndType(uint64_t seq, ValueType t);
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
// Append the serialization of "key" to *result.
|
|
|
|
extern void AppendInternalKey(std::string* result,
|
|
|
|
const ParsedInternalKey& key);
|
|
|
|
|
|
|
|
// Attempt to parse an internal key from "internal_key". On success,
|
|
|
|
// stores the parsed data in "*result", and returns true.
|
|
|
|
//
|
|
|
|
// On error, returns false, leaves "*result" in an undefined state.
|
|
|
|
extern bool ParseInternalKey(const Slice& internal_key,
|
|
|
|
ParsedInternalKey* result);
|
|
|
|
|
|
|
|
// Returns the user key portion of an internal key.
|
|
|
|
inline Slice ExtractUserKey(const Slice& internal_key) {
|
|
|
|
assert(internal_key.size() >= 8);
|
|
|
|
return Slice(internal_key.data(), internal_key.size() - 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ValueType ExtractValueType(const Slice& internal_key) {
|
|
|
|
assert(internal_key.size() >= 8);
|
|
|
|
const size_t n = internal_key.size();
|
|
|
|
uint64_t num = DecodeFixed64(internal_key.data() + n - 8);
|
|
|
|
unsigned char c = num & 0xff;
|
|
|
|
return static_cast<ValueType>(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
// A comparator for internal keys that uses a specified comparator for
|
|
|
|
// the user key portion and breaks ties by decreasing sequence number.
|
|
|
|
class InternalKeyComparator : public Comparator {
|
|
|
|
private:
|
|
|
|
const Comparator* user_comparator_;
|
2013-06-10 22:28:58 +02:00
|
|
|
std::string name_;
|
2011-03-18 23:37:00 +01:00
|
|
|
public:
|
2013-06-10 22:28:58 +02:00
|
|
|
explicit InternalKeyComparator(const Comparator* c) : user_comparator_(c),
|
2013-10-05 07:32:05 +02:00
|
|
|
name_("rocksdb.InternalKeyComparator:" +
|
2013-06-10 22:28:58 +02:00
|
|
|
std::string(user_comparator_->Name())) {
|
|
|
|
}
|
2014-01-27 22:53:22 +01:00
|
|
|
virtual ~InternalKeyComparator() {}
|
2013-06-10 22:28:58 +02:00
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
virtual const char* Name() const;
|
|
|
|
virtual int Compare(const Slice& a, const Slice& b) const;
|
|
|
|
virtual void FindShortestSeparator(
|
|
|
|
std::string* start,
|
|
|
|
const Slice& limit) const;
|
|
|
|
virtual void FindShortSuccessor(std::string* key) const;
|
|
|
|
|
|
|
|
const Comparator* user_comparator() const { return user_comparator_; }
|
|
|
|
|
|
|
|
int Compare(const InternalKey& a, const InternalKey& b) const;
|
2014-01-27 22:53:22 +01:00
|
|
|
int Compare(const ParsedInternalKey& a, const ParsedInternalKey& b) const;
|
2011-03-18 23:37:00 +01:00
|
|
|
};
|
|
|
|
|
2012-04-17 17:36:46 +02:00
|
|
|
// Filter policy wrapper that converts from internal keys to user keys
|
|
|
|
class InternalFilterPolicy : public FilterPolicy {
|
|
|
|
private:
|
|
|
|
const FilterPolicy* const user_policy_;
|
|
|
|
public:
|
|
|
|
explicit InternalFilterPolicy(const FilterPolicy* p) : user_policy_(p) { }
|
|
|
|
virtual const char* Name() const;
|
|
|
|
virtual void CreateFilter(const Slice* keys, int n, std::string* dst) const;
|
|
|
|
virtual bool KeyMayMatch(const Slice& key, const Slice& filter) const;
|
|
|
|
};
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
// Modules in this directory should keep internal keys wrapped inside
|
|
|
|
// the following class instead of plain strings so that we do not
|
|
|
|
// incorrectly use string comparisons instead of an InternalKeyComparator.
|
|
|
|
class InternalKey {
|
|
|
|
private:
|
|
|
|
std::string rep_;
|
|
|
|
public:
|
|
|
|
InternalKey() { } // Leave rep_ as empty to indicate it is invalid
|
|
|
|
InternalKey(const Slice& user_key, SequenceNumber s, ValueType t) {
|
|
|
|
AppendInternalKey(&rep_, ParsedInternalKey(user_key, s, t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecodeFrom(const Slice& s) { rep_.assign(s.data(), s.size()); }
|
|
|
|
Slice Encode() const {
|
|
|
|
assert(!rep_.empty());
|
|
|
|
return rep_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice user_key() const { return ExtractUserKey(rep_); }
|
|
|
|
|
|
|
|
void SetFrom(const ParsedInternalKey& p) {
|
|
|
|
rep_.clear();
|
|
|
|
AppendInternalKey(&rep_, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear() { rep_.clear(); }
|
2011-10-06 01:30:28 +02:00
|
|
|
|
2012-12-16 03:28:36 +01:00
|
|
|
std::string DebugString(bool hex = false) const;
|
2011-03-18 23:37:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline int InternalKeyComparator::Compare(
|
|
|
|
const InternalKey& a, const InternalKey& b) const {
|
|
|
|
return Compare(a.Encode(), b.Encode());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ParseInternalKey(const Slice& internal_key,
|
|
|
|
ParsedInternalKey* result) {
|
|
|
|
const size_t n = internal_key.size();
|
|
|
|
if (n < 8) return false;
|
|
|
|
uint64_t num = DecodeFixed64(internal_key.data() + n - 8);
|
|
|
|
unsigned char c = num & 0xff;
|
|
|
|
result->sequence = num >> 8;
|
|
|
|
result->type = static_cast<ValueType>(c);
|
2014-01-27 22:53:22 +01:00
|
|
|
assert(result->type <= ValueType::kMaxValue);
|
2011-03-18 23:37:00 +01:00
|
|
|
result->user_key = Slice(internal_key.data(), n - 8);
|
2013-03-21 23:59:47 +01:00
|
|
|
return (c <= static_cast<unsigned char>(kValueTypeForSeek));
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
2013-02-15 23:31:24 +01:00
|
|
|
// Update the sequence number in the internal key
|
2013-03-06 06:53:28 +01:00
|
|
|
inline void UpdateInternalKey(char* internal_key,
|
|
|
|
const size_t internal_key_size,
|
2013-02-15 23:31:24 +01:00
|
|
|
uint64_t seq, ValueType t) {
|
2013-03-06 06:53:28 +01:00
|
|
|
assert(internal_key_size >= 8);
|
|
|
|
char* seqtype = internal_key + internal_key_size - 8;
|
2013-02-15 23:31:24 +01:00
|
|
|
uint64_t newval = (seq << 8) | t;
|
|
|
|
EncodeFixed64(seqtype, newval);
|
|
|
|
}
|
|
|
|
|
2013-06-14 07:09:08 +02:00
|
|
|
// Get the sequence number from the internal key
|
|
|
|
inline uint64_t GetInternalKeySeqno(const Slice& internal_key) {
|
|
|
|
const size_t n = internal_key.size();
|
|
|
|
assert(n >= 8);
|
|
|
|
uint64_t num = DecodeFixed64(internal_key.data() + n - 8);
|
|
|
|
return num >> 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-22 04:36:45 +02:00
|
|
|
// A helper class useful for DBImpl::Get()
|
|
|
|
class LookupKey {
|
|
|
|
public:
|
|
|
|
// Initialize *this for looking up user_key at a snapshot with
|
|
|
|
// the specified sequence number.
|
|
|
|
LookupKey(const Slice& user_key, SequenceNumber sequence);
|
|
|
|
|
|
|
|
~LookupKey();
|
|
|
|
|
|
|
|
// Return a key suitable for lookup in a MemTable.
|
|
|
|
Slice memtable_key() const { return Slice(start_, end_ - start_); }
|
|
|
|
|
|
|
|
// Return an internal key (suitable for passing to an internal iterator)
|
|
|
|
Slice internal_key() const { return Slice(kstart_, end_ - kstart_); }
|
|
|
|
|
|
|
|
// Return the user key
|
|
|
|
Slice user_key() const { return Slice(kstart_, end_ - kstart_ - 8); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// We construct a char array of the form:
|
|
|
|
// klength varint32 <-- start_
|
|
|
|
// userkey char[klength] <-- kstart_
|
|
|
|
// tag uint64
|
|
|
|
// <-- end_
|
|
|
|
// The array is a suitable MemTable key.
|
|
|
|
// The suffix starting with "userkey" can be used as an InternalKey.
|
|
|
|
const char* start_;
|
|
|
|
const char* kstart_;
|
|
|
|
const char* end_;
|
|
|
|
char space_[200]; // Avoid allocation for short keys
|
|
|
|
|
|
|
|
// No copying allowed
|
|
|
|
LookupKey(const LookupKey&);
|
|
|
|
void operator=(const LookupKey&);
|
|
|
|
};
|
|
|
|
|
|
|
|
inline LookupKey::~LookupKey() {
|
|
|
|
if (start_ != space_) delete[] start_;
|
|
|
|
}
|
|
|
|
|
2014-04-08 01:56:26 +02:00
|
|
|
class IterKey {
|
|
|
|
public:
|
|
|
|
IterKey() : key_(space_), buf_size_(sizeof(space_)), key_size_(0) {}
|
|
|
|
|
2014-04-09 02:30:45 +02:00
|
|
|
~IterKey() { ResetBuffer(); }
|
2014-04-08 01:56:26 +02:00
|
|
|
|
2014-04-09 02:30:45 +02:00
|
|
|
Slice GetKey() const { return Slice(key_, key_size_); }
|
2014-04-08 01:56:26 +02:00
|
|
|
|
2014-04-09 02:30:45 +02:00
|
|
|
void Clear() { key_size_ = 0; }
|
2014-04-08 01:56:26 +02:00
|
|
|
|
|
|
|
void SetUserKey(const Slice& user_key) {
|
|
|
|
size_t size = user_key.size();
|
|
|
|
EnlargeBufferIfNeeded(size);
|
|
|
|
memcpy(key_, user_key.data(), size);
|
2014-04-09 02:30:45 +02:00
|
|
|
key_size_ = size;
|
2014-04-08 01:56:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetInternalKey(const Slice& user_key, SequenceNumber s,
|
|
|
|
ValueType value_type = kValueTypeForSeek) {
|
|
|
|
size_t usize = user_key.size();
|
|
|
|
EnlargeBufferIfNeeded(usize + sizeof(uint64_t));
|
|
|
|
memcpy(key_, user_key.data(), usize);
|
|
|
|
EncodeFixed64(key_ + usize, PackSequenceAndType(s, value_type));
|
2014-04-09 02:30:45 +02:00
|
|
|
key_size_ = usize + sizeof(uint64_t);
|
2014-04-08 01:56:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetInternalKey(const ParsedInternalKey& parsed_key) {
|
|
|
|
SetInternalKey(parsed_key.user_key, parsed_key.sequence, parsed_key.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
char* key_;
|
|
|
|
size_t buf_size_;
|
|
|
|
size_t key_size_;
|
|
|
|
char space_[32]; // Avoid allocation for short keys
|
|
|
|
|
2014-04-09 02:30:45 +02:00
|
|
|
void ResetBuffer() {
|
|
|
|
if (key_ != nullptr && key_ != space_) {
|
|
|
|
delete[] key_;
|
|
|
|
}
|
|
|
|
key_ = space_;
|
2014-04-24 02:51:16 +02:00
|
|
|
buf_size_ = sizeof(space_);
|
2014-04-09 02:30:45 +02:00
|
|
|
key_size_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enlarge the buffer size if needed based on key_size.
|
|
|
|
// By default, static allocated buffer is used. Once there is a key
|
|
|
|
// larger than the static allocated buffer, another buffer is dynamically
|
|
|
|
// allocated, until a larger key buffer is requested. In that case, we
|
|
|
|
// reallocate buffer and delete the old one.
|
|
|
|
void EnlargeBufferIfNeeded(size_t key_size) {
|
|
|
|
// If size is smaller than buffer size, continue using current buffer,
|
|
|
|
// or the static allocated one, as default
|
|
|
|
if (key_size > buf_size_) {
|
|
|
|
// Need to enlarge the buffer.
|
|
|
|
ResetBuffer();
|
|
|
|
key_ = new char[key_size];
|
|
|
|
buf_size_ = key_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-08 01:56:26 +02:00
|
|
|
// No copying allowed
|
|
|
|
IterKey(const IterKey&) = delete;
|
|
|
|
void operator=(const IterKey&) = delete;
|
|
|
|
};
|
|
|
|
|
2014-04-10 23:19:43 +02:00
|
|
|
class InternalKeySliceTransform : public SliceTransform {
|
|
|
|
public:
|
|
|
|
explicit InternalKeySliceTransform(const SliceTransform* transform)
|
|
|
|
: transform_(transform) {}
|
|
|
|
|
|
|
|
virtual const char* Name() const { return transform_->Name(); }
|
|
|
|
|
|
|
|
virtual Slice Transform(const Slice& src) const {
|
|
|
|
auto user_key = ExtractUserKey(src);
|
|
|
|
return transform_->Transform(user_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool InDomain(const Slice& src) const {
|
|
|
|
auto user_key = ExtractUserKey(src);
|
|
|
|
return transform_->InDomain(user_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool InRange(const Slice& dst) const {
|
|
|
|
auto user_key = ExtractUserKey(dst);
|
|
|
|
return transform_->InRange(user_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
const SliceTransform* user_prefix_extractor() const { return transform_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Like comparator, InternalKeySliceTransform will not take care of the
|
|
|
|
// deletion of transform_
|
|
|
|
const SliceTransform* const transform_;
|
|
|
|
};
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|