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 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 
Status(final Code code, final SubCode subCode, final String state)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    */
Status(final byte code, final byte subCode, final String state)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 
getCode()34   public Code getCode() {
35     return code;
36   }
37 
getSubCode()38   public SubCode getSubCode() {
39     return subCode;
40   }
41 
getState()42   public String getState() {
43     return state;
44   }
45 
getCodeString()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 
57   // should stay in sync with /include/rocksdb/status.h:Code and /java/rocksjni/portal.h:toJavaStatusCode
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),
72     TryAgain(           (byte)0xD),
73     Undefined(          (byte)0x7F);
74 
75     private final byte value;
76 
Code(final byte value)77     Code(final byte value) {
78       this.value = value;
79     }
80 
getCode(final byte value)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(
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      */
getValue()96     public byte getValue() {
97       return value;
98     }
99   }
100 
101   // should stay in sync with /include/rocksdb/status.h:SubCode and /java/rocksjni/portal.h:toJavaStatusSubCode
102   public enum SubCode {
103     None(         (byte)0x0),
104     MutexTimeout( (byte)0x1),
105     LockTimeout(  (byte)0x2),
106     LockLimit(    (byte)0x3),
107     NoSpace(      (byte)0x4),
108     Deadlock(     (byte)0x5),
109     StaleFile(    (byte)0x6),
110     MemoryLimit(  (byte)0x7),
111     Undefined(    (byte)0x7F);
112 
113     private final byte value;
114 
SubCode(final byte value)115     SubCode(final byte value) {
116       this.value = value;
117     }
118 
getSubCode(final byte value)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(
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      */
getValue()134     public byte getValue() {
135       return value;
136     }
137   }
138 }
139