block_fetcher.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "memory/memory_allocator.h"
  11. #include "table/block_based/block.h"
  12. #include "table/block_based/block_type.h"
  13. #include "table/format.h"
  14. namespace ROCKSDB_NAMESPACE {
  15. // Retrieves a single block of a given file. Utilizes the prefetch buffer and/or
  16. // persistent cache provided (if any) to try to avoid reading from the file
  17. // directly. Note that both the prefetch buffer and the persistent cache are
  18. // optional; also, note that the persistent cache may be configured to store either
  19. // compressed or uncompressed blocks.
  20. //
  21. // If the retrieved block is compressed and the do_uncompress flag is set,
  22. // BlockFetcher uncompresses the block (using the uncompression dictionary,
  23. // if provided, to prime the compression algorithm), and returns the resulting
  24. // uncompressed block data. Otherwise, it returns the original block.
  25. //
  26. // Two read options affect the behavior of BlockFetcher: if verify_checksums is
  27. // true, the checksum of the (original) block is checked; if fill_cache is true,
  28. // the block is added to the persistent cache if needed.
  29. //
  30. // Memory for uncompressed and compressed blocks is allocated as needed
  31. // using memory_allocator and memory_allocator_compressed, respectively
  32. // (if provided; otherwise, the default allocator is used).
  33. class BlockFetcher {
  34. public:
  35. BlockFetcher(RandomAccessFileReader* file,
  36. FilePrefetchBuffer* prefetch_buffer, const Footer& footer,
  37. const ReadOptions& read_options, const BlockHandle& handle,
  38. BlockContents* contents, const ImmutableCFOptions& ioptions,
  39. bool do_uncompress, bool maybe_compressed, BlockType block_type,
  40. const UncompressionDict& uncompression_dict,
  41. const PersistentCacheOptions& cache_options,
  42. MemoryAllocator* memory_allocator = nullptr,
  43. MemoryAllocator* memory_allocator_compressed = nullptr,
  44. bool for_compaction = false)
  45. : file_(file),
  46. prefetch_buffer_(prefetch_buffer),
  47. footer_(footer),
  48. read_options_(read_options),
  49. handle_(handle),
  50. contents_(contents),
  51. ioptions_(ioptions),
  52. do_uncompress_(do_uncompress),
  53. maybe_compressed_(maybe_compressed),
  54. block_type_(block_type),
  55. uncompression_dict_(uncompression_dict),
  56. cache_options_(cache_options),
  57. memory_allocator_(memory_allocator),
  58. memory_allocator_compressed_(memory_allocator_compressed),
  59. for_compaction_(for_compaction) {}
  60. Status ReadBlockContents();
  61. CompressionType get_compression_type() const { return compression_type_; }
  62. private:
  63. static const uint32_t kDefaultStackBufferSize = 5000;
  64. RandomAccessFileReader* file_;
  65. FilePrefetchBuffer* prefetch_buffer_;
  66. const Footer& footer_;
  67. const ReadOptions read_options_;
  68. const BlockHandle& handle_;
  69. BlockContents* contents_;
  70. const ImmutableCFOptions& ioptions_;
  71. bool do_uncompress_;
  72. bool maybe_compressed_;
  73. BlockType block_type_;
  74. const UncompressionDict& uncompression_dict_;
  75. const PersistentCacheOptions& cache_options_;
  76. MemoryAllocator* memory_allocator_;
  77. MemoryAllocator* memory_allocator_compressed_;
  78. Status status_;
  79. Slice slice_;
  80. char* used_buf_ = nullptr;
  81. size_t block_size_;
  82. CacheAllocationPtr heap_buf_;
  83. CacheAllocationPtr compressed_buf_;
  84. char stack_buf_[kDefaultStackBufferSize];
  85. bool got_from_prefetch_buffer_ = false;
  86. ROCKSDB_NAMESPACE::CompressionType compression_type_;
  87. bool for_compaction_ = false;
  88. // return true if found
  89. bool TryGetUncompressBlockFromPersistentCache();
  90. // return true if found
  91. bool TryGetFromPrefetchBuffer();
  92. bool TryGetCompressedBlockFromPersistentCache();
  93. void PrepareBufferForBlockFromFile();
  94. // Copy content from used_buf_ to new heap buffer.
  95. void CopyBufferToHeap();
  96. void GetBlockContents();
  97. void InsertCompressedBlockToPersistentCacheIfNeeded();
  98. void InsertUncompressedBlockToPersistentCacheIfNeeded();
  99. void CheckBlockChecksum();
  100. };
  101. } // namespace ROCKSDB_NAMESPACE