plain_table_builder.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #pragma once
  6. #ifndef ROCKSDB_LITE
  7. #include <stdint.h>
  8. #include <string>
  9. #include <vector>
  10. #include "db/version_edit.h"
  11. #include "rocksdb/options.h"
  12. #include "rocksdb/status.h"
  13. #include "rocksdb/table.h"
  14. #include "rocksdb/table_properties.h"
  15. #include "table/plain/plain_table_bloom.h"
  16. #include "table/plain/plain_table_index.h"
  17. #include "table/plain/plain_table_key_coding.h"
  18. #include "table/table_builder.h"
  19. namespace ROCKSDB_NAMESPACE {
  20. class BlockBuilder;
  21. class BlockHandle;
  22. class WritableFile;
  23. class TableBuilder;
  24. // The builder class of PlainTable. For description of PlainTable format
  25. // See comments of class PlainTableFactory, where instances of
  26. // PlainTableReader are created.
  27. class PlainTableBuilder: public TableBuilder {
  28. public:
  29. // Create a builder that will store the contents of the table it is
  30. // building in *file. Does not close the file. It is up to the
  31. // caller to close the file after calling Finish(). The output file
  32. // will be part of level specified by 'level'. A value of -1 means
  33. // that the caller does not know which level the output file will reside.
  34. PlainTableBuilder(
  35. const ImmutableCFOptions& ioptions, const MutableCFOptions& moptions,
  36. const std::vector<std::unique_ptr<IntTblPropCollectorFactory>>*
  37. int_tbl_prop_collector_factories,
  38. uint32_t column_family_id, WritableFileWriter* file,
  39. uint32_t user_key_size, EncodingType encoding_type,
  40. size_t index_sparseness, uint32_t bloom_bits_per_key,
  41. const std::string& column_family_name, uint32_t num_probes = 6,
  42. size_t huge_page_tlb_size = 0, double hash_table_ratio = 0,
  43. bool store_index_in_file = false);
  44. // No copying allowed
  45. PlainTableBuilder(const PlainTableBuilder&) = delete;
  46. void operator=(const PlainTableBuilder&) = delete;
  47. // REQUIRES: Either Finish() or Abandon() has been called.
  48. ~PlainTableBuilder();
  49. // Add key,value to the table being constructed.
  50. // REQUIRES: key is after any previously added key according to comparator.
  51. // REQUIRES: Finish(), Abandon() have not been called
  52. void Add(const Slice& key, const Slice& value) override;
  53. // Return non-ok iff some error has been detected.
  54. Status status() const override;
  55. // Finish building the table. Stops using the file passed to the
  56. // constructor after this function returns.
  57. // REQUIRES: Finish(), Abandon() have not been called
  58. Status Finish() override;
  59. // Indicate that the contents of this builder should be abandoned. Stops
  60. // using the file passed to the constructor after this function returns.
  61. // If the caller is not going to call Finish(), it must call Abandon()
  62. // before destroying this builder.
  63. // REQUIRES: Finish(), Abandon() have not been called
  64. void Abandon() override;
  65. // Number of calls to Add() so far.
  66. uint64_t NumEntries() const override;
  67. // Size of the file generated so far. If invoked after a successful
  68. // Finish() call, returns the size of the final generated file.
  69. uint64_t FileSize() const override;
  70. TableProperties GetTableProperties() const override { return properties_; }
  71. bool SaveIndexInFile() const { return store_index_in_file_; }
  72. // Get file checksum
  73. const std::string& GetFileChecksum() const override { return file_checksum_; }
  74. // Get file checksum function name
  75. const char* GetFileChecksumFuncName() const override;
  76. private:
  77. Arena arena_;
  78. const ImmutableCFOptions& ioptions_;
  79. const MutableCFOptions& moptions_;
  80. std::vector<std::unique_ptr<IntTblPropCollector>>
  81. table_properties_collectors_;
  82. BloomBlockBuilder bloom_block_;
  83. std::unique_ptr<PlainTableIndexBuilder> index_builder_;
  84. WritableFileWriter* file_;
  85. uint64_t offset_ = 0;
  86. uint32_t bloom_bits_per_key_;
  87. size_t huge_page_tlb_size_;
  88. Status status_;
  89. TableProperties properties_;
  90. PlainTableKeyEncoder encoder_;
  91. bool store_index_in_file_;
  92. std::vector<uint32_t> keys_or_prefixes_hashes_;
  93. bool closed_ = false; // Either Finish() or Abandon() has been called.
  94. const SliceTransform* prefix_extractor_;
  95. // Store file checksum. If checksum is disabled, its value is "0".
  96. std::string file_checksum_ = kUnknownFileChecksum;
  97. Slice GetPrefix(const Slice& target) const {
  98. assert(target.size() >= 8); // target is internal key
  99. return GetPrefixFromUserKey(GetUserKey(target));
  100. }
  101. Slice GetPrefix(const ParsedInternalKey& target) const {
  102. return GetPrefixFromUserKey(target.user_key);
  103. }
  104. Slice GetUserKey(const Slice& key) const {
  105. return Slice(key.data(), key.size() - 8);
  106. }
  107. Slice GetPrefixFromUserKey(const Slice& user_key) const {
  108. if (!IsTotalOrderMode()) {
  109. return prefix_extractor_->Transform(user_key);
  110. } else {
  111. // Use empty slice as prefix if prefix_extractor is not set.
  112. // In that case,
  113. // it falls back to pure binary search and
  114. // total iterator seek is supported.
  115. return Slice();
  116. }
  117. }
  118. bool IsTotalOrderMode() const { return (prefix_extractor_ == nullptr); }
  119. };
  120. } // namespace ROCKSDB_NAMESPACE
  121. #endif // ROCKSDB_LITE