leveldb
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
leveldb::anonymous_namespace{db_iter.cc}::DBIter Class Reference
Inheritance diagram for leveldb::anonymous_namespace{db_iter.cc}::DBIter:
Inheritance graph
[legend]
Collaboration diagram for leveldb::anonymous_namespace{db_iter.cc}::DBIter:
Collaboration graph
[legend]

Public Types

enum  Direction { kForward, kReverse }
 
- Public Types inherited from leveldb::Iterator
typedef void(* CleanupFunction) (void *arg1, void *arg2)
 

Public Member Functions

 DBIter (DBImpl *db, const Comparator *cmp, Iterator *iter, SequenceNumber s, uint32_t seed)
 
virtual ~DBIter ()
 
virtual bool Valid () const
 
virtual Slice key () const
 
virtual Slice value () const
 
virtual Status status () const
 
virtual void Next ()
 
virtual void Prev ()
 
virtual void Seek (const Slice &target)
 
virtual void SeekToFirst ()
 
virtual void SeekToLast ()
 
- Public Member Functions inherited from leveldb::Iterator
 Iterator ()
 
virtual ~Iterator ()
 
void RegisterCleanup (CleanupFunction function, void *arg1, void *arg2)
 

Private Member Functions

void FindNextUserEntry (bool skipping, std::string *skip)
 
void FindPrevUserEntry ()
 
bool ParseKey (ParsedInternalKey *key)
 
void SaveKey (const Slice &k, std::string *dst)
 
void ClearSavedValue ()
 
ssize_t RandomPeriod ()
 
 DBIter (const DBIter &)
 
void operator= (const DBIter &)
 

Private Attributes

DBImpldb_
 
const Comparator *const user_comparator_
 
Iterator *const iter_
 
SequenceNumber const sequence_
 
Status status_
 
std::string saved_key_
 
std::string saved_value_
 
Direction direction_
 
bool valid_
 
Random rnd_
 
ssize_t bytes_counter_
 

Detailed Description

Definition at line 39 of file db_iter.cc.

Member Enumeration Documentation

§ Direction

enum leveldb::anonymous_namespace{db_iter.cc}::DBIter::Direction
Enumerator
kForward 
kReverse 

Definition at line 46 of file db_iter.cc.

Constructor & Destructor Documentation

§ DBIter() [1/2]

leveldb::anonymous_namespace{db_iter.cc}::DBIter::DBIter ( DBImpl db,
const Comparator cmp,
Iterator iter,
SequenceNumber  s,
uint32_t  seed 
)
inline

§ ~DBIter()

virtual leveldb::anonymous_namespace{db_iter.cc}::DBIter::~DBIter ( )
inlinevirtual

Definition at line 62 of file db_iter.cc.

62  {
63  delete iter_;
64  }

§ DBIter() [2/2]

leveldb::anonymous_namespace{db_iter.cc}::DBIter::DBIter ( const DBIter )
private

Member Function Documentation

§ ClearSavedValue()

void leveldb::anonymous_namespace{db_iter.cc}::DBIter::ClearSavedValue ( )
inlineprivate

Definition at line 97 of file db_iter.cc.

97  {
98  if (saved_value_.capacity() > 1048576) {
99  std::string empty;
100  swap(empty, saved_value_);
101  } else {
102  saved_value_.clear();
103  }
104  }

§ FindNextUserEntry()

void leveldb::anonymous_namespace{db_iter.cc}::DBIter::FindNextUserEntry ( bool  skipping,
std::string *  skip 
)
private

Definition at line 173 of file db_iter.cc.

