index_reader_common.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #pragma once
  10. #include "table/block_based/block_based_table_reader.h"
  11. #include "table/block_based/reader_common.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. // Encapsulates common functionality for the various index reader
  14. // implementations. Provides access to the index block regardless of whether
  15. // it is owned by the reader or stored in the cache, or whether it is pinned
  16. // in the cache or not.
  17. class BlockBasedTable::IndexReaderCommon : public BlockBasedTable::IndexReader {
  18. public:
  19. IndexReaderCommon(const BlockBasedTable* t,
  20. CachableEntry<Block>&& index_block)
  21. : table_(t), index_block_(std::move(index_block)) {
  22. assert(table_ != nullptr);
  23. }
  24. void EraseFromCacheBeforeDestruction(
  25. uint32_t /*uncache_aggressiveness*/) override;
  26. protected:
  27. static Status ReadIndexBlock(const BlockBasedTable* table,
  28. FilePrefetchBuffer* prefetch_buffer,
  29. const ReadOptions& read_options, bool use_cache,
  30. GetContext* get_context,
  31. BlockCacheLookupContext* lookup_context,
  32. CachableEntry<Block>* index_block);
  33. const BlockBasedTable* table() const { return table_; }
  34. const InternalKeyComparator* internal_comparator() const {
  35. assert(table_ != nullptr);
  36. assert(table_->get_rep() != nullptr);
  37. return &table_->get_rep()->internal_comparator;
  38. }
  39. bool index_has_first_key() const {
  40. assert(table_ != nullptr);
  41. assert(table_->get_rep() != nullptr);
  42. return table_->get_rep()->index_has_first_key;
  43. }
  44. bool index_key_includes_seq() const {
  45. assert(table_ != nullptr);
  46. assert(table_->get_rep() != nullptr);
  47. return table_->get_rep()->index_key_includes_seq;
  48. }
  49. bool index_value_is_full() const {
  50. assert(table_ != nullptr);
  51. assert(table_->get_rep() != nullptr);
  52. return table_->get_rep()->index_value_is_full;
  53. }
  54. bool cache_index_blocks() const {
  55. assert(table_ != nullptr);
  56. assert(table_->get_rep() != nullptr);
  57. return table_->get_rep()->table_options.cache_index_and_filter_blocks;
  58. }
  59. bool user_defined_timestamps_persisted() const {
  60. assert(table_ != nullptr);
  61. assert(table_->get_rep() != nullptr);
  62. return table_->get_rep()->user_defined_timestamps_persisted;
  63. }
  64. Status GetOrReadIndexBlock(GetContext* get_context,
  65. BlockCacheLookupContext* lookup_context,
  66. CachableEntry<Block>* index_block,
  67. const ReadOptions& read_options) const;
  68. size_t ApproximateIndexBlockMemoryUsage() const {
  69. assert(!index_block_.GetOwnValue() || index_block_.GetValue() != nullptr);
  70. return index_block_.GetOwnValue()
  71. ? index_block_.GetValue()->ApproximateMemoryUsage()
  72. : 0;
  73. }
  74. private:
  75. const BlockBasedTable* table_;
  76. CachableEntry<Block> index_block_;
  77. };
  78. } // namespace ROCKSDB_NAMESPACE