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.
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"
27 class PlainTableBuilder
: public TableBuilder
{
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.
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);
45 // REQUIRES: Either Finish() or Abandon() has been called.
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
;
53 // Return non-ok iff some error has been detected.
54 Status
status() const override
;
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
;
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
;
68 // Number of calls to Add() so far.
69 uint64_t NumEntries() const override
;
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
;
75 TableProperties
GetTableProperties() const override
{ return properties_
; }
77 bool SaveIndexInFile() const { return store_index_in_file_
; }
81 const ImmutableCFOptions
& ioptions_
;
82 std::vector
<std::unique_ptr
<IntTblPropCollector
>>
83 table_properties_collectors_
;
85 BloomBlockBuilder bloom_block_
;
86 std::unique_ptr
<PlainTableIndexBuilder
> index_builder_
;
88 WritableFileWriter
* file_
;
90 uint32_t bloom_bits_per_key_
;
91 size_t huge_page_tlb_size_
;
93 TableProperties properties_
;
94 PlainTableKeyEncoder encoder_
;
96 bool store_index_in_file_
;
98 std::vector
<uint32_t> keys_or_prefixes_hashes_
;
99 bool closed_
= false; // Either Finish() or Abandon() has been called.
101 const SliceTransform
* prefix_extractor_
;
103 Slice
GetPrefix(const Slice
& target
) const {
104 assert(target
.size() >= 8); // target is internal key
105 return GetPrefixFromUserKey(GetUserKey(target
));
108 Slice
GetPrefix(const ParsedInternalKey
& target
) const {
109 return GetPrefixFromUserKey(target
.user_key
);
112 Slice
GetUserKey(const Slice
& key
) const {
113 return Slice(key
.data(), key
.size() - 8);
116 Slice
GetPrefixFromUserKey(const Slice
& user_key
) const {
117 if (!IsTotalOrderMode()) {
118 return prefix_extractor_
->Transform(user_key
);
120 // Use empty slice as prefix if prefix_extractor is not set.
122 // it falls back to pure binary search and
123 // total iterator seek is supported.
128 bool IsTotalOrderMode() const { return (prefix_extractor_
== nullptr); }
130 // No copying allowed
131 PlainTableBuilder(const PlainTableBuilder
&) = delete;
132 void operator=(const PlainTableBuilder
&) = delete;
135 } // namespace rocksdb
137 #endif // ROCKSDB_LITE