partitioned_index_reader.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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/index_reader_common.h"
  11. #include "util/hash_containers.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. // Index that allows binary search lookup in a two-level index structure.
  14. class PartitionIndexReader : public BlockBasedTable::IndexReaderCommon {
  15. public:
  16. // Read the partition index from the file and create an instance for
  17. // `PartitionIndexReader`.
  18. // On success, index_reader will be populated; otherwise it will remain
  19. // unmodified.
  20. static Status Create(const BlockBasedTable* table, const ReadOptions& ro,
  21. FilePrefetchBuffer* prefetch_buffer, bool use_cache,
  22. bool prefetch, bool pin,
  23. BlockCacheLookupContext* lookup_context,
  24. std::unique_ptr<IndexReader>* index_reader);
  25. // return a two-level iterator: first level is on the partition index
  26. InternalIteratorBase<IndexValue>* NewIterator(
  27. const ReadOptions& read_options, bool /* disable_prefix_seek */,
  28. IndexBlockIter* iter, GetContext* get_context,
  29. BlockCacheLookupContext* lookup_context) override;
  30. Status CacheDependencies(const ReadOptions& ro, bool pin,
  31. FilePrefetchBuffer* tail_prefetch_buffer) override;
  32. size_t ApproximateMemoryUsage() const override {
  33. size_t usage = ApproximateIndexBlockMemoryUsage();
  34. #ifdef ROCKSDB_MALLOC_USABLE_SIZE
  35. usage += malloc_usable_size(const_cast<PartitionIndexReader*>(this));
  36. #else
  37. usage += sizeof(*this);
  38. #endif // ROCKSDB_MALLOC_USABLE_SIZE
  39. // TODO(myabandeh): more accurate estimate of partition_map_ mem usage
  40. return usage;
  41. }
  42. void EraseFromCacheBeforeDestruction(
  43. uint32_t /*uncache_aggressiveness*/) override;
  44. private:
  45. PartitionIndexReader(const BlockBasedTable* t,
  46. CachableEntry<Block>&& index_block)
  47. : IndexReaderCommon(t, std::move(index_block)) {}
  48. // For partition blocks pinned in cache. This is expected to be "all or
  49. // none" so that !partition_map_.empty() can use an iterator expecting
  50. // all partitions to be saved here.
  51. UnorderedMap<uint64_t, CachableEntry<Block>> partition_map_;
  52. };
  53. } // namespace ROCKSDB_NAMESPACE