LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

127 líneas
4.0 KiB

hace 1 mes
hace 1 mes
hace 1 mes
hace 1 mes
  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. #ifndef STORAGE_LEVELDB_INCLUDE_STATUS_H_
  13. #define STORAGE_LEVELDB_INCLUDE_STATUS_H_
  14. #include <algorithm>
  15. #include <string>
  16. #include "leveldb/export.h"
  17. #include "leveldb/slice.h"
  18. namespace leveldb {
  19. class LEVELDB_EXPORT Status {
  20. public:
  21. // Create a success status.
  22. Status() noexcept : state_(nullptr) {}
  23. ~Status() { delete[] state_; }
  24. Status(const Status& rhs);
  25. Status& operator=(const Status& rhs);
  26. Status(Status&& rhs) noexcept : state_(rhs.state_) { rhs.state_ = nullptr; }
  27. Status& operator=(Status&& rhs) noexcept;
  28. // Return a success status.
  29. static Status OK() { return Status(); }
  30. // Return error status of an appropriate type.
  31. static Status NotFound(const Slice& msg, const Slice& msg2 = Slice()) {
  32. return Status(kNotFound, msg, msg2);
  33. }
  34. static Status Corruption(const Slice& msg, const Slice& msg2 = Slice()) {
  35. return Status(kCorruption, msg, msg2);
  36. }
  37. static Status NotSupported(const Slice& msg, const Slice& msg2 = Slice()) {
  38. return Status(kNotSupported, msg, msg2);
  39. }
  40. static Status InvalidArgument(const Slice& msg, const Slice& msg2 = Slice()) {
  41. return Status(kInvalidArgument, msg, msg2);
  42. }
  43. static Status IOError(const Slice& msg, const Slice& msg2 = Slice()) {
  44. return Status(kIOError, msg, msg2);
  45. }
  46. static Status Expire(const Slice& msg, const Slice& msg2 = Slice()) {
  47. return Status(kExpire, msg, msg2);
  48. }
  49. // Returns true iff the status indicates success.
  50. bool ok() const { return (state_ == nullptr); }
  51. // Returns true iff the status indicates a NotFound error.
  52. bool IsNotFound() const { return code() == kNotFound; }
  53. // Returns true iff the status indicates a Corruption error.
  54. bool IsCorruption() const { return code() == kCorruption; }
  55. // Returns true iff the status indicates an IOError.
  56. bool IsIOError() const { return code() == kIOError; }
  57. // Returns true iff the status indicates a NotSupportedError.
  58. bool IsNotSupportedError() const { return code() == kNotSupported; }
  59. // Returns true iff the status indicates an InvalidArgument.
  60. bool IsInvalidArgument() const { return code() == kInvalidArgument; }
  61. bool IsExpire()const{return code() == kExpire;}
  62. // Return a string representation of this status suitable for printing.
  63. // Returns the string "OK" for success.
  64. std::string ToString() const;
  65. private:
  66. enum Code {
  67. kOk = 0,
  68. kNotFound = 1,
  69. kCorruption = 2,
  70. kNotSupported = 3,
  71. kInvalidArgument = 4,
  72. kIOError = 5,
  73. kExpire = 6
  74. };
  75. Code code() const {
  76. return (state_ == nullptr) ? kOk : static_cast<Code>(state_[4]);
  77. }
  78. Status(Code code, const Slice& msg, const Slice& msg2);
  79. static const char* CopyState(const char* s);
  80. // OK status has a null state_. Otherwise, state_ is a new[] array
  81. // of the following form:
  82. // state_[0..3] == length of message
  83. // state_[4] == code
  84. // state_[5..] == message
  85. const char* state_;
  86. };
  87. inline Status::Status(const Status& rhs) {
  88. state_ = (rhs.state_ == nullptr) ? nullptr : CopyState(rhs.state_);
  89. }
  90. inline Status& Status::operator=(const Status& rhs) {
  91. // The following condition catches both aliasing (when this == &rhs),
  92. // and the common case where both rhs and *this are ok.
  93. if (state_ != rhs.state_) {
  94. delete[] state_;
  95. state_ = (rhs.state_ == nullptr) ? nullptr : CopyState(rhs.state_);
  96. }
  97. return *this;
  98. }
  99. inline Status& Status::operator=(Status&& rhs) noexcept {
  100. std::swap(state_, rhs.state_);
  101. return *this;
  102. }
  103. } // namespace leveldb
  104. #endif // STORAGE_LEVELDB_INCLUDE_STATUS_H_