/src/rocksdb/table/table_builder.h
Line | Count | Source |
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 | | |
10 | | #pragma once |
11 | | |
12 | | #include <stdint.h> |
13 | | |
14 | | #include <string> |
15 | | #include <utility> |
16 | | #include <vector> |
17 | | |
18 | | #include "db/dbformat.h" |
19 | | #include "db/seqno_to_time_mapping.h" |
20 | | #include "db/table_properties_collector.h" |
21 | | #include "file/writable_file_writer.h" |
22 | | #include "options/cf_options.h" |
23 | | #include "rocksdb/options.h" |
24 | | #include "rocksdb/table_properties.h" |
25 | | #include "table/unique_id_impl.h" |
26 | | #include "trace_replay/block_cache_tracer.h" |
27 | | #include "util/cast_util.h" |
28 | | |
29 | | namespace ROCKSDB_NAMESPACE { |
30 | | |
31 | | class Slice; |
32 | | class Status; |
33 | | |
34 | | struct TableReaderOptions { |
35 | | // @param skip_filters Disables loading/accessing the filter block |
36 | | TableReaderOptions( |
37 | | const ImmutableOptions& _ioptions, |
38 | | const std::shared_ptr<const SliceTransform>& _prefix_extractor, |
39 | | UnownedPtr<CompressionManager> _compression_manager, |
40 | | const EnvOptions& _env_options, |
41 | | const InternalKeyComparator& _internal_comparator, |
42 | | uint8_t _block_protection_bytes_per_key, bool _skip_filters = false, |
43 | | bool _immortal = false, bool _force_direct_prefetch = false, |
44 | | int _level = -1, BlockCacheTracer* const _block_cache_tracer = nullptr, |
45 | | size_t _max_file_size_for_l0_meta_pin = 0, |
46 | | const std::string& _cur_db_session_id = "", uint64_t _cur_file_num = 0, |
47 | | UniqueId64x2 _unique_id = {}, SequenceNumber _largest_seqno = 0, |
48 | | uint64_t _tail_size = 0, bool _user_defined_timestamps_persisted = true) |
49 | 96.8k | : ioptions(_ioptions), |
50 | 96.8k | prefix_extractor(_prefix_extractor), |
51 | 96.8k | compression_manager(_compression_manager), |
52 | 96.8k | env_options(_env_options), |
53 | 96.8k | internal_comparator(_internal_comparator), |
54 | 96.8k | skip_filters(_skip_filters), |
55 | 96.8k | immortal(_immortal), |
56 | 96.8k | force_direct_prefetch(_force_direct_prefetch), |
57 | 96.8k | level(_level), |
58 | 96.8k | largest_seqno(_largest_seqno), |
59 | 96.8k | block_cache_tracer(_block_cache_tracer), |
60 | 96.8k | max_file_size_for_l0_meta_pin(_max_file_size_for_l0_meta_pin), |
61 | 96.8k | cur_db_session_id(_cur_db_session_id), |
62 | 96.8k | cur_file_num(_cur_file_num), |
63 | 96.8k | unique_id(_unique_id), |
64 | 96.8k | block_protection_bytes_per_key(_block_protection_bytes_per_key), |
65 | 96.8k | tail_size(_tail_size), |
66 | 96.8k | user_defined_timestamps_persisted(_user_defined_timestamps_persisted) {} |
67 | | |
68 | | const ImmutableOptions& ioptions; |
69 | | const std::shared_ptr<const SliceTransform>& prefix_extractor; |
70 | | // NOTE: the compression manager is not saved, just potentially a decompressor |
71 | | // from it, so we don't need a shared_ptr copy |
72 | | UnownedPtr<CompressionManager> compression_manager; |
73 | | const EnvOptions& env_options; |
74 | | const InternalKeyComparator& internal_comparator; |
75 | | // This is only used for BlockBasedTable (reader) |
76 | | bool skip_filters; |
77 | | // Whether the table will be valid as long as the DB is open |
78 | | bool immortal; |
79 | | // When data prefetching is needed, even if direct I/O is off, read data to |
80 | | // fetch into RocksDB's buffer, rather than relying |
81 | | // RandomAccessFile::Prefetch(). |
82 | | bool force_direct_prefetch; |
83 | | // What level this table/file is on, -1 for "not set, don't know." Used |
84 | | // for level-specific statistics. |
85 | | int level; |
86 | | // largest seqno in the table (or 0 means unknown???) |
87 | | SequenceNumber largest_seqno; |
88 | | BlockCacheTracer* const block_cache_tracer; |
89 | | // Largest L0 file size whose meta-blocks may be pinned (can be zero when |
90 | | // unknown). |
91 | | const size_t max_file_size_for_l0_meta_pin; |
92 | | |
93 | | std::string cur_db_session_id; |
94 | | |
95 | | uint64_t cur_file_num; |
96 | | |
97 | | // Known unique_id or {}, kNullUniqueId64x2 means unknown |
98 | | UniqueId64x2 unique_id; |
99 | | |
100 | | uint8_t block_protection_bytes_per_key; |
101 | | |
102 | | uint64_t tail_size; |
103 | | |
104 | | // Whether the key in the table contains user-defined timestamps. |
105 | | bool user_defined_timestamps_persisted; |
106 | | }; |
107 | | |
108 | | struct TableBuilderOptions : public TablePropertiesCollectorFactory::Context { |
109 | | TableBuilderOptions( |
110 | | const ImmutableOptions& _ioptions, const MutableCFOptions& _moptions, |
111 | | const ReadOptions& _read_options, const WriteOptions& _write_options, |
112 | | const InternalKeyComparator& _internal_comparator, |
113 | | const InternalTblPropCollFactories* _internal_tbl_prop_coll_factories, |
114 | | CompressionType _compression_type, |
115 | | const CompressionOptions& _compression_opts, uint32_t _column_family_id, |
116 | | const std::string& _column_family_name, int _level, |
117 | | const int64_t _newest_key_time, bool _is_bottommost = false, |
118 | | TableFileCreationReason _reason = TableFileCreationReason::kMisc, |
119 | | const int64_t _oldest_key_time = 0, |
120 | | const uint64_t _file_creation_time = 0, const std::string& _db_id = "", |
121 | | const std::string& _db_session_id = "", |
122 | | const uint64_t _target_file_size = 0, const uint64_t _cur_file_num = 0, |
123 | | const SequenceNumber _last_level_inclusive_max_seqno_threshold = |
124 | | kMaxSequenceNumber) |
125 | 20.6k | : TablePropertiesCollectorFactory::Context( |
126 | 20.6k | _column_family_id, _level, _ioptions.num_levels, |
127 | 20.6k | _last_level_inclusive_max_seqno_threshold), |
128 | 20.6k | ioptions(_ioptions), |
129 | 20.6k | moptions(_moptions), |
130 | 20.6k | read_options(_read_options), |
131 | 20.6k | write_options(_write_options), |
132 | 20.6k | internal_comparator(_internal_comparator), |
133 | 20.6k | internal_tbl_prop_coll_factories(_internal_tbl_prop_coll_factories), |
134 | 20.6k | compression_type(_compression_type), |
135 | 20.6k | compression_opts(_compression_opts), |
136 | 20.6k | column_family_name(_column_family_name), |
137 | 20.6k | oldest_key_time(_oldest_key_time), |
138 | 20.6k | newest_key_time(_newest_key_time), |
139 | 20.6k | target_file_size(_target_file_size), |
140 | 20.6k | file_creation_time(_file_creation_time), |
141 | 20.6k | db_id(_db_id), |
142 | 20.6k | db_session_id(_db_session_id), |
143 | 20.6k | is_bottommost(_is_bottommost), |
144 | 20.6k | reason(_reason), |
145 | 20.6k | cur_file_num(_cur_file_num) {} |
146 | | |
147 | | const ImmutableOptions& ioptions; |
148 | | const MutableCFOptions& moptions; |
149 | | const ReadOptions& read_options; |
150 | | const WriteOptions& write_options; |
151 | | const InternalKeyComparator& internal_comparator; |
152 | | const InternalTblPropCollFactories* internal_tbl_prop_coll_factories; |
153 | | const CompressionType compression_type; |
154 | | const CompressionOptions& compression_opts; |
155 | | const std::string& column_family_name; |
156 | | const int64_t oldest_key_time; |
157 | | const int64_t newest_key_time; |
158 | | const uint64_t target_file_size; |
159 | | const uint64_t file_creation_time; |
160 | | const std::string db_id; |
161 | | const std::string db_session_id; |
162 | | // BEGIN for FilterBuildingContext |
163 | | const bool is_bottommost; |
164 | | const TableFileCreationReason reason; |
165 | | // END for FilterBuildingContext |
166 | | |
167 | | // XXX: only used by BlockBasedTableBuilder for SstFileWriter. If you |
168 | | // want to skip filters, that should be (for example) null filter_policy |
169 | | // in the table options of the ioptions.table_factory |
170 | | bool skip_filters = false; |
171 | | const uint64_t cur_file_num; |
172 | | }; |
173 | | |
174 | | // TableBuilder provides the interface used to build a Table |
175 | | // (an immutable and sorted map from keys to values). |
176 | | // |
177 | | // Multiple threads can invoke const methods on a TableBuilder without |
178 | | // external synchronization, but if any of the threads may call a |
179 | | // non-const method, all threads accessing the same TableBuilder must use |
180 | | // external synchronization. |
181 | | class TableBuilder { |
182 | | public: |
183 | | // REQUIRES: Either Finish() or Abandon() has been called. |
184 | 20.6k | virtual ~TableBuilder() {} |
185 | | |
186 | | // Add key,value to the table being constructed. |
187 | | // REQUIRES: key is after any previously added key according to comparator. |
188 | | // REQUIRES: Finish(), Abandon() have not been called |
189 | | virtual void Add(const Slice& key, const Slice& value) = 0; |
190 | | |
191 | | // Return non-ok iff some error has been detected. |
192 | | virtual Status status() const = 0; |
193 | | |
194 | | // Return non-ok iff some error happens during IO. |
195 | | virtual IOStatus io_status() const = 0; |
196 | | |
197 | | // Finish building the table. |
198 | | // REQUIRES: Finish(), Abandon() have not been called |
199 | | virtual Status Finish() = 0; |
200 | | |
201 | | // Indicate that the contents of this builder should be abandoned. |
202 | | // If the caller is not going to call Finish(), it must call Abandon() |
203 | | // before destroying this builder. |
204 | | // REQUIRES: Finish(), Abandon() have not been called |
205 | | virtual void Abandon() = 0; |
206 | | |
207 | | // Number of calls to Add() so far. |
208 | | virtual uint64_t NumEntries() const = 0; |
209 | | |
210 | | // Whether the output file is completely empty. It has neither entries |
211 | | // or tombstones. |
212 | 0 | virtual bool IsEmpty() const { |
213 | 0 | return NumEntries() == 0 && GetTableProperties().num_range_deletions == 0; |
214 | 0 | } |
215 | | |
216 | | // Size of the file before its content is compressed. |
217 | 0 | virtual uint64_t PreCompressionSize() const { return 0; } |
218 | | |
219 | | // Size of the file generated so far. If invoked after a successful |
220 | | // Finish() call, returns the size of the final generated file. |
221 | | virtual uint64_t FileSize() const = 0; |
222 | | |
223 | | // Estimated size of the file generated so far. This is used when |
224 | | // FileSize() cannot estimate final SST size, e.g. parallel compression |
225 | | // is enabled. |
226 | 0 | virtual uint64_t EstimatedFileSize() const { return FileSize(); } |
227 | | |
228 | 0 | virtual uint64_t GetTailSize() const { return 0; } |
229 | | |
230 | | // If the user defined table properties collector suggest the file to |
231 | | // be further compacted. |
232 | 0 | virtual bool NeedCompact() const { return false; } |
233 | | |
234 | | // Returns table properties |
235 | | virtual TableProperties GetTableProperties() const = 0; |
236 | | |
237 | | // Return file checksum |
238 | | virtual std::string GetFileChecksum() const = 0; |
239 | | |
240 | | // Return file checksum function name |
241 | | virtual const char* GetFileChecksumFuncName() const = 0; |
242 | | |
243 | | // Set the sequence number to time mapping. `relevant_mapping` must be in |
244 | | // enforced state (ready to encode to string). |
245 | | virtual void SetSeqnoTimeTableProperties( |
246 | | const SeqnoToTimeMapping& /*relevant_mapping*/, |
247 | 0 | uint64_t /*oldest_ancestor_time*/) {} |
248 | | |
249 | | // If this builder used CPU work from threads other than the caller, return |
250 | | // the CPU microseconds used. 0 = no work outside calling thread, or not |
251 | | // supported. |
252 | 0 | virtual uint64_t GetWorkerCPUMicros() const { return 0; } |
253 | | }; |
254 | | |
255 | | } // namespace ROCKSDB_NAMESPACE |