table_builder.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 <string>
  12. #include <utility>
  13. #include <vector>
  14. #include "db/dbformat.h"
  15. #include "db/table_properties_collector.h"
  16. #include "file/writable_file_writer.h"
  17. #include "options/cf_options.h"
  18. #include "rocksdb/options.h"
  19. #include "rocksdb/table_properties.h"
  20. #include "trace_replay/block_cache_tracer.h"
  21. namespace ROCKSDB_NAMESPACE {
  22. class Slice;
  23. class Status;
  24. struct TableReaderOptions {
  25. // @param skip_filters Disables loading/accessing the filter block
  26. TableReaderOptions(const ImmutableCFOptions& _ioptions,
  27. const SliceTransform* _prefix_extractor,
  28. const EnvOptions& _env_options,
  29. const InternalKeyComparator& _internal_comparator,
  30. bool _skip_filters = false, bool _immortal = false,
  31. int _level = -1,
  32. BlockCacheTracer* const _block_cache_tracer = nullptr)
  33. : TableReaderOptions(_ioptions, _prefix_extractor, _env_options,
  34. _internal_comparator, _skip_filters, _immortal,
  35. _level, 0 /* _largest_seqno */,
  36. _block_cache_tracer) {}
  37. // @param skip_filters Disables loading/accessing the filter block
  38. TableReaderOptions(const ImmutableCFOptions& _ioptions,
  39. const SliceTransform* _prefix_extractor,
  40. const EnvOptions& _env_options,
  41. const InternalKeyComparator& _internal_comparator,
  42. bool _skip_filters, bool _immortal, int _level,
  43. SequenceNumber _largest_seqno,
  44. BlockCacheTracer* const _block_cache_tracer)
  45. : ioptions(_ioptions),
  46. prefix_extractor(_prefix_extractor),
  47. env_options(_env_options),
  48. internal_comparator(_internal_comparator),
  49. skip_filters(_skip_filters),
  50. immortal(_immortal),
  51. level(_level),
  52. largest_seqno(_largest_seqno),
  53. block_cache_tracer(_block_cache_tracer) {}
  54. const ImmutableCFOptions& ioptions;
  55. const SliceTransform* prefix_extractor;
  56. const EnvOptions& env_options;
  57. const InternalKeyComparator& internal_comparator;
  58. // This is only used for BlockBasedTable (reader)
  59. bool skip_filters;
  60. // Whether the table will be valid as long as the DB is open
  61. bool immortal;
  62. // what level this table/file is on, -1 for "not set, don't know"
  63. int level;
  64. // largest seqno in the table
  65. SequenceNumber largest_seqno;
  66. BlockCacheTracer* const block_cache_tracer;
  67. };
  68. struct TableBuilderOptions {
  69. TableBuilderOptions(
  70. const ImmutableCFOptions& _ioptions, const MutableCFOptions& _moptions,
  71. const InternalKeyComparator& _internal_comparator,
  72. const std::vector<std::unique_ptr<IntTblPropCollectorFactory>>*
  73. _int_tbl_prop_collector_factories,
  74. CompressionType _compression_type, uint64_t _sample_for_compression,
  75. const CompressionOptions& _compression_opts, bool _skip_filters,
  76. const std::string& _column_family_name, int _level,
  77. const uint64_t _creation_time = 0, const int64_t _oldest_key_time = 0,
  78. const uint64_t _target_file_size = 0,
  79. const uint64_t _file_creation_time = 0)
  80. : ioptions(_ioptions),
  81. moptions(_moptions),
  82. internal_comparator(_internal_comparator),
  83. int_tbl_prop_collector_factories(_int_tbl_prop_collector_factories),
  84. compression_type(_compression_type),
  85. sample_for_compression(_sample_for_compression),
  86. compression_opts(_compression_opts),
  87. skip_filters(_skip_filters),
  88. column_family_name(_column_family_name),
  89. level(_level),
  90. creation_time(_creation_time),
  91. oldest_key_time(_oldest_key_time),
  92. target_file_size(_target_file_size),
  93. file_creation_time(_file_creation_time) {}
  94. const ImmutableCFOptions& ioptions;
  95. const MutableCFOptions& moptions;
  96. const InternalKeyComparator& internal_comparator;
  97. const std::vector<std::unique_ptr<IntTblPropCollectorFactory>>*
  98. int_tbl_prop_collector_factories;
  99. CompressionType compression_type;
  100. uint64_t sample_for_compression;
  101. const CompressionOptions& compression_opts;
  102. bool skip_filters; // only used by BlockBasedTableBuilder
  103. const std::string& column_family_name;
  104. int level; // what level this table/file is on, -1 for "not set, don't know"
  105. const uint64_t creation_time;
  106. const int64_t oldest_key_time;
  107. const uint64_t target_file_size;
  108. const uint64_t file_creation_time;
  109. };
  110. // TableBuilder provides the interface used to build a Table
  111. // (an immutable and sorted map from keys to values).
  112. //
  113. // Multiple threads can invoke const methods on a TableBuilder without
  114. // external synchronization, but if any of the threads may call a
  115. // non-const method, all threads accessing the same TableBuilder must use
  116. // external synchronization.
  117. class TableBuilder {
  118. public:
  119. // REQUIRES: Either Finish() or Abandon() has been called.
  120. virtual ~TableBuilder() {}
  121. // Add key,value to the table being constructed.
  122. // REQUIRES: key is after any previously added key according to comparator.
  123. // REQUIRES: Finish(), Abandon() have not been called
  124. virtual void Add(const Slice& key, const Slice& value) = 0;
  125. // Return non-ok iff some error has been detected.
  126. virtual Status status() const = 0;
  127. // Finish building the table.
  128. // REQUIRES: Finish(), Abandon() have not been called
  129. virtual Status Finish() = 0;
  130. // Indicate that the contents of this builder should be abandoned.
  131. // If the caller is not going to call Finish(), it must call Abandon()
  132. // before destroying this builder.
  133. // REQUIRES: Finish(), Abandon() have not been called
  134. virtual void Abandon() = 0;
  135. // Number of calls to Add() so far.
  136. virtual uint64_t NumEntries() const = 0;
  137. // Size of the file generated so far. If invoked after a successful
  138. // Finish() call, returns the size of the final generated file.
  139. virtual uint64_t FileSize() const = 0;
  140. // If the user defined table properties collector suggest the file to
  141. // be further compacted.
  142. virtual bool NeedCompact() const { return false; }
  143. // Returns table properties
  144. virtual TableProperties GetTableProperties() const = 0;
  145. // Return file checksum
  146. virtual const std::string& GetFileChecksum() const = 0;
  147. // Return file checksum function name
  148. virtual const char* GetFileChecksumFuncName() const = 0;
  149. };
  150. } // namespace ROCKSDB_NAMESPACE