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 
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,
21                        FilePrefetchBuffer* prefetch_buffer, bool use_cache,
22                        bool prefetch, bool pin,
23                        BlockCacheLookupContext* lookup_context,
24                        std::unique_ptr<IndexReader>* index_reader);
25 
26   // return a two-level iterator: first level is on the partition index
27   InternalIteratorBase<IndexValue>* NewIterator(
28       const ReadOptions& read_options, bool /* disable_prefix_seek */,
29       IndexBlockIter* iter, GetContext* get_context,
30       BlockCacheLookupContext* lookup_context) override;
31 
32   void CacheDependencies(bool pin) override;
ApproximateMemoryUsage()33   size_t ApproximateMemoryUsage() const override {
34     size_t usage = ApproximateIndexBlockMemoryUsage();
35 #ifdef ROCKSDB_MALLOC_USABLE_SIZE
36     usage += malloc_usable_size(const_cast<PartitionIndexReader*>(this));
37 #else
38     usage += sizeof(*this);
39 #endif  // ROCKSDB_MALLOC_USABLE_SIZE
40     // TODO(myabandeh): more accurate estimate of partition_map_ mem usage
41     return usage;
42   }
43 
44  private:
PartitionIndexReader(const BlockBasedTable * t,CachableEntry<Block> && index_block)45   PartitionIndexReader(const BlockBasedTable* t,
46                        CachableEntry<Block>&& index_block)
47       : IndexReaderCommon(t, std::move(index_block)) {}
48 
49   std::unordered_map<uint64_t, CachableEntry<Block>> partition_map_;
50 };
51 }  // namespace ROCKSDB_NAMESPACE
52