26
26
#include " util/coding.h"
27
27
#include " util/perf_context_imp.h"
28
28
#include " util/stop_watch.h"
29
+ #include " table/block_based_table_options.h"
29
30
30
31
namespace rocksdb {
31
32
@@ -45,9 +46,9 @@ struct BlockBasedTable::Rep {
45
46
Status status;
46
47
unique_ptr<RandomAccessFile> file;
47
48
char cache_key_prefix[kMaxCacheKeyPrefixSize ];
48
- size_t cache_key_prefix_size;
49
+ size_t cache_key_prefix_size = 0 ;
49
50
char compressed_cache_key_prefix[kMaxCacheKeyPrefixSize ];
50
- size_t compressed_cache_key_prefix_size;
51
+ size_t compressed_cache_key_prefix_size = 0 ;
51
52
52
53
// Handle to metaindex_block: saved from footer
53
54
BlockHandle metaindex_handle;
@@ -220,20 +221,21 @@ Cache::Handle* GetFromBlockCache(
220
221
221
222
} // end of anonymous namespace
222
223
223
- Status BlockBasedTable::Open (const Options& options,
224
- const EnvOptions& soptions ,
225
- unique_ptr<RandomAccessFile> && file,
226
- uint64_t size ,
224
+ Status BlockBasedTable::Open (const Options& options, const EnvOptions& soptions,
225
+ const BlockBasedTableOptions& table_options ,
226
+ unique_ptr<RandomAccessFile>&& file,
227
+ uint64_t file_size ,
227
228
unique_ptr<TableReader>* table_reader) {
228
229
table_reader->reset ();
229
- if (size < Footer::kEncodedLength ) {
230
+
231
+ if (file_size < Footer::kEncodedLength ) {
230
232
return Status::InvalidArgument (" file is too short to be an sstable" );
231
233
}
232
234
233
235
char footer_space[Footer::kEncodedLength ];
234
236
Slice footer_input;
235
- Status s = file->Read (size - Footer:: kEncodedLength , Footer::kEncodedLength ,
236
- &footer_input, footer_space);
237
+ Status s = file->Read (file_size - Footer::kEncodedLength ,
238
+ Footer:: kEncodedLength , &footer_input, footer_space);
237
239
if (!s.ok ()) return s;
238
240
239
241
// Check that we actually read the whole footer from the file. It may be
@@ -277,11 +279,21 @@ Status BlockBasedTable::Open(const Options& options,
277
279
}
278
280
}
279
281
280
- // Initialize index/filter blocks. If block cache is not specified,
281
- // these blocks will be kept in member variables in Rep, which will
282
- // reside in the memory as long as this table object is alive; otherwise
283
- // they will be added to block cache.
284
- if (!options.block_cache ) {
282
+ // Will use block cache for index/filter blocks access?
283
+ if (options.block_cache && table_options.cache_index_and_filter_blocks ) {
284
+ // Call IndexBlockReader() to implicitly add index to the block_cache
285
+ unique_ptr<Iterator> iter (new_table->IndexBlockReader (ReadOptions ()));
286
+ s = iter->status ();
287
+
288
+ if (s.ok ()) {
289
+ // Call GetFilter() to implicitly add filter to the block_cache
290
+ auto filter_entry = new_table->GetFilter ();
291
+ filter_entry.Release (options.block_cache .get ());
292
+ }
293
+ } else {
294
+ // If we don't use block cache for index/filter blocks access, we'll
295
+ // pre-load these blocks, which will kept in member variables in Rep
296
+ // and with a same life-time as this table object.
285
297
Block* index_block = nullptr ;
286
298
// TODO: we never really verify check sum for index block
287
299
s = ReadBlockFromFile (
@@ -309,18 +321,7 @@ Status BlockBasedTable::Open(const Options& options,
309
321
} else {
310
322
delete index_block;
311
323
}
312
- } else {
313
- // Call IndexBlockReader() to implicitly add index to the block_cache
314
- unique_ptr<Iterator> iter (
315
- new_table->IndexBlockReader (ReadOptions ())
316
- );
317
- s = iter->status ();
318
324
319
- if (s.ok ()) {
320
- // Call GetFilter() to implicitly add filter to the block_cache
321
- auto filter_entry = new_table->GetFilter ();
322
- filter_entry.Release (options.block_cache .get ());
323
- }
324
325
}
325
326
326
327
if (s.ok ()) {
@@ -836,7 +837,6 @@ BlockBasedTable::GetFilter(bool no_io) const {
836
837
// Get the iterator from the index block.
837
838
Iterator* BlockBasedTable::IndexBlockReader (const ReadOptions& options) const {
838
839
if (rep_->index_block ) {
839
- assert (!rep_->options .block_cache );
840
840
return rep_->index_block ->NewIterator (rep_->options .comparator );
841
841
}
842
842
0 commit comments