2011-03-18 22:37:00 +00: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.
|
|
|
|
|
|
|
|
#include "db/memtable.h"
|
|
|
|
#include "db/dbformat.h"
|
2011-03-30 18:35:40 +00:00
|
|
|
#include "leveldb/comparator.h"
|
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/iterator.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "util/coding.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
static Slice GetLengthPrefixedSlice(const char* data) {
|
|
|
|
uint32_t len;
|
|
|
|
const char* p = data;
|
|
|
|
p = GetVarint32Ptr(p, p + 5, &len); // +5: we assume "p" is not corrupted
|
|
|
|
return Slice(p, len);
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
MemTable::MemTable(const InternalKeyComparator& comparator)
|
|
|
|
: comparator_(comparator), refs_(0), table_(comparator_, &arena_) {}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
MemTable::~MemTable() { assert(refs_ == 0); }
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
size_t MemTable::ApproximateMemoryUsage() { return arena_.MemoryUsage(); }
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
int MemTable::KeyComparator::operator()(const char* aptr,
|
|
|
|
const char* bptr) const {
|
2011-03-18 22:37:00 +00:00
|
|
|
// Internal keys are encoded as length-prefixed strings.
|
|
|
|
Slice a = GetLengthPrefixedSlice(aptr);
|
|
|
|
Slice b = GetLengthPrefixedSlice(bptr);
|
|
|
|
return comparator.Compare(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode a suitable internal key target for "target" and return it.
|
|
|
|
// Uses *scratch as scratch space, and the returned pointer will point
|
|
|
|
// into this scratch space.
|
|
|
|
static const char* EncodeKey(std::string* scratch, const Slice& target) {
|
|
|
|
scratch->clear();
|
|
|
|
PutVarint32(scratch, target.size());
|
|
|
|
scratch->append(target.data(), target.size());
|
|
|
|
return scratch->data();
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
class MemTableIterator : public Iterator {
|
2011-03-18 22:37:00 +00:00
|
|
|
public:
|
2019-05-02 11:01:00 -07:00
|
|
|
explicit MemTableIterator(MemTable::Table* table) : iter_(table) {}
|
2011-05-28 00:53:58 +00:00
|
|
|
|
2019-05-04 17:40:21 -07:00
|
|
|
MemTableIterator(const MemTableIterator&) = delete;
|
|
|
|
MemTableIterator& operator=(const MemTableIterator&) = delete;
|
|
|
|
|
|
|
|
~MemTableIterator() override = default;
|
|
|
|
|
|
|
|
bool Valid() const override { return iter_.Valid(); }
|
|
|
|
void Seek(const Slice& k) override { iter_.Seek(EncodeKey(&tmp_, k)); }
|
|
|
|
void SeekToFirst() override { iter_.SeekToFirst(); }
|
|
|
|
void SeekToLast() override { iter_.SeekToLast(); }
|
|
|
|
void Next() override { iter_.Next(); }
|
|
|
|
void Prev() override { iter_.Prev(); }
|
|
|
|
Slice key() const override { return GetLengthPrefixedSlice(iter_.key()); }
|
|
|
|
Slice value() const override {
|
2011-05-28 00:53:58 +00:00
|
|
|
Slice key_slice = GetLengthPrefixedSlice(iter_.key());
|
2011-03-18 22:37:00 +00:00
|
|
|
return GetLengthPrefixedSlice(key_slice.data() + key_slice.size());
|
|
|
|
}
|
|
|
|
|
2019-05-04 17:40:21 -07:00
|
|
|
Status status() const override { return Status::OK(); }
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
private:
|
2011-05-28 00:53:58 +00:00
|
|
|
MemTable::Table::Iterator iter_;
|
2019-05-02 11:01:00 -07:00
|
|
|
std::string tmp_; // For passing to EncodeKey
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
Iterator* MemTable::NewIterator() { return new MemTableIterator(&table_); }
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void MemTable::Add(SequenceNumber s, ValueType type, const Slice& key,
|
2011-03-18 22:37:00 +00:00
|
|
|
const Slice& value) {
|
|
|
|
// Format of an entry is concatenation of:
|
|
|
|
// key_size : varint32 of internal_key.size()
|
|
|
|
// key bytes : char[internal_key.size()]
|
|
|
|
// value_size : varint32 of value.size()
|
|
|
|
// value bytes : char[value.size()]
|
|
|
|
size_t key_size = key.size();
|
|
|
|
size_t val_size = value.size();
|
|
|
|
size_t internal_key_size = key_size + 8;
|
2019-05-02 11:01:00 -07:00
|
|
|
const size_t encoded_len = VarintLength(internal_key_size) +
|
|
|
|
internal_key_size + VarintLength(val_size) +
|
|
|
|
val_size;
|
2011-03-18 22:37:00 +00:00
|
|
|
char* buf = arena_.Allocate(encoded_len);
|
|
|
|
char* p = EncodeVarint32(buf, internal_key_size);
|
|
|
|
memcpy(p, key.data(), key_size);
|
|
|
|
p += key_size;
|
|
|
|
EncodeFixed64(p, (s << 8) | type);
|
|
|
|
p += 8;
|
|
|
|
p = EncodeVarint32(p, val_size);
|
|
|
|
memcpy(p, value.data(), val_size);
|
2018-04-16 12:27:06 -07:00
|
|
|
assert(p + val_size == buf + encoded_len);
|
2011-03-18 22:37:00 +00:00
|
|
|
table_.Insert(buf);
|
|
|
|
}
|
|
|
|
|
2011-06-22 02:36:45 +00:00
|
|
|
bool MemTable::Get(const LookupKey& key, std::string* value, Status* s) {
|
|
|
|
Slice memkey = key.memtable_key();
|
|
|
|
Table::Iterator iter(&table_);
|
|
|
|
iter.Seek(memkey.data());
|
|
|
|
if (iter.Valid()) {
|
|
|
|
// entry format is:
|
|
|
|
// klength varint32
|
|
|
|
// userkey char[klength]
|
|
|
|
// tag uint64
|
|
|
|
// vlength varint32
|
|
|
|
// value char[vlength]
|
|
|
|
// Check that it belongs to same user key. We do not check the
|
|
|
|
// sequence number since the Seek() call above should have skipped
|
|
|
|
// all entries with overly large sequence numbers.
|
|
|
|
const char* entry = iter.key();
|
|
|
|
uint32_t key_length;
|
2019-05-02 11:01:00 -07:00
|
|
|
const char* key_ptr = GetVarint32Ptr(entry, entry + 5, &key_length);
|
2011-06-22 02:36:45 +00:00
|
|
|
if (comparator_.comparator.user_comparator()->Compare(
|
2019-05-02 11:01:00 -07:00
|
|
|
Slice(key_ptr, key_length - 8), key.user_key()) == 0) {
|
2011-06-22 02:36:45 +00:00
|
|
|
// Correct user key
|
|
|
|
const uint64_t tag = DecodeFixed64(key_ptr + key_length - 8);
|
|
|
|
switch (static_cast<ValueType>(tag & 0xff)) {
|
|
|
|
case kTypeValue: {
|
|
|
|
Slice v = GetLengthPrefixedSlice(key_ptr + key_length);
|
|
|
|
value->assign(v.data(), v.size());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case kTypeDeletion:
|
|
|
|
*s = Status::NotFound(Slice());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace leveldb
|