1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
42 .newAtomicReference();
43 private final Map<String, ColumnAttrib> columnAttribByVariableNameMap = Maps.newHashMap();
44 private final Set<ColumnAttrib> columnAttribSet = Sets.newHashSet();
45
46 private ColumnAttrib keyAttrib = null;
47 private List<String> evalList = null;
48 private int expressionTreeCacheSize = 25;
49 private String mappingName = null;
50 private String tableName = null;
51
52
53
54 public Mapping() {
55 }
56
57 protected Mapping(final String mappingName, final String tableName) {
58 this.mappingName = mappingName;
59 this.tableName = tableName;
60 }
61
62 public abstract Collection<String> getMappingFamilyNames() throws HBqlException;
63
64 public Set<ColumnAttrib> getColumnAttribSet() {
65 return this.columnAttribSet;
66 }
67
68
69 private Map<String, ColumnAttrib> getColumnAttribByVariableNameMap() {
70 return this.columnAttribByVariableNameMap;
71 }
72
73 public boolean containsVariableName(final String varname) {
74 return this.getColumnAttribByVariableNameMap().containsKey(varname);
75 }
76
77 public ColumnAttrib getAttribByVariableName(final String name) {
78 return this.getColumnAttribByVariableNameMap().get(name);
79 }
80
81 public void resetDefaultValues() throws HBqlException {
82 for (final ColumnAttrib attrib : this.getColumnAttribSet())
83 attrib.resetDefaultValue();
84 }
85
86 protected void addAttribToVariableNameMap(final ColumnAttrib attrib,
87 final String... attribNames) throws HBqlException {
88
89 this.getColumnAttribSet().add(attrib);
90
91 for (final String attribName : attribNames) {
92 if (this.getColumnAttribByVariableNameMap().containsKey(attribName))
93 throw new HBqlException(attribName + " already declared");
94
95 this.getColumnAttribByVariableNameMap().put(attribName, attrib);
96 }
97 }
98
99 private AtomicReference<Map<String, ExpressionTree>> getAtomicEvalMap() {
100 return this.atomicEvalMap;
101 }
102
103 public Map<String, ExpressionTree> getEvalMap() {
104
105 if (this.getAtomicEvalMap().get() == null) {
106 synchronized (this) {
107 if (this.getAtomicEvalMap().get() == null) {
108 final Map<String, ExpressionTree> val = Maps.newHashMap();
109 this.getAtomicEvalMap().set(val);
110 this.evalList = Lists.newArrayList();
111 }
112 }
113 }
114 return this.getAtomicEvalMap().get();
115 }
116
117 public String getMappingName() {
118 return this.mappingName;
119 }
120
121 private List<String> getEvalList() {
122 return this.evalList;
123 }
124
125 public String toString() {
126 return this.getMappingName();
127 }
128
129 public int getEvalCacheSize() {
130 return this.expressionTreeCacheSize;
131 }
132
133 public ColumnAttrib getKeyAttrib() {
134 return this.keyAttrib;
135 }
136
137 protected void setKeyAttrib(final ColumnAttrib keyAttrib) {
138 this.keyAttrib = keyAttrib;
139 }
140
141 public String getTableName() {
142 return this.tableName;
143 }
144
145 public void setEvalCacheSize(final int size) {
146
147 if (size > 0) {
148 this.expressionTreeCacheSize = size;
149
150
151 final Map<String, ExpressionTree> map = this.getEvalMap();
152 final List<String> list = this.getEvalList();
153 map.clear();
154 list.clear();
155 }
156 }
157
158 public synchronized void addToExpressionTreeCache(final String exprStr, final ExpressionTree expressionTree) {
159
160 final Map<String, ExpressionTree> map = this.getEvalMap();
161
162 if (!map.containsKey(exprStr)) {
163
164 final List<String> list = this.getEvalList();
165
166 list.add(exprStr);
167 map.put(exprStr, expressionTree);
168
169 if (list.size() > this.getEvalCacheSize()) {
170 final String firstOne = list.get(0);
171 map.remove(firstOne);
172 list.remove(0);
173 }
174 }
175 }
176 }