/src/leveldb/db/memtable.cc
Line | Count | Source |
1 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
2 | | // Use of this source code is governed by a BSD-style license that can be |
3 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
4 | | |
5 | | #include "db/memtable.h" |
6 | | #include "db/dbformat.h" |
7 | | #include "leveldb/comparator.h" |
8 | | #include "leveldb/env.h" |
9 | | #include "leveldb/iterator.h" |
10 | | #include "util/coding.h" |
11 | | |
12 | | namespace leveldb { |
13 | | |
14 | 64.1M | static Slice GetLengthPrefixedSlice(const char* data) { |
15 | 64.1M | uint32_t len; |
16 | 64.1M | const char* p = data; |
17 | 64.1M | p = GetVarint32Ptr(p, p + 5, &len); // +5: we assume "p" is not corrupted |
18 | 64.1M | return Slice(p, len); |
19 | 64.1M | } |
20 | | |
21 | | MemTable::MemTable(const InternalKeyComparator& comparator) |
22 | 195k | : comparator_(comparator), refs_(0), table_(comparator_, &arena_) {} |
23 | | |
24 | 195k | MemTable::~MemTable() { assert(refs_ == 0); } |
25 | | |
26 | 3.39M | size_t MemTable::ApproximateMemoryUsage() { return arena_.MemoryUsage(); } |
27 | | |
28 | | int MemTable::KeyComparator::operator()(const char* aptr, |
29 | 28.4M | const char* bptr) const { |
30 | | // Internal keys are encoded as length-prefixed strings. |
31 | 28.4M | Slice a = GetLengthPrefixedSlice(aptr); |
32 | 28.4M | Slice b = GetLengthPrefixedSlice(bptr); |
33 | 28.4M | return comparator.Compare(a, b); |
34 | 28.4M | } |
35 | | |
36 | | // Encode a suitable internal key target for "target" and return it. |
37 | | // Uses *scratch as scratch space, and the returned pointer will point |
38 | | // into this scratch space. |
39 | 0 | static const char* EncodeKey(std::string* scratch, const Slice& target) { |
40 | 0 | scratch->clear(); |
41 | 0 | PutVarint32(scratch, target.size()); |
42 | 0 | scratch->append(target.data(), target.size()); |
43 | 0 | return scratch->data(); |
44 | 0 | } |
45 | | |
46 | | class MemTableIterator : public Iterator { |
47 | | public: |
48 | 226k | explicit MemTableIterator(MemTable::Table* table) : iter_(table) {} |
49 | | |
50 | | MemTableIterator(const MemTableIterator&) = delete; |
51 | | MemTableIterator& operator=(const MemTableIterator&) = delete; |
52 | | |
53 | 226k | ~MemTableIterator() override = default; |
54 | | |
55 | 2.76M | bool Valid() const override { return iter_.Valid(); } |
56 | 0 | void Seek(const Slice& k) override { iter_.Seek(EncodeKey(&tmp_, k)); } |
57 | 142k | void SeekToFirst() override { iter_.SeekToFirst(); } |
58 | 0 | void SeekToLast() override { iter_.SeekToLast(); } |
59 | 2.43M | void Next() override { iter_.Next(); } |
60 | 0 | void Prev() override { iter_.Prev(); } |
61 | 2.49M | Slice key() const override { return GetLengthPrefixedSlice(iter_.key()); } |
62 | 2.43M | Slice value() const override { |
63 | 2.43M | Slice key_slice = GetLengthPrefixedSlice(iter_.key()); |
64 | 2.43M | return GetLengthPrefixedSlice(key_slice.data() + key_slice.size()); |
65 | 2.43M | } |
66 | | |
67 | 78.0k | Status status() const override { return Status::OK(); } |
68 | | |
69 | | private: |
70 | | MemTable::Table::Iterator iter_; |
71 | | std::string tmp_; // For passing to EncodeKey |
72 | | }; |
73 | | |
74 | 226k | Iterator* MemTable::NewIterator() { return new MemTableIterator(&table_); } |
75 | | |
76 | | void MemTable::Add(SequenceNumber s, ValueType type, const Slice& key, |
77 | 3.36M | const Slice& value) { |
78 | | // Format of an entry is concatenation of: |
79 | | // key_size : varint32 of internal_key.size() |
80 | | // key bytes : char[internal_key.size()] |
81 | | // tag : uint64((sequence << 8) | type) |
82 | | // value_size : varint32 of value.size() |
83 | | // value bytes : char[value.size()] |
84 | 3.36M | size_t key_size = key.size(); |
85 | 3.36M | size_t val_size = value.size(); |
86 | 3.36M | size_t internal_key_size = key_size + 8; |
87 | 3.36M | const size_t encoded_len = VarintLength(internal_key_size) + |
88 | 3.36M | internal_key_size + VarintLength(val_size) + |
89 | 3.36M | val_size; |
90 | 3.36M | char* buf = arena_.Allocate(encoded_len); |
91 | 3.36M | char* p = EncodeVarint32(buf, internal_key_size); |
92 | 3.36M | std::memcpy(p, key.data(), key_size); |
93 | 3.36M | p += key_size; |
94 | 3.36M | EncodeFixed64(p, (s << 8) | type); |
95 | 3.36M | p += 8; |
96 | 3.36M | p = EncodeVarint32(p, val_size); |
97 | 3.36M | std::memcpy(p, value.data(), val_size); |
98 | 3.36M | assert(p + val_size == buf + encoded_len); |
99 | 3.36M | table_.Insert(buf); |
100 | 3.36M | } |
101 | | |
102 | 50.1k | bool MemTable::Get(const LookupKey& key, std::string* value, Status* s) { |
103 | 50.1k | Slice memkey = key.memtable_key(); |
104 | 50.1k | Table::Iterator iter(&table_); |
105 | 50.1k | iter.Seek(memkey.data()); |
106 | 50.1k | if (iter.Valid()) { |
107 | | // entry format is: |
108 | | // klength varint32 |
109 | | // userkey char[klength] |
110 | | // tag uint64 |
111 | | // vlength varint32 |
112 | | // value char[vlength] |
113 | | // Check that it belongs to same user key. We do not check the |
114 | | // sequence number since the Seek() call above should have skipped |
115 | | // all entries with overly large sequence numbers. |
116 | 14.2k | const char* entry = iter.key(); |
117 | 14.2k | uint32_t key_length; |
118 | 14.2k | const char* key_ptr = GetVarint32Ptr(entry, entry + 5, &key_length); |
119 | 14.2k | if (comparator_.comparator.user_comparator()->Compare( |
120 | 14.2k | Slice(key_ptr, key_length - 8), key.user_key()) == 0) { |
121 | | // Correct user key |
122 | 6.90k | const uint64_t tag = DecodeFixed64(key_ptr + key_length - 8); |
123 | 6.90k | switch (static_cast<ValueType>(tag & 0xff)) { |
124 | 4.18k | case kTypeValue: { |
125 | 4.18k | Slice v = GetLengthPrefixedSlice(key_ptr + key_length); |
126 | 4.18k | value->assign(v.data(), v.size()); |
127 | 4.18k | return true; |
128 | 0 | } |
129 | 2.72k | case kTypeDeletion: |
130 | 2.72k | *s = Status::NotFound(Slice()); |
131 | 2.72k | return true; |
132 | 6.90k | } |
133 | 6.90k | } |
134 | 14.2k | } |
135 | 43.2k | return false; |
136 | 50.1k | } |
137 | | |
138 | | } // namespace leveldb |