2011-03-18 22:37:00 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
//
|
|
|
|
// A Status encapsulates the result of an operation. It may indicate success,
|
|
|
|
// or it may indicate an error with an associated error message.
|
2011-05-21 02:17:43 +00:00
|
|
|
//
|
|
|
|
// Multiple threads can invoke const methods on a Status without
|
|
|
|
// external synchronization, but if any of the threads may call a
|
|
|
|
// non-const method, all threads accessing the same Status must use
|
|
|
|
// external synchronization.
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
#ifndef STORAGE_LEVELDB_INCLUDE_STATUS_H_
|
|
|
|
#define STORAGE_LEVELDB_INCLUDE_STATUS_H_
|
|
|
|
|
|
|
|
#include <string>
|
2011-03-30 18:49:03 +00:00
|
|
|
#include "leveldb/slice.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
class Status {
|
|
|
|
public:
|
|
|
|
// Create a success status.
|
2013-03-01 02:04:58 +00:00
|
|
|
Status() : state_(nullptr) { }
|
2011-05-21 02:17:43 +00:00
|
|
|
~Status() { delete[] state_; }
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Copy the specified status.
|
|
|
|
Status(const Status& s);
|
|
|
|
void operator=(const Status& s);
|
|
|
|
|
|
|
|
// Return a success status.
|
|
|
|
static Status OK() { return Status(); }
|
|
|
|
|
|
|
|
// Return error status of an appropriate type.
|
|
|
|
static Status NotFound(const Slice& msg, const Slice& msg2 = Slice()) {
|
2011-05-21 02:17:43 +00:00
|
|
|
return Status(kNotFound, msg, msg2);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
static Status Corruption(const Slice& msg, const Slice& msg2 = Slice()) {
|
|
|
|
return Status(kCorruption, msg, msg2);
|
|
|
|
}
|
|
|
|
static Status NotSupported(const Slice& msg, const Slice& msg2 = Slice()) {
|
|
|
|
return Status(kNotSupported, msg, msg2);
|
|
|
|
}
|
|
|
|
static Status InvalidArgument(const Slice& msg, const Slice& msg2 = Slice()) {
|
|
|
|
return Status(kInvalidArgument, msg, msg2);
|
|
|
|
}
|
|
|
|
static Status IOError(const Slice& msg, const Slice& msg2 = Slice()) {
|
|
|
|
return Status(kIOError, msg, msg2);
|
|
|
|
}
|
2013-03-21 22:59:47 +00:00
|
|
|
static Status MergeInProgress(const Slice& msg, const Slice& msg2 = Slice()) {
|
|
|
|
return Status(kMergeInProgress, msg, msg2);
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Returns true iff the status indicates success.
|
2013-03-01 02:04:58 +00:00
|
|
|
bool ok() const { return (state_ == nullptr); }
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Returns true iff the status indicates a NotFound error.
|
|
|
|
bool IsNotFound() const { return code() == kNotFound; }
|
|
|
|
|
2012-03-30 20:15:49 +00:00
|
|
|
// Returns true iff the status indicates a Corruption error.
|
|
|
|
bool IsCorruption() const { return code() == kCorruption; }
|
|
|
|
|
2013-01-16 22:25:09 +00:00
|
|
|
// Returns true iff the status indicates a NotSupported error.
|
|
|
|
bool IsNotSupported() const { return code() == kNotSupported; }
|
|
|
|
|
|
|
|
// Returns true iff the status indicates an InvalidArgument error.
|
|
|
|
bool IsInvalidArgument() const { return code() == kInvalidArgument; }
|
|
|
|
|
2012-03-30 20:15:49 +00:00
|
|
|
// Returns true iff the status indicates an IOError.
|
|
|
|
bool IsIOError() const { return code() == kIOError; }
|
|
|
|
|
2013-03-21 22:59:47 +00:00
|
|
|
// Returns true iff the status indicates an MergeInProgress.
|
|
|
|
bool IsMergeInProgress() const { return code() == kMergeInProgress; }
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
// Return a string representation of this status suitable for printing.
|
|
|
|
// Returns the string "OK" for success.
|
|
|
|
std::string ToString() const;
|
|
|
|
|
|
|
|
private:
|
2013-03-01 02:04:58 +00:00
|
|
|
// OK status has a nullptr state_. Otherwise, state_ is a new[] array
|
2011-05-21 02:17:43 +00:00
|
|
|
// of the following form:
|
|
|
|
// state_[0..3] == length of message
|
|
|
|
// state_[4] == code
|
|
|
|
// state_[5..] == message
|
|
|
|
const char* state_;
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
enum Code {
|
|
|
|
kOk = 0,
|
|
|
|
kNotFound = 1,
|
|
|
|
kCorruption = 2,
|
|
|
|
kNotSupported = 3,
|
|
|
|
kInvalidArgument = 4,
|
2013-03-21 22:59:47 +00:00
|
|
|
kIOError = 5,
|
|
|
|
kMergeInProgress = 6
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
2011-05-21 02:17:43 +00:00
|
|
|
Code code() const {
|
2013-03-01 02:04:58 +00:00
|
|
|
return (state_ == nullptr) ? kOk : static_cast<Code>(state_[4]);
|
2011-05-21 02:17:43 +00:00
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2011-05-21 02:17:43 +00:00
|
|
|
Status(Code code, const Slice& msg, const Slice& msg2);
|
|
|
|
static const char* CopyState(const char* s);
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
inline Status::Status(const Status& s) {
|
2013-03-01 02:04:58 +00:00
|
|
|
state_ = (s.state_ == nullptr) ? nullptr : CopyState(s.state_);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
inline void Status::operator=(const Status& s) {
|
2011-05-21 02:17:43 +00:00
|
|
|
// The following condition catches both aliasing (when this == &s),
|
|
|
|
// and the common case where both s and *this are ok.
|
|
|
|
if (state_ != s.state_) {
|
|
|
|
delete[] state_;
|
2013-03-01 02:04:58 +00:00
|
|
|
state_ = (s.state_ == nullptr) ? nullptr : CopyState(s.state_);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace leveldb
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
#endif // STORAGE_LEVELDB_INCLUDE_STATUS_H_
|