View Javadoc

1   /*
2    * Copyright (c) 2011.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.hbql.executor;
22  
23  import org.apache.hadoop.hbase.hbql.client.AsyncExecutorManager;
24  import org.apache.hadoop.hbase.hbql.client.HBqlException;
25  
26  import java.util.List;
27  
28  public class AsyncExecutorDefinition {
29  
30      private final String                 name;
31      private final List<ExecutorProperty> executorPropertyList;
32  
33      private ExecutorProperty minThreadCount = null;
34      private ExecutorProperty maxThreadCount = null;
35      private ExecutorProperty keepAliveSecs  = null;
36  
37      public AsyncExecutorDefinition(final String name, final List<ExecutorProperty> executorPropertyList) {
38          this.name = name;
39          this.executorPropertyList = executorPropertyList;
40      }
41  
42      public String getName() {
43          return this.name;
44      }
45  
46      private List<ExecutorProperty> getExecutorPropertyList() {
47          return this.executorPropertyList;
48      }
49  
50      private ExecutorProperty validateProperty(final ExecutorProperty assignee,
51                                                final ExecutorProperty value) throws HBqlException {
52          if (assignee != null)
53              throw new HBqlException("Multiple " + value.getPropertyType().getDescription()
54                                      + " values for " + this.getName() + " not allowed");
55          return value;
56      }
57  
58      public void validatePropertyList() throws HBqlException {
59  
60          if (this.getExecutorPropertyList() == null)
61              return;
62  
63          for (final ExecutorProperty executorProperty : this.getExecutorPropertyList()) {
64  
65              executorProperty.validate();
66  
67              switch (executorProperty.getEnumType()) {
68  
69                  case MIN_THREAD_COUNT:
70                      this.minThreadCount = this.validateProperty(this.minThreadCount, executorProperty);
71                      break;
72  
73                  case MAX_THREAD_COUNT:
74                      this.maxThreadCount = this.validateProperty(this.maxThreadCount, executorProperty);
75                      break;
76  
77                  case KEEP_ALIVE_SECS:
78                      this.keepAliveSecs = this.validateProperty(this.keepAliveSecs, executorProperty);
79                      break;
80              }
81          }
82      }
83  
84      public int getMinThreadCount() throws HBqlException {
85          if (this.minThreadCount != null)
86              return this.minThreadCount.getIntegerValue();
87          else
88              return AsyncExecutorManager.defaultMinThreadCount;
89      }
90  
91      public int getMaxThreadCount() throws HBqlException {
92          if (this.maxThreadCount != null)
93              return this.maxThreadCount.getIntegerValue();
94          else
95              return AsyncExecutorManager.defaultMaxThreadCount;
96      }
97  
98      public long getKeepAliveSecs() throws HBqlException {
99          if (this.keepAliveSecs != null)
100             return this.keepAliveSecs.getIntegerValue();
101         else
102             return AsyncExecutorManager.defaultKeepAliveSecs;
103     }
104 
105     public String asString() {
106         try {
107             return " MIN_THREAD_COUNT : " + this.getMinThreadCount()
108                    + ", MAX_THREAD_COUNT : " + this.getMaxThreadCount()
109                    + ", KEEP_ALIVE_SECS : " + this.getKeepAliveSecs();
110         }
111         catch (HBqlException e) {
112             return "Invalid expression";
113         }
114     }
115 }