1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.client;
19
20 import java.io.IOException;
21 import java.util.Map;
22
23 import com.google.common.collect.Maps;
24 import org.apache.hadoop.hbase.classification.InterfaceAudience;
25 import org.apache.hadoop.hbase.classification.InterfaceStability;
26 import org.apache.hadoop.hbase.exceptions.DeserializationException;
27 import org.apache.hadoop.hbase.filter.Filter;
28 import org.apache.hadoop.hbase.io.TimeRange;
29 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
30 import org.apache.hadoop.hbase.security.access.AccessControlConstants;
31 import org.apache.hadoop.hbase.security.access.Permission;
32 import org.apache.hadoop.hbase.security.visibility.Authorizations;
33 import org.apache.hadoop.hbase.security.visibility.VisibilityConstants;
34 import com.google.common.collect.ArrayListMultimap;
35 import com.google.common.collect.ListMultimap;
36 import org.apache.hadoop.hbase.util.Bytes;
37
38 @InterfaceAudience.Public
39 @InterfaceStability.Evolving
40 public abstract class Query extends OperationWithAttributes {
41 private static final String ISOLATION_LEVEL = "_isolationlevel_";
42 protected Filter filter = null;
43 protected int targetReplicaId = -1;
44 protected Consistency consistency = Consistency.STRONG;
45 protected Map<byte[], TimeRange> colFamTimeRangeMap = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
46
47
48
49
50 public Filter getFilter() {
51 return filter;
52 }
53
54
55
56
57
58
59
60
61 public Query setFilter(Filter filter) {
62 this.filter = filter;
63 return this;
64 }
65
66
67
68
69
70 public Query setAuthorizations(Authorizations authorizations) {
71 this.setAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY, ProtobufUtil
72 .toAuthorizations(authorizations).toByteArray());
73 return this;
74 }
75
76
77
78
79
80 public Authorizations getAuthorizations() throws DeserializationException {
81 byte[] authorizationsBytes = this.getAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY);
82 if (authorizationsBytes == null) return null;
83 return ProtobufUtil.toAuthorizations(authorizationsBytes);
84 }
85
86
87
88
89 public byte[] getACL() {
90 return getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL);
91 }
92
93
94
95
96
97 public Query setACL(String user, Permission perms) {
98 setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
99 ProtobufUtil.toUsersAndPermissions(user, perms).toByteArray());
100 return this;
101 }
102
103
104
105
106 public Query setACL(Map<String, Permission> perms) {
107 ListMultimap<String, Permission> permMap = ArrayListMultimap.create();
108 for (Map.Entry<String, Permission> entry : perms.entrySet()) {
109 permMap.put(entry.getKey(), entry.getValue());
110 }
111 setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
112 ProtobufUtil.toUsersAndPermissions(permMap).toByteArray());
113 return this;
114 }
115
116
117
118
119
120 public Consistency getConsistency() {
121 return consistency;
122 }
123
124
125
126
127
128 public Query setConsistency(Consistency consistency) {
129 this.consistency = consistency;
130 return this;
131 }
132
133
134
135
136
137
138
139
140 public Query setReplicaId(int Id) {
141 this.targetReplicaId = Id;
142 return this;
143 }
144
145
146
147
148
149 public int getReplicaId() {
150 return this.targetReplicaId;
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164 public Query setIsolationLevel(IsolationLevel level) {
165 setAttribute(ISOLATION_LEVEL, level.toBytes());
166 return this;
167 }
168
169
170
171
172
173
174
175 public IsolationLevel getIsolationLevel() {
176 byte[] attr = getAttribute(ISOLATION_LEVEL);
177 return attr == null ? IsolationLevel.READ_COMMITTED :
178 IsolationLevel.fromBytes(attr);
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public Query setColumnFamilyTimeRange(byte[] cf, long minStamp, long maxStamp) {
196 try {
197 colFamTimeRangeMap.put(cf, new TimeRange(minStamp, maxStamp));
198 return this;
199 } catch (IOException ioe) {
200 throw new IllegalArgumentException(ioe);
201 }
202 }
203
204
205
206
207 public Map<byte[], TimeRange> getColumnFamilyTimeRange() {
208 return this.colFamTimeRangeMap;
209 }
210
211
212 }