Coverage Report

Created: 2024-07-27 06:53

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