]>
Commit | Line | Data |
---|---|---|
f67539c2 | 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. |
7c673cae FG |
2 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
3 | // Use of this source code is governed by a BSD-style license that can be | |
4 | // found in the LICENSE file. See the AUTHORS file for names of contributors. | |
5 | ||
6 | #ifndef ROCKSDB_LITE | |
f67539c2 | 7 | #include "table/adaptive/adaptive_table_factory.h" |
7c673cae | 8 | |
7c673cae | 9 | #include "port/port.h" |
1e59de90 TL |
10 | #include "table/format.h" |
11 | #include "table/table_builder.h" | |
7c673cae | 12 | |
f67539c2 | 13 | namespace ROCKSDB_NAMESPACE { |
7c673cae FG |
14 | |
15 | AdaptiveTableFactory::AdaptiveTableFactory( | |
16 | std::shared_ptr<TableFactory> table_factory_to_write, | |
17 | std::shared_ptr<TableFactory> block_based_table_factory, | |
18 | std::shared_ptr<TableFactory> plain_table_factory, | |
19 | std::shared_ptr<TableFactory> cuckoo_table_factory) | |
20 | : table_factory_to_write_(table_factory_to_write), | |
21 | block_based_table_factory_(block_based_table_factory), | |
22 | plain_table_factory_(plain_table_factory), | |
23 | cuckoo_table_factory_(cuckoo_table_factory) { | |
24 | if (!plain_table_factory_) { | |
25 | plain_table_factory_.reset(NewPlainTableFactory()); | |
26 | } | |
27 | if (!block_based_table_factory_) { | |
28 | block_based_table_factory_.reset(NewBlockBasedTableFactory()); | |
29 | } | |
30 | if (!cuckoo_table_factory_) { | |
31 | cuckoo_table_factory_.reset(NewCuckooTableFactory()); | |
32 | } | |
33 | if (!table_factory_to_write_) { | |
34 | table_factory_to_write_ = block_based_table_factory_; | |
35 | } | |
36 | } | |
37 | ||
38 | extern const uint64_t kPlainTableMagicNumber; | |
39 | extern const uint64_t kLegacyPlainTableMagicNumber; | |
40 | extern const uint64_t kBlockBasedTableMagicNumber; | |
41 | extern const uint64_t kLegacyBlockBasedTableMagicNumber; | |
42 | extern const uint64_t kCuckooTableMagicNumber; | |
43 | ||
44 | Status AdaptiveTableFactory::NewTableReader( | |
20effc67 | 45 | const ReadOptions& ro, const TableReaderOptions& table_reader_options, |
494da23a TL |
46 | std::unique_ptr<RandomAccessFileReader>&& file, uint64_t file_size, |
47 | std::unique_ptr<TableReader>* table, | |
20effc67 | 48 | bool prefetch_index_and_filter_in_cache) const { |
7c673cae | 49 | Footer footer; |
20effc67 TL |
50 | IOOptions opts; |
51 | auto s = ReadFooterFromFile(opts, file.get(), nullptr /* prefetch_buffer */, | |
11fdf7f2 | 52 | file_size, &footer); |
7c673cae FG |
53 | if (!s.ok()) { |
54 | return s; | |
55 | } | |
56 | if (footer.table_magic_number() == kPlainTableMagicNumber || | |
57 | footer.table_magic_number() == kLegacyPlainTableMagicNumber) { | |
58 | return plain_table_factory_->NewTableReader( | |
59 | table_reader_options, std::move(file), file_size, table); | |
60 | } else if (footer.table_magic_number() == kBlockBasedTableMagicNumber || | |
1e59de90 | 61 | footer.table_magic_number() == kLegacyBlockBasedTableMagicNumber) { |
7c673cae | 62 | return block_based_table_factory_->NewTableReader( |
20effc67 TL |
63 | ro, table_reader_options, std::move(file), file_size, table, |
64 | prefetch_index_and_filter_in_cache); | |
7c673cae FG |
65 | } else if (footer.table_magic_number() == kCuckooTableMagicNumber) { |
66 | return cuckoo_table_factory_->NewTableReader( | |
67 | table_reader_options, std::move(file), file_size, table); | |
68 | } else { | |
69 | return Status::NotSupported("Unidentified table format"); | |
70 | } | |
71 | } | |
72 | ||
73 | TableBuilder* AdaptiveTableFactory::NewTableBuilder( | |
1e59de90 | 74 | const TableBuilderOptions& table_builder_options, |
7c673cae | 75 | WritableFileWriter* file) const { |
1e59de90 | 76 | return table_factory_to_write_->NewTableBuilder(table_builder_options, file); |
7c673cae FG |
77 | } |
78 | ||
20effc67 | 79 | std::string AdaptiveTableFactory::GetPrintableOptions() const { |
7c673cae FG |
80 | std::string ret; |
81 | ret.reserve(20000); | |
82 | const int kBufferSize = 200; | |
83 | char buffer[kBufferSize]; | |
84 | ||
85 | if (table_factory_to_write_) { | |
86 | snprintf(buffer, kBufferSize, " write factory (%s) options:\n%s\n", | |
87 | (table_factory_to_write_->Name() ? table_factory_to_write_->Name() | |
88 | : ""), | |
20effc67 | 89 | table_factory_to_write_->GetPrintableOptions().c_str()); |
7c673cae FG |
90 | ret.append(buffer); |
91 | } | |
92 | if (plain_table_factory_) { | |
93 | snprintf(buffer, kBufferSize, " %s options:\n%s\n", | |
94 | plain_table_factory_->Name() ? plain_table_factory_->Name() : "", | |
20effc67 | 95 | plain_table_factory_->GetPrintableOptions().c_str()); |
7c673cae FG |
96 | ret.append(buffer); |
97 | } | |
98 | if (block_based_table_factory_) { | |
99 | snprintf( | |
100 | buffer, kBufferSize, " %s options:\n%s\n", | |
101 | (block_based_table_factory_->Name() ? block_based_table_factory_->Name() | |
102 | : ""), | |
20effc67 | 103 | block_based_table_factory_->GetPrintableOptions().c_str()); |
7c673cae FG |
104 | ret.append(buffer); |
105 | } | |
106 | if (cuckoo_table_factory_) { | |
107 | snprintf(buffer, kBufferSize, " %s options:\n%s\n", | |
108 | cuckoo_table_factory_->Name() ? cuckoo_table_factory_->Name() : "", | |
20effc67 | 109 | cuckoo_table_factory_->GetPrintableOptions().c_str()); |
7c673cae FG |
110 | ret.append(buffer); |
111 | } | |
112 | return ret; | |
113 | } | |
114 | ||
115 | extern TableFactory* NewAdaptiveTableFactory( | |
116 | std::shared_ptr<TableFactory> table_factory_to_write, | |
117 | std::shared_ptr<TableFactory> block_based_table_factory, | |
118 | std::shared_ptr<TableFactory> plain_table_factory, | |
119 | std::shared_ptr<TableFactory> cuckoo_table_factory) { | |
120 | return new AdaptiveTableFactory(table_factory_to_write, | |
1e59de90 TL |
121 | block_based_table_factory, |
122 | plain_table_factory, cuckoo_table_factory); | |
7c673cae FG |
123 | } |
124 | ||
f67539c2 | 125 | } // namespace ROCKSDB_NAMESPACE |
7c673cae | 126 | #endif // ROCKSDB_LITE |