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