Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
HdfsCachingOp.java
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.analysis;
16 
17 import java.math.BigDecimal;
18 
21 import com.cloudera.impala.thrift.THdfsCachingOp;
22 import com.google.common.base.Preconditions;
23 
28 public class HdfsCachingOp implements ParseNode {
29  private final THdfsCachingOp cacheOp_;
30  private final BigDecimal parsedReplication_;
31 
35  public HdfsCachingOp() {
36  cacheOp_ = new THdfsCachingOp(false);
37  parsedReplication_ = null;
38  }
39 
44  public HdfsCachingOp(String cachePoolName, BigDecimal replication) {
45  cacheOp_ = new THdfsCachingOp(true);
46  cacheOp_.setCache_pool_name(cachePoolName);
47  parsedReplication_ = replication;
48  }
49 
50  @Override
51  public void analyze(Analyzer analyzer) throws AnalysisException {
52  if (cacheOp_.isSet_cached()) {
53  String poolName = cacheOp_.getCache_pool_name();
54  Preconditions.checkNotNull(poolName);
55  if (poolName.isEmpty()) {
56  throw new AnalysisException("Cache pool name cannot be empty.");
57  }
58 
59  HdfsCachePool cachePool = analyzer.getCatalog().getHdfsCachePool(poolName);
60  if (cachePool == null) {
61  throw new AnalysisException(
62  "The specified cache pool does not exist: " + poolName);
63  }
64 
65  if (parsedReplication_ != null && (parsedReplication_.longValue() <= 0 ||
66  parsedReplication_.longValue() > Short.MAX_VALUE)) {
67  throw new AnalysisException(
68  "Cache replication factor must be between 0 and Short.MAX_VALUE");
69  }
70 
71  if (parsedReplication_ != null) {
72  cacheOp_.setReplication(parsedReplication_.shortValue());
73  }
74  }
75  }
76 
77  @Override
78  public String toSql() {
79  return !shouldCache() ? "UNCACHED" : "CACHED IN '" + getCachePoolName() + "' WITH " +
80  "REPLICATION = " + parsedReplication_.longValue();
81  }
82 
83  public THdfsCachingOp toThrift() { return cacheOp_; }
84 
85  public boolean shouldCache() { return cacheOp_.isSet_cached(); }
86 
87  public String getCachePoolName() {
88  return shouldCache() ? cacheOp_.getCache_pool_name() : null;
89  }
90 }
HdfsCachingOp(String cachePoolName, BigDecimal replication)