]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/include/rocksdb/io_status.h
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / rocksdb / include / rocksdb / io_status.h
CommitLineData
f67539c2
TL
1// Copyright (c) 2019-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// An IOStatus encapsulates the result of an operation. It may indicate
7// success, or it may indicate an error with an associated error message.
8//
9// Multiple threads can invoke const methods on an IOStatus without
10// external synchronization, but if any of the threads may call a
11// non-const method, all threads accessing the same IOStatus must use
12// external synchronization.
13
14#pragma once
15
16#include <string>
1e59de90 17
f67539c2
TL
18#include "rocksdb/slice.h"
19#ifdef OS_WIN
20#include <string.h>
21#endif
22#include <cstring>
1e59de90 23
f67539c2
TL
24#include "status.h"
25
26namespace ROCKSDB_NAMESPACE {
27
28class IOStatus : public Status {
29 public:
30 using Code = Status::Code;
31 using SubCode = Status::SubCode;
32
1e59de90 33 enum IOErrorScope : unsigned char {
f67539c2
TL
34 kIOErrorScopeFileSystem,
35 kIOErrorScopeFile,
36 kIOErrorScopeRange,
37 kIOErrorScopeMax,
38 };
39
40 // Create a success status.
41 IOStatus() : IOStatus(kOk, kNone) {}
42 ~IOStatus() {}
43
44 // Copy the specified status.
45 IOStatus(const IOStatus& s);
46 IOStatus& operator=(const IOStatus& s);
1e59de90
TL
47 IOStatus(IOStatus&& s) noexcept;
48 IOStatus& operator=(IOStatus&& s) noexcept;
f67539c2
TL
49 bool operator==(const IOStatus& rhs) const;
50 bool operator!=(const IOStatus& rhs) const;
51
52 void SetRetryable(bool retryable) { retryable_ = retryable; }
53 void SetDataLoss(bool data_loss) { data_loss_ = data_loss; }
1e59de90
TL
54 void SetScope(IOErrorScope scope) {
55 scope_ = static_cast<unsigned char>(scope);
56 }
f67539c2
TL
57
58 bool GetRetryable() const { return retryable_; }
59 bool GetDataLoss() const { return data_loss_; }
1e59de90 60 IOErrorScope GetScope() const { return static_cast<IOErrorScope>(scope_); }
f67539c2
TL
61
62 // Return a success status.
63 static IOStatus OK() { return IOStatus(); }
64
65 static IOStatus NotSupported(const Slice& msg, const Slice& msg2 = Slice()) {
66 return IOStatus(kNotSupported, msg, msg2);
67 }
68 static IOStatus NotSupported(SubCode msg = kNone) {
69 return IOStatus(kNotSupported, msg);
70 }
71
72 // Return error status of an appropriate type.
73 static IOStatus NotFound(const Slice& msg, const Slice& msg2 = Slice()) {
74 return IOStatus(kNotFound, msg, msg2);
75 }
76 // Fast path for not found without malloc;
77 static IOStatus NotFound(SubCode msg = kNone) {
78 return IOStatus(kNotFound, msg);
79 }
80
81 static IOStatus Corruption(const Slice& msg, const Slice& msg2 = Slice()) {
82 return IOStatus(kCorruption, msg, msg2);
83 }
84 static IOStatus Corruption(SubCode msg = kNone) {
85 return IOStatus(kCorruption, msg);
86 }
87
88 static IOStatus InvalidArgument(const Slice& msg,
89 const Slice& msg2 = Slice()) {
90 return IOStatus(kInvalidArgument, msg, msg2);
91 }
92 static IOStatus InvalidArgument(SubCode msg = kNone) {
93 return IOStatus(kInvalidArgument, msg);
94 }
95
96 static IOStatus IOError(const Slice& msg, const Slice& msg2 = Slice()) {
97 return IOStatus(kIOError, msg, msg2);
98 }
99 static IOStatus IOError(SubCode msg = kNone) {
100 return IOStatus(kIOError, msg);
101 }
102
103 static IOStatus Busy(SubCode msg = kNone) { return IOStatus(kBusy, msg); }
104 static IOStatus Busy(const Slice& msg, const Slice& msg2 = Slice()) {
105 return IOStatus(kBusy, msg, msg2);
106 }
107
108 static IOStatus TimedOut(SubCode msg = kNone) {
109 return IOStatus(kTimedOut, msg);
110 }
111 static IOStatus TimedOut(const Slice& msg, const Slice& msg2 = Slice()) {
112 return IOStatus(kTimedOut, msg, msg2);
113 }
114
115 static IOStatus NoSpace() { return IOStatus(kIOError, kNoSpace); }
116 static IOStatus NoSpace(const Slice& msg, const Slice& msg2 = Slice()) {
117 return IOStatus(kIOError, kNoSpace, msg, msg2);
118 }
119
120 static IOStatus PathNotFound() { return IOStatus(kIOError, kPathNotFound); }
121 static IOStatus PathNotFound(const Slice& msg, const Slice& msg2 = Slice()) {
122 return IOStatus(kIOError, kPathNotFound, msg, msg2);
123 }
124
20effc67
TL
125 static IOStatus IOFenced() { return IOStatus(kIOError, kIOFenced); }
126 static IOStatus IOFenced(const Slice& msg, const Slice& msg2 = Slice()) {
127 return IOStatus(kIOError, kIOFenced, msg, msg2);
128 }
129
1e59de90
TL
130 static IOStatus Aborted(SubCode msg = kNone) {
131 return IOStatus(kAborted, msg);
132 }
133 static IOStatus Aborted(const Slice& msg, const Slice& msg2 = Slice()) {
134 return IOStatus(kAborted, msg, msg2);
135 }
136
f67539c2
TL
137 // Return a string representation of this status suitable for printing.
138 // Returns the string "OK" for success.
139 // std::string ToString() const;
140
141 private:
142 friend IOStatus status_to_io_status(Status&&);
f67539c2
TL
143
144 explicit IOStatus(Code _code, SubCode _subcode = kNone)
1e59de90 145 : Status(_code, _subcode, false, false, kIOErrorScopeFileSystem) {}
f67539c2
TL
146
147 IOStatus(Code _code, SubCode _subcode, const Slice& msg, const Slice& msg2);
148 IOStatus(Code _code, const Slice& msg, const Slice& msg2)
149 : IOStatus(_code, kNone, msg, msg2) {}
150};
151
152inline IOStatus::IOStatus(Code _code, SubCode _subcode, const Slice& msg,
153 const Slice& msg2)
1e59de90 154 : Status(_code, _subcode, false, false, kIOErrorScopeFileSystem) {
f67539c2
TL
155 assert(code_ != kOk);
156 assert(subcode_ != kMaxSubCode);
157 const size_t len1 = msg.size();
158 const size_t len2 = msg2.size();
159 const size_t size = len1 + (len2 ? (2 + len2) : 0);
160 char* const result = new char[size + 1]; // +1 for null terminator
161 memcpy(result, msg.data(), len1);
162 if (len2) {
163 result[len1] = ':';
164 result[len1 + 1] = ' ';
165 memcpy(result + len1 + 2, msg2.data(), len2);
166 }
167 result[size] = '\0'; // null terminator for C style string
1e59de90 168 state_.reset(result);
f67539c2
TL
169}
170
171inline IOStatus::IOStatus(const IOStatus& s) : Status(s.code_, s.subcode_) {
20effc67
TL
172#ifdef ROCKSDB_ASSERT_STATUS_CHECKED
173 s.checked_ = true;
174#endif // ROCKSDB_ASSERT_STATUS_CHECKED
f67539c2
TL
175 retryable_ = s.retryable_;
176 data_loss_ = s.data_loss_;
177 scope_ = s.scope_;
1e59de90 178 state_ = (s.state_ == nullptr) ? nullptr : CopyState(s.state_.get());
f67539c2
TL
179}
180inline IOStatus& IOStatus::operator=(const IOStatus& s) {
181 // The following condition catches both aliasing (when this == &s),
182 // and the common case where both s and *this are ok.
183 if (this != &s) {
20effc67
TL
184#ifdef ROCKSDB_ASSERT_STATUS_CHECKED
185 s.checked_ = true;
186 checked_ = false;
187#endif // ROCKSDB_ASSERT_STATUS_CHECKED
f67539c2
TL
188 code_ = s.code_;
189 subcode_ = s.subcode_;
190 retryable_ = s.retryable_;
191 data_loss_ = s.data_loss_;
192 scope_ = s.scope_;
1e59de90 193 state_ = (s.state_ == nullptr) ? nullptr : CopyState(s.state_.get());
f67539c2
TL
194 }
195 return *this;
196}
197
1e59de90 198inline IOStatus::IOStatus(IOStatus&& s) noexcept : IOStatus() {
f67539c2
TL
199 *this = std::move(s);
200}
201
1e59de90 202inline IOStatus& IOStatus::operator=(IOStatus&& s) noexcept {
f67539c2 203 if (this != &s) {
20effc67
TL
204#ifdef ROCKSDB_ASSERT_STATUS_CHECKED
205 s.checked_ = true;
206 checked_ = false;
207#endif // ROCKSDB_ASSERT_STATUS_CHECKED
f67539c2
TL
208 code_ = std::move(s.code_);
209 s.code_ = kOk;
210 subcode_ = std::move(s.subcode_);
211 s.subcode_ = kNone;
212 retryable_ = s.retryable_;
f67539c2 213 data_loss_ = s.data_loss_;
f67539c2 214 scope_ = s.scope_;
20effc67 215 s.scope_ = kIOErrorScopeFileSystem;
1e59de90 216 state_ = std::move(s.state_);
f67539c2
TL
217 }
218 return *this;
219}
220
221inline bool IOStatus::operator==(const IOStatus& rhs) const {
20effc67
TL
222#ifdef ROCKSDB_ASSERT_STATUS_CHECKED
223 checked_ = true;
224 rhs.checked_ = true;
225#endif // ROCKSDB_ASSERT_STATUS_CHECKED
f67539c2
TL
226 return (code_ == rhs.code_);
227}
228
229inline bool IOStatus::operator!=(const IOStatus& rhs) const {
20effc67
TL
230#ifdef ROCKSDB_ASSERT_STATUS_CHECKED
231 checked_ = true;
232 rhs.checked_ = true;
233#endif // ROCKSDB_ASSERT_STATUS_CHECKED
f67539c2
TL
234 return !(*this == rhs);
235}
236
20effc67 237inline IOStatus status_to_io_status(Status&& status) {
1e59de90
TL
238 IOStatus io_s;
239 Status& s = io_s;
240 s = std::move(status);
241 return io_s;
20effc67
TL
242}
243
f67539c2 244} // namespace ROCKSDB_NAMESPACE