HBASE-26688 Threads shared EMPTY_RESULT may lead to unexpected client job down. ...
[hbase.git] / hbase-client / src / main / java / org / apache / hadoop / hbase / client / TableState.java
blob40612e9b20256f782ee096ae7dd91b9f63822f7d
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.client;
20 import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
21 import org.apache.hadoop.hbase.TableName;
22 import org.apache.yetus.audience.InterfaceAudience;
23 import org.apache.yetus.audience.InterfaceStability;
24 import org.apache.hadoop.hbase.exceptions.DeserializationException;
25 import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
27 /**
28 * Represents table state.
30 @InterfaceAudience.Private
31 public class TableState {
33 @InterfaceAudience.Private
34 @InterfaceStability.Evolving
35 public static enum State {
36 ENABLED,
37 DISABLED,
38 DISABLING,
39 ENABLING;
41 /**
42 * Covert from PB version of State
44 * @param state convert from
45 * @return POJO
47 public static State convert(HBaseProtos.TableState.State state) {
48 State ret;
49 switch (state) {
50 case ENABLED:
51 ret = State.ENABLED;
52 break;
53 case DISABLED:
54 ret = State.DISABLED;
55 break;
56 case DISABLING:
57 ret = State.DISABLING;
58 break;
59 case ENABLING:
60 ret = State.ENABLING;
61 break;
62 default:
63 throw new IllegalStateException(state.toString());
65 return ret;
68 /**
69 * Covert to PB version of State
71 * @return PB
73 public HBaseProtos.TableState.State convert() {
74 HBaseProtos.TableState.State state;
75 switch (this) {
76 case ENABLED:
77 state = HBaseProtos.TableState.State.ENABLED;
78 break;
79 case DISABLED:
80 state = HBaseProtos.TableState.State.DISABLED;
81 break;
82 case DISABLING:
83 state = HBaseProtos.TableState.State.DISABLING;
84 break;
85 case ENABLING:
86 state = HBaseProtos.TableState.State.ENABLING;
87 break;
88 default:
89 throw new IllegalStateException(this.toString());
91 return state;
96 private final TableName tableName;
97 private final State state;
99 /**
100 * @return True if table is {@link State#ENABLED}.
102 public boolean isEnabled() {
103 return isInStates(State.ENABLED);
107 * @return True if table is {@link State#ENABLING}.
109 public boolean isEnabling() {
110 return isInStates(State.ENABLING);
114 * @return True if {@link State#ENABLED} or {@link State#ENABLING}
116 public boolean isEnabledOrEnabling() {
117 return isInStates(State.ENABLED, State.ENABLING);
121 * @return True if table is disabled.
123 public boolean isDisabled() {
124 return isInStates(State.DISABLED);
128 * @return True if table is disabling.
130 public boolean isDisabling() {
131 return isInStates(State.DISABLING);
135 * @return True if {@link State#DISABLED} or {@link State#DISABLED}
137 public boolean isDisabledOrDisabling() {
138 return isInStates(State.DISABLED, State.DISABLING);
142 * Create instance of TableState.
143 * @param tableName name of the table
144 * @param state table state
146 public TableState(TableName tableName, State state) {
147 this.tableName = tableName;
148 this.state = state;
152 * @return table state
154 public State getState() {
155 return state;
159 * Table name for state
161 * @return milliseconds
163 public TableName getTableName() {
164 return tableName;
168 * Check that table in given states
169 * @param state state
170 * @return true if satisfies
172 public boolean inStates(State state) {
173 return this.state.equals(state);
177 * Check that table in given states
178 * @param states state list
179 * @return true if satisfies
181 public boolean inStates(State... states) {
182 for (State s : states) {
183 if (s.equals(this.state))
184 return true;
186 return false;
191 * Covert to PB version of TableState
192 * @return PB
194 public HBaseProtos.TableState convert() {
195 return HBaseProtos.TableState.newBuilder()
196 .setState(this.state.convert()).build();
200 * Covert from PB version of TableState
202 * @param tableName table this state of
203 * @param tableState convert from
204 * @return POJO
206 public static TableState convert(TableName tableName, HBaseProtos.TableState tableState) {
207 TableState.State state = State.convert(tableState.getState());
208 return new TableState(tableName, state);
211 public static TableState parseFrom(TableName tableName, byte[] bytes)
212 throws DeserializationException {
213 try {
214 return convert(tableName, HBaseProtos.TableState.parseFrom(bytes));
215 } catch (InvalidProtocolBufferException e) {
216 throw new DeserializationException(e);
221 * Static version of state checker
222 * @param target equals to any of
223 * @return true if satisfies
225 public boolean isInStates(State... target) {
226 for (State tableState : target) {
227 if (this.state.equals(tableState)) {
228 return true;
231 return false;
234 @Override
235 public boolean equals(Object o) {
236 if (this == o) return true;
237 if (o == null || getClass() != o.getClass()) return false;
239 TableState that = (TableState) o;
241 if (state != that.state) return false;
242 if (tableName != null ? !tableName.equals(that.tableName) : that.tableName != null)
243 return false;
245 return true;
248 @Override
249 public int hashCode() {
250 int result = (tableName != null ? tableName.hashCode() : 0);
251 result = 31 * result + (state != null ? state.hashCode() : 0);
252 return result;
255 @Override
256 public String toString() {
257 return "tableName=" + tableName + ", state=" + state;