binary_search_index_reader.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. namespace ROCKSDB_NAMESPACE {
  12. // Index that allows binary search lookup for the first key of each block.
  13. // This class can be viewed as a thin wrapper for `Block` class which already
  14. // supports binary search.
  15. class BinarySearchIndexReader : public BlockBasedTable::IndexReaderCommon {
  16. public:
  17. // Read index from the file and create an intance for
  18. // `BinarySearchIndexReader`.
  19. // On success, index_reader will be populated; otherwise it will remain
  20. // unmodified.
  21. static Status Create(const BlockBasedTable* table, const ReadOptions& ro,
  22. FilePrefetchBuffer* prefetch_buffer, bool use_cache,
  23. bool prefetch, bool pin,
  24. BlockCacheLookupContext* lookup_context,
  25. std::unique_ptr<IndexReader>* index_reader);
  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. size_t ApproximateMemoryUsage() const override {
  31. size_t usage = ApproximateIndexBlockMemoryUsage();
  32. #ifdef ROCKSDB_MALLOC_USABLE_SIZE
  33. usage += malloc_usable_size(const_cast<BinarySearchIndexReader*>(this));
  34. #else
  35. usage += sizeof(*this);
  36. #endif // ROCKSDB_MALLOC_USABLE_SIZE
  37. return usage;
  38. }
  39. private:
  40. BinarySearchIndexReader(const BlockBasedTable* t,
  41. CachableEntry<Block>&& index_block)
  42. : IndexReaderCommon(t, std::move(index_block)) {}
  43. };
  44. } // namespace ROCKSDB_NAMESPACE