blob_db_iterator.h 3.6 KB

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