2
2
// This source code is licensed under the BSD-style license found in the
3
3
// LICENSE file in the root directory of this source tree. An additional grant
4
4
// of patent rights can be found in the PATENTS file in the same directory.
5
-
6
5
#include " table/meta_blocks.h"
7
6
8
7
#include < map>
8
+ #include < string>
9
9
10
10
#include " rocksdb/table.h"
11
+ #include " rocksdb/table_properties.h"
11
12
#include " table/block.h"
12
13
#include " table/format.h"
13
14
#include " util/coding.h"
@@ -104,9 +105,8 @@ bool NotifyCollectTableCollectorsOnAdd(
104
105
Status s = collector->Add (key, value);
105
106
all_succeeded = all_succeeded && s.ok ();
106
107
if (!s.ok ()) {
107
- LogPropertiesCollectionError (
108
- info_log, " Add" , /* method */ collector->Name ()
109
- );
108
+ LogPropertiesCollectionError (info_log, " Add" /* method */ ,
109
+ collector->Name ());
110
110
}
111
111
}
112
112
return all_succeeded;
@@ -123,9 +123,8 @@ bool NotifyCollectTableCollectorsOnFinish(
123
123
124
124
all_succeeded = all_succeeded && s.ok ();
125
125
if (!s.ok ()) {
126
- LogPropertiesCollectionError (
127
- info_log, " Finish" , /* method */ collector->Name ()
128
- );
126
+ LogPropertiesCollectionError (info_log, " Finish" /* method */ ,
127
+ collector->Name ());
129
128
} else {
130
129
builder->Add (user_collected_properties);
131
130
}
@@ -151,37 +150,29 @@ Status ReadProperties(
151
150
BlockContents block_contents;
152
151
ReadOptions read_options;
153
152
read_options.verify_checksums = false ;
154
- Status s = ReadBlockContents (
155
- file,
156
- read_options,
157
- handle,
158
- &block_contents,
159
- env,
160
- false
161
- );
153
+ Status s = ReadBlockContents (file, read_options, handle, &block_contents, env,
154
+ false );
162
155
163
156
if (!s.ok ()) {
164
157
return s;
165
158
}
166
159
167
160
Block properties_block (block_contents);
168
161
std::unique_ptr<Iterator> iter (
169
- properties_block.NewIterator (BytewiseComparator ())
170
- );
162
+ properties_block.NewIterator (BytewiseComparator ()));
171
163
172
164
// All pre-defined properties of type uint64_t
173
165
std::unordered_map<std::string, uint64_t *> predefined_uint64_properties = {
174
- { TablePropertiesNames::kDataSize , &table_properties->data_size },
175
- { TablePropertiesNames::kIndexSize , &table_properties->index_size },
176
- { TablePropertiesNames::kFilterSize , &table_properties->filter_size },
177
- { TablePropertiesNames::kRawKeySize , &table_properties->raw_key_size },
178
- { TablePropertiesNames::kRawValueSize , &table_properties->raw_value_size },
179
- { TablePropertiesNames::kNumDataBlocks ,
180
- &table_properties->num_data_blocks },
181
- { TablePropertiesNames::kNumEntries , &table_properties->num_entries },
182
- { TablePropertiesNames::kFormatVersion , &table_properties->format_version },
183
- { TablePropertiesNames::kFixedKeyLen , &table_properties->fixed_key_len },
184
- };
166
+ {TablePropertiesNames::kDataSize , &table_properties->data_size },
167
+ {TablePropertiesNames::kIndexSize , &table_properties->index_size },
168
+ {TablePropertiesNames::kFilterSize , &table_properties->filter_size },
169
+ {TablePropertiesNames::kRawKeySize , &table_properties->raw_key_size },
170
+ {TablePropertiesNames::kRawValueSize , &table_properties->raw_value_size },
171
+ {TablePropertiesNames::kNumDataBlocks ,
172
+ &table_properties->num_data_blocks },
173
+ {TablePropertiesNames::kNumEntries , &table_properties->num_entries },
174
+ {TablePropertiesNames::kFormatVersion , &table_properties->format_version },
175
+ {TablePropertiesNames::kFixedKeyLen , &table_properties->fixed_key_len }};
185
176
186
177
std::string last_key;
187
178
for (iter->SeekToFirst (); iter->Valid (); iter->Next ()) {
@@ -192,10 +183,8 @@ Status ReadProperties(
192
183
193
184
auto key = iter->key ().ToString ();
194
185
// properties block is strictly sorted with no duplicate key.
195
- assert (
196
- last_key.empty () ||
197
- BytewiseComparator ()->Compare (key, last_key) > 0
198
- );
186
+ assert (last_key.empty () ||
187
+ BytewiseComparator ()->Compare (key, last_key) > 0 );
199
188
last_key = key;
200
189
201
190
auto raw_val = iter->value ();
@@ -218,8 +207,7 @@ Status ReadProperties(
218
207
} else {
219
208
// handle user-collected properties
220
209
table_properties->user_collected_properties .insert (
221
- std::make_pair (key, raw_val.ToString ())
222
- );
210
+ {key, raw_val.ToString ()});
223
211
}
224
212
}
225
213
@@ -244,43 +232,54 @@ Status ReadTableProperties(
244
232
BlockContents metaindex_contents;
245
233
ReadOptions read_options;
246
234
read_options.verify_checksums = false ;
247
- s = ReadBlockContents (
248
- file,
249
- read_options,
250
- metaindex_handle,
251
- &metaindex_contents,
252
- env,
253
- false
254
- );
235
+ s = ReadBlockContents (file, read_options, metaindex_handle,
236
+ &metaindex_contents, env, false );
255
237
if (!s.ok ()) {
256
238
return s;
257
239
}
258
240
Block metaindex_block (metaindex_contents);
259
241
std::unique_ptr<Iterator> meta_iter (
260
- metaindex_block.NewIterator (BytewiseComparator ())
261
- );
242
+ metaindex_block.NewIterator (BytewiseComparator ()));
262
243
263
244
// -- Read property block
264
245
meta_iter->Seek (kPropertiesBlock );
265
246
TableProperties table_properties;
266
247
if (meta_iter->Valid () &&
267
248
meta_iter->key () == kPropertiesBlock &&
268
249
meta_iter->status ().ok ()) {
269
- s = ReadProperties (
270
- meta_iter->value (),
271
- file,
272
- env,
273
- info_log,
274
- properties
275
- );
250
+ s = ReadProperties (meta_iter->value (), file, env, info_log, properties);
276
251
} else {
277
252
s = Status::Corruption (
278
- " Unable to read the property block from the plain table"
279
- );
253
+ " Unable to read the property block from the plain table" );
280
254
}
281
255
282
256
return s;
283
257
}
284
258
259
+ Status ReadTableMagicNumber (const std::string& file_path,
260
+ const Options& options,
261
+ const EnvOptions& env_options,
262
+ uint64_t * table_magic_number) {
263
+ unique_ptr<RandomAccessFile> file;
264
+ Status s = options.env ->NewRandomAccessFile (file_path, &file, env_options);
265
+ if (!s.ok ()) {
266
+ return s;
267
+ }
268
+
269
+ uint64_t file_size;
270
+ options.env ->GetFileSize (file_path, &file_size);
271
+ if (file_size < Footer::kEncodedLength ) {
272
+ return Status::InvalidArgument (" file is too short to be an sstable" );
273
+ }
274
+
275
+ Footer footer;
276
+ s = ReadFooterFromFile (file.get (), file_size, &footer);
277
+ if (!s.ok ()) {
278
+ return s;
279
+ }
280
+
281
+ *table_magic_number = footer.table_magic_number ();
282
+ return Status::OK ();
283
+ }
285
284
286
285
} // namespace rocksdb
0 commit comments