173  {
174  // Loop until we hit an acceptable entry to yield
175  assert(iter_->Valid());
176  assert(direction_ == kForward);
177  do {
178  ParsedInternalKey ikey;
179  if (ParseKey(&ikey) && ikey.sequence <= sequence_) {
180  switch (ikey.type) {
181  case kTypeDeletion:
182  // Arrange to skip all upcoming entries for this key since
183  // they are hidden by this deletion.
184  SaveKey(ikey.user_key, skip);
185  skipping = true;
186  break;
187  case kTypeValue:
188  if (skipping &&
189  user_comparator_->Compare(ikey.user_key, *skip) <= 0) {
190  // Entry hidden
191  } else {
192  valid_ = true;
193  saved_key_.clear();
194  return;
195  }
196  break;
197  }
198  }
199  iter_->Next();
200  } while (iter_->Valid());
201  saved_key_.clear();
202  valid_ = false;
203 }
bool ParseKey(ParsedInternalKey *key)
Definition: db_iter.cc:130
virtual void Next()=0
void SaveKey(const Slice &k, std::string *dst)
Definition: db_iter.cc:93
virtual int Compare(const Slice &a, const Slice &b) const =0
virtual bool Valid() const =0

§ FindPrevUserEntry()

void leveldb::anonymous_namespace{db_iter.cc}::DBIter::FindPrevUserEntry ( )
private

Definition at line 232 of file db_iter.cc.

232  {
233  assert(direction_ == kReverse);
234 
235  ValueType value_type = kTypeDeletion;
236  if (iter_->Valid()) {
237  do {
238  ParsedInternalKey ikey;
239  if (ParseKey(&ikey) && ikey.sequence <= sequence_) {
240  if ((value_type != kTypeDeletion) &&
241  user_comparator_->Compare(ikey.user_key, saved_key_) < 0) {
242  // We encountered a non-deleted value in entries for previous keys,
243  break;
244  }
245  value_type = ikey.type;
246  if (value_type == kTypeDeletion) {
247  saved_key_.clear();
248  ClearSavedValue();
249  } else {
250  Slice raw_value = iter_->value();
251  if (saved_value_.capacity() > raw_value.size() + 1048576) {
252  std::string empty;
253  swap(empty, saved_value_);
254  }
256  saved_value_.assign(raw_value.data(), raw_value.size());
257  }
258  }
259  iter_->Prev();
260  } while (iter_->Valid());
261  }
262 
263  if (value_type == kTypeDeletion) {
264  // End
265  valid_ = false;
266  saved_key_.clear();
267  ClearSavedValue();
269  } else {
270  valid_ = true;
271  }
272 }
bool ParseKey(ParsedInternalKey *key)
Definition: db_iter.cc:130
virtual Slice key() const =0
virtual Slice value() const =0
ValueType
Definition: dbformat.h:51
virtual void Prev()=0
void SaveKey(const Slice &k, std::string *dst)
Definition: db_iter.cc:93
Slice ExtractUserKey(const Slice &internal_key)
Definition: dbformat.h:98
virtual int Compare(const Slice &a, const Slice &b) const =0
virtual bool Valid() const =0
Here is the call graph for this function:

§ key()

virtual Slice leveldb::anonymous_namespace{db_iter.cc}::DBIter::key ( ) const
inlinevirtual

Implements leveldb::Iterator.

Definition at line 66 of file db_iter.cc.

Here is the call graph for this function:

§ Next()

void leveldb::anonymous_namespace{db_iter.cc}::DBIter::Next ( )
virtual

Implements leveldb::Iterator.

Definition at line 146 of file db_iter.cc.

146  {
147  assert(valid_);
148 
149  if (direction_ == kReverse) { // Switch directions?
151  // iter_ is pointing just before the entries for this->key(),
152  // so advance into the range of entries for this->key() and then
153  // use the normal skipping code below.
154  if (!iter_->Valid()) {
155  iter_->SeekToFirst();
156  } else {
157  iter_->Next();
158  }
159  if (!iter_->Valid()) {
160  valid_ = false;
161  saved_key_.clear();
162  return;
163  }
164  // saved_key_ already contains the key to skip past.
165  } else {
166  // Store in saved_key_ the current key so we skip it below.
168  }
169 
171 }
virtual Slice key() const =0
virtual void SeekToFirst()=0
virtual void Next()=0
void SaveKey(const Slice &k, std::string *dst)
Definition: db_iter.cc:93
Slice ExtractUserKey(const Slice &internal_key)
Definition: dbformat.h:98
virtual bool Valid() const =0
void FindNextUserEntry(bool skipping, std::string *skip)
Definition: db_iter.cc:173
Here is the call graph for this function:

