index_reader_common.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/index_reader_common.h"
  10. #include "block_cache.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. Status BlockBasedTable::IndexReaderCommon::ReadIndexBlock(
  13. const BlockBasedTable* table, FilePrefetchBuffer* prefetch_buffer,
  14. const ReadOptions& read_options, bool use_cache, GetContext* get_context,
  15. BlockCacheLookupContext* lookup_context,
  16. CachableEntry<Block>* index_block) {
  17. PERF_TIMER_GUARD(read_index_block_nanos);
  18. assert(table != nullptr);
  19. assert(index_block != nullptr);
  20. assert(index_block->IsEmpty());
  21. const Rep* const rep = table->get_rep();
  22. assert(rep != nullptr);
  23. const Status s = table->RetrieveBlock(
  24. prefetch_buffer, read_options, rep->index_handle, rep->decompressor.get(),
  25. &index_block->As<Block_kIndex>(), get_context, lookup_context,
  26. /* for_compaction */ false, use_cache,
  27. /* async_read */ false, /* use_block_cache_for_lookup */ true);
  28. return s;
  29. }
  30. Status BlockBasedTable::IndexReaderCommon::GetOrReadIndexBlock(
  31. GetContext* get_context, BlockCacheLookupContext* lookup_context,
  32. CachableEntry<Block>* index_block, const ReadOptions& ro) const {
  33. assert(index_block != nullptr);
  34. if (!index_block_.IsEmpty()) {
  35. index_block->SetUnownedValue(index_block_.GetValue());
  36. return Status::OK();
  37. }
  38. return ReadIndexBlock(table_, /*prefetch_buffer=*/nullptr, ro,
  39. cache_index_blocks(), get_context, lookup_context,
  40. index_block);
  41. }
  42. void BlockBasedTable::IndexReaderCommon::EraseFromCacheBeforeDestruction(
  43. uint32_t uncache_aggressiveness) {
  44. if (uncache_aggressiveness > 0) {
  45. if (index_block_.IsCached()) {
  46. index_block_.ResetEraseIfLastRef();
  47. } else {
  48. table()->EraseFromCache(table()->get_rep()->index_handle);
  49. }
  50. }
  51. }
  52. } // namespace ROCKSDB_NAMESPACE