leveldb
Public Member Functions | Private Member Functions | Private Attributes | List of all members
leveldb::BlockBuilder Class Reference

#include <block_builder.h>

Collaboration diagram for leveldb::BlockBuilder:
Collaboration graph
[legend]

Public Member Functions

 BlockBuilder (const Options *options)
 
void Reset ()
 
void Add (const Slice &key, const Slice &value)
 
Slice Finish ()
 
size_t CurrentSizeEstimate () const
 
bool empty () const
 

Private Member Functions

 BlockBuilder (const BlockBuilder &)
 
void operator= (const BlockBuilder &)
 

Private Attributes

const Optionsoptions_
 
std::string buffer_
 
std::vector< uint32_t > restarts_
 
int counter_
 
bool finished_
 
std::string last_key_
 

Detailed Description

Definition at line 17 of file block_builder.h.

Constructor & Destructor Documentation

§ BlockBuilder() [1/2]

leveldb::BlockBuilder::BlockBuilder ( const Options options)
explicit

Definition at line 39 of file block_builder.cc.

40  : options_(options),
41  restarts_(),
42  counter_(0),
43  finished_(false) {
44  assert(options->block_restart_interval >= 1);
45  restarts_.push_back(0); // First restart point is at offset 0
46 }
std::vector< uint32_t > restarts_
Definition: block_builder.h:45
const Options * options_
Definition: block_builder.h:43

§ BlockBuilder() [2/2]

leveldb::BlockBuilder::BlockBuilder ( const BlockBuilder )
private

Member Function Documentation

§ Add()

void leveldb::BlockBuilder::Add ( const Slice key,
const Slice value 
)

Definition at line 73 of file block_builder.cc.

73  {
74  Slice last_key_piece(last_key_);
75  assert(!finished_);
76  assert(counter_ <= options_->block_restart_interval);
77  assert(buffer_.empty() // No values yet?
78  || options_->comparator->Compare(key, last_key_piece) > 0);
79  size_t shared = 0;
80  if (counter_ < options_->block_restart_interval) {
81  // See how much sharing to do with previous string
82  const size_t min_length = std::min(last_key_piece.size(), key.size());
83  while ((shared < min_length) && (last_key_piece[shared] == key[shared])) {
84  shared++;
85  }
86  } else {
87  // Restart compression
88  restarts_.push_back(buffer_.size());
89  counter_ = 0;
90  }
91  const size_t non_shared = key.size() - shared;
92 
93  // Add "<shared><non_shared><value_size>" to buffer_
94  PutVarint32(&buffer_, shared);
95  PutVarint32(&buffer_, non_shared);
96  PutVarint32(&buffer_, value.size());
97 
98  // Add string delta to buffer_ followed by value
99  buffer_.append(key.data() + shared, non_shared);
100  buffer_.append(value.data(), value.size());
101 
102  // Update state
103  last_key_.resize(shared);
104  last_key_.append(key.data() + shared, non_shared);
105  assert(Slice(last_key_) == key);
106  counter_++;
107 }
std::vector< uint32_t > restarts_
Definition: block_builder.h:45
const Options * options_
Definition: block_builder.h:43
const Comparator * comparator
Definition: options.h:41
virtual int Compare(const Slice &a, const Slice &b) const =0
void PutVarint32(std::string *dst, uint32_t v)
Definition: coding.cc:75
Here is the call graph for this function:
Here is the caller graph for this function:

§ CurrentSizeEstimate()

size_t leveldb::BlockBuilder::CurrentSizeEstimate ( ) const

Definition at line 57 of file block_builder.cc.

57  {
58  return (buffer_.size() + // Raw data buffer
59  restarts_.size() * sizeof(uint32_t) + // Restart array
60  sizeof(uint32_t)); // Restart array length
61 }
std::vector< uint32_t > restarts_
Definition: block_builder.h:45
Here is the caller graph for this function:

§ empty()

bool leveldb::BlockBuilder::empty ( ) const
inline

Definition at line 38 of file block_builder.h.

38  {
39  return buffer_.empty();
40  }
Here is the caller graph for this function:

§ Finish()

Slice leveldb::BlockBuilder::Finish ( )

Definition at line 63 of file block_builder.cc.

63  {
64  // Append restart array
65  for (size_t i = 0; i < restarts_.size(); i++) {
67  }
68  PutFixed32(&buffer_, restarts_.size());
69  finished_ = true;
70  return Slice(buffer_);
71 }
void PutFixed32(std::string *dst, uint32_t value)
Definition: coding.cc:35
std::vector< uint32_t > restarts_
Definition: block_builder.h:45
Here is the call graph for this function:
Here is the caller graph for this function:

§ operator=()

void leveldb::BlockBuilder::operator= ( const BlockBuilder )
private

§ Reset()

void leveldb::BlockBuilder::Reset ( )

Definition at line 48 of file block_builder.cc.

48  {
49  buffer_.clear();
50  restarts_.clear();
51  restarts_.push_back(0); // First restart point is at offset 0
52  counter_ = 0;
53  finished_ = false;
54  last_key_.clear();
55 }
std::vector< uint32_t > restarts_
Definition: block_builder.h:45
Here is the caller graph for this function:

Member Data Documentation

§ buffer_

std::string leveldb::BlockBuilder::buffer_
private

Definition at line 44 of file block_builder.h.

§ counter_

int leveldb::BlockBuilder::counter_
private

Definition at line 46 of file block_builder.h.

§ finished_

bool leveldb::BlockBuilder::finished_
private

Definition at line 47 of file block_builder.h.

§ last_key_

std::string leveldb::BlockBuilder::last_key_
private

Definition at line 48 of file block_builder.h.

§ options_

const Options* leveldb::BlockBuilder::options_
private

Definition at line 43 of file block_builder.h.

§ restarts_

std::vector<uint32_t> leveldb::BlockBuilder::restarts_
private

Definition at line 45 of file block_builder.h.


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