YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/table/block_based_table_builder.h
Line
Count
Source
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under the BSD-style license found in the
3
//  LICENSE file in the root directory of this source tree. An additional grant
4
//  of patent rights can be found in the PATENTS file in the same directory.
5
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
21
// Use of this source code is governed by a BSD-style license that can be
22
// found in the LICENSE file. See the AUTHORS file for names of contributors.
23
24
#ifndef YB_ROCKSDB_TABLE_BLOCK_BASED_TABLE_BUILDER_H
25
#define YB_ROCKSDB_TABLE_BLOCK_BASED_TABLE_BUILDER_H
26
27
#include <stdint.h>
28
29
#include <limits>
30
#include <string>
31
#include <utility>
32
#include <vector>
33
34
#include "yb/rocksdb/options.h"
35
#include "yb/rocksdb/status.h"
36
#include "yb/rocksdb/table.h"
37
#include "yb/rocksdb/table/table_builder.h"
38
39
namespace rocksdb {
40
41
class BlockBuilder;
42
class BlockHandle;
43
class WritableFile;
44
struct BlockBasedTableOptions;
45
46
extern const uint64_t kBlockBasedTableMagicNumber;
47
extern const uint64_t kLegacyBlockBasedTableMagicNumber;
48
49
class BlockBasedTableBuilder : public TableBuilder {
50
 public:
51
  // Create a builder that will store the contents of the table it is
52
  // building in *file.  Does not close the file.  It is up to the
53
  // caller to close the file after calling Finish().
54
  BlockBasedTableBuilder(
55
      const ImmutableCFOptions& ioptions,
56
      const BlockBasedTableOptions& table_options,
57
      const std::shared_ptr<const InternalKeyComparator>& internal_comparator,
58
      const IntTblPropCollectorFactories& int_tbl_prop_collector_factories,
59
      uint32_t column_family_id, WritableFileWriter* metadata_file,
60
      WritableFileWriter* data_file,
61
      const CompressionType compression_type,
62
      const CompressionOptions& compression_opts, const bool skip_filters);
63
64
  // REQUIRES: Either Finish() or Abandon() has been called.
65
  ~BlockBasedTableBuilder();
66
67
  // Add key,value to the table being constructed.
68
  // REQUIRES: key is after any previously added key according to comparator.
69
  // REQUIRES: Finish(), Abandon() have not been called
70
  void Add(const Slice& key, const Slice& value) override;
71
72
  // Return non-ok iff some error has been detected.
73
  Status status() const override;
74
75
  // Finish building the table.  Stops using the file passed to the
76
  // constructor after this function returns.
77
  // REQUIRES: Finish(), Abandon() have not been called
78
  Status Finish() override;
79
80
  // Indicate that the contents of this builder should be abandoned.  Stops
81
  // using the file passed to the constructor after this function returns.
82
  // If the caller is not going to call Finish(), it must call Abandon()
83
  // before destroying this builder.
84
  // REQUIRES: Finish(), Abandon() have not been called
85
  void Abandon() override;
86
87
  // Number of calls to Add() so far.
88
  uint64_t NumEntries() const override;
89
90
  // Total size of the file(s) generated so far.  If invoked after a successful
91
  // Finish() call, returns the total size of the final generated file(s).
92
  uint64_t TotalFileSize() const override;
93
94
  // Size of the base file generated so far.  If invoked after a successful Finish() call, returns
95
  // the size of the final generated base file. SST is either stored in single base file, or
96
  // metadata is stored in base file while data is split among data files (S-Blocks).
97
  // Block-based SST are always stored in separate files, other SST types are stored in one file
98
  // each.
99
  uint64_t BaseFileSize() const override;
100
101
  bool NeedCompact() const override;
102
103
  // Get table properties
104
  TableProperties GetTableProperties() const override;
105
106
  void TEST_skip_writing_key_value_encoding_format();
107
108
 private:
109
  struct FileWriterWithOffsetAndCachePrefix;
110
111
102M
  bool ok() const { return status().ok(); }
112
  // Call block's Finish() method and then write the finalize block contents to
113
  // file. Returns number of bytes written to file.
114
  size_t WriteBlock(BlockBuilder* block, BlockHandle* handle,
115
                    FileWriterWithOffsetAndCachePrefix* writer_info);
116
  // Directly write block content to the file. Returns number of bytes written to file.
117
  size_t WriteBlock(const Slice& block_contents, BlockHandle* handle,
118
      FileWriterWithOffsetAndCachePrefix* writer_info);
119
  size_t WriteRawBlock(const Slice& data, CompressionType, BlockHandle* handle,
120
      FileWriterWithOffsetAndCachePrefix* writer_info);
121
  Status InsertBlockInCache(const Slice& block_contents,
122
                            const CompressionType type,
123
      const BlockHandle* handle,
124
      FileWriterWithOffsetAndCachePrefix* writer_info);
125
126
  struct Rep;
127
  class BlockBasedTablePropertiesCollectorFactory;
128
  class BlockBasedTablePropertiesCollector;
129
  Rep* rep_;
130
131
  // Flush the current data block into disk. next_block_first_key should be nullptr if this is the
132
  // last block written to disk.
133
  // REQUIRES: Finish(), Abandon() have not been called.
134
  void FlushDataBlock(const Slice& next_block_first_key);
135
136
  // Flush the current filter block into disk. next_block_first_filter_key should be nullptr if this
137
  // is the last block written to disk.
138
  // REQUIRES: Finish(), Abandon() have not been called.
139
  void FlushFilterBlock(const Slice* const next_block_first_filter_key);
140
141
  // Some compression libraries fail when the raw size is bigger than int. If
142
  // uncompressed size is bigger than kCompressionSizeLimit, don't compress it
143
  const uint64_t kCompressionSizeLimit = std::numeric_limits<int>::max();
144
145
  // No copying allowed
146
  BlockBasedTableBuilder(const BlockBasedTableBuilder&) = delete;
147
  void operator=(const BlockBasedTableBuilder&) = delete;
148
};
149
150
}  // namespace rocksdb
151
152
#endif  // YB_ROCKSDB_TABLE_BLOCK_BASED_TABLE_BUILDER_H