小组成员:谢瑞阳、徐翔宇
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 rivejä
7.9 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include "leveldb/table_builder.h"
  5. #include <assert.h>
  6. #include "leveldb/comparator.h"
  7. #include "leveldb/env.h"
  8. #include "leveldb/filter_policy.h"
  9. #include "leveldb/options.h"
  10. #include "table/block_builder.h"
  11. #include "table/filter_block.h"
  12. #include "table/format.h"
  13. #include "util/coding.h"
  14. #include "util/crc32c.h"
  15. namespace leveldb {
  16. struct TableBuilder::Rep {
  17. Rep(const Options& opt, WritableFile* f)
  18. : options(opt),
  19. index_block_options(opt),
  20. file(f),
  21. offset(0),
  22. data_block(&options),
  23. index_block(&index_block_options),
  24. num_entries(0),
  25. closed(false),
  26. filter_block(opt.filter_policy == nullptr
  27. ? nullptr
  28. : new FilterBlockBuilder(opt.filter_policy)),
  29. pending_index_entry(false) {
  30. index_block_options.block_restart_interval = 1;
  31. }
  32. Options options;
  33. Options index_block_options;
  34. WritableFile* file;
  35. uint64_t offset;
  36. Status status;
  37. BlockBuilder data_block;
  38. BlockBuilder index_block;
  39. std::string last_key;
  40. int64_t num_entries;
  41. bool closed; // Either Finish() or Abandon() has been called.
  42. FilterBlockBuilder* filter_block;
  43. // We do not emit the index entry for a block until we have seen the
  44. // first key for the next data block. This allows us to use shorter
  45. // keys in the index block. For example, consider a block boundary
  46. // between the keys "the quick brown fox" and "the who". We can use
  47. // "the r" as the key for the index block entry since it is >= all
  48. // entries in the first block and < all entries in subsequent
  49. // blocks.
  50. //
  51. // Invariant: r->pending_index_entry is true only if data_block is empty.
  52. bool pending_index_entry;
  53. BlockHandle pending_handle; // Handle to add to index block
  54. std::string compressed_output;
  55. };
  56. TableBuilder::TableBuilder(const Options& options, WritableFile* file)
  57. : rep_(new Rep(options, file)) {
  58. if (rep_->filter_block != nullptr) {
  59. rep_->filter_block->StartBlock(0);
  60. }
  61. }
  62. TableBuilder::~TableBuilder() {
  63. assert(rep_->closed); // Catch errors where caller forgot to call Finish()
  64. delete rep_->filter_block;
  65. delete rep_;
  66. }
  67. Status TableBuilder::ChangeOptions(const Options& options) {
  68. // Note: if more fields are added to Options, update
  69. // this function to catch changes that should not be allowed to
  70. // change in the middle of building a Table.
  71. if (options.comparator != rep_->options.comparator) {
  72. return Status::InvalidArgument("changing comparator while building table");
  73. }
  74. // Note that any live BlockBuilders point to rep_->options and therefore
  75. // will automatically pick up the updated options.
  76. rep_->options = options;
  77. rep_->index_block_options = options;
  78. rep_->index_block_options.block_restart_interval = 1;
  79. return Status::OK();
  80. }
  81. void TableBuilder::Add(const Slice& key, const Slice& value) {
  82. Rep* r = rep_;
  83. assert(!r->closed);
  84. if (!ok()) return;
  85. if (r->num_entries > 0) {
  86. assert(r->options.comparator->Compare(key, Slice(r->last_key)) > 0);
  87. }
  88. if (r->pending_index_entry) {
  89. assert(r->data_block.empty());
  90. r->options.comparator->FindShortestSeparator(&r->last_key, key);
  91. std::string handle_encoding;
  92. r->pending_handle.EncodeTo(&handle_encoding);
  93. r->index_block.Add(r->last_key, Slice(handle_encoding));
  94. r->pending_index_entry = false;
  95. }
  96. if (r->filter_block != nullptr) {
  97. r->filter_block->AddKey(key);
  98. }
  99. r->last_key.assign(key.data(), key.size());
  100. r->num_entries++;
  101. r->data_block.Add(key, value);
  102. const size_t estimated_block_size = r->data_block.CurrentSizeEstimate();
  103. if (estimated_block_size >= r->options.block_size) {
  104. Flush();
  105. }
  106. }
  107. void TableBuilder::Flush() {
  108. Rep* r = rep_;
  109. assert(!r->closed);
  110. if (!ok()) return;
  111. if (r->data_block.empty()) return;
  112. assert(!r->pending_index_entry);
  113. WriteBlock(&r->data_block, &r->pending_handle);
  114. if (ok()) {
  115. r->pending_index_entry = true;
  116. r->status = r->file->Flush();
  117. }
  118. if (r->filter_block != nullptr) {
  119. r->filter_block->StartBlock(r->offset);
  120. }
  121. }
  122. void TableBuilder::WriteBlock(BlockBuilder* block, BlockHandle* handle) {
  123. // File format contains a sequence of blocks where each block has:
  124. // block_data: uint8[n]
  125. // type: uint8
  126. // crc: uint32
  127. assert(ok());
  128. Rep* r = rep_;
  129. Slice raw = block->Finish();
  130. Slice block_contents;
  131. CompressionType type = r->options.compression;
  132. // TODO(postrelease): Support more compression options: zlib?
  133. switch (type) {
  134. case kNoCompression:
  135. block_contents = raw;
  136. break;
  137. case kSnappyCompression: {
  138. std::string* compressed = &r->compressed_output;
  139. if (port::Snappy_Compress(raw.data(), raw.size(), compressed) &&
  140. compressed->size() < raw.size() - (raw.size() / 8u)) {
  141. block_contents = *compressed;
  142. } else {
  143. // Snappy not supported, or compressed less than 12.5%, so just
  144. // store uncompressed form
  145. block_contents = raw;
  146. type = kNoCompression;
  147. }
  148. break;
  149. }
  150. }
  151. WriteRawBlock(block_contents, type, handle);
  152. r->compressed_output.clear();
  153. block->Reset();
  154. }
  155. void TableBuilder::WriteRawBlock(const Slice& block_contents,
  156. CompressionType type, BlockHandle* handle) {
  157. Rep* r = rep_;
  158. handle->set_offset(r->offset);
  159. handle->set_size(block_contents.size());
  160. r->status = r->file->Append(block_contents);
  161. if (r->status.ok()) {
  162. char trailer[kBlockTrailerSize];
  163. trailer[0] = type;
  164. uint32_t crc = crc32c::Value(block_contents.data(), block_contents.size());
  165. crc = crc32c::Extend(crc, trailer, 1); // Extend crc to cover block type
  166. EncodeFixed32(trailer + 1, crc32c::Mask(crc));
  167. r->status = r->file->Append(Slice(trailer, kBlockTrailerSize));
  168. if (r->status.ok()) {
  169. r->offset += block_contents.size() + kBlockTrailerSize;
  170. }
  171. }
  172. }
  173. Status TableBuilder::status() const { return rep_->status; }
  174. Status TableBuilder::Finish() {
  175. Rep* r = rep_;
  176. Flush();
  177. assert(!r->closed);
  178. r->closed = true;
  179. BlockHandle filter_block_handle, metaindex_block_handle, index_block_handle;
  180. // Write filter block
  181. if (ok() && r->filter_block != nullptr) {
  182. WriteRawBlock(r->filter_block->Finish(), kNoCompression,
  183. &filter_block_handle);
  184. }
  185. // Write metaindex block
  186. if (ok()) {
  187. BlockBuilder meta_index_block(&r->options);
  188. if (r->filter_block != nullptr) {
  189. // Add mapping from "filter.Name" to location of filter data
  190. std::string key = "filter.";
  191. key.append(r->options.filter_policy->Name());
  192. std::string handle_encoding;
  193. filter_block_handle.EncodeTo(&handle_encoding);
  194. meta_index_block.Add(key, handle_encoding);
  195. }
  196. // TODO(postrelease): Add stats and other meta blocks
  197. WriteBlock(&meta_index_block, &metaindex_block_handle);
  198. }
  199. // Write index block
  200. if (ok()) {
  201. if (r->pending_index_entry) {
  202. r->options.comparator->FindShortSuccessor(&r->last_key);
  203. std::string handle_encoding;
  204. r->pending_handle.EncodeTo(&handle_encoding);
  205. r->index_block.Add(r->last_key, Slice(handle_encoding));
  206. r->pending_index_entry = false;
  207. }
  208. WriteBlock(&r->index_block, &index_block_handle);
  209. }
  210. // Write footer
  211. if (ok()) {
  212. Footer footer;
  213. footer.set_metaindex_handle(metaindex_block_handle);
  214. footer.set_index_handle(index_block_handle);
  215. std::string footer_encoding;
  216. footer.EncodeTo(&footer_encoding);
  217. r->status = r->file->Append(footer_encoding);
  218. if (r->status.ok()) {
  219. r->offset += footer_encoding.size();
  220. }
  221. }
  222. return r->status;
  223. }
  224. void TableBuilder::Abandon() {
  225. Rep* r = rep_;
  226. assert(!r->closed);
  227. r->closed = true;
  228. }
  229. uint64_t TableBuilder::NumEntries() const { return rep_->num_entries; }
  230. uint64_t TableBuilder::FileSize() const { return rep_->offset; }
  231. } // namespace leveldb