hash_index_reader.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  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).
  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. #include "table/block_based/hash_index_reader.h"
  10. #include "table/block_fetcher.h"
  11. #include "table/meta_blocks.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. Status HashIndexReader::Create(const BlockBasedTable* table,
  14. const ReadOptions& ro,
  15. FilePrefetchBuffer* prefetch_buffer,
  16. InternalIterator* meta_index_iter,
  17. bool use_cache, bool prefetch, bool pin,
  18. BlockCacheLookupContext* lookup_context,
  19. std::unique_ptr<IndexReader>* index_reader) {
  20. assert(table != nullptr);
  21. assert(index_reader != nullptr);
  22. assert(!pin || prefetch);
  23. const BlockBasedTable::Rep* rep = table->get_rep();
  24. assert(rep != nullptr);
  25. CachableEntry<Block> index_block;
  26. if (prefetch || !use_cache) {
  27. const Status s =
  28. ReadIndexBlock(table, prefetch_buffer, ro, use_cache,
  29. /*get_context=*/nullptr, lookup_context, &index_block);
  30. if (!s.ok()) {
  31. return s;
  32. }
  33. if (use_cache && !pin) {
  34. index_block.Reset();
  35. }
  36. }
  37. // Note, failure to create prefix hash index does not need to be a
  38. // hard error. We can still fall back to the original binary search index.
  39. // So, Create will succeed regardless, from this point on.
  40. index_reader->reset(new HashIndexReader(table, std::move(index_block)));
  41. // Get prefixes block
  42. BlockHandle prefixes_handle;
  43. Status s =
  44. FindMetaBlock(meta_index_iter, kHashIndexPrefixesBlock, &prefixes_handle);
  45. if (!s.ok()) {
  46. // TODO: log error
  47. return Status::OK();
  48. }
  49. // Get index metadata block
  50. BlockHandle prefixes_meta_handle;
  51. s = FindMetaBlock(meta_index_iter, kHashIndexPrefixesMetadataBlock,
  52. &prefixes_meta_handle);
  53. if (!s.ok()) {
  54. // TODO: log error
  55. return Status::OK();
  56. }
  57. RandomAccessFileReader* const file = rep->file.get();
  58. const Footer& footer = rep->footer;
  59. const ImmutableOptions& ioptions = rep->ioptions;
  60. const PersistentCacheOptions& cache_options = rep->persistent_cache_options;
  61. MemoryAllocator* const memory_allocator =
  62. GetMemoryAllocator(rep->table_options);
  63. // Read contents for the blocks
  64. BlockContents prefixes_contents;
  65. BlockFetcher prefixes_block_fetcher(
  66. file, prefetch_buffer, footer, ro, prefixes_handle, &prefixes_contents,
  67. ioptions, true /*decompress*/, true /*maybe_compressed*/,
  68. BlockType::kHashIndexPrefixes, rep->decompressor.get(), cache_options,
  69. memory_allocator);
  70. s = prefixes_block_fetcher.ReadBlockContents();
  71. if (!s.ok()) {
  72. return s;
  73. }
  74. BlockContents prefixes_meta_contents;
  75. BlockFetcher prefixes_meta_block_fetcher(
  76. file, prefetch_buffer, footer, ro, prefixes_meta_handle,
  77. &prefixes_meta_contents, ioptions, true /*decompress*/,
  78. true /*maybe_compressed*/, BlockType::kHashIndexMetadata,
  79. rep->decompressor.get(), cache_options, memory_allocator);
  80. s = prefixes_meta_block_fetcher.ReadBlockContents();
  81. if (!s.ok()) {
  82. // TODO: log error
  83. return Status::OK();
  84. }
  85. BlockPrefixIndex* prefix_index = nullptr;
  86. assert(rep->table_prefix_extractor);
  87. s = BlockPrefixIndex::Create(rep->table_prefix_extractor.get(),
  88. prefixes_contents.data,
  89. prefixes_meta_contents.data, &prefix_index);
  90. // TODO: log error
  91. if (s.ok()) {
  92. HashIndexReader* const hash_index_reader =
  93. static_cast<HashIndexReader*>(index_reader->get());
  94. hash_index_reader->prefix_index_.reset(prefix_index);
  95. }
  96. return Status::OK();
  97. }
  98. InternalIteratorBase<IndexValue>* HashIndexReader::NewIterator(
  99. const ReadOptions& read_options, bool disable_prefix_seek,
  100. IndexBlockIter* iter, GetContext* get_context,
  101. BlockCacheLookupContext* lookup_context) {
  102. const BlockBasedTable::Rep* rep = table()->get_rep();
  103. CachableEntry<Block> index_block;
  104. const Status s = GetOrReadIndexBlock(get_context, lookup_context,
  105. &index_block, read_options);
  106. if (!s.ok()) {
  107. if (iter != nullptr) {
  108. iter->Invalidate(s);
  109. return iter;
  110. }
  111. return NewErrorInternalIterator<IndexValue>(s);
  112. }
  113. Statistics* kNullStats = nullptr;
  114. const bool total_order_seek =
  115. read_options.total_order_seek || disable_prefix_seek;
  116. // We don't return pinned data from index blocks, so no need
  117. // to set `block_contents_pinned`.
  118. auto it = index_block.GetValue()->NewIndexIterator(
  119. internal_comparator()->user_comparator(),
  120. rep->get_global_seqno(BlockType::kIndex), iter, kNullStats,
  121. total_order_seek, index_has_first_key(), index_key_includes_seq(),
  122. index_value_is_full(), false /* block_contents_pinned */,
  123. user_defined_timestamps_persisted(), prefix_index_.get());
  124. assert(it != nullptr);
  125. index_block.TransferTo(it);
  126. return it;
  127. }
  128. } // namespace ROCKSDB_NAMESPACE