19 assert(
size_ >=
sizeof(uint32_t));
24 :
data_(contents.data.data()),
26 owned_(contents.heap_allocated) {
27 if (
size_ <
sizeof(uint32_t)) {
30 size_t max_restarts_allowed = (
size_-
sizeof(uint32_t)) /
sizeof(uint32_t);
53 static inline const char*
DecodeEntry(
const char* p,
const char* limit,
56 uint32_t* value_length) {
57 if (limit - p < 3)
return NULL;
58 *shared =
reinterpret_cast<const unsigned char*
>(p)[0];
59 *non_shared =
reinterpret_cast<const unsigned char*
>(p)[1];
60 *value_length =
reinterpret_cast<const unsigned char*
>(p)[2];
61 if ((*shared | *non_shared | *value_length) < 128) {
66 if ((p =
GetVarint32Ptr(p, limit, non_shared)) == NULL)
return NULL;
67 if ((p =
GetVarint32Ptr(p, limit, value_length)) == NULL)
return NULL;
70 if (static_cast<uint32_t>(limit - p) < (*non_shared + *value_length)) {
91 return comparator_->
Compare(a, b);
96 return (value_.
data() + value_.
size()) - data_;
100 assert(index < num_restarts_);
101 return DecodeFixed32(data_ + restarts_ + index *
sizeof(uint32_t));
106 restart_index_ = index;
110 uint32_t offset = GetRestartPoint(index);
111 value_ =
Slice(data_ + offset, 0);
118 uint32_t num_restarts)
119 : comparator_(comparator),
122 num_restarts_(num_restarts),
124 restart_index_(num_restarts_) {
125 assert(num_restarts_ > 0);
128 virtual bool Valid()
const {
return current_ < restarts_; }
148 const uint32_t original = current_;
149 while (GetRestartPoint(restart_index_) >= original) {
150 if (restart_index_ == 0) {
152 current_ = restarts_;
153 restart_index_ = num_restarts_;
159 SeekToRestartPoint(restart_index_);
162 }
while (ParseNextKey() && NextEntryOffset() < original);
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,
176 &shared, &non_shared, &value_length);
177 if (key_ptr == NULL || (shared != 0)) {
181 Slice mid_key(key_ptr, non_shared);
182 if (Compare(mid_key, target) < 0) {
194 SeekToRestartPoint(left);
196 if (!ParseNextKey()) {
199 if (Compare(key_, target) >= 0) {
206 SeekToRestartPoint(0);
211 SeekToRestartPoint(num_restarts_ - 1);
212 while (ParseNextKey() && NextEntryOffset() < restarts_) {
219 current_ = restarts_;
220 restart_index_ = num_restarts_;
227 current_ = NextEntryOffset();
228 const char* p = data_ + current_;
229 const char* limit = data_ + restarts_;
232 current_ = restarts_;
233 restart_index_ = num_restarts_;
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) {
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_) {
257 if (
size_ <
sizeof(uint32_t)) {
261 if (num_restarts == 0) {
static const char * DecodeEntry(const char *p, const char *limit, uint32_t *shared, uint32_t *non_shared, uint32_t *value_length)
virtual Slice value() const
uint32_t NextEntryOffset() const
virtual Slice key() const
const char * GetVarint32Ptr(const char *p, const char *limit, uint32_t *v)
uint32_t NumRestarts() const
virtual Status status() const
uint32_t DecodeFixed32(const char *ptr)
void SeekToRestartPoint(uint32_t index)
Iterator * NewIterator(const Comparator *comparator)
static Status Corruption(const Slice &msg, const Slice &msg2=Slice())
const Comparator *const comparator_
Iterator * NewErrorIterator(const Status &status)
virtual bool Valid() const
virtual void SeekToFirst()
Iterator * NewEmptyIterator()
virtual void Seek(const Slice &target)
virtual void SeekToLast()
int Compare(const Slice &a, const Slice &b) const
Iter(const Comparator *comparator, const char *data, uint32_t restarts, uint32_t num_restarts)
virtual int Compare(const Slice &a, const Slice &b) const =0
const char * data() const
uint32_t const num_restarts_
Block(const BlockContents &contents)
uint32_t GetRestartPoint(uint32_t index)