class MemTableIterator : public Iterator class DBIter : public Iterator class ModelIter : public Iterator class Version::LevelFileNumIterator : public Iterator class Block::Iter : public Iterator class EmptyIterator : public Iterator class MergingIterator : public Iterator class KeyConvertingIterator : public Iterator class TwoLevelIterator : public Iterator
MemTableIterator
1 2 3 4 5 6 7
// 对跳表的iterator进行了简单的封装 classMemTableIterator :public Iterator { // ... private: MemTable::Table::Iterator iter_; std::string tmp_; // For passing to EncodeKey };
// Memtables and sstables that make the DB representation contain // (userkey,seq,type) => uservalue entries. DBIter // combines multiple entries for the same userkey found in the DB // representation into a single entry while accounting for sequence // numbers, deletion markers, overwrites, etc. classDBIter :public Iterator { public: // Which direction is the iterator currently moving? // (1) When moving forward, the internal iterator is positioned at // the exact entry that yields this->key(), this->value() // (2) When moving backwards, the internal iterator is positioned // just before all entries whose user key == this->key(). enumDirection { kForward, kReverse };
// Picks the number of bytes that can be read until a compaction is scheduled. size_tRandomCompactionPeriod(){ return rnd_.Uniform(2 * config::kReadBytesPeriod); }
DBImpl* db_; const Comparator* const user_comparator_; Iterator* const iter_; SequenceNumber const sequence_; Status status_; std::string saved_key_; // == current key when direction_==kReverse std::string saved_value_; // == current raw value when direction_==kReverse Direction direction_; bool valid_; Random rnd_; size_t bytes_until_read_sampling_; };
// An internal iterator. For a given version/level pair, yields // information about the files in the level. For a given entry, key() // is the largest key that occurs in the file, and value() is an // 16-byte value containing the file number and file size, both // encoded using EncodeFixed64. classVersion::LevelFileNumIterator : public Iterator { public: LevelFileNumIterator(const InternalKeyComparator& icmp, const std::vector<FileMetaData*>* flist) : icmp_(icmp), flist_(flist), index_(flist->size()) { // Marks as invalid } boolValid()constoverride{ return index_ < flist_->size(); } voidSeek(const Slice& target)override{ index_ = FindFile(icmp_, *flist_, target); } voidSeekToFirst()override{ index_ = 0; } voidSeekToLast()override{ index_ = flist_->empty() ? 0 : flist_->size() - 1; } voidNext()override{ assert(Valid()); index_++; } voidPrev()override{ assert(Valid()); if (index_ == 0) { index_ = flist_->size(); // Marks as invalid } else { index_--; } } Slice key()constoverride{ assert(Valid()); return (*flist_)[index_]->largest.Encode(); } Slice value()constoverride{ assert(Valid()); EncodeFixed64(value_buf_, (*flist_)[index_]->number); EncodeFixed64(value_buf_ + 8, (*flist_)[index_]->file_size); returnSlice(value_buf_, sizeof(value_buf_)); } Status status()constoverride{ return Status::OK(); }
BlockFunction block_function_; void* arg_; const ReadOptions options_; Status status_; IteratorWrapper index_iter_; IteratorWrapper data_iter_; // May be nullptr // If data_iter_ is non-null, then "data_block_handle_" holds the // "index_value" passed to block_function_ to create the data_iter_. std::string data_block_handle_; };
// A helper class that converts internal format keys into user keys classKeyConvertingIterator :public Iterator { public: explicitKeyConvertingIterator(Iterator* iter) : iter_(iter) {}