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