§ operator=()

void leveldb::anonymous_namespace{db_iter.cc}::DBIter::operator= ( const DBIter )
private

§ ParseKey()

bool leveldb::anonymous_namespace{db_iter.cc}::DBIter::ParseKey ( ParsedInternalKey key)
inlineprivate

Definition at line 130 of file db_iter.cc.

130  {
131  Slice k = iter_->key();
132  ssize_t n = k.size() + iter_->value().size();
133  bytes_counter_ -= n;
134  while (bytes_counter_ < 0) {
136  db_->RecordReadSample(k);
137  }
138  if (!ParseInternalKey(k, ikey)) {
139  status_ = Status::Corruption("corrupted internal key in DBIter");
140  return false;
141  } else {
142  return true;
143  }
144 }
virtual Slice key() const =0
virtual Slice value() const =0
void RecordReadSample(Slice key)
Definition: db_impl.cc:1168
bool ParseInternalKey(const Slice &internal_key, ParsedInternalKey *result)
Definition: dbformat.h:176
static Status Corruption(const Slice &msg, const Slice &msg2=Slice())
Definition: status.h:38
size_t size() const
Definition: slice.h:43
Here is the call graph for this function:

§ Prev()

void leveldb::anonymous_namespace{db_iter.cc}::DBIter::Prev ( )
virtual

Implements leveldb::Iterator.

Definition at line 205 of file db_iter.cc.

205  {
206  assert(valid_);
207 
208  if (direction_ == kForward) { // Switch directions?
209  // iter_ is pointing at the current entry. Scan backwards until
210  // the key changes so we can use the normal reverse scanning code.
211  assert(iter_->Valid()); // Otherwise valid_ would have been false
213  while (true) {
214  iter_->Prev();
215  if (!iter_->Valid()) {
216  valid_ = false;
217  saved_key_.clear();
218  ClearSavedValue();
219  return;
220  }
222  saved_key_) < 0) {
223  break;
224  }
225  }
227  }
228 
230 }
virtual Slice key() const =0
virtual void Prev()=0
void SaveKey(const Slice &k, std::string *dst)
Definition: db_iter.cc:93
Slice ExtractUserKey(const Slice &internal_key)
Definition: dbformat.h:98
virtual int Compare(const Slice &a, const Slice &b) const =0
virtual bool Valid() const =0
Here is the call graph for this function:

§ RandomPeriod()

ssize_t leveldb::anonymous_namespace{db_iter.cc}::DBIter::RandomPeriod ( )
inlineprivate

Definition at line 107 of file db_iter.cc.

107  {
109  }
uint32_t Uniform(int n)
Definition: random.h:48
static const int kReadBytesPeriod
Definition: dbformat.h:42

§ SaveKey()

void leveldb::anonymous_namespace{db_iter.cc}::DBIter::SaveKey ( const Slice k,
std::string *  dst 
)
inlineprivate

Definition at line 93 of file db_iter.cc.

93  {
94  dst->assign(k.data(), k.size());
95  }
Here is the call graph for this function:

§ Seek()

void leveldb::anonymous_namespace{db_iter.cc}::DBIter::Seek ( const Slice target)
virtual

Implements leveldb::Iterator.

Definition at line 274 of file db_iter.cc.

