plain_table_builder.h 5.3 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. #include <stdint.h>
  7. #include <string>
  8. #include <vector>
  9. #include "db/version_edit.h"
  10. #include "rocksdb/options.h"
  11. #include "rocksdb/status.h"
  12. #include "rocksdb/table.h"
  13. #include "rocksdb/table_properties.h"
  14. #include "table/plain/plain_table_bloom.h"
  15. #include "table/plain/plain_table_index.h"
  16. #include "table/plain/plain_table_key_coding.h"
  17. #include "table/table_builder.h"
  18. namespace ROCKSDB_NAMESPACE {
  19. class BlockBuilder;
  20. class BlockHandle;
  21. class WritableFile;
  22. class TableBuilder;
  23. // The builder class of PlainTable. For description of PlainTable format
  24. // See comments of class PlainTableFactory, where instances of
  25. // PlainTableReader are created.
  26. class PlainTableBuilder : public TableBuilder {
  27. public:
  28. // Create a builder that will store the contents of the table it is
  29. // building in *file. Does not close the file. It is up to the
  30. // caller to close the file after calling Finish(). The output file
  31. // will be part of level specified by 'level'. A value of -1 means
  32. // that the caller does not know which level the output file will reside.
  33. PlainTableBuilder(
  34. const ImmutableOptions& ioptions, const MutableCFOptions& moptions,
  35. const InternalTblPropCollFactories* internal_tbl_prop_coll_factories,
  36. uint32_t column_family_id, int level_at_creation,
  37. WritableFileWriter* file, uint32_t user_key_size,
  38. EncodingType encoding_type, size_t index_sparseness,
  39. uint32_t bloom_bits_per_key, const std::string& column_family_name,
  40. uint32_t num_probes = 6, size_t huge_page_tlb_size = 0,
  41. double hash_table_ratio = 0, bool store_index_in_file = false,
  42. const std::string& db_id = "", const std::string& db_session_id = "",
  43. uint64_t file_number = 0);
  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 { return status_; }
  55. // Return non-ok iff some error happens during IO.
  56. IOStatus io_status() const override { return io_status_; }
  57. // Finish building the table. Stops using the file passed to the
  58. // constructor after this function returns.
  59. // REQUIRES: Finish(), Abandon() have not been called
  60. Status Finish() override;
  61. // Indicate that the contents of this builder should be abandoned. Stops
  62. // using the file passed to the constructor after this function returns.
  63. // If the caller is not going to call Finish(), it must call Abandon()
  64. // before destroying this builder.
  65. // REQUIRES: Finish(), Abandon() have not been called
  66. void Abandon() override;
  67. // Number of calls to Add() so far.
  68. uint64_t NumEntries() const override;
  69. // Size of the file generated so far. If invoked after a successful
  70. // Finish() call, returns the size of the final generated file.
  71. uint64_t FileSize() const override;
  72. TableProperties GetTableProperties() const override { return properties_; }
  73. bool SaveIndexInFile() const { return store_index_in_file_; }
  74. // Get file checksum
  75. std::string GetFileChecksum() const override;
  76. // Get file checksum function name
  77. const char* GetFileChecksumFuncName() const override;
  78. void SetSeqnoTimeTableProperties(const SeqnoToTimeMapping& relevant_mapping,
  79. uint64_t uint_64) override;
  80. private:
  81. Arena arena_;
  82. const ImmutableOptions& ioptions_;
  83. const MutableCFOptions& moptions_;
  84. std::vector<std::unique_ptr<InternalTblPropColl>>
  85. table_properties_collectors_;
  86. BloomBlockBuilder bloom_block_;
  87. std::unique_ptr<PlainTableIndexBuilder> index_builder_;
  88. WritableFileWriter* file_;
  89. uint64_t offset_ = 0;
  90. uint32_t bloom_bits_per_key_;
  91. size_t huge_page_tlb_size_;
  92. Status status_;
  93. IOStatus io_status_;
  94. TableProperties properties_;
  95. PlainTableKeyEncoder encoder_;
  96. bool store_index_in_file_;
  97. std::vector<uint32_t> keys_or_prefixes_hashes_;
  98. bool closed_ = false; // Either Finish() or Abandon() has been called.
  99. const SliceTransform* prefix_extractor_;
  100. Slice GetPrefix(const Slice& target) const {
  101. assert(target.size() >= 8); // target is internal key
  102. return GetPrefixFromUserKey(ExtractUserKey(target));
  103. }
  104. Slice GetPrefix(const ParsedInternalKey& target) const {
  105. return GetPrefixFromUserKey(target.user_key);
  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