YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/table/plain_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 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
21
#ifndef ROCKSDB_TABLE_PLAIN_TABLE_BUILDER_H
22
#define ROCKSDB_TABLE_PLAIN_TABLE_BUILDER_H
23
24
#ifndef ROCKSDB_LITE
25
#include <stdint.h>
26
#include <vector>
27
#include "yb/rocksdb/options.h"
28
#include "yb/rocksdb/status.h"
29
#include "yb/rocksdb/table/table_builder.h"
30
#include "yb/rocksdb/table/plain_table_key_coding.h"
31
#include "yb/rocksdb/table.h"
32
#include "yb/rocksdb/table_properties.h"
33
#include "yb/rocksdb/table/bloom_block.h"
34
#include "yb/rocksdb/table/plain_table_index.h"
35
36
namespace rocksdb {
37
38
class BlockBuilder;
39
class BlockHandle;
40
class WritableFile;
41
class TableBuilder;
42
43
class PlainTableBuilder: public TableBuilder {
44
 public:
45
  // Create a builder that will store the contents of the table it is
46
  // building in *file.  Does not close the file.  It is up to the
47
  // caller to close the file after calling Finish(). The output file
48
  // will be part of level specified by 'level'.  A value of -1 means
49
  // that the caller does not know which level the output file will reside.
50
  PlainTableBuilder(
51
      const ImmutableCFOptions& ioptions,
52
      const IntTblPropCollectorFactories& int_tbl_prop_collector_factories,
53
      uint32_t column_family_id, WritableFileWriter* file,
54
      uint32_t user_key_size, EncodingType encoding_type,
55
      size_t index_sparseness, uint32_t bloom_bits_per_key,
56
      uint32_t num_probes = 6, size_t huge_page_tlb_size = 0,
57
      double hash_table_ratio = 0, bool store_index_in_file = false);
58
59
  // REQUIRES: Either Finish() or Abandon() has been called.
60
  ~PlainTableBuilder();
61
62
  // Add key,value to the table being constructed.
63
  // REQUIRES: key is after any previously added key according to comparator.
64
  // REQUIRES: Finish(), Abandon() have not been called
65
  void Add(const Slice& key, const Slice& value) override;
66
67
  // Return non-ok iff some error has been detected.
68
  Status status() const override;
69
70
  // Finish building the table.  Stops using the file passed to the
71
  // constructor after this function returns.
72
  // REQUIRES: Finish(), Abandon() have not been called
73
  Status Finish() override;
74
75
  // Indicate that the contents of this builder should be abandoned.  Stops
76
  // using the file passed to the constructor after this function returns.
77
  // If the caller is not going to call Finish(), it must call Abandon()
78
  // before destroying this builder.
79
  // REQUIRES: Finish(), Abandon() have not been called
80
  void Abandon() override;
81
82
  // Number of calls to Add() so far.
83
  uint64_t NumEntries() const override;
84
85
  // Size of the file generated so far.  If invoked after a successful
86
  // Finish() call, returns the size of the final generated file.
87
  uint64_t TotalFileSize() const override;
88
89
  // For plain table SST file format - the same as TotalFileSize, since it uses the only base file
90
  // for both data and metadata.
91
  uint64_t BaseFileSize() const override;
92
93
2.04k
  TableProperties GetTableProperties() const override { return properties_; }
94
95
355k
  bool SaveIndexInFile() const { return store_index_in_file_; }
96
97
 private:
98
  Arena arena_;
99
  const ImmutableCFOptions& ioptions_;
100
  std::vector<std::unique_ptr<IntTblPropCollector>>
101
      table_properties_collectors_;
102
103
  BloomBlockBuilder bloom_block_;
104
  std::unique_ptr<PlainTableIndexBuilder> index_builder_;
105
106
  WritableFileWriter* file_;
107
  uint64_t offset_ = 0;
108
  uint32_t bloom_bits_per_key_;
109
  size_t huge_page_tlb_size_;
110
  Status status_;
111
  TableProperties properties_;
112
  PlainTableKeyEncoder encoder_;
113
114
  bool store_index_in_file_;
115
116
  std::vector<uint32_t> keys_or_prefixes_hashes_;
117
  bool closed_ = false;  // Either Finish() or Abandon() has been called.
118
119
  const SliceTransform* prefix_extractor_;
120
121
0
  Slice GetPrefix(const Slice& target) const {
122
0
    assert(target.size() >= 8);  // target is internal key
123
0
    return GetPrefixFromUserKey(GetUserKey(target));
124
0
  }
125
126
104
  Slice GetPrefix(const ParsedInternalKey& target) const {
127
104
    return GetPrefixFromUserKey(target.user_key);
128
104
  }
129
130
0
  Slice GetUserKey(const Slice& key) const {
131
0
    return Slice(key.data(), key.size() - 8);
132
0
  }
133
134
104
  Slice GetPrefixFromUserKey(const Slice& user_key) const {
135
104
    if (!IsTotalOrderMode()) {
136
88
      return prefix_extractor_->Transform(user_key);
137
16
    } else {
138
      // Use empty slice as prefix if prefix_extractor is not set.
139
      // In that case,
140
      // it falls back to pure binary search and
141
      // total iterator seek is supported.
142
16
      return Slice();
143
16
    }
144
104
  }
145
146
112
  bool IsTotalOrderMode() const { return (prefix_extractor_ == nullptr); }
147
148
  // No copying allowed
149
  PlainTableBuilder(const PlainTableBuilder&) = delete;
150
  void operator=(const PlainTableBuilder&) = delete;
151
};
152
153
}  // namespace rocksdb
154
155
#endif  // ROCKSDB_LITE
156
157
#endif  // ROCKSDB_TABLE_PLAIN_TABLE_BUILDER_H