// 迭代器类,迭代key-value classIterator { public: // Initialize an iterator over the specified list. // The returned iterator is not valid. explicitIterator(const SkipList *list);
// Returns true iff the iterator is positioned at a valid node. boolValid()const;
// Returns the key at the current position. // REQUIRES: Valid() const Key &key()const;
// Advances to the next position. // REQUIRES: Valid() voidNext();
// Advances to the previous position. // REQUIRES: Valid() voidPrev();
// Advance to the first entry with a key >= target voidSeek(const Key &target);
// Position at the first entry in list. // Final state of iterator is Valid() iff list is not empty. voidSeekToFirst();
// Position at the last entry in list. // Final state of iterator is Valid() iff list is not empty. voidSeekToLast();
// Add an entry into memtable that maps key to value at the // specified sequence number and with the specified type. // Typically value will be empty if type==kTypeDeletion. // 添加一个key,type可以用来指示删除key,即把对应的value设置为空 voidAdd(SequenceNumber seq, ValueType type, const Slice &key, const Slice &value);
// If memtable contains a value for key, store it in *value and return true. // If memtable contains a deletion for key, store a NotFound() error // in *status and return true. // Else, return false. // 查找一个key,如果是被删除的key不会返回success boolGet(const LookupKey &key, std::string *value, Status *s); };
// 添加一个key-value voidMemTable::Add(SequenceNumber s, ValueType type, const Slice &key, const Slice &value) { // Format of an entry is concatenation of: // key_size : varint32 of internal_key.size() // key bytes : char[internal_key.size()] // tag : uint64((sequence << 8) | type) // 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; constsize_t encoded_len = VarintLength(internal_key_size) + internal_key_size + VarintLength(val_size) + val_size; char *buf = arena_.Allocate(encoded_len); char *p = EncodeVarint32(buf, internal_key_size); std::memcpy(p, key.data(), key_size); p += key_size; EncodeFixed64(p, (s << 8) | type); p += 8; p = EncodeVarint32(p, val_size); std::memcpy(p, value.data(), val_size); assert(p + val_size == buf + encoded_len); table_.Insert(buf); }
// 获取一个key boolMemTable::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. constchar *entry = iter.key(); uint32_t key_length; constchar *key_ptr = GetVarint32Ptr(entry, entry + 5, &key_length); if (comparator_.comparator.user_comparator()->Compare( Slice(key_ptr, key_length - 8), key.user_key()) == 0) { // Correct user key constuint64_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()); returntrue; } case kTypeDeletion: *s = Status::NotFound(Slice()); returntrue; } } } returnfalse; }