blob_db_iterator.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #pragma once
  6. #include "db/arena_wrapped_db_iter.h"
  7. #include "rocksdb/iterator.h"
  8. #include "util/stop_watch.h"
  9. #include "utilities/blob_db/blob_db_impl.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. class Statistics;
  12. class SystemClock;
  13. namespace blob_db {
  14. using ROCKSDB_NAMESPACE::ManagedSnapshot;
  15. class BlobDBIterator : public Iterator {
  16. public:
  17. BlobDBIterator(ManagedSnapshot* snapshot, ArenaWrappedDBIter* iter,
  18. BlobDBImpl* blob_db, SystemClock* clock,
  19. Statistics* statistics)
  20. : snapshot_(snapshot),
  21. iter_(iter),
  22. blob_db_(blob_db),
  23. clock_(clock),
  24. statistics_(statistics) {}
  25. virtual ~BlobDBIterator() = default;
  26. bool Valid() const override {
  27. if (!iter_->Valid()) {
  28. return false;
  29. }
  30. return status_.ok();
  31. }
  32. Status status() const override {
  33. if (!iter_->status().ok()) {
  34. return iter_->status();
  35. }
  36. return status_;
  37. }
  38. void SeekToFirst() override {
  39. StopWatch seek_sw(clock_, statistics_, BLOB_DB_SEEK_MICROS);
  40. RecordTick(statistics_, BLOB_DB_NUM_SEEK);
  41. iter_->SeekToFirst();
  42. while (UpdateBlobValue()) {
  43. iter_->Next();
  44. }
  45. }
  46. void SeekToLast() override {
  47. StopWatch seek_sw(clock_, statistics_, BLOB_DB_SEEK_MICROS);
  48. RecordTick(statistics_, BLOB_DB_NUM_SEEK);
  49. iter_->SeekToLast();
  50. while (UpdateBlobValue()) {
  51. iter_->Prev();
  52. }
  53. }
  54. void Seek(const Slice& target) override {
  55. StopWatch seek_sw(clock_, statistics_, BLOB_DB_SEEK_MICROS);
  56. RecordTick(statistics_, BLOB_DB_NUM_SEEK);
  57. iter_->Seek(target);
  58. while (UpdateBlobValue()) {
  59. iter_->Next();
  60. }
  61. }
  62. void SeekForPrev(const Slice& target) override {
  63. StopWatch seek_sw(clock_, statistics_, BLOB_DB_SEEK_MICROS);
  64. RecordTick(statistics_, BLOB_DB_NUM_SEEK);
  65. iter_->SeekForPrev(target);
  66. while (UpdateBlobValue()) {
  67. iter_->Prev();
  68. }
  69. }
  70. void Next() override {
  71. assert(Valid());
  72. StopWatch next_sw(clock_, statistics_, BLOB_DB_NEXT_MICROS);
  73. RecordTick(statistics_, BLOB_DB_NUM_NEXT);
  74. iter_->Next();
  75. while (UpdateBlobValue()) {
  76. iter_->Next();
  77. }
  78. }
  79. void Prev() override {
  80. assert(Valid());
  81. StopWatch prev_sw(clock_, statistics_, BLOB_DB_PREV_MICROS);
  82. RecordTick(statistics_, BLOB_DB_NUM_PREV);
  83. iter_->Prev();
  84. while (UpdateBlobValue()) {
  85. iter_->Prev();
  86. }
  87. }
  88. Slice key() const override {
  89. assert(Valid());
  90. return iter_->key();
  91. }
  92. Slice value() const override {
  93. assert(Valid());
  94. if (!iter_->IsBlob()) {
  95. return iter_->value();
  96. }
  97. return value_;
  98. }
  99. // Iterator::Refresh() not supported.
  100. private:
  101. // Return true if caller should continue to next value.
  102. bool UpdateBlobValue() {
  103. value_.Reset();
  104. status_ = Status::OK();
  105. if (iter_->Valid() && iter_->status().ok() && iter_->IsBlob()) {
  106. Status s = blob_db_->GetBlobValue(iter_->key(), iter_->value(), &value_);
  107. if (s.IsNotFound()) {
  108. return true;
  109. } else {
  110. if (!s.ok()) {
  111. status_ = s;
  112. }
  113. return false;
  114. }
  115. } else {
  116. return false;
  117. }
  118. }
  119. std::unique_ptr<ManagedSnapshot> snapshot_;
  120. std::unique_ptr<ArenaWrappedDBIter> iter_;
  121. BlobDBImpl* blob_db_;
  122. SystemClock* clock_;
  123. Statistics* statistics_;
  124. Status status_;
  125. PinnableSlice value_;
  126. };
  127. } // namespace blob_db
  128. } // namespace ROCKSDB_NAMESPACE