/Users/deen/code/yugabyte-db/src/yb/rocksdb/table/adaptive_table_factory.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
2 | | // Use of this source code is governed by a BSD-style license that can be |
3 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
4 | | // |
5 | | // The following only applies to changes made to this file as part of YugaByte development. |
6 | | // |
7 | | // Portions Copyright (c) YugaByte, Inc. |
8 | | // |
9 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
10 | | // in compliance with the License. You may obtain a copy of the License at |
11 | | // |
12 | | // http://www.apache.org/licenses/LICENSE-2.0 |
13 | | // |
14 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
15 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
16 | | // or implied. See the License for the specific language governing permissions and limitations |
17 | | // under the License. |
18 | | // |
19 | | |
20 | | #ifndef ROCKSDB_LITE |
21 | | |
22 | | #include "yb/rocksdb/table/adaptive_table_factory.h" |
23 | | |
24 | | #include "yb/rocksdb/table/format.h" |
25 | | |
26 | | namespace rocksdb { |
27 | | |
28 | | AdaptiveTableFactory::AdaptiveTableFactory( |
29 | | std::shared_ptr<TableFactory> table_factory_to_write, |
30 | | std::shared_ptr<TableFactory> block_based_table_factory, |
31 | | std::shared_ptr<TableFactory> plain_table_factory) |
32 | | : table_factory_to_write_(table_factory_to_write), |
33 | | block_based_table_factory_(block_based_table_factory), |
34 | 2 | plain_table_factory_(plain_table_factory) { |
35 | 2 | if (!table_factory_to_write_) { |
36 | 0 | table_factory_to_write_ = block_based_table_factory_; |
37 | 0 | } |
38 | 2 | if (!plain_table_factory_) { |
39 | 2 | plain_table_factory_.reset(NewPlainTableFactory()); |
40 | 2 | } |
41 | 2 | if (!block_based_table_factory_) { |
42 | 2 | block_based_table_factory_.reset(NewBlockBasedTableFactory()); |
43 | 2 | } |
44 | 2 | } |
45 | | |
46 | | extern const uint64_t kPlainTableMagicNumber; |
47 | | extern const uint64_t kLegacyPlainTableMagicNumber; |
48 | | extern const uint64_t kBlockBasedTableMagicNumber; |
49 | | extern const uint64_t kLegacyBlockBasedTableMagicNumber; |
50 | | |
51 | | Status AdaptiveTableFactory::NewTableReader( |
52 | | const TableReaderOptions& table_reader_options, |
53 | | unique_ptr<RandomAccessFileReader>&& file, uint64_t file_size, |
54 | 8 | unique_ptr<TableReader>* table) const { |
55 | 8 | Footer footer; |
56 | 8 | auto s = ReadFooterFromFile(file.get(), file_size, &footer); |
57 | 8 | if (!s.ok()) { |
58 | 0 | return s; |
59 | 0 | } |
60 | 8 | if (footer.table_magic_number() == kPlainTableMagicNumber || |
61 | 8 | footer.table_magic_number() == kLegacyPlainTableMagicNumber4 ) { |
62 | 4 | return plain_table_factory_->NewTableReader( |
63 | 4 | table_reader_options, std::move(file), file_size, table); |
64 | 4 | } else if (footer.table_magic_number() == kBlockBasedTableMagicNumber || |
65 | 4 | footer.table_magic_number() == kLegacyBlockBasedTableMagicNumber0 ) { |
66 | 4 | return block_based_table_factory_->NewTableReader( |
67 | 4 | table_reader_options, std::move(file), file_size, table); |
68 | 4 | } else { |
69 | 0 | return STATUS(NotSupported, "Unidentified table format"); |
70 | 0 | } |
71 | 8 | } |
72 | | |
73 | | TableBuilder* AdaptiveTableFactory::NewTableBuilder( |
74 | | const TableBuilderOptions &table_builder_options, uint32_t column_family_id, |
75 | 2 | WritableFileWriter* base_file, WritableFileWriter* data_file) const { |
76 | 2 | return table_factory_to_write_->NewTableBuilder(table_builder_options, |
77 | 2 | column_family_id, base_file, data_file); |
78 | 2 | } |
79 | | |
80 | 2 | bool AdaptiveTableFactory::IsSplitSstForWriteSupported() const { |
81 | 2 | return table_factory_to_write_->IsSplitSstForWriteSupported(); |
82 | 2 | } |
83 | | |
84 | 4 | std::string AdaptiveTableFactory::GetPrintableTableOptions() const { |
85 | 4 | std::string ret; |
86 | 4 | ret.reserve(20000); |
87 | 4 | const int kBufferSize = 200; |
88 | 4 | char buffer[kBufferSize]; |
89 | | |
90 | 4 | if (!table_factory_to_write_) { |
91 | 0 | snprintf(buffer, kBufferSize, " write factory (%s) options:\n%s\n", |
92 | 0 | table_factory_to_write_->Name(), |
93 | 0 | table_factory_to_write_->GetPrintableTableOptions().c_str()); |
94 | 0 | ret.append(buffer); |
95 | 0 | } |
96 | 4 | if (!plain_table_factory_) { |
97 | 0 | snprintf(buffer, kBufferSize, " %s options:\n%s\n", |
98 | 0 | plain_table_factory_->Name(), |
99 | 0 | plain_table_factory_->GetPrintableTableOptions().c_str()); |
100 | 0 | ret.append(buffer); |
101 | 0 | } |
102 | 4 | if (!block_based_table_factory_) { |
103 | 0 | snprintf(buffer, kBufferSize, " %s options:\n%s\n", |
104 | 0 | block_based_table_factory_->Name(), |
105 | 0 | block_based_table_factory_->GetPrintableTableOptions().c_str()); |
106 | 0 | ret.append(buffer); |
107 | 0 | } |
108 | 4 | return ret; |
109 | 4 | } |
110 | | |
111 | | extern TableFactory* NewAdaptiveTableFactory( |
112 | | std::shared_ptr<TableFactory> table_factory_to_write, |
113 | | std::shared_ptr<TableFactory> block_based_table_factory, |
114 | 2 | std::shared_ptr<TableFactory> plain_table_factory) { |
115 | 2 | return new AdaptiveTableFactory(table_factory_to_write, |
116 | 2 | block_based_table_factory, plain_table_factory); |
117 | 2 | } |
118 | | |
119 | | } // namespace rocksdb |
120 | | #endif // ROCKSDB_LITE |