mirror of
https://github.com/google/leveldb.git
synced 2025-06-11 12:43:19 +08:00
Merge 2a35c6d429
into ac691084fd
This commit is contained in:
commit
26313efe16
@ -81,10 +81,61 @@ class Block::Iter : public Iterator {
|
|||||||
uint32_t const restarts_; // Offset of restart array (list of fixed32)
|
uint32_t const restarts_; // Offset of restart array (list of fixed32)
|
||||||
uint32_t const num_restarts_; // Number of uint32_t entries in restart array
|
uint32_t const num_restarts_; // Number of uint32_t entries in restart array
|
||||||
|
|
||||||
|
class IterKey {
|
||||||
|
private:
|
||||||
|
char* buf_;
|
||||||
|
size_t size_;
|
||||||
|
size_t capacity_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
IterKey()
|
||||||
|
: buf_(nullptr),
|
||||||
|
size_(0),
|
||||||
|
capacity_(0) {
|
||||||
|
}
|
||||||
|
|
||||||
|
IterKey(const IterKey&) = delete;
|
||||||
|
IterKey& operator=(const IterKey&) = delete;
|
||||||
|
|
||||||
|
~IterKey() {
|
||||||
|
delete []buf_;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append s to its back at pos. pos must be larger than current size. If
|
||||||
|
// buf_ is smaller than pos + len, buf_ would be expanded.
|
||||||
|
void TrimAppend(size_t pos, const char* s, size_t len) {
|
||||||
|
assert(pos <= size_);
|
||||||
|
|
||||||
|
if (buf_ == nullptr) {
|
||||||
|
size_ = len;
|
||||||
|
capacity_ = len;
|
||||||
|
buf_ = new char[capacity_];
|
||||||
|
|
||||||
|
memcpy(buf_, s, len);
|
||||||
|
} else {
|
||||||
|
// expand buf_
|
||||||
|
if ((pos + len) > capacity_) {
|
||||||
|
capacity_ = pos + len;
|
||||||
|
char* new_buf = new char[capacity_];
|
||||||
|
memcpy(new_buf, buf_, pos);
|
||||||
|
delete []buf_;
|
||||||
|
buf_ = new_buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_ = pos + len;
|
||||||
|
memcpy(buf_ + pos, s, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear() { size_ = 0; }
|
||||||
|
const char* data() const { return buf_; }
|
||||||
|
size_t size() const { return size_; }
|
||||||
|
};
|
||||||
|
|
||||||
// current_ is offset in data_ of current entry. >= restarts_ if !Valid
|
// current_ is offset in data_ of current entry. >= restarts_ if !Valid
|
||||||
uint32_t current_;
|
uint32_t current_;
|
||||||
uint32_t restart_index_; // Index of restart block in which current_ falls
|
uint32_t restart_index_; // Index of restart block in which current_ falls
|
||||||
std::string key_;
|
IterKey key_;
|
||||||
Slice value_;
|
Slice value_;
|
||||||
Status status_;
|
Status status_;
|
||||||
|
|
||||||
@ -128,7 +179,7 @@ class Block::Iter : public Iterator {
|
|||||||
Status status() const override { return status_; }
|
Status status() const override { return status_; }
|
||||||
Slice key() const override {
|
Slice key() const override {
|
||||||
assert(Valid());
|
assert(Valid());
|
||||||
return key_;
|
return Slice(key_.data(), key_.size());
|
||||||
}
|
}
|
||||||
Slice value() const override {
|
Slice value() const override {
|
||||||
assert(Valid());
|
assert(Valid());
|
||||||
@ -220,7 +271,7 @@ class Block::Iter : public Iterator {
|
|||||||
if (!ParseNextKey()) {
|
if (!ParseNextKey()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Compare(key_, target) >= 0) {
|
if (Compare(Slice(key_.data(), key_.size()), target) >= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -265,8 +316,7 @@ class Block::Iter : public Iterator {
|
|||||||
CorruptionError();
|
CorruptionError();
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
key_.resize(shared);
|
key_.TrimAppend(shared, p, non_shared);
|
||||||
key_.append(p, non_shared);
|
|
||||||
value_ = Slice(p + non_shared, value_length);
|
value_ = Slice(p + non_shared, value_length);
|
||||||
while (restart_index_ + 1 < num_restarts_ &&
|
while (restart_index_ + 1 < num_restarts_ &&
|
||||||
GetRestartPoint(restart_index_ + 1) < current_) {
|
GetRestartPoint(restart_index_ + 1) < current_) {
|
||||||
|
Loading…
Reference in New Issue
Block a user