Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
status.h
Go to the documentation of this file.
1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 //
5 // A Status encapsulates the result of an operation. It may indicate success,
6 // or it may indicate an error with an associated error message.
7 //
8 // Multiple threads can invoke const methods on a Status without
9 // external synchronization, but if any of the threads may call a
10 // non-const method, all threads accessing the same Status must use
11 // external synchronization.
12 
13 // Adapted from Apache Kudu, TensorFlow
14 
15 #ifndef ARROW_STATUS_H_
16 #define ARROW_STATUS_H_
17 
18 #include <cstring>
19 #include <iosfwd>
20 #include <string>
21 
22 #ifdef ARROW_EXTRA_ERROR_CONTEXT
23 #include <sstream>
24 #endif
25 
26 #include "arrow/util/macros.h"
27 #include "arrow/util/visibility.h"
28 
29 #ifdef ARROW_EXTRA_ERROR_CONTEXT
30 
31 #define RETURN_NOT_OK(s) \
32  do { \
33  ::arrow::Status _s = (s); \
34  if (ARROW_PREDICT_FALSE(!_s.ok())) { \
35  std::stringstream ss; \
36  ss << __FILE__ << ":" << __LINE__ << " code: " << #s << "\n" << _s.message(); \
37  return Status(_s.code(), ss.str()); \
38  } \
39  } while (0)
40 
41 #else
42 
43 #define RETURN_NOT_OK(s) \
44  do { \
45  ::arrow::Status _s = (s); \
46  if (ARROW_PREDICT_FALSE(!_s.ok())) { \
47  return _s; \
48  } \
49  } while (false)
50 
51 #endif // ARROW_EXTRA_ERROR_CONTEXT
52 
53 #define RETURN_NOT_OK_ELSE(s, else_) \
54  do { \
55  ::arrow::Status _s = (s); \
56  if (!_s.ok()) { \
57  else_; \
58  return _s; \
59  } \
60  } while (false)
61 
62 // This is used by other codebases. The macros above
63 // should probably have that prefix, but there is a
64 // lot of code that already uses that macro and changing
65 // it would be of no benefit.
66 #define ARROW_RETURN_NOT_OK(s) RETURN_NOT_OK(s)
67 
68 namespace arrow {
69 
70 enum class StatusCode : char {
71  OK = 0,
72  OutOfMemory = 1,
73  KeyError = 2,
74  TypeError = 3,
75  Invalid = 4,
76  IOError = 5,
77  CapacityError = 6,
78  UnknownError = 9,
79  NotImplemented = 10,
80  SerializationError = 11,
81  PythonError = 12,
82  PlasmaObjectExists = 20,
84  PlasmaStoreFull = 22,
86 };
87 
88 #if defined(__clang__)
89 // Only clang supports warn_unused_result as a type annotation.
90 class ARROW_MUST_USE_RESULT ARROW_EXPORT Status;
91 #endif
92 
93 class ARROW_EXPORT Status {
94  public:
95  // Create a success status.
96  Status() : state_(NULL) {}
97  ~Status() { delete state_; }
98 
99  Status(StatusCode code, const std::string& msg);
100 
101  // Copy the specified status.
102  Status(const Status& s);
103  void operator=(const Status& s);
104 
105  // Return a success status.
106  static Status OK() { return Status(); }
107 
108  // Return error status of an appropriate type.
109  static Status OutOfMemory(const std::string& msg) {
110  return Status(StatusCode::OutOfMemory, msg);
111  }
112 
113  static Status KeyError(const std::string& msg) {
114  return Status(StatusCode::KeyError, msg);
115  }
116 
117  static Status TypeError(const std::string& msg) {
118  return Status(StatusCode::TypeError, msg);
119  }
120 
121  static Status UnknownError(const std::string& msg) {
122  return Status(StatusCode::UnknownError, msg);
123  }
124 
125  static Status NotImplemented(const std::string& msg) {
126  return Status(StatusCode::NotImplemented, msg);
127  }
128 
129  static Status Invalid(const std::string& msg) {
130  return Status(StatusCode::Invalid, msg);
131  }
132 
133  static Status CapacityError(const std::string& msg) {
134  return Status(StatusCode::CapacityError, msg);
135  }
136 
137  static Status IOError(const std::string& msg) {
138  return Status(StatusCode::IOError, msg);
139  }
140 
141  static Status SerializationError(const std::string& msg) {
143  }
144 
145  static Status PlasmaObjectExists(const std::string& msg) {
147  }
148 
149  static Status PlasmaObjectNonexistent(const std::string& msg) {
151  }
152 
153  static Status PlasmaObjectAlreadySealed(const std::string& msg) {
155  }
156 
157  static Status PlasmaStoreFull(const std::string& msg) {
158  return Status(StatusCode::PlasmaStoreFull, msg);
159  }
160 
161  // Returns true iff the status indicates success.
162  bool ok() const { return (state_ == NULL); }
163 
164  bool IsOutOfMemory() const { return code() == StatusCode::OutOfMemory; }
165  bool IsKeyError() const { return code() == StatusCode::KeyError; }
166  bool IsInvalid() const { return code() == StatusCode::Invalid; }
167  bool IsIOError() const { return code() == StatusCode::IOError; }
168  bool IsCapacityError() const { return code() == StatusCode::CapacityError; }
169  bool IsTypeError() const { return code() == StatusCode::TypeError; }
170  bool IsUnknownError() const { return code() == StatusCode::UnknownError; }
171  bool IsNotImplemented() const { return code() == StatusCode::NotImplemented; }
172  // An object could not be serialized or deserialized.
173  bool IsSerializationError() const { return code() == StatusCode::SerializationError; }
174  // An error is propagated from a nested Python function.
175  bool IsPythonError() const { return code() == StatusCode::PythonError; }
176  // An object with this object ID already exists in the plasma store.
177  bool IsPlasmaObjectExists() const { return code() == StatusCode::PlasmaObjectExists; }
178  // An object was requested that doesn't exist in the plasma store.
180  return code() == StatusCode::PlasmaObjectNonexistent;
181  }
182  // An already sealed object is tried to be sealed again.
184  return code() == StatusCode::PlasmaObjectAlreadySealed;
185  }
186  // An object is too large to fit into the plasma store.
187  bool IsPlasmaStoreFull() const { return code() == StatusCode::PlasmaStoreFull; }
188 
189  // Return a string representation of this status suitable for printing.
190  // Returns the string "OK" for success.
191  std::string ToString() const;
192 
193  // Return a string representation of the status code, without the message
194  // text or posix code information.
195  std::string CodeAsString() const;
196 
197  StatusCode code() const { return ok() ? StatusCode::OK : state_->code; }
198 
199  std::string message() const { return ok() ? "" : state_->msg; }
200 
201  private:
202  struct State {
203  StatusCode code;
204  std::string msg;
205  };
206  // OK status has a `NULL` state_. Otherwise, `state_` points to
207  // a `State` structure containing the error code and message(s)
208  State* state_;
209 
210  void CopyFrom(const State* s);
211 };
212 
213 static inline std::ostream& operator<<(std::ostream& os, const Status& x) {
214  os << x.ToString();
215  return os;
216 }
217 
218 inline Status::Status(const Status& s)
219  : state_((s.state_ == NULL) ? NULL : new State(*s.state_)) {}
220 
221 inline void Status::operator=(const Status& s) {
222  // The following condition catches both aliasing (when this == &s),
223  // and the common case where both s and *this are ok.
224  if (state_ != s.state_) {
225  CopyFrom(s.state_);
226  }
227 }
228 
229 } // namespace arrow
230 
231 #endif // ARROW_STATUS_H_
bool IsPlasmaObjectNonexistent() const
Definition: status.h:179
StatusCode code() const
Definition: status.h:197
bool IsUnknownError() const
Definition: status.h:170
StatusCode
Definition: status.h:70
bool IsPlasmaObjectExists() const
Definition: status.h:177
static Status PlasmaObjectExists(const std::string &msg)
Definition: status.h:145
Definition: status.h:93
Status()
Definition: status.h:96
bool ok() const
Definition: status.h:162
void operator=(const Status &s)
Definition: status.h:221
#define ARROW_MUST_USE_RESULT
Definition: macros.h:58
bool IsOutOfMemory() const
Definition: status.h:164
bool IsCapacityError() const
Definition: status.h:168
static Status OK()
Definition: status.h:106
static Status KeyError(const std::string &msg)
Definition: status.h:113
static Status PlasmaObjectNonexistent(const std::string &msg)
Definition: status.h:149
static Status Invalid(const std::string &msg)
Definition: status.h:129
bool IsIOError() const
Definition: status.h:167
static Status IOError(const std::string &msg)
Definition: status.h:137
bool IsKeyError() const
Definition: status.h:165
static Status UnknownError(const std::string &msg)
Definition: status.h:121
static Status TypeError(const std::string &msg)
Definition: status.h:117
bool IsSerializationError() const
Definition: status.h:173
bool IsTypeError() const
Definition: status.h:169
Top-level namespace for Apache Arrow C++ API.
Definition: adapter.h:32
bool IsPythonError() const
Definition: status.h:175
static Status OutOfMemory(const std::string &msg)
Definition: status.h:109
bool IsNotImplemented() const
Definition: status.h:171
~Status()
Definition: status.h:97
static Status NotImplemented(const std::string &msg)
Definition: status.h:125
bool IsPlasmaStoreFull() const
Definition: status.h:187
std::string message() const
Definition: status.h:199
static Status PlasmaObjectAlreadySealed(const std::string &msg)
Definition: status.h:153
bool IsPlasmaObjectAlreadySealed() const
Definition: status.h:183
static Status SerializationError(const std::string &msg)
Definition: status.h:141
bool IsInvalid() const
Definition: status.h:166
static Status CapacityError(const std::string &msg)
Definition: status.h:133
static Status PlasmaStoreFull(const std::string &msg)
Definition: status.h:157