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

#include <table_builder.h>

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

Classes

struct  Rep
 

Public Member Functions

 TableBuilder (const Options &options, WritableFile *file)
 
 ~TableBuilder ()
 
Status ChangeOptions (const Options &options)
 
void Add (const Slice &key, const Slice &value)
 
void Flush ()
 
Status status () const
 
Status Finish ()
 
void Abandon ()
 
uint64_t NumEntries () const
 
uint64_t FileSize () const
 

Private Member Functions

bool ok () const
 
void WriteBlock (BlockBuilder *block, BlockHandle *handle)
 
void WriteRawBlock (const Slice &data, CompressionType, BlockHandle *handle)
 
 TableBuilder (const TableBuilder &)
 
void operator= (const TableBuilder &)
 

Private Attributes

Reprep_
 

Detailed Description

Definition at line 26 of file table_builder.h.

Constructor & Destructor Documentation

§ TableBuilder() [1/2]

leveldb::TableBuilder::TableBuilder ( const Options options,
WritableFile file 
)

Definition at line 63 of file table_builder.cc.

64  : rep_(new Rep(options, file)) {
65  if (rep_->filter_block != NULL) {
67  }
68 }
void StartBlock(uint64_t block_offset)
Definition: filter_block.cc:22
FilterBlockBuilder * filter_block
Here is the call graph for this function:

§ ~TableBuilder()

leveldb::TableBuilder::~TableBuilder ( )

Definition at line 70 of file table_builder.cc.

70  {
71  assert(rep_->closed); // Catch errors where caller forgot to call Finish()
72  delete rep_->filter_block;
73  delete rep_;
74 }
FilterBlockBuilder * filter_block

§ TableBuilder() [2/2]

leveldb::TableBuilder::TableBuilder ( const TableBuilder )
private

Member Function Documentation

§ Abandon()

void leveldb::TableBuilder::Abandon ( )

Definition at line 256 of file table_builder.cc.

256  {
257  Rep* r = rep_;
258  assert(!r->closed);
259  r->closed = true;
260 }
Here is the caller graph for this function:

§ Add()

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

Definition at line 92 of file table_builder.cc.

92  {
93  Rep* r = rep_;
94  assert(!r->closed);
95  if (!ok()) return;
96  if (r->num_entries > 0) {
97  assert(r->options.comparator->Compare(key, Slice(r->last_key)) > 0);
98  }
99 
100  if (r->pending_index_entry) {
101  assert(r->data_block.empty());
102  r->options.comparator->FindShortestSeparator(&r->last_key, key);
103  std::string handle_encoding;
104  r->pending_handle.EncodeTo(&handle_encoding);
105  r->index_block.Add(r->last_key, Slice(handle_encoding));
106  r->pending_index_entry = false;
107  }
108 
109  if (r->filter_block != NULL) {
110  r->filter_block->AddKey(key);
111  }
112 
113  r->last_key.assign(key.data(), key.size());
114  r->num_entries++;
115  r->data_block.Add(key, value);
116 
117  const size_t estimated_block_size = r->data_block.CurrentSizeEstimate();
118  if (estimated_block_size >= r->options.block_size) {
119  Flush();
120  }
121 }
Here is the call graph for this function:
Here is the caller graph for this function:

§ ChangeOptions()

Status leveldb::TableBuilder::ChangeOptions ( const Options options)

Definition at line 76 of file table_builder.cc.

76  {
77  // Note: if more fields are added to Options, update
78  // this function to catch changes that should not be allowed to
79  // change in the middle of building a Table.
80  if (options.comparator != rep_->options.comparator) {
81  return Status::InvalidArgument("changing comparator while building table");
82  }
83 
84  // Note that any live BlockBuilders point to rep_->options and therefore
85  // will automatically pick up the updated options.
86  rep_->options = options;
87  rep_->index_block_options = options;
89  return Status::OK();
90 }
int block_restart_interval
Definition: options.h:113
static Status OK()
Definition: status.h:32
const Comparator * comparator
Definition: options.h:41
static Status InvalidArgument(const Slice &msg, const Slice &msg2=Slice())
Definition: status.h:44
Here is the call graph for this function:

§ FileSize()

uint64_t leveldb::TableBuilder::FileSize ( ) const

Definition at line 266 of file table_builder.cc.

266  {
267  return rep_->offset;
268 }
Here is the caller graph for this function:

§ Finish()

Status leveldb::TableBuilder::Finish ( )

Definition at line 199 of file table_builder.cc.

199  {
200  Rep* r = rep_;
201  Flush();
202  assert(!r->closed);
203  r->closed = true;
204 
205  BlockHandle filter_block_handle, metaindex_block_handle, index_block_handle;
206 
207  // Write filter block
208  if (ok() && r->filter_block != NULL) {
209  WriteRawBlock(r->filter_block->Finish(), kNoCompression,
210  &filter_block_handle);
211  }
212 
213  // Write metaindex block
214  if (ok()) {
215  BlockBuilder meta_index_block(&r->options);
216  if (r->filter_block != NULL) {
217  // Add mapping from "filter.Name" to location of filter data
218  std::string key = "filter.";
219  key.append(r->options.filter_policy->Name());
220  std::string handle_encoding;
221  filter_block_handle.EncodeTo(&handle_encoding);
222  meta_index_block.Add(key, handle_encoding);
223  }
224 
225  // TODO(postrelease): Add stats and other meta blocks
226  WriteBlock(&meta_index_block, &metaindex_block_handle);
227  }
228 
229  // Write index block
230  if (ok()) {
231  if (r->pending_index_entry) {
232  r->options.comparator->FindShortSuccessor(&r->last_key);
233  std::string handle_encoding;
234  r->pending_handle.EncodeTo(&handle_encoding);
235  r->index_block.Add(r->last_key, Slice(handle_encoding));
236  r->pending_index_entry = false;
237  }
238  WriteBlock(&r->index_block, &index_block_handle);
239  }
240 
241  // Write footer
242  if (ok()) {
243  Footer footer;
244  footer.set_metaindex_handle(metaindex_block_handle);
245  footer.set_index_handle(index_block_handle);
246  std::string footer_encoding;
247  footer.EncodeTo(&footer_encoding);
248  r->status = r->file->Append(footer_encoding);
249  if (r->status.ok()) {
250  r->offset += footer_encoding.size();
251  }
252  }
253  return r->status;
254 }
void WriteRawBlock(const Slice &data, CompressionType, BlockHandle *handle)
void WriteBlock(BlockBuilder *block, BlockHandle *handle)
Here is the call graph for this function:
Here is the caller graph for this function:

§ Flush()

void leveldb::TableBuilder::Flush ( )

Definition at line 123 of file table_builder.cc.

123  {
124  Rep* r = rep_;
125  assert(!r->closed);
126  if (!ok()) return;
127  if (r->data_block.empty()) return;
128  assert(!r->pending_index_entry);
129  WriteBlock(&r->data_block, &r->pending_handle);
130  if (ok()) {
131  r->pending_index_entry = true;
132  r->status = r->file->Flush();
133  }
134  if (r->filter_block != NULL) {
135  r->filter_block->StartBlock(r->offset);
136  }
137 }
void WriteBlock(BlockBuilder *block, BlockHandle *handle)
Here is the call graph for this function:
Here is the caller graph for this function:

§ NumEntries()

uint64_t leveldb::TableBuilder::NumEntries ( ) const

Definition at line 262 of file table_builder.cc.

262  {
263  return rep_->num_entries;
264 }
Here is the caller graph for this function:

§ ok()

bool leveldb::TableBuilder::ok ( ) const
inlineprivate

Definition at line 78 of file table_builder.h.

78 { return status().ok(); }
Status status() const
bool ok() const
Definition: status.h:52
Here is the call graph for this function:
Here is the caller graph for this function:

§ operator=()

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

§ status()

Status leveldb::TableBuilder::status ( ) const

Definition at line 195 of file table_builder.cc.

195  {
196  return rep_->status;
197 }
Here is the caller graph for this function:

§ WriteBlock()

void leveldb::TableBuilder::WriteBlock ( BlockBuilder block,
BlockHandle handle 
)
private

Definition at line 139 of file table_builder.cc.

139  {
140  // File format contains a sequence of blocks where each block has:
141  // block_data: uint8[n]
142  // type: uint8
143  // crc: uint32
144  assert(ok());
145  Rep* r = rep_;
146  Slice raw = block->Finish();
147 
148  Slice block_contents;
149  CompressionType type = r->options.compression;
150  // TODO(postrelease): Support more compression options: zlib?
151  switch (type) {
152  case kNoCompression:
153  block_contents = raw;
154  break;
155 
156  case kSnappyCompression: {
157  std::string* compressed = &r->compressed_output;
158  if (port::Snappy_Compress(raw.data(), raw.size(), compressed) &&
159  compressed->size() < raw.size() - (raw.size() / 8u)) {
160  block_contents = *compressed;
161  } else {
162  // Snappy not supported, or compressed less than 12.5%, so just
163  // store uncompressed form
164  block_contents = raw;
165  type = kNoCompression;
166  }
167  break;
168  }
169  }
170  WriteRawBlock(block_contents, type, handle);
171  r->compressed_output.clear();
172  block->Reset();
173 }
void WriteRawBlock(const Slice &data, CompressionType, BlockHandle *handle)
CompressionType
Definition: options.h:23
Here is the call graph for this function:
Here is the caller graph for this function:

§ WriteRawBlock()

void leveldb::TableBuilder::WriteRawBlock ( const Slice data,
CompressionType  type,
BlockHandle handle 
)
private

Definition at line 175 of file table_builder.cc.

177  {
178  Rep* r = rep_;
179  handle->set_offset(r->offset);
180  handle->set_size(block_contents.size());
181  r->status = r->file->Append(block_contents);
182  if (r->status.ok()) {
183  char trailer[kBlockTrailerSize];
184  trailer[0] = type;
185  uint32_t crc = crc32c::Value(block_contents.data(), block_contents.size());
186  crc = crc32c::Extend(crc, trailer, 1); // Extend crc to cover block type
187  EncodeFixed32(trailer+1, crc32c::Mask(crc));
188  r->status = r->file->Append(Slice(trailer, kBlockTrailerSize));
189  if (r->status.ok()) {
190  r->offset += block_contents.size() + kBlockTrailerSize;
191  }
192  }
193 }
void EncodeFixed32(char *buf, uint32_t value)
Definition: coding.cc:9
uint32_t Mask(uint32_t crc)
Definition: crc32c.h:31
uint32_t Value(const char *data, size_t n)
Definition: crc32c.h:20
static const size_t kBlockTrailerSize
Definition: format.h:84
uint32_t Extend(uint32_t crc, const char *buf, size_t size)
Definition: crc32c.cc:286
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

§ rep_

Rep* leveldb::TableBuilder::rep_
private

Definition at line 82 of file table_builder.h.


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