Back to home page

OSCL-LXR

 
 

    


0001 /**
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements.  See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership.  The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License.  You may obtain a copy of the License at
0009  *
0010  *     http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing, software
0013  * distributed under the License is distributed on an "AS IS" BASIS,
0014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015  * See the License for the specific language governing permissions and
0016  * limitations under the License.
0017  */
0018 
0019 package org.apache.hive.service.cli;
0020 
0021 import org.apache.hive.service.cli.thrift.TOperationState;
0022 
0023 /**
0024  * OperationState.
0025  *
0026  */
0027 public enum OperationState {
0028   INITIALIZED(TOperationState.INITIALIZED_STATE, false),
0029   RUNNING(TOperationState.RUNNING_STATE, false),
0030   FINISHED(TOperationState.FINISHED_STATE, true),
0031   CANCELED(TOperationState.CANCELED_STATE, true),
0032   CLOSED(TOperationState.CLOSED_STATE, true),
0033   ERROR(TOperationState.ERROR_STATE, true),
0034   UNKNOWN(TOperationState.UKNOWN_STATE, false),
0035   PENDING(TOperationState.PENDING_STATE, false);
0036 
0037   private final TOperationState tOperationState;
0038   private final boolean terminal;
0039 
0040   OperationState(TOperationState tOperationState, boolean terminal) {
0041     this.tOperationState = tOperationState;
0042     this.terminal = terminal;
0043   }
0044 
0045   // must be sync with TOperationState in order
0046   public static OperationState getOperationState(TOperationState tOperationState) {
0047     return OperationState.values()[tOperationState.getValue()];
0048   }
0049 
0050   public static void validateTransition(OperationState oldState,
0051       OperationState newState)
0052           throws HiveSQLException {
0053     switch (oldState) {
0054     case INITIALIZED:
0055       switch (newState) {
0056       case PENDING:
0057       case RUNNING:
0058       case CANCELED:
0059       case CLOSED:
0060         return;
0061       }
0062       break;
0063     case PENDING:
0064       switch (newState) {
0065       case RUNNING:
0066       case FINISHED:
0067       case CANCELED:
0068       case ERROR:
0069       case CLOSED:
0070         return;
0071       }
0072       break;
0073     case RUNNING:
0074       switch (newState) {
0075       case FINISHED:
0076       case CANCELED:
0077       case ERROR:
0078       case CLOSED:
0079         return;
0080       }
0081       break;
0082     case FINISHED:
0083     case CANCELED:
0084     case ERROR:
0085       if (OperationState.CLOSED.equals(newState)) {
0086         return;
0087       }
0088       break;
0089     default:
0090       // fall-through
0091     }
0092     throw new HiveSQLException("Illegal Operation state transition " +
0093         "from " + oldState + " to " + newState);
0094   }
0095 
0096   public void validateTransition(OperationState newState)
0097       throws HiveSQLException {
0098     validateTransition(this, newState);
0099   }
0100 
0101   public TOperationState toTOperationState() {
0102     return tOperationState;
0103   }
0104 
0105   public boolean isTerminal() {
0106     return terminal;
0107   }
0108 }