Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
memtable.cc
Go to the documentation of this file.
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 static Slice GetLengthPrefixedSlice(const char* data) {
15  uint32_t len;
16  const char* p = data;
17  p = GetVarint32Ptr(p, p + 5, &len); // +5: we assume "p" is not corrupted
18  return Slice(p, len);
19 }
20 
22  : comparator_(cmp),
23  refs_(0),
24  table_(comparator_, &arena_) {
25 }
26 
28  assert(refs_ == 0);
29 }
30 
32 
33 int MemTable::KeyComparator::operator()(const char* aptr, const char* bptr)
34  const {
35  // Internal keys are encoded as length-prefixed strings.
36  Slice a = GetLengthPrefixedSlice(aptr);
37  Slice b = GetLengthPrefixedSlice(bptr);
38  return comparator.Compare(a, b);
39 }
40 
41 // Encode a suitable internal key target for "target" and return it.
42 // Uses *scratch as scratch space, and the returned pointer will point
43 // into this scratch space.
44 static const char* EncodeKey(std::string* scratch, const Slice& target) {
45  scratch->clear();
46  PutVarint32(scratch, target.size());
47  scratch->append(target.data(), target.size());
48  return scratch->data();
49 }
50 
51 class MemTableIterator: public Iterator {
52  public:
53  explicit MemTableIterator(MemTable::Table* table) : iter_(table) { }
54 
55  virtual bool Valid() const { return iter_.Valid(); }
56  virtual void Seek(const Slice& k) { iter_.Seek(EncodeKey(&tmp_, k)); }
57  virtual void SeekToFirst() { iter_.SeekToFirst(); }
58  virtual void SeekToLast() { iter_.SeekToLast(); }
59  virtual void Next() { iter_.Next(); }
60  virtual void Prev() { iter_.Prev(); }
61  virtual Slice key() const { return GetLengthPrefixedSlice(iter_.key()); }
62  virtual Slice value() const {
63  Slice key_slice = GetLengthPrefixedSlice(iter_.key());
64  return GetLengthPrefixedSlice(key_slice.data() + key_slice.size());
65  }
66 
67  virtual Status status() const { return Status::OK(); }
68 
69  private:
70  MemTable::Table::Iterator iter_;
71  std::string tmp_; // For passing to EncodeKey
72 
73  // No copying allowed
75  void operator=(const MemTableIterator&);
76 };
77 
79  return new MemTableIterator(&table_);
80 }
81 
83  const Slice& key,
84  const Slice& value) {
85  // Format of an entry is concatenation of:
86  // key_size : varint32 of internal_key.size()
87  // key bytes : char[internal_key.size()]
88  // value_size : varint32 of value.size()
89  // value bytes : char[value.size()]
90  size_t key_size = key.size();
91  size_t val_size = value.size();
92  size_t internal_key_size = key_size + 8;
93  const size_t encoded_len =
94  VarintLength(internal_key_size) + internal_key_size +
95  VarintLength(val_size) + val_size;
96  char* buf = arena_.Allocate(encoded_len);
97  char* p = EncodeVarint32(buf, internal_key_size);
98  memcpy(p, key.data(), key_size);
99  p += key_size;
100  EncodeFixed64(p, (s << 8) | type);
101  p += 8;
102  p = EncodeVarint32(p, val_size);
103  memcpy(p, value.data(), val_size);
104  assert((p + val_size) - buf == encoded_len);
105  table_.Insert(buf);
106 }
107 
108 bool MemTable::Get(const LookupKey& key, std::string* value, Status* s) {
109  Slice memkey = key.memtable_key();
110  Table::Iterator iter(&table_);
111  iter.Seek(memkey.data());
112  if (iter.Valid()) {
113  // entry format is:
114  // klength varint32
115  // userkey char[klength]
116  // tag uint64
117  // vlength varint32
118  // value char[vlength]
119  // Check that it belongs to same user key. We do not check the
120  // sequence number since the Seek() call above should have skipped
121  // all entries with overly large sequence numbers.
122  const char* entry = iter.key();
124  const char* key_ptr = GetVarint32Ptr(entry, entry+5, &key_length);
126  Slice(key_ptr, key_length - 8),
127  key.user_key()) == 0) {
128  // Correct user key
129  const uint64_t tag = DecodeFixed64(key_ptr + key_length - 8);
130  switch (static_cast<ValueType>(tag & 0xff)) {
131  case kTypeValue: {
132  Slice v = GetLengthPrefixedSlice(key_ptr + key_length);
133  value->assign(v.data(), v.size());
134  return true;
135  }
136  case kTypeDeletion:
137  *s = Status::NotFound(Slice());
138  return true;
139  }
140  }
141  }
142  return false;
143 }
144 
145 } // namespace leveldb
int operator()(const char *a, const char *b) const
Definition: memtable.cc:33
virtual void Prev()
Definition: memtable.cc:60
size_t ApproximateMemoryUsage()
Definition: memtable.cc:31
std::string * value
Definition: version_set.cc:270
uint64_t DecodeFixed64(const char *ptr)
Definition: coding.h:72
Slice user_key() const
Definition: dbformat.h:204
const char * data() const
Definition: slice.h:40
const char * GetVarint32Ptr(const char *p, const char *limit, uint32_t *v)
Definition: coding.h:89
MemTable::Table::Iterator iter_
Definition: memtable.cc:70
Iterator *const iter_
Definition: db_iter.cc:113
char * Allocate(size_t bytes)
Definition: arena.h:52
ValueType
Definition: dbformat.h:51
char * EncodeVarint32(char *dst, uint32_t v)
Definition: coding.cc:47
static Status OK()
Definition: status.h:32
friend class MemTableIterator
Definition: memtable.h:74
int refs_
Definition: memenv.cc:137
uint64_t SequenceNumber
Definition: dbformat.h:63
size_t key_length
Definition: cache.cc:32
MemTable(const InternalKeyComparator &comparator)
Definition: memtable.cc:21
virtual bool Valid() const
Definition: memtable.cc:55
void EncodeFixed64(char *buf, uint64_t value)
Definition: coding.cc:20
const Comparator * comparator_
Definition: merger.cc:142
MemTableIterator(MemTable::Table *table)
Definition: memtable.cc:53
virtual Slice key() const
Definition: memtable.cc:61
void Insert(const Key &key)
Definition: skiplist.h:334
unsigned int uint32_t
Definition: stdint.h:21
virtual int Compare(const Slice &a, const Slice &b) const
Definition: dbformat.cc:50
HandleTable table_
Definition: cache.cc:167
size_t size() const
Definition: slice.h:43
unsigned long long uint64_t
Definition: stdint.h:22
virtual void SeekToLast()
Definition: memtable.cc:58
Iterator * NewIterator()
Definition: memtable.cc:78
static Status NotFound(const Slice &msg, const Slice &msg2=Slice())
Definition: status.h:35
virtual void Next()
Definition: memtable.cc:59
int VarintLength(uint64_t v)
Definition: coding.cc:103
const Comparator * user_comparator() const
Definition: dbformat.h:125
virtual Slice value() const
Definition: memtable.cc:62
virtual Status status() const
Definition: memtable.cc:67
Slice memtable_key() const
Definition: dbformat.h:198
bool Get(const LookupKey &key, std::string *value, Status *s)
Definition: memtable.cc:108
virtual int Compare(const Slice &a, const Slice &b) const =0
const InternalKeyComparator comparator
Definition: memtable.h:70
const Comparator * cmp
Definition: table_test.cc:80
virtual void Seek(const Slice &k)
Definition: memtable.cc:56
void Add(SequenceNumber seq, ValueType type, const Slice &key, const Slice &value)
Definition: memtable.cc:82
virtual void SeekToFirst()
Definition: memtable.cc:57
void operator=(const MemTable &)
void PutVarint32(std::string *dst, uint32_t v)
Definition: coding.cc:75
size_t MemoryUsage() const
Definition: arena.h:29
KeyComparator comparator_
Definition: memtable.h:79