]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
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 | #include <stdint.h> | |
12 | #include <limits> | |
13 | #include <string> | |
14 | #include <utility> | |
15 | #include <vector> | |
16 | ||
17 | #include "rocksdb/flush_block_policy.h" | |
11fdf7f2 | 18 | #include "rocksdb/listener.h" |
7c673cae FG |
19 | #include "rocksdb/options.h" |
20 | #include "rocksdb/status.h" | |
11fdf7f2 | 21 | #include "table/meta_blocks.h" |
7c673cae | 22 | #include "table/table_builder.h" |
11fdf7f2 | 23 | #include "util/compression.h" |
7c673cae FG |
24 | |
25 | namespace rocksdb { | |
26 | ||
27 | class BlockBuilder; | |
28 | class BlockHandle; | |
29 | class WritableFile; | |
30 | struct BlockBasedTableOptions; | |
31 | ||
32 | extern const uint64_t kBlockBasedTableMagicNumber; | |
33 | extern const uint64_t kLegacyBlockBasedTableMagicNumber; | |
34 | ||
35 | class BlockBasedTableBuilder : public TableBuilder { | |
36 | public: | |
37 | // Create a builder that will store the contents of the table it is | |
38 | // building in *file. Does not close the file. It is up to the | |
39 | // caller to close the file after calling Finish(). | |
7c673cae | 40 | BlockBasedTableBuilder( |
11fdf7f2 | 41 | const ImmutableCFOptions& ioptions, const MutableCFOptions& moptions, |
7c673cae FG |
42 | const BlockBasedTableOptions& table_options, |
43 | const InternalKeyComparator& internal_comparator, | |
44 | const std::vector<std::unique_ptr<IntTblPropCollectorFactory>>* | |
45 | int_tbl_prop_collector_factories, | |
46 | uint32_t column_family_id, WritableFileWriter* file, | |
47 | const CompressionType compression_type, | |
494da23a TL |
48 | const uint64_t sample_for_compression, |
49 | const CompressionOptions& compression_opts, const bool skip_filters, | |
11fdf7f2 | 50 | const std::string& column_family_name, const uint64_t creation_time = 0, |
494da23a | 51 | const uint64_t oldest_key_time = 0, const uint64_t target_file_size = 0); |
7c673cae FG |
52 | |
53 | // REQUIRES: Either Finish() or Abandon() has been called. | |
54 | ~BlockBasedTableBuilder(); | |
55 | ||
11fdf7f2 TL |
56 | // No copying allowed |
57 | BlockBasedTableBuilder(const BlockBasedTableBuilder&) = delete; | |
58 | BlockBasedTableBuilder& operator=(const BlockBasedTableBuilder&) = delete; | |
59 | ||
7c673cae FG |
60 | // Add key,value to the table being constructed. |
61 | // REQUIRES: key is after any previously added key according to comparator. | |
62 | // REQUIRES: Finish(), Abandon() have not been called | |
63 | void Add(const Slice& key, const Slice& value) override; | |
64 | ||
65 | // Return non-ok iff some error has been detected. | |
66 | Status status() const override; | |
67 | ||
68 | // Finish building the table. Stops using the file passed to the | |
69 | // constructor after this function returns. | |
70 | // REQUIRES: Finish(), Abandon() have not been called | |
71 | Status Finish() override; | |
72 | ||
73 | // Indicate that the contents of this builder should be abandoned. Stops | |
74 | // using the file passed to the constructor after this function returns. | |
75 | // If the caller is not going to call Finish(), it must call Abandon() | |
76 | // before destroying this builder. | |
77 | // REQUIRES: Finish(), Abandon() have not been called | |
78 | void Abandon() override; | |
79 | ||
80 | // Number of calls to Add() so far. | |
81 | uint64_t NumEntries() const override; | |
82 | ||
83 | // Size of the file generated so far. If invoked after a successful | |
84 | // Finish() call, returns the size of the final generated file. | |
85 | uint64_t FileSize() const override; | |
86 | ||
87 | bool NeedCompact() const override; | |
88 | ||
89 | // Get table properties | |
90 | TableProperties GetTableProperties() const override; | |
91 | ||
92 | private: | |
93 | bool ok() const { return status().ok(); } | |
94 | ||
494da23a TL |
95 | // Transition state from buffered to unbuffered. See `Rep::State` API comment |
96 | // for details of the states. | |
97 | // REQUIRES: `rep_->state == kBuffered` | |
98 | void EnterUnbuffered(); | |
99 | ||
7c673cae FG |
100 | // Call block's Finish() method |
101 | // and then write the compressed block contents to file. | |
102 | void WriteBlock(BlockBuilder* block, BlockHandle* handle, bool is_data_block); | |
103 | ||
104 | // Compress and write block content to the file. | |
105 | void WriteBlock(const Slice& block_contents, BlockHandle* handle, | |
106 | bool is_data_block); | |
107 | // Directly write data to the file. | |
11fdf7f2 TL |
108 | void WriteRawBlock(const Slice& data, CompressionType, BlockHandle* handle, |
109 | bool is_data_block = false); | |
7c673cae FG |
110 | Status InsertBlockInCache(const Slice& block_contents, |
111 | const CompressionType type, | |
112 | const BlockHandle* handle); | |
11fdf7f2 TL |
113 | |
114 | void WriteFilterBlock(MetaIndexBuilder* meta_index_builder); | |
115 | void WriteIndexBlock(MetaIndexBuilder* meta_index_builder, | |
116 | BlockHandle* index_block_handle); | |
117 | void WritePropertiesBlock(MetaIndexBuilder* meta_index_builder); | |
118 | void WriteCompressionDictBlock(MetaIndexBuilder* meta_index_builder); | |
119 | void WriteRangeDelBlock(MetaIndexBuilder* meta_index_builder); | |
494da23a TL |
120 | void WriteFooter(BlockHandle& metaindex_block_handle, |
121 | BlockHandle& index_block_handle); | |
11fdf7f2 | 122 | |
7c673cae FG |
123 | struct Rep; |
124 | class BlockBasedTablePropertiesCollectorFactory; | |
125 | class BlockBasedTablePropertiesCollector; | |
126 | Rep* rep_; | |
127 | ||
128 | // Advanced operation: flush any buffered key/value pairs to file. | |
129 | // Can be used to ensure that two adjacent entries never live in | |
130 | // the same data block. Most clients should not need to use this method. | |
131 | // REQUIRES: Finish(), Abandon() have not been called | |
132 | void Flush(); | |
133 | ||
134 | // Some compression libraries fail when the raw size is bigger than int. If | |
135 | // uncompressed size is bigger than kCompressionSizeLimit, don't compress it | |
136 | const uint64_t kCompressionSizeLimit = std::numeric_limits<int>::max(); | |
7c673cae FG |
137 | }; |
138 | ||
494da23a | 139 | Slice CompressBlock(const Slice& raw, const CompressionInfo& info, |
7c673cae | 140 | CompressionType* type, uint32_t format_version, |
494da23a TL |
141 | bool do_sample, std::string* compressed_output, |
142 | std::string* sampled_output_fast, | |
143 | std::string* sampled_output_slow); | |
7c673cae FG |
144 | |
145 | } // namespace rocksdb |