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