binary_search_index_reader.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include "table/block_based/binary_search_index_reader.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. Status BinarySearchIndexReader::Create(
  12. const BlockBasedTable* table, const ReadOptions& ro,
  13. FilePrefetchBuffer* prefetch_buffer, bool use_cache, bool prefetch,
  14. bool pin, BlockCacheLookupContext* lookup_context,
  15. std::unique_ptr<IndexReader>* index_reader) {
  16. assert(table != nullptr);
  17. assert(table->get_rep());
  18. assert(!pin || prefetch);
  19. assert(index_reader != nullptr);
  20. CachableEntry<Block> index_block;
  21. if (prefetch || !use_cache) {
  22. const Status s =
  23. ReadIndexBlock(table, prefetch_buffer, ro, use_cache,
  24. /*get_context=*/nullptr, lookup_context, &index_block);
  25. if (!s.ok()) {
  26. return s;
  27. }
  28. if (use_cache && !pin) {
  29. index_block.Reset();
  30. }
  31. }
  32. index_reader->reset(
  33. new BinarySearchIndexReader(table, std::move(index_block)));
  34. return Status::OK();
  35. }
  36. InternalIteratorBase<IndexValue>* BinarySearchIndexReader::NewIterator(
  37. const ReadOptions& read_options, bool /* disable_prefix_seek */,
  38. IndexBlockIter* iter, GetContext* get_context,
  39. BlockCacheLookupContext* lookup_context) {
  40. const BlockBasedTable::Rep* rep = table()->get_rep();
  41. CachableEntry<Block> index_block;
  42. const Status s = GetOrReadIndexBlock(get_context, lookup_context,
  43. &index_block, read_options);
  44. if (!s.ok()) {
  45. if (iter != nullptr) {
  46. iter->Invalidate(s);
  47. return iter;
  48. }
  49. return NewErrorInternalIterator<IndexValue>(s);
  50. }
  51. Statistics* kNullStats = nullptr;
  52. // We don't return pinned data from index blocks, so no need
  53. // to set `block_contents_pinned`.
  54. auto it = index_block.GetValue()->NewIndexIterator(
  55. internal_comparator()->user_comparator(),
  56. rep->get_global_seqno(BlockType::kIndex), iter, kNullStats, true,
  57. index_has_first_key(), index_key_includes_seq(), index_value_is_full(),
  58. false /* block_contents_pinned */, user_defined_timestamps_persisted());
  59. assert(it != nullptr);
  60. index_block.TransferTo(it);
  61. return it;
  62. }
  63. } // namespace ROCKSDB_NAMESPACE