Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CatalogDeltaLog.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.Map;
18 import java.util.SortedMap;
19 import java.util.TreeMap;
20 
21 import com.cloudera.impala.thrift.TCatalogObject;
22 import com.cloudera.impala.thrift.TTable;
23 import com.google.common.base.Preconditions;
24 
43 public class CatalogDeltaLog {
44  // Map of the catalog version an object was removed from the catalog
45  // to the catalog object, ordered by catalog version.
46  private SortedMap<Long, TCatalogObject> removedCatalogObjects_ =
47  new TreeMap<Long, TCatalogObject>();
48 
52  public synchronized void addRemovedObject(TCatalogObject catalogObject) {
53  Preconditions.checkNotNull(catalogObject);
54  removedCatalogObjects_.put(catalogObject.getCatalog_version(), catalogObject);
55  }
56 
63  public synchronized void garbageCollect(long currentCatalogVersion) {
64  // Nothing will be garbage collected so avoid creating a new object.
65  if (!removedCatalogObjects_.isEmpty() &&
66  removedCatalogObjects_.firstKey() < currentCatalogVersion) {
67  removedCatalogObjects_ = new TreeMap<Long, TCatalogObject>(
68  removedCatalogObjects_.tailMap(currentCatalogVersion));
69  }
70  }
71 
77  public synchronized boolean wasObjectRemovedAfter(TCatalogObject catalogObject) {
78  Preconditions.checkNotNull(catalogObject);
79  if (removedCatalogObjects_.isEmpty()) return false;
80 
81  // Get all the items that were removed after the catalog version of this object.
82  SortedMap<Long, TCatalogObject> candidateObjects =
83  removedCatalogObjects_.tailMap(catalogObject.getCatalog_version());
84  for (Map.Entry<Long, TCatalogObject> entry: candidateObjects.entrySet()) {
85  if (objectNamesMatch(catalogObject, entry.getValue())) return true;
86  }
87  return false;
88  }
89 
94  private boolean objectNamesMatch(TCatalogObject first, TCatalogObject second) {
95  if (first.getType() != second.getType()) return false;
96  switch (first.getType()) {
97  case DATABASE:
98  return first.getDb().getDb_name().equalsIgnoreCase(second.getDb().getDb_name());
99  case TABLE:
100  case VIEW:
101  TTable firstTbl = first.getTable();
102  return firstTbl.getDb_name().equalsIgnoreCase(second.getTable().getDb_name()) &&
103  firstTbl.getTbl_name().equalsIgnoreCase(second.getTable().getTbl_name());
104  case FUNCTION:
105  return first.getFn().getSignature().equals(second.getFn().getSignature()) &&
106  first.getFn().getName().equals(second.getFn().getName());
107  case ROLE:
108  return first.getRole().getRole_name().equalsIgnoreCase(
109  second.getRole().getRole_name());
110  case PRIVILEGE:
111  return first.getPrivilege().getPrivilege_name().equalsIgnoreCase(
112  second.getPrivilege().getPrivilege_name()) &&
113  first.getPrivilege().getRole_id() == second.getPrivilege().getRole_id();
114  default: return false;
115  }
116  }
117 }
SortedMap< Long, TCatalogObject > removedCatalogObjects_
synchronized boolean wasObjectRemovedAfter(TCatalogObject catalogObject)
synchronized void addRemovedObject(TCatalogObject catalogObject)
boolean objectNamesMatch(TCatalogObject first, TCatalogObject second)
synchronized void garbageCollect(long currentCatalogVersion)