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 for the first key of each block.
14 // This class can be viewed as a thin wrapper for `Block` class which already
15 // supports binary search.
16 class BinarySearchIndexReader : public BlockBasedTable::IndexReaderCommon {
17  public:
18   // Read index from the file and create an intance for
19   // `BinarySearchIndexReader`.
20   // On success, index_reader will be populated; otherwise it will remain
21   // unmodified.
22   static Status Create(const BlockBasedTable* table, const ReadOptions& ro,
23                        FilePrefetchBuffer* prefetch_buffer, bool use_cache,
24                        bool prefetch, bool pin,
25                        BlockCacheLookupContext* lookup_context,
26                        std::unique_ptr<IndexReader>* index_reader);
27 
28   InternalIteratorBase<IndexValue>* NewIterator(
29       const ReadOptions& read_options, bool /* disable_prefix_seek */,
30       IndexBlockIter* iter, GetContext* get_context,
31       BlockCacheLookupContext* lookup_context) override;
32 
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<BinarySearchIndexReader*>(this));
37 #else
38     usage += sizeof(*this);
39 #endif  // ROCKSDB_MALLOC_USABLE_SIZE
40     return usage;
41   }
42 
43  private:
BinarySearchIndexReader(const BlockBasedTable * t,CachableEntry<Block> && index_block)44   BinarySearchIndexReader(const BlockBasedTable* t,
45                           CachableEntry<Block>&& index_block)
46       : IndexReaderCommon(t, std::move(index_block)) {}
47 };
48 }  // namespace ROCKSDB_NAMESPACE
49