table_reader.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <memory>
  11. #include "db/range_tombstone_fragmenter.h"
  12. #include "rocksdb/slice_transform.h"
  13. #include "table/get_context.h"
  14. #include "table/internal_iterator.h"
  15. #include "table/multiget_context.h"
  16. #include "table/table_reader_caller.h"
  17. namespace ROCKSDB_NAMESPACE {
  18. class Iterator;
  19. struct ParsedInternalKey;
  20. class Slice;
  21. class Arena;
  22. struct ReadOptions;
  23. struct TableProperties;
  24. class GetContext;
  25. class MultiGetContext;
  26. // A Table (also referred to as SST) is a sorted map from strings to strings.
  27. // Tables are immutable and persistent. A Table may be safely accessed from
  28. // multiple threads without external synchronization. Table readers are used
  29. // for reading various types of table formats supported by rocksdb including
  30. // BlockBasedTable, PlainTable and CuckooTable format.
  31. class TableReader {
  32. public:
  33. virtual ~TableReader() {}
  34. // Returns a new iterator over the table contents.
  35. // The result of NewIterator() is initially invalid (caller must
  36. // call one of the Seek methods on the iterator before using it).
  37. // arena: If not null, the arena needs to be used to allocate the Iterator.
  38. // When destroying the iterator, the caller will not call "delete"
  39. // but Iterator::~Iterator() directly. The destructor needs to destroy
  40. // all the states but those allocated in arena.
  41. // skip_filters: disables checking the bloom filters even if they exist. This
  42. // option is effective only for block-based table format.
  43. // compaction_readahead_size: its value will only be used if caller =
  44. // kCompaction
  45. virtual InternalIterator* NewIterator(
  46. const ReadOptions&, const SliceTransform* prefix_extractor, Arena* arena,
  47. bool skip_filters, TableReaderCaller caller,
  48. size_t compaction_readahead_size = 0) = 0;
  49. virtual FragmentedRangeTombstoneIterator* NewRangeTombstoneIterator(
  50. const ReadOptions& /*read_options*/) {
  51. return nullptr;
  52. }
  53. // Given a key, return an approximate byte offset in the file where
  54. // the data for that key begins (or would begin if the key were
  55. // present in the file). The returned value is in terms of file
  56. // bytes, and so includes effects like compression of the underlying data.
  57. // E.g., the approximate offset of the last key in the table will
  58. // be close to the file length.
  59. virtual uint64_t ApproximateOffsetOf(const Slice& key,
  60. TableReaderCaller caller) = 0;
  61. // Given start and end keys, return the approximate data size in the file
  62. // between the keys. The returned value is in terms of file bytes, and so
  63. // includes effects like compression of the underlying data.
  64. virtual uint64_t ApproximateSize(const Slice& start, const Slice& end,
  65. TableReaderCaller caller) = 0;
  66. // Set up the table for Compaction. Might change some parameters with
  67. // posix_fadvise
  68. virtual void SetupForCompaction() = 0;
  69. virtual std::shared_ptr<const TableProperties> GetTableProperties() const = 0;
  70. // Prepare work that can be done before the real Get()
  71. virtual void Prepare(const Slice& /*target*/) {}
  72. // Report an approximation of how much memory has been used.
  73. virtual size_t ApproximateMemoryUsage() const = 0;
  74. // Calls get_context->SaveValue() repeatedly, starting with
  75. // the entry found after a call to Seek(key), until it returns false.
  76. // May not make such a call if filter policy says that key is not present.
  77. //
  78. // get_context->MarkKeyMayExist needs to be called when it is configured to be
  79. // memory only and the key is not found in the block cache.
  80. //
  81. // readOptions is the options for the read
  82. // key is the key to search for
  83. // skip_filters: disables checking the bloom filters even if they exist. This
  84. // option is effective only for block-based table format.
  85. virtual Status Get(const ReadOptions& readOptions, const Slice& key,
  86. GetContext* get_context,
  87. const SliceTransform* prefix_extractor,
  88. bool skip_filters = false) = 0;
  89. virtual void MultiGet(const ReadOptions& readOptions,
  90. const MultiGetContext::Range* mget_range,
  91. const SliceTransform* prefix_extractor,
  92. bool skip_filters = false) {
  93. for (auto iter = mget_range->begin(); iter != mget_range->end(); ++iter) {
  94. *iter->s = Get(readOptions, iter->ikey, iter->get_context,
  95. prefix_extractor, skip_filters);
  96. }
  97. }
  98. // Prefetch data corresponding to a give range of keys
  99. // Typically this functionality is required for table implementations that
  100. // persists the data on a non volatile storage medium like disk/SSD
  101. virtual Status Prefetch(const Slice* begin = nullptr,
  102. const Slice* end = nullptr) {
  103. (void) begin;
  104. (void) end;
  105. // Default implementation is NOOP.
  106. // The child class should implement functionality when applicable
  107. return Status::OK();
  108. }
  109. // convert db file to a human readable form
  110. virtual Status DumpTable(WritableFile* /*out_file*/) {
  111. return Status::NotSupported("DumpTable() not supported");
  112. }
  113. // check whether there is corruption in this db file
  114. virtual Status VerifyChecksum(const ReadOptions& /*read_options*/,
  115. TableReaderCaller /*caller*/) {
  116. return Status::NotSupported("VerifyChecksum() not supported");
  117. }
  118. };
  119. } // namespace ROCKSDB_NAMESPACE