Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Db.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.catalog;
16 
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 
21 import org.apache.log4j.Logger;
22 
24 import com.cloudera.impala.thrift.TCatalogObjectType;
25 import com.cloudera.impala.thrift.TDatabase;
26 import com.cloudera.impala.thrift.TFunctionCategory;
28 import com.google.common.base.Preconditions;
29 import com.google.common.collect.Lists;
30 
44 public class Db implements CatalogObject {
45  private static final Logger LOG = Logger.getLogger(Db.class);
46  private final Catalog parentCatalog_;
47  private final TDatabase thriftDb_;
49 
50  // Table metadata cache.
51  private final CatalogObjectCache<Table> tableCache_;
52 
53  // All of the registered user functions. The key is the user facing name (e.g. "myUdf"),
54  // and the values are all the overloaded variants (e.g. myUdf(double), myUdf(string))
55  // This includes both UDFs and UDAs. Updates are made thread safe by synchronizing
56  // on this map.
57  private final HashMap<String, List<Function>> functions_;
58 
59  // If true, this database is an Impala system database.
60  // (e.g. can't drop it, can't add tables to it, etc).
61  private boolean isSystemDb_ = false;
62 
63  public Db(String name, Catalog catalog) {
64  thriftDb_ = new TDatabase(name.toLowerCase());
65  parentCatalog_ = catalog;
66  tableCache_ = new CatalogObjectCache<Table>();
67  functions_ = new HashMap<String, List<Function>>();
68  }
69 
70  public void setIsSystemDb(boolean b) { isSystemDb_ = b; }
71 
75  public static Db fromTDatabase(TDatabase db, Catalog parentCatalog) {
76  return new Db(db.getDb_name(), parentCatalog);
77  }
78 
79  public boolean isSystemDb() { return isSystemDb_; }
80  public TDatabase toThrift() { return thriftDb_; }
81  public String getName() { return thriftDb_.getDb_name(); }
82  public TCatalogObjectType getCatalogObjectType() {
83  return TCatalogObjectType.DATABASE;
84  }
85 
89  public void addTable(Table table) {
90  tableCache_.add(table);
91  }
92 
96  public List<String> getAllTableNames() {
97  return Lists.newArrayList(tableCache_.keySet());
98  }
99 
100  public boolean containsTable(String tableName) {
101  return tableCache_.contains(tableName.toLowerCase());
102  }
103 
108  public Table getTable(String tblName) {
109  return tableCache_.get(tblName);
110  }
111 
115  public Table removeTable(String tableName) {
116  return tableCache_.remove(tableName.toLowerCase());
117  }
118 
122  public int numFunctions() {
123  synchronized (functions_) {
124  return functions_.size();
125  }
126  }
127 
131  public boolean containsFunction(String name) {
132  synchronized (functions_) {
133  return functions_.get(name) != null;
134  }
135  }
136 
137  /*
138  * See comment in Catalog.
139  */
140  public Function getFunction(Function desc, Function.CompareMode mode) {
141  synchronized (functions_) {
142  List<Function> fns = functions_.get(desc.functionName());
143  if (fns == null) return null;
144 
145  // First check for identical
146  for (Function f: fns) {
147  if (f.compare(desc, Function.CompareMode.IS_IDENTICAL)) return f;
148  }
149  if (mode == Function.CompareMode.IS_IDENTICAL) return null;
150 
151  // Next check for indistinguishable
152  for (Function f: fns) {
153  if (f.compare(desc, Function.CompareMode.IS_INDISTINGUISHABLE)) return f;
154  }
155  if (mode == Function.CompareMode.IS_INDISTINGUISHABLE) return null;
156 
157  // Finally check for is_subtype
158  for (Function f: fns) {
159  if (f.compare(desc, Function.CompareMode.IS_SUPERTYPE_OF)) return f;
160  }
161  }
162  return null;
163  }
164 
165  public Function getFunction(String signatureString) {
166  synchronized (functions_) {
167  for (List<Function> fns: functions_.values()) {
168  for (Function f: fns) {
169  if (f.signatureString().equals(signatureString)) return f;
170  }
171  }
172  }
173  return null;
174  }
175 
179  public boolean addFunction(Function fn) {
180  Preconditions.checkState(fn.dbName().equals(getName()));
181  // TODO: add this to persistent store
182  synchronized (functions_) {
183  if (getFunction(fn, Function.CompareMode.IS_INDISTINGUISHABLE) != null) {
184  return false;
185  }
186  List<Function> fns = functions_.get(fn.functionName());
187  if (fns == null) {
188  fns = Lists.newArrayList();
189  functions_.put(fn.functionName(), fns);
190  }
191  return fns.add(fn);
192  }
193  }
194 
199  // TODO: remove this from persistent store.
200  synchronized (functions_) {
201  Function fn = getFunction(desc, Function.CompareMode.IS_INDISTINGUISHABLE);
202  if (fn == null) return null;
203  List<Function> fns = functions_.get(desc.functionName());
204  Preconditions.checkNotNull(fns);
205  fns.remove(fn);
206  if (fns.isEmpty()) functions_.remove(desc.functionName());
207  return fn;
208  }
209  }
210 
216  public Function removeFunction(String signatureStr) {
217  synchronized (functions_) {
218  Function targetFn = getFunction(signatureStr);
219  if (targetFn != null) return removeFunction(targetFn);
220  }
221  return null;
222  }
223 
228  public void addScalarBuiltin(String fnName, String symbol, boolean varArgs,
229  Type retType, Type ... args) {
230  addScalarBuiltin(fnName, symbol, null, null, varArgs, retType, args);
231  }
232 
236  public void addScalarBuiltin(String fnName, String symbol, String prepareFnSymbol,
237  String closeFnSymbol, boolean varArgs, Type retType, Type ... args) {
238  Preconditions.checkState(isSystemDb());
240  fnName, Lists.newArrayList(args), varArgs, retType,
241  symbol, prepareFnSymbol, closeFnSymbol, false));
242  }
243 
247  public void addBuiltin(Function fn) {
248  Preconditions.checkState(isSystemDb());
249  Preconditions.checkState(fn != null);
250  Preconditions.checkState(getFunction(fn, CompareMode.IS_IDENTICAL) == null);
251  addFunction(fn);
252  }
253 
259  protected HashMap<String, List<Function>> getAllFunctions() {
260  return functions_;
261  }
262 
266  public List<Function> getFunctions(TFunctionCategory category,
267  PatternMatcher fnPattern) {
268  List<Function> functions = Lists.newArrayList();
269  synchronized (functions_) {
270  for (Map.Entry<String, List<Function>> fns: functions_.entrySet()) {
271  if (fnPattern.matches(fns.getKey())) {
272  for (Function fn: fns.getValue()) {
273  if (!fn.userVisible()) continue;
274  if (category == null
275  || (category == TFunctionCategory.SCALAR && fn instanceof ScalarFunction)
276  || (category == TFunctionCategory.AGGREGATE
277  && fn instanceof AggregateFunction
278  && ((AggregateFunction)fn).isAggregateFn())
279  || (category == TFunctionCategory.ANALYTIC
280  && fn instanceof AggregateFunction
281  && ((AggregateFunction)fn).isAnalyticFn())) {
282  functions.add(fn);
283  }
284  }
285  }
286  }
287  }
288  return functions;
289  }
290 
291  @Override
292  public long getCatalogVersion() { return catalogVersion_; }
293  @Override
294  public void setCatalogVersion(long newVersion) { catalogVersion_ = newVersion; }
296 
297  @Override
298  public boolean isLoaded() { return true; }
299 }
List< String > getAllTableNames()
Definition: Db.java:96
final Catalog parentCatalog_
Definition: Db.java:46
void addTable(Table table)
Definition: Db.java:89
final CatalogObjectCache< Table > tableCache_
Definition: Db.java:51
void addScalarBuiltin(String fnName, String symbol, boolean varArgs, Type retType, Type...args)
Definition: Db.java:228
TCatalogObjectType getCatalogObjectType()
Definition: Db.java:82
Table removeTable(String tableName)
Definition: Db.java:115
boolean containsTable(String tableName)
Definition: Db.java:100
static final long INITIAL_CATALOG_VERSION
Definition: Catalog.java:57
Table getTable(String tblName)
Definition: Db.java:108
static ScalarFunction createBuiltin(String name, ArrayList< Type > argTypes, boolean hasVarArgs, Type retType, String symbol, String prepareFnSymbol, String closeFnSymbol, boolean isOperator)
Function removeFunction(Function desc)
Definition: Db.java:198
boolean addFunction(Function fn)
Definition: Db.java:179
Function removeFunction(String signatureStr)
Definition: Db.java:216
static final Logger LOG
Definition: Db.java:45
HashMap< String, List< Function > > getAllFunctions()
Definition: Db.java:259
List< Function > getFunctions(TFunctionCategory category, PatternMatcher fnPattern)
Definition: Db.java:266
Function getFunction(String signatureString)
Definition: Db.java:165
static Db fromTDatabase(TDatabase db, Catalog parentCatalog)
Definition: Db.java:75
void addBuiltin(Function fn)
Definition: Db.java:247
void setCatalogVersion(long newVersion)
Definition: Db.java:294
Db(String name, Catalog catalog)
Definition: Db.java:63
boolean containsFunction(String name)
Definition: Db.java:131
final TDatabase thriftDb_
Definition: Db.java:47
Function getFunction(Function desc, Function.CompareMode mode)
Definition: Db.java:140
string name
Definition: cpu-info.cc:50
final HashMap< String, List< Function > > functions_
Definition: Db.java:57
void addScalarBuiltin(String fnName, String symbol, String prepareFnSymbol, String closeFnSymbol, boolean varArgs, Type retType, Type...args)
Definition: Db.java:236
void setIsSystemDb(boolean b)
Definition: Db.java:70