leveldb
Public Member Functions | Private Member Functions | Private Attributes | List of all members
leveldb::Block::Iter Class Reference
Inheritance diagram for leveldb::Block::Iter:
Inheritance graph
[legend]
Collaboration diagram for leveldb::Block::Iter:
Collaboration graph
[legend]

Public Member Functions

 Iter (const Comparator *comparator, const char *data, uint32_t restarts, uint32_t num_restarts)
 
virtual bool Valid () const
 
virtual Status status () const
 
virtual Slice key () const
 
virtual Slice value () 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

int Compare (const Slice &a, const Slice &b) const
 
uint32_t NextEntryOffset () const
 
uint32_t GetRestartPoint (uint32_t index)
 
void SeekToRestartPoint (uint32_t index)
 
void CorruptionError ()
 
bool ParseNextKey ()
 

Private Attributes

const Comparator *const comparator_
 
const char *const data_
 
uint32_t const restarts_
 
uint32_t const num_restarts_
 
uint32_t current_
 
uint32_t restart_index_
 
std::string key_
 
Slice value_
 
Status status_
 

Additional Inherited Members

- Public Types inherited from leveldb::Iterator
typedef void(* CleanupFunction) (void *arg1, void *arg2)
 

Detailed Description

Definition at line 76 of file block.cc.

Constructor & Destructor Documentation

§ Iter()

leveldb::Block::Iter::Iter ( const Comparator comparator,
const char *  data,
uint32_t  restarts,
uint32_t  num_restarts 
)
inline

Definition at line 115 of file block.cc.

119  : comparator_(comparator),
120  data_(data),
121  restarts_(restarts),
122  num_restarts_(num_restarts),
125  assert(num_restarts_ > 0);
126  }
uint32_t current_
Definition: block.cc:84
const Comparator *const comparator_
Definition: block.cc:78
uint32_t const restarts_
Definition: block.cc:80
const char *const data_
Definition: block.cc:79
uint32_t restart_index_
Definition: block.cc:85
uint32_t const num_restarts_
Definition: block.cc:81

Member Function Documentation

§ Compare()

int leveldb::Block::Iter::Compare ( const Slice a,
const Slice b 
) const
inlineprivate

Definition at line 90 of file block.cc.

90  {
91  return comparator_->Compare(a, b);
92  }
const Comparator *const comparator_
Definition: block.cc:78
virtual int Compare(const Slice &a, const Slice &b) const =0
Here is the call graph for this function:

§ CorruptionError()

void leveldb::Block::Iter::CorruptionError ( )
inlineprivate

Definition at line 218 of file block.cc.

218  {
221  status_ = Status::Corruption("bad entry in block");
222  key_.clear();
223  value_.clear();
224  }
uint32_t current_
Definition: block.cc:84
static Status Corruption(const Slice &msg, const Slice &msg2=Slice())
Definition: status.h:38
uint32_t const restarts_
Definition: block.cc:80
uint32_t restart_index_
Definition: block.cc:85
void clear()
Definition: slice.h:56
std::string key_
Definition: block.cc:86
uint32_t const num_restarts_
Definition: block.cc:81
Here is the call graph for this function:

§ GetRestartPoint()

uint32_t leveldb::Block::Iter::GetRestartPoint ( uint32_t  index)
inlineprivate

Definition at line 99 of file block.cc.

99  {
100  assert(index < num_restarts_);
101  return DecodeFixed32(data_ + restarts_ + index * sizeof(uint32_t));
102  }
uint32_t DecodeFixed32(const char *ptr)
Definition: coding.h:58
uint32_t const restarts_
Definition: block.cc:80
const char *const data_
Definition: block.cc:79
uint32_t const num_restarts_
Definition: block.cc:81
Here is the call graph for this function:

§ key()

virtual Slice leveldb::Block::Iter::key ( ) const
inlinevirtual

Implements leveldb::Iterator.

Definition at line 130 of file block.cc.

130  {
131  assert(Valid());
132  return key_;
133  }
virtual bool Valid() const
Definition: block.cc:128
std::string key_
Definition: block.cc:86

§ Next()

virtual void leveldb::Block::Iter::Next ( )
inlinevirtual

Implements leveldb::Iterator.

Definition at line 139 of file block.cc.

139  {
140  assert(Valid());
141  ParseNextKey();
142  }
bool ParseNextKey()
Definition: block.cc:226
virtual bool Valid() const
Definition: block.cc:128

§ NextEntryOffset()

