Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CatalogObjectCache.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.Iterator;
18 import java.util.List;
19 import java.util.Set;
20 import java.util.concurrent.ConcurrentHashMap;
21 
22 import org.apache.log4j.Logger;
23 
24 import com.google.common.base.Preconditions;
25 import com.google.common.collect.Lists;
26 
31 public class CatalogObjectCache<T extends CatalogObject> implements Iterable<T> {
32  private static final Logger LOG = Logger.getLogger(CatalogObjectCache.class);
33  private final boolean caseInsensitiveKeys_;
34 
39  public CatalogObjectCache() {
40  this(true);
41  }
42 
47  public CatalogObjectCache(boolean caseInsensitiveKeys) {
48  caseInsensitiveKeys_ = caseInsensitiveKeys;
49  }
50 
51  // Map of lower-case object name to CatalogObject. New entries are added
52  // by calling add(). Updates of the cache must be synchronized because adding
53  // new entries may require two cache accesses that must be performed atomically.
54  // TODO: For simplicity, consider using a (non-concurrent) HashMap and marking
55  // all methods as synchronized.
56  private final ConcurrentHashMap<String, T> metadataCache_ =
57  new ConcurrentHashMap<String, T>();
58 
67  public synchronized boolean add(T catalogObject) {
68  Preconditions.checkNotNull(catalogObject);
69  String key = catalogObject.getName();
70  if (caseInsensitiveKeys_) key = key.toLowerCase();
71  T existingItem = metadataCache_.putIfAbsent(key, catalogObject);
72  if (existingItem == null) return true;
73 
74  if (existingItem.getCatalogVersion() < catalogObject.getCatalogVersion()) {
75  // When existingItem != null it indicates there was already an existing entry
76  // associated with the key. Add the updated object iff it has a catalog
77  // version greater than the existing entry.
78  metadataCache_.put(key, catalogObject);
79  return true;
80  }
81  return false;
82  }
83 
88  public synchronized T remove(String name) {
89  if (caseInsensitiveKeys_) name = name.toLowerCase();
90  return metadataCache_.remove(name);
91  }
92 
96  public synchronized void clear() {
97  metadataCache_.clear();
98  }
99 
106  public Set<String> keySet() {
107  return metadataCache_.keySet();
108  }
109 
113  public List<T> getValues() {
114  return Lists.newArrayList(metadataCache_.values());
115  }
116 
120  public boolean contains(String name) {
121  if (caseInsensitiveKeys_) name = name.toLowerCase();
122  return metadataCache_.containsKey(name);
123  }
124 
130  public T get(String name) {
131  if (caseInsensitiveKeys_) name = name.toLowerCase();
132  return metadataCache_.get(name);
133  }
134 
141  @Override
142  public Iterator<T> iterator() {
143  return metadataCache_.values().iterator();
144  }
145 }
string name
Definition: cpu-info.cc:50