block_based_table_builder.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #pragma once
  10. #include <stdint.h>
  11. #include <limits>
  12. #include <string>
  13. #include <utility>
  14. #include <vector>
  15. #include "db/version_edit.h"
  16. #include "rocksdb/flush_block_policy.h"
  17. #include "rocksdb/listener.h"
  18. #include "rocksdb/options.h"
  19. #include "rocksdb/status.h"
  20. #include "table/meta_blocks.h"
  21. #include "table/table_builder.h"
  22. #include "util/compression.h"
  23. namespace ROCKSDB_NAMESPACE {
  24. class BlockBuilder;
  25. class BlockHandle;
  26. class WritableFile;
  27. struct BlockBasedTableOptions;
  28. extern const uint64_t kBlockBasedTableMagicNumber;
  29. extern const uint64_t kLegacyBlockBasedTableMagicNumber;
  30. class BlockBasedTableBuilder : public TableBuilder {
  31. public:
  32. // Create a builder that will store the contents of the table it is
  33. // building in *file. Does not close the file. It is up to the
  34. // caller to close the file after calling Finish().
  35. BlockBasedTableBuilder(
  36. const ImmutableCFOptions& ioptions, const MutableCFOptions& moptions,
  37. const BlockBasedTableOptions& table_options,
  38. const InternalKeyComparator& internal_comparator,
  39. const std::vector<std::unique_ptr<IntTblPropCollectorFactory>>*
  40. int_tbl_prop_collector_factories,
  41. uint32_t column_family_id, WritableFileWriter* file,
  42. const CompressionType compression_type,
  43. const uint64_t sample_for_compression,
  44. const CompressionOptions& compression_opts, const bool skip_filters,
  45. const std::string& column_family_name, const int level_at_creation,
  46. const uint64_t creation_time = 0, const uint64_t oldest_key_time = 0,
  47. const uint64_t target_file_size = 0,
  48. const uint64_t file_creation_time = 0);
  49. // No copying allowed
  50. BlockBasedTableBuilder(const BlockBasedTableBuilder&) = delete;
  51. BlockBasedTableBuilder& operator=(const BlockBasedTableBuilder&) = delete;
  52. // REQUIRES: Either Finish() or Abandon() has been called.
  53. ~BlockBasedTableBuilder();
  54. // Add key,value to the table being constructed.
  55. // REQUIRES: key is after any previously added key according to comparator.
  56. // REQUIRES: Finish(), Abandon() have not been called
  57. void Add(const Slice& key, const Slice& value) override;
  58. // Return non-ok iff some error has been detected.
  59. Status status() const override;
  60. // Finish building the table. Stops using the file passed to the
  61. // constructor after this function returns.
  62. // REQUIRES: Finish(), Abandon() have not been called
  63. Status Finish() override;
  64. // Indicate that the contents of this builder should be abandoned. Stops
  65. // using the file passed to the constructor after this function returns.
  66. // If the caller is not going to call Finish(), it must call Abandon()
  67. // before destroying this builder.
  68. // REQUIRES: Finish(), Abandon() have not been called
  69. void Abandon() override;
  70. // Number of calls to Add() so far.
  71. uint64_t NumEntries() const override;
  72. // Size of the file generated so far. If invoked after a successful
  73. // Finish() call, returns the size of the final generated file.
  74. uint64_t FileSize() const override;
  75. bool NeedCompact() const override;
  76. // Get table properties
  77. TableProperties GetTableProperties() const override;
  78. // Get file checksum
  79. const std::string& GetFileChecksum() const override { return file_checksum_; }
  80. // Get file checksum function name
  81. const char* GetFileChecksumFuncName() const override;
  82. private:
  83. bool ok() const { return status().ok(); }
  84. // Transition state from buffered to unbuffered. See `Rep::State` API comment
  85. // for details of the states.
  86. // REQUIRES: `rep_->state == kBuffered`
  87. void EnterUnbuffered();
  88. // Call block's Finish() method
  89. // and then write the compressed block contents to file.
  90. void WriteBlock(BlockBuilder* block, BlockHandle* handle, bool is_data_block);
  91. // Compress and write block content to the file.
  92. void WriteBlock(const Slice& block_contents, BlockHandle* handle,
  93. bool is_data_block);
  94. // Directly write data to the file.
  95. void WriteRawBlock(const Slice& data, CompressionType, BlockHandle* handle,
  96. bool is_data_block = false);
  97. Status InsertBlockInCache(const Slice& block_contents,
  98. const CompressionType type,
  99. const BlockHandle* handle);
  100. void WriteFilterBlock(MetaIndexBuilder* meta_index_builder);
  101. void WriteIndexBlock(MetaIndexBuilder* meta_index_builder,
  102. BlockHandle* index_block_handle);
  103. void WritePropertiesBlock(MetaIndexBuilder* meta_index_builder);
  104. void WriteCompressionDictBlock(MetaIndexBuilder* meta_index_builder);
  105. void WriteRangeDelBlock(MetaIndexBuilder* meta_index_builder);
  106. void WriteFooter(BlockHandle& metaindex_block_handle,
  107. BlockHandle& index_block_handle);
  108. struct Rep;
  109. class BlockBasedTablePropertiesCollectorFactory;
  110. class BlockBasedTablePropertiesCollector;
  111. Rep* rep_;
  112. // Advanced operation: flush any buffered key/value pairs to file.
  113. // Can be used to ensure that two adjacent entries never live in
  114. // the same data block. Most clients should not need to use this method.
  115. // REQUIRES: Finish(), Abandon() have not been called
  116. void Flush();
  117. // Some compression libraries fail when the raw size is bigger than int. If
  118. // uncompressed size is bigger than kCompressionSizeLimit, don't compress it
  119. const uint64_t kCompressionSizeLimit = std::numeric_limits<int>::max();
  120. // Store file checksum. If checksum is disabled, its value is "0".
  121. std::string file_checksum_ = kUnknownFileChecksum;
  122. };
  123. Slice CompressBlock(const Slice& raw, const CompressionInfo& info,
  124. CompressionType* type, uint32_t format_version,
  125. bool do_sample, std::string* compressed_output,
  126. std::string* sampled_output_fast,
  127. std::string* sampled_output_slow);
  128. } // namespace ROCKSDB_NAMESPACE