uint32_t leveldb::Block::Iter::NextEntryOffset ( ) const
inlineprivate

Definition at line 95 of file block.cc.

95  {
96  return (value_.data() + value_.size()) - data_;
97  }
const char *const data_
Definition: block.cc:79
const char * data() const
Definition: slice.h:40
size_t size() const
Definition: slice.h:43
Here is the call graph for this function:

§ ParseNextKey()

bool leveldb::Block::Iter::ParseNextKey ( )
inlineprivate

Definition at line 226 of file block.cc.

226  {
228  const char* p = data_ + current_;
229  const char* limit = data_ + restarts_; // Restarts come right after data
230  if (p >= limit) {
231  // No more entries to return. Mark as invalid.
232  current_ = restarts_;
234  return false;
235  }
236 
237  // Decode next entry
238  uint32_t shared, non_shared, value_length;
239  p = DecodeEntry(p, limit, &shared, &non_shared, &value_length);
240  if (p == NULL || key_.size() < shared) {
241  CorruptionError();
242  return false;
243  } else {
244  key_.resize(shared);
245  key_.append(p, non_shared);
246  value_ = Slice(p + non_shared, value_length);
247  while (restart_index_ + 1 < num_restarts_ &&
248  GetRestartPoint(restart_index_ + 1) < current_) {
249  ++restart_index_;
250  }
251  return true;
252  }
253  }
static const char * DecodeEntry(const char *p, const char *limit, uint32_t *shared, uint32_t *non_shared, uint32_t *value_length)
Definition: block.cc:53
uint32_t NextEntryOffset() const
Definition: block.cc:95
uint32_t current_
Definition: block.cc:84
uint32_t const restarts_
Definition: block.cc:80
const char *const data_
Definition: block.cc:79
uint32_t restart_index_
Definition: block.cc:85
void CorruptionError()
Definition: block.cc:218
std::string key_
Definition: block.cc:86
uint32_t const num_restarts_
Definition: block.cc:81
uint32_t GetRestartPoint(uint32_t index)
Definition: block.cc:99
Here is the call graph for this function:

§ Prev()

virtual void leveldb::Block::Iter::Prev ( )
inlinevirtual

Implements leveldb::Iterator.

Definition at line 144 of file block.cc.

144  {
145  assert(Valid());
146 
147  // Scan backwards to a restart point before current_
148  const uint32_t original = current_;
149  while (GetRestartPoint(restart_index_) >= original) {
150  if (restart_index_ == 0) {
151  // No more entries
154  return;
155  }
156  restart_index_--;
157  }
158 
160  do {
161  // Loop until end of current entry hits the start of original entry
162  } while (ParseNextKey() && NextEntryOffset() < original);
163  }
uint32_t NextEntryOffset() const
Definition: block.cc:95
void SeekToRestartPoint(uint32_t index)
Definition: block.cc:104
uint32_t current_
Definition: block.cc:84
bool ParseNextKey()
Definition: block.cc:226
virtual bool Valid() const
Definition: block.cc:128
uint32_t const restarts_
Definition: block.cc:80
uint32_t restart_index_
Definition: block.cc:85
uint32_t const num_restarts_
Definition: block.cc:81
uint32_t GetRestartPoint(uint32_t index)
Definition: block.cc:99

§ Seek()

virtual void leveldb::Block::Iter::Seek ( const Slice target)
inlinevirtual

Implements leveldb::Iterator.

Definition at line 165 of file block.cc.

165  {
166  // Binary search in restart array to find the last restart point
167  // with a key < target
168  uint32_t left = 0;
169  uint32_t right = num_restarts_ - 1;
170  while (left < right) {
171  uint32_t mid = (left + right + 1) / 2;
172  uint32_t region_offset = GetRestartPoint(mid);
173  uint32_t shared, non_shared, value_length;
174  const char* key_ptr = DecodeEntry(data_ + region_offset,
175  data_ + restarts_,
176  &shared, &non_shared, &value_length);
177  if (key_ptr == NULL || (shared != 0)) {
178  CorruptionError();
179  return;
180  }
181  Slice mid_key(key_ptr, non_shared);
182  if (Compare(mid_key, target) < 0) {
183  // Key at "mid" is smaller than "target". Therefore all
184  // blocks before "mid" are uninteresting.
185  left = mid;
186  } else {
187  // Key at "mid" is >= "target". Therefore all blocks at or
188  // after "mid" are uninteresting.
189  right = mid - 1;
190  }
191  }
192 
193  // Linear search (within restart block) for first key >= target
194  SeekToRestartPoint(left);
195  while (true) {
196  if (!ParseNextKey()) {
197  return;
198  }
199  if (Compare(key_, target) >= 0) {
200  return;
201  }
202  }
203  }
static const char * DecodeEntry(const char *p, const char *limit, uint32_t *shared, uint32_t *non_shared, uint32_t *value_length)
Definition: block.cc:53
void SeekToRestartPoint(uint32_t index)
Definition: block.cc:104
bool ParseNextKey()
Definition: block.cc:226
uint32_t const restarts_
Definition: block.cc:80
const char *const data_
Definition: block.cc:79
int Compare(const Slice &a, const Slice &b) const
Definition: block.cc:90
void CorruptionError()
Definition: block.cc:218
std::string key_
Definition: block.cc:86
uint32_t const num_restarts_
Definition: block.cc:81
uint32_t GetRestartPoint(uint32_t index)
Definition: block.cc:99
Here is the call graph for this function:

§ SeekToFirst()

virtual void leveldb::Block::Iter::SeekToFirst ( )
inlinevirtual

Implements leveldb::Iterator.

Definition at line 205 of file block.cc.

205  {
207  ParseNextKey();
208  }
void SeekToRestartPoint(uint32_t index)
Definition: block.cc:104
bool ParseNextKey()
Definition: block.cc:226

§ SeekToLast()

virtual void leveldb::Block::Iter::SeekToLast ( )
inlinevirtual

Implements leveldb::Iterator.

Definition at line 210 of file block.cc.

210  {
212  while (ParseNextKey() && NextEntryOffset() < restarts_) {
213  // Keep skipping
214  }
215  }
uint32_t NextEntryOffset() const
Definition: block.cc:95
void SeekToRestartPoint(uint32_t index)
Definition: block.cc:104
bool ParseNextKey()
Definition: block.cc:226
uint32_t const restarts_
Definition: block.cc:80
uint32_t const num_restarts_
Definition: block.cc:81

§ SeekToRestartPoint()

void leveldb::Block::Iter::SeekToRestartPoint ( uint32_t  index)
inlineprivate

Definition at line 104 of file block.cc.

104  {
105  key_.clear();
106  restart_index_ = index;
107  // current_ will be fixed by ParseNextKey();
108 
109  // ParseNextKey() starts at the end of value_, so set value_ accordingly
110  uint32_t offset = GetRestartPoint(index);
111  value_ = Slice(data_ + offset, 0);
112  }
const char *const data_
Definition: block.cc:79
uint32_t restart_index_
Definition: block.cc:85
std::string key_
Definition: block.cc:86
uint32_t GetRestartPoint(uint32_t index)
Definition: block.cc:99

§ status()

virtual Status leveldb::Block::Iter::status ( ) const
inlinevirtual

Implements leveldb::Iterator.

Definition at line 129 of file block.cc.

129 { return status_; }

§ Valid()

virtual bool leveldb::Block::Iter::Valid ( ) const
inlinevirtual

Implements leveldb::Iterator.

Definition at line 128 of file block.cc.

128 { return current_ < restarts_; }
uint32_t current_
Definition: block.cc:84
uint32_t const restarts_
Definition: block.cc:80

§ value()

virtual Slice leveldb::Block::Iter::value ( ) const
inlinevirtual

Implements leveldb::Iterator.

Definition at line 134 of file block.cc.

134  {
135  assert(Valid());
136  return value_;
137  }
virtual bool Valid() const
Definition: block.cc:128

Member Data Documentation

§ comparator_

const Comparator* const leveldb::Block::Iter::comparator_
private

Definition at line 78 of file block.cc.

§ current_

uint32_t leveldb::Block::Iter::current_
private

Definition at line 84 of file block.cc.

§ data_

const char* const leveldb::Block::Iter::data_
private

Definition at line 79 of file block.cc.

§ key_

std::string leveldb::Block::Iter::key_
private

Definition at line 86 of file block.cc.

§ num_restarts_

uint32_t const leveldb::Block::Iter::num_restarts_
private

Definition at line 81 of file block.cc.

§ restart_index_

uint32_t leveldb::Block::Iter::restart_index_
private

Definition at line 85 of file block.cc.

§ restarts_

uint32_t const leveldb::Block::Iter::restarts_
private

Definition at line 80 of file block.cc.

§ status_

Status leveldb::Block::Iter::status_
private

Definition at line 88 of file block.cc.

§ value_

Slice leveldb::Block::Iter::value_
private

Definition at line 87 of file block.cc.


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