View Javadoc

1   /*
2    * Copyright (c) 2010.  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.mapping;
22  
23  import org.apache.expreval.expr.ExpressionTree;
24  import org.apache.hadoop.hbase.hbql.client.HBqlException;
25  import org.apache.hadoop.hbase.hbql.util.AtomicReferences;
26  import org.apache.hadoop.hbase.hbql.util.Lists;
27  import org.apache.hadoop.hbase.hbql.util.Maps;
28  import org.apache.hadoop.hbase.hbql.util.Sets;
29  
30  import java.io.Serializable;
31  import java.util.Collection;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.Set;
35  import java.util.concurrent.atomic.AtomicReference;
36  
37  public abstract class Mapping implements Serializable {
38  
39      private static final long serialVersionUID = 3L;
40  
41      private final transient AtomicReference<Map<String, ExpressionTree>> atomicEvalMap = AtomicReferences.newAtomicReference();
42      private final Map<String, ColumnAttrib> columnAttribByVariableNameMap = Maps.newHashMap();
43      private final Set<ColumnAttrib> columnAttribSet = Sets.newHashSet();
44  
45      private ColumnAttrib keyAttrib = null;
46      private List<String> evalList = null;
47      private int expressionTreeCacheSize = 25;
48      private String mappingName = null;
49      private String tableName = null;
50  
51  
52      // For serialization
53      public Mapping() {
54      }
55  
56      protected Mapping(final String mappingName, final String tableName) {
57          this.mappingName = mappingName;
58          this.tableName = tableName;
59      }
60  
61      public abstract Collection<String> getMappingFamilyNames() throws HBqlException;
62  
63      public Set<ColumnAttrib> getColumnAttribSet() {
64          return this.columnAttribSet;
65      }
66  
67      // *** columnAttribByVariableNameMap calls
68      private Map<String, ColumnAttrib> getColumnAttribByVariableNameMap() {
69          return this.columnAttribByVariableNameMap;
70      }
71  
72      public boolean containsVariableName(final String varname) {
73          return this.getColumnAttribByVariableNameMap().containsKey(varname);
74      }
75  
76      public ColumnAttrib getAttribByVariableName(final String name) {
77          return this.getColumnAttribByVariableNameMap().get(name);
78      }
79  
80      public void resetDefaultValues() throws HBqlException {
81          for (final ColumnAttrib attrib : this.getColumnAttribSet())
82              attrib.resetDefaultValue();
83      }
84  
85      protected void addAttribToVariableNameMap(final ColumnAttrib attrib,
86                                                final String... attribNames) throws HBqlException {
87  
88          this.getColumnAttribSet().add(attrib);
89  
90          for (final String attribName : attribNames) {
91              if (this.getColumnAttribByVariableNameMap().containsKey(attribName))
92                  throw new HBqlException(attribName + " already declared");
93  
94              this.getColumnAttribByVariableNameMap().put(attribName, attrib);
95          }
96      }
97  
98      private AtomicReference<Map<String, ExpressionTree>> getAtomicEvalMap() {
99          return this.atomicEvalMap;
100     }
101 
102     public Map<String, ExpressionTree> getEvalMap() {
103 
104         if (this.getAtomicEvalMap().get() == null) {
105             synchronized (this) {
106                 if (this.getAtomicEvalMap().get() == null) {
107                     final Map<String, ExpressionTree> val = Maps.newHashMap();
108                     this.getAtomicEvalMap().set(val);
109                     this.evalList = Lists.newArrayList();
110                 }
111             }
112         }
113         return this.getAtomicEvalMap().get();
114     }
115 
116     public String getMappingName() {
117         return this.mappingName;
118     }
119 
120     private List<String> getEvalList() {
121         return this.evalList;
122     }
123 
124     public String toString() {
125         return this.getMappingName();
126     }
127 
128     public int getEvalCacheSize() {
129         return this.expressionTreeCacheSize;
130     }
131 
132     public ColumnAttrib getKeyAttrib() {
133         return this.keyAttrib;
134     }
135 
136     protected void setKeyAttrib(final ColumnAttrib keyAttrib) {
137         this.keyAttrib = keyAttrib;
138     }
139 
140     public String getTableName() {
141         return this.tableName;
142     }
143 
144     public void setEvalCacheSize(final int size) {
145 
146         if (size > 0) {
147             this.expressionTreeCacheSize = size;
148 
149             // Reset existing cache
150             final Map<String, ExpressionTree> map = this.getEvalMap();
151             final List<String> list = this.getEvalList();
152             map.clear();
153             list.clear();
154         }
155     }
156 
157     public synchronized void addToExpressionTreeCache(final String exprStr, final ExpressionTree expressionTree) {
158 
159         final Map<String, ExpressionTree> map = this.getEvalMap();
160 
161         if (!map.containsKey(exprStr)) {
162 
163             final List<String> list = this.getEvalList();
164 
165             list.add(exprStr);
166             map.put(exprStr, expressionTree);
167 
168             if (list.size() > this.getEvalCacheSize()) {
169                 final String firstOne = list.get(0);
170                 map.remove(firstOne);
171                 list.remove(0);
172             }
173         }
174     }
175 }