status.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #include "rocksdb/status.h"
  10. #include <stdio.h>
  11. #ifdef OS_WIN
  12. #include <string.h>
  13. #endif
  14. #include <cstring>
  15. #include "port/port.h"
  16. namespace ROCKSDB_NAMESPACE {
  17. const char* Status::CopyState(const char* state) {
  18. #ifdef OS_WIN
  19. const size_t cch = std::strlen(state) + 1; // +1 for the null terminator
  20. char* result = new char[cch];
  21. errno_t ret
  22. #if defined(_MSC_VER)
  23. ;
  24. #else
  25. __attribute__((__unused__));
  26. #endif
  27. ret = strncpy_s(result, cch, state, cch - 1);
  28. result[cch - 1] = '\0';
  29. assert(ret == 0);
  30. return result;
  31. #else
  32. const size_t cch = std::strlen(state) + 1; // +1 for the null terminator
  33. return std::strncpy(new char[cch], state, cch);
  34. #endif
  35. }
  36. static const char* msgs[static_cast<int>(Status::kMaxSubCode)] = {
  37. "", // kNone
  38. "Timeout Acquiring Mutex", // kMutexTimeout
  39. "Timeout waiting to lock key", // kLockTimeout
  40. "Failed to acquire lock due to max_num_locks limit", // kLockLimit
  41. "No space left on device", // kNoSpace
  42. "Deadlock", // kDeadlock
  43. "Stale file handle", // kStaleFile
  44. "Memory limit reached", // kMemoryLimit
  45. "Space limit reached", // kSpaceLimit
  46. "No such file or directory", // kPathNotFound
  47. // KMergeOperandsInsufficientCapacity
  48. "Insufficient capacity for merge operands",
  49. // kManualCompactionPaused
  50. "Manual compaction paused",
  51. };
  52. Status::Status(Code _code, SubCode _subcode, const Slice& msg,
  53. const Slice& msg2)
  54. : code_(_code), subcode_(_subcode), sev_(kNoError) {
  55. assert(code_ != kOk);
  56. assert(subcode_ != kMaxSubCode);
  57. const size_t len1 = msg.size();
  58. const size_t len2 = msg2.size();
  59. const size_t size = len1 + (len2 ? (2 + len2) : 0);
  60. char* const result = new char[size + 1]; // +1 for null terminator
  61. memcpy(result, msg.data(), len1);
  62. if (len2) {
  63. result[len1] = ':';
  64. result[len1 + 1] = ' ';
  65. memcpy(result + len1 + 2, msg2.data(), len2);
  66. }
  67. result[size] = '\0'; // null terminator for C style string
  68. state_ = result;
  69. }
  70. std::string Status::ToString() const {
  71. char tmp[30];
  72. const char* type;
  73. switch (code_) {
  74. case kOk:
  75. return "OK";
  76. case kNotFound:
  77. type = "NotFound: ";
  78. break;
  79. case kCorruption:
  80. type = "Corruption: ";
  81. break;
  82. case kNotSupported:
  83. type = "Not implemented: ";
  84. break;
  85. case kInvalidArgument:
  86. type = "Invalid argument: ";
  87. break;
  88. case kIOError:
  89. type = "IO error: ";
  90. break;
  91. case kMergeInProgress:
  92. type = "Merge in progress: ";
  93. break;
  94. case kIncomplete:
  95. type = "Result incomplete: ";
  96. break;
  97. case kShutdownInProgress:
  98. type = "Shutdown in progress: ";
  99. break;
  100. case kTimedOut:
  101. type = "Operation timed out: ";
  102. break;
  103. case kAborted:
  104. type = "Operation aborted: ";
  105. break;
  106. case kBusy:
  107. type = "Resource busy: ";
  108. break;
  109. case kExpired:
  110. type = "Operation expired: ";
  111. break;
  112. case kTryAgain:
  113. type = "Operation failed. Try again.: ";
  114. break;
  115. case kColumnFamilyDropped:
  116. type = "Column family dropped: ";
  117. break;
  118. default:
  119. snprintf(tmp, sizeof(tmp), "Unknown code(%d): ",
  120. static_cast<int>(code()));
  121. type = tmp;
  122. break;
  123. }
  124. std::string result(type);
  125. if (subcode_ != kNone) {
  126. uint32_t index = static_cast<int32_t>(subcode_);
  127. assert(sizeof(msgs) > index);
  128. result.append(msgs[index]);
  129. }
  130. if (state_ != nullptr) {
  131. result.append(state_);
  132. }
  133. return result;
  134. }
  135. } // namespace ROCKSDB_NAMESPACE