274  {
276  ClearSavedValue();
277  saved_key_.clear();
279  &saved_key_, ParsedInternalKey(target, sequence_, kValueTypeForSeek));
281  if (iter_->Valid()) {
282  FindNextUserEntry(false, &saved_key_ /* temporary storage */);
283  } else {
284  valid_ = false;
285  }
286 }
virtual void Seek(const Slice &target)=0
void AppendInternalKey(std::string *result, const ParsedInternalKey &key)
Definition: dbformat.cc:18
static const ValueType kValueTypeForSeek
Definition: dbformat.h:61
virtual bool Valid() const =0
void FindNextUserEntry(bool skipping, std::string *skip)
Definition: db_iter.cc:173
Here is the call graph for this function:

§ SeekToFirst()

void leveldb::anonymous_namespace{db_iter.cc}::DBIter::SeekToFirst ( )
virtual

Implements leveldb::Iterator.

Definition at line 288 of file db_iter.cc.

288  {
290  ClearSavedValue();
291  iter_->SeekToFirst();
292  if (iter_->Valid()) {
293  FindNextUserEntry(false, &saved_key_ /* temporary storage */);
294  } else {
295  valid_ = false;
296  }
297 }
virtual void SeekToFirst()=0
virtual bool Valid() const =0
void FindNextUserEntry(bool skipping, std::string *skip)
Definition: db_iter.cc:173

§ SeekToLast()

void leveldb::anonymous_namespace{db_iter.cc}::DBIter::SeekToLast ( )
virtual

§ status()

virtual Status leveldb::anonymous_namespace{db_iter.cc}::DBIter::status ( ) const
inlinevirtual

Implements leveldb::Iterator.

Definition at line 74 of file db_iter.cc.

74  {
75  if (status_.ok()) {
76  return iter_->status();
77  } else {
78  return status_;
79  }
80  }
virtual Status status() const =0
bool ok() const
Definition: status.h:52

§ Valid()

virtual bool leveldb::anonymous_namespace{db_iter.cc}::DBIter::Valid ( ) const
inlinevirtual

Implements leveldb::Iterator.

Definition at line 65 of file db_iter.cc.

§ value()

virtual Slice leveldb::anonymous_namespace{db_iter.cc}::DBIter::value ( ) const
inlinevirtual

Member Data Documentation

§ bytes_counter_

ssize_t leveldb::anonymous_namespace{db_iter.cc}::DBIter::bytes_counter_
private

Definition at line 123 of file db_iter.cc.

§ db_

DBImpl* leveldb::anonymous_namespace{db_iter.cc}::DBIter::db_
private

Definition at line 111 of file db_iter.cc.

§ direction_

Direction leveldb::anonymous_namespace{db_iter.cc}::DBIter::direction_
private

Definition at line 119 of file db_iter.cc.

§ iter_

Iterator* const leveldb::anonymous_namespace{db_iter.cc}::DBIter::iter_
private

Definition at line 113 of file db_iter.cc.

§ rnd_

Random leveldb::anonymous_namespace{db_iter.cc}::DBIter::rnd_
private

Definition at line 122 of file db_iter.cc.

§ saved_key_

std::string leveldb::anonymous_namespace{db_iter.cc}::DBIter::saved_key_
private

Definition at line 117 of file db_iter.cc.

§ saved_value_

std::string leveldb::anonymous_namespace{db_iter.cc}::DBIter::saved_value_
private

Definition at line 118 of file db_iter.cc.

§ sequence_

SequenceNumber const leveldb::anonymous_namespace{db_iter.cc}::DBIter::sequence_
private

Definition at line 114 of file db_iter.cc.

§ status_

Status leveldb::anonymous_namespace{db_iter.cc}::DBIter::status_
private

Definition at line 116 of file db_iter.cc.

§ user_comparator_

const Comparator* const leveldb::anonymous_namespace{db_iter.cc}::DBIter::user_comparator_
private

Definition at line 112 of file db_iter.cc.

§ valid_

bool leveldb::anonymous_namespace{db_iter.cc}::DBIter::valid_
private

Definition at line 120 of file db_iter.cc.


The documentation for this class was generated from the following file: