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