]>
Commit | Line | Data |
---|---|---|
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 | package org.rocksdb; | |
7 | ||
8 | /** | |
9 | * Represents the status returned by a function call in RocksDB. | |
10 | * | |
11 | * Currently only used with {@link RocksDBException} when the | |
12 | * status is not {@link Code#Ok} | |
13 | */ | |
14 | public class Status { | |
15 | private final Code code; | |
16 | /* @Nullable */ private final SubCode subCode; | |
17 | /* @Nullable */ private final String state; | |
18 | ||
19 | public Status(final Code code, final SubCode subCode, final String state) { | |
20 | this.code = code; | |
21 | this.subCode = subCode; | |
22 | this.state = state; | |
23 | } | |
24 | ||
25 | /** | |
26 | * Intentionally private as this will be called from JNI | |
27 | */ | |
28 | private Status(final byte code, final byte subCode, final String state) { | |
29 | this.code = Code.getCode(code); | |
30 | this.subCode = SubCode.getSubCode(subCode); | |
31 | this.state = state; | |
32 | } | |
33 | ||
34 | public Code getCode() { | |
35 | return code; | |
36 | } | |
37 | ||
38 | public SubCode getSubCode() { | |
39 | return subCode; | |
40 | } | |
41 | ||
42 | public String getState() { | |
43 | return state; | |
44 | } | |
45 | ||
46 | public String getCodeString() { | |
47 | final StringBuilder builder = new StringBuilder() | |
48 | .append(code.name()); | |
49 | if(subCode != null && subCode != SubCode.None) { | |
50 | builder.append("(") | |
51 | .append(subCode.name()) | |
52 | .append(")"); | |
53 | } | |
54 | return builder.toString(); | |
55 | } | |
56 | ||
11fdf7f2 | 57 | // should stay in sync with /include/rocksdb/status.h:Code and /java/rocksjni/portal.h:toJavaStatusCode |
7c673cae FG |
58 | public enum Code { |
59 | Ok( (byte)0x0), | |
60 | NotFound( (byte)0x1), | |
61 | Corruption( (byte)0x2), | |
62 | NotSupported( (byte)0x3), | |
63 | InvalidArgument( (byte)0x4), | |
64 | IOError( (byte)0x5), | |
65 | MergeInProgress( (byte)0x6), | |
66 | Incomplete( (byte)0x7), | |
67 | ShutdownInProgress( (byte)0x8), | |
68 | TimedOut( (byte)0x9), | |
69 | Aborted( (byte)0xA), | |
70 | Busy( (byte)0xB), | |
71 | Expired( (byte)0xC), | |
11fdf7f2 TL |
72 | TryAgain( (byte)0xD), |
73 | Undefined( (byte)0x7F); | |
7c673cae FG |
74 | |
75 | private final byte value; | |
76 | ||
77 | Code(final byte value) { | |
78 | this.value = value; | |
79 | } | |
80 | ||
81 | public static Code getCode(final byte value) { | |
82 | for (final Code code : Code.values()) { | |
83 | if (code.value == value){ | |
84 | return code; | |
85 | } | |
86 | } | |
87 | throw new IllegalArgumentException( | |
11fdf7f2 TL |
88 | "Illegal value provided for Code (" + value + ")."); |
89 | } | |
90 | ||
91 | /** | |
92 | * Returns the byte value of the enumerations value. | |
93 | * | |
94 | * @return byte representation | |
95 | */ | |
96 | public byte getValue() { | |
97 | return value; | |
7c673cae FG |
98 | } |
99 | } | |
100 | ||
11fdf7f2 | 101 | // should stay in sync with /include/rocksdb/status.h:SubCode and /java/rocksjni/portal.h:toJavaStatusSubCode |
7c673cae FG |
102 | public enum SubCode { |
103 | None( (byte)0x0), | |
104 | MutexTimeout( (byte)0x1), | |
105 | LockTimeout( (byte)0x2), | |
106 | LockLimit( (byte)0x3), | |
11fdf7f2 TL |
107 | NoSpace( (byte)0x4), |
108 | Deadlock( (byte)0x5), | |
109 | StaleFile( (byte)0x6), | |
110 | MemoryLimit( (byte)0x7), | |
111 | Undefined( (byte)0x7F); | |
7c673cae FG |
112 | |
113 | private final byte value; | |
114 | ||
115 | SubCode(final byte value) { | |
116 | this.value = value; | |
117 | } | |
118 | ||
119 | public static SubCode getSubCode(final byte value) { | |
120 | for (final SubCode subCode : SubCode.values()) { | |
121 | if (subCode.value == value){ | |
122 | return subCode; | |
123 | } | |
124 | } | |
125 | throw new IllegalArgumentException( | |
11fdf7f2 TL |
126 | "Illegal value provided for SubCode (" + value + ")."); |
127 | } | |
128 | ||
129 | /** | |
130 | * Returns the byte value of the enumerations value. | |
131 | * | |
132 | * @return byte representation | |
133 | */ | |
134 | public byte getValue() { | |
135 | return value; | |
7c673cae FG |
136 | } |
137 | } | |
138 | } |