iterator.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "rocksdb/iterator.h"
  10. #include "memory/arena.h"
  11. #include "table/internal_iterator.h"
  12. #include "table/iterator_wrapper.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. Status Iterator::GetProperty(std::string prop_name, std::string* prop) {
  15. if (prop == nullptr) {
  16. return Status::InvalidArgument("prop is nullptr");
  17. }
  18. if (prop_name == "rocksdb.iterator.is-key-pinned") {
  19. *prop = "0";
  20. return Status::OK();
  21. }
  22. if (prop_name == "rocksdb.iterator.is-value-pinned") {
  23. *prop = "0";
  24. return Status::OK();
  25. }
  26. return Status::InvalidArgument("Unidentified property.");
  27. }
  28. namespace {
  29. class EmptyIterator : public Iterator {
  30. public:
  31. explicit EmptyIterator(const Status& s) : status_(s) {}
  32. bool Valid() const override { return false; }
  33. void Seek(const Slice& /*target*/) override {}
  34. void SeekForPrev(const Slice& /*target*/) override {}
  35. void SeekToFirst() override {}
  36. void SeekToLast() override {}
  37. void Next() override { assert(false); }
  38. void Prev() override { assert(false); }
  39. Slice key() const override {
  40. assert(false);
  41. return Slice();
  42. }
  43. Slice value() const override {
  44. assert(false);
  45. return Slice();
  46. }
  47. Status status() const override { return status_; }
  48. private:
  49. Status status_;
  50. };
  51. template <class TValue = Slice>
  52. class EmptyInternalIterator : public InternalIteratorBase<TValue> {
  53. public:
  54. explicit EmptyInternalIterator(const Status& s) : status_(s) {}
  55. bool Valid() const override { return false; }
  56. void Seek(const Slice& /*target*/) override {}
  57. void SeekForPrev(const Slice& /*target*/) override {}
  58. void SeekToFirst() override {}
  59. void SeekToLast() override {}
  60. void Next() override { assert(false); }
  61. void Prev() override { assert(false); }
  62. Slice key() const override {
  63. assert(false);
  64. return Slice();
  65. }
  66. TValue value() const override {
  67. assert(false);
  68. return TValue();
  69. }
  70. uint64_t write_unix_time() const override {
  71. assert(false);
  72. return std::numeric_limits<uint64_t>::max();
  73. }
  74. Status status() const override { return status_; }
  75. private:
  76. Status status_;
  77. };
  78. } // namespace
  79. Iterator* NewEmptyIterator() { return new EmptyIterator(Status::OK()); }
  80. Iterator* NewErrorIterator(const Status& status) {
  81. return new EmptyIterator(status);
  82. }
  83. template <class TValue>
  84. InternalIteratorBase<TValue>* NewErrorInternalIterator(const Status& status) {
  85. return new EmptyInternalIterator<TValue>(status);
  86. }
  87. template InternalIteratorBase<IndexValue>* NewErrorInternalIterator(
  88. const Status& status);
  89. template InternalIteratorBase<Slice>* NewErrorInternalIterator(
  90. const Status& status);
  91. template <class TValue>
  92. InternalIteratorBase<TValue>* NewErrorInternalIterator(const Status& status,
  93. Arena* arena) {
  94. if (arena == nullptr) {
  95. return NewErrorInternalIterator<TValue>(status);
  96. } else {
  97. auto mem = arena->AllocateAligned(sizeof(EmptyInternalIterator<TValue>));
  98. return new (mem) EmptyInternalIterator<TValue>(status);
  99. }
  100. }
  101. template InternalIteratorBase<IndexValue>* NewErrorInternalIterator(
  102. const Status& status, Arena* arena);
  103. template InternalIteratorBase<Slice>* NewErrorInternalIterator(
  104. const Status& status, Arena* arena);
  105. template <class TValue>
  106. InternalIteratorBase<TValue>* NewEmptyInternalIterator() {
  107. return new EmptyInternalIterator<TValue>(Status::OK());
  108. }
  109. template InternalIteratorBase<IndexValue>* NewEmptyInternalIterator();
  110. template InternalIteratorBase<Slice>* NewEmptyInternalIterator();
  111. template <class TValue>
  112. InternalIteratorBase<TValue>* NewEmptyInternalIterator(Arena* arena) {
  113. if (arena == nullptr) {
  114. return NewEmptyInternalIterator<TValue>();
  115. } else {
  116. auto mem = arena->AllocateAligned(sizeof(EmptyInternalIterator<TValue>));
  117. return new (mem) EmptyInternalIterator<TValue>(Status::OK());
  118. }
  119. }
  120. template InternalIteratorBase<IndexValue>* NewEmptyInternalIterator(
  121. Arena* arena);
  122. template InternalIteratorBase<Slice>* NewEmptyInternalIterator(Arena* arena);
  123. } // namespace ROCKSDB_NAMESPACE