]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | // This source code is licensed under the BSD-style license found in the | |
3 | // LICENSE file in the root directory of this source tree. An additional grant | |
4 | // of patent rights can be found in the PATENTS file in the same 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 | ||
10 | #include "rocksdb/status.h" | |
11 | #include <stdio.h> | |
12 | #include <cstring> | |
13 | #include "port/port.h" | |
14 | ||
15 | namespace rocksdb { | |
16 | ||
17 | const char* Status::CopyState(const char* state) { | |
18 | char* const result = | |
19 | new char[std::strlen(state) + 1]; // +1 for the null terminator | |
20 | std::strcpy(result, state); | |
21 | return result; | |
22 | } | |
23 | ||
24 | Status::Status(Code _code, SubCode _subcode, const Slice& msg, const Slice& msg2) | |
25 | : code_(_code), subcode_(_subcode) { | |
26 | assert(code_ != kOk); | |
27 | assert(subcode_ != kMaxSubCode); | |
28 | const size_t len1 = msg.size(); | |
29 | const size_t len2 = msg2.size(); | |
30 | const size_t size = len1 + (len2 ? (2 + len2) : 0); | |
31 | char* const result = new char[size + 1]; // +1 for null terminator | |
32 | memcpy(result, msg.data(), len1); | |
33 | if (len2) { | |
34 | result[len1] = ':'; | |
35 | result[len1 + 1] = ' '; | |
36 | memcpy(result + len1 + 2, msg2.data(), len2); | |
37 | } | |
38 | result[size] = '\0'; // null terminator for C style string | |
39 | state_ = result; | |
40 | } | |
41 | ||
42 | std::string Status::ToString() const { | |
43 | char tmp[30]; | |
44 | const char* type; | |
45 | switch (code_) { | |
46 | case kOk: | |
47 | return "OK"; | |
48 | case kNotFound: | |
49 | type = "NotFound: "; | |
50 | break; | |
51 | case kCorruption: | |
52 | type = "Corruption: "; | |
53 | break; | |
54 | case kNotSupported: | |
55 | type = "Not implemented: "; | |
56 | break; | |
57 | case kInvalidArgument: | |
58 | type = "Invalid argument: "; | |
59 | break; | |
60 | case kIOError: | |
61 | type = "IO error: "; | |
62 | break; | |
63 | case kMergeInProgress: | |
64 | type = "Merge in progress: "; | |
65 | break; | |
66 | case kIncomplete: | |
67 | type = "Result incomplete: "; | |
68 | break; | |
69 | case kShutdownInProgress: | |
70 | type = "Shutdown in progress: "; | |
71 | break; | |
72 | case kTimedOut: | |
73 | type = "Operation timed out: "; | |
74 | break; | |
75 | case kAborted: | |
76 | type = "Operation aborted: "; | |
77 | break; | |
78 | case kBusy: | |
79 | type = "Resource busy: "; | |
80 | break; | |
81 | case kExpired: | |
82 | type = "Operation expired: "; | |
83 | break; | |
84 | case kTryAgain: | |
85 | type = "Operation failed. Try again.: "; | |
86 | break; | |
87 | default: | |
88 | snprintf(tmp, sizeof(tmp), "Unknown code(%d): ", | |
89 | static_cast<int>(code())); | |
90 | type = tmp; | |
91 | break; | |
92 | } | |
93 | std::string result(type); | |
94 | if (subcode_ != kNone) { | |
95 | uint32_t index = static_cast<int32_t>(subcode_); | |
96 | assert(sizeof(msgs) > index); | |
97 | result.append(msgs[index]); | |
98 | } | |
99 | ||
100 | if (state_ != nullptr) { | |
101 | result.append(state_); | |
102 | } | |
103 | return result; | |
104 | } | |
105 | ||
106 | } // namespace rocksdb |