1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
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 protected Boolean loadColumnFamiliesOnDemand = null;
47
48 /**
49 * @return Filter
50 */
51 public Filter getFilter() {
52 return filter;
53 }
54
55 /**
56 * Apply the specified server-side filter when performing the Query.
57 * Only {@link Filter#filterKeyValue(Cell)} is called AFTER all tests
58 * for ttl, column match, deletes and max versions have been run.
59 * @param filter filter to run on the server
60 * @return this for invocation chaining
61 */
62 public Query setFilter(Filter filter) {
63 this.filter = filter;
64 return this;
65 }
66
67 /**
68 * Sets the authorizations to be used by this Query
69 * @param authorizations
70 */
71 public Query setAuthorizations(Authorizations authorizations) {
72 this.setAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY, ProtobufUtil
73 .toAuthorizations(authorizations).toByteArray());
74 return this;
75 }
76
77 /**
78 * @return The authorizations this Query is associated with.
79 * @throws DeserializationException
80 */
81 public Authorizations getAuthorizations() throws DeserializationException {
82 byte[] authorizationsBytes = this.getAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY);
83 if (authorizationsBytes == null) return null;
84 return ProtobufUtil.toAuthorizations(authorizationsBytes);
85 }
86
87 /**
88 * @return The serialized ACL for this operation, or null if none
89 */
90 public byte[] getACL() {
91 return getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL);
92 }
93
94 /**
95 * @param user User short name
96 * @param perms Permissions for the user
97 */
98 public Query setACL(String user, Permission perms) {
99 setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
100 ProtobufUtil.toUsersAndPermissions(user, perms).toByteArray());
101 return this;
102 }
103
104 /**
105 * @param perms A map of permissions for a user or users
106 */
107 public Query setACL(Map<String, Permission> perms) {
108 ListMultimap<String, Permission> permMap = ArrayListMultimap.create();
109 for (Map.Entry<String, Permission> entry : perms.entrySet()) {
110 permMap.put(entry.getKey(), entry.getValue());
111 }
112 setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
113 ProtobufUtil.toUsersAndPermissions(permMap).toByteArray());
114 return this;
115 }
116
117 /**
118 * Returns the consistency level for this operation
119 * @return the consistency level
120 */
121 public Consistency getConsistency() {
122 return consistency;
123 }
124
125 /**
126 * Sets the consistency level for this operation
127 * @param consistency the consistency level
128 */
129 public Query setConsistency(Consistency consistency) {
130 this.consistency = consistency;
131 return this;
132 }
133
134 /**
135 * Specify region replica id where Query will fetch data from. Use this together with
136 * {@link #setConsistency(Consistency)} passing {@link Consistency#TIMELINE} to read data from
137 * a specific replicaId.
138 * <br><b> Expert: </b>This is an advanced API exposed. Only use it if you know what you are doing
139 * @param Id
140 */
141 public Query setReplicaId(int Id) {
142 this.targetReplicaId = Id;
143 return this;
144 }
145
146 /**
147 * Returns region replica id where Query will fetch data from.
148 * @return region replica id or -1 if not set.
149 */
150 public int getReplicaId() {
151 return this.targetReplicaId;
152 }
153
154 /**
155 * Set the isolation level for this query. If the
156 * isolation level is set to READ_UNCOMMITTED, then
157 * this query will return data from committed and
158 * uncommitted transactions. If the isolation level
159 * is set to READ_COMMITTED, then this query will return
160 * data from committed transactions only. If a isolation
161 * level is not explicitly set on a Query, then it
162 * is assumed to be READ_COMMITTED.
163 * @param level IsolationLevel for this query
164 */
165 public Query setIsolationLevel(IsolationLevel level) {
166 setAttribute(ISOLATION_LEVEL, level.toBytes());
167 return this;
168 }
169
170 /**
171 * @return The isolation level of this query.
172 * If no isolation level was set for this query object,
173 * then it returns READ_COMMITTED.
174 * @return The IsolationLevel for this query
175 */
176 public IsolationLevel getIsolationLevel() {
177 byte[] attr = getAttribute(ISOLATION_LEVEL);
178 return attr == null ? IsolationLevel.READ_COMMITTED :
179 IsolationLevel.fromBytes(attr);
180 }
181
182 /**
183 * Set the value indicating whether loading CFs on demand should be allowed (cluster
184 * default is false). On-demand CF loading doesn't load column families until necessary, e.g.
185 * if you filter on one column, the other column family data will be loaded only for the rows
186 * that are included in result, not all rows like in normal case.
187 * With column-specific filters, like SingleColumnValueFilter w/filterIfMissing == true,
188 * this can deliver huge perf gains when there's a cf with lots of data; however, it can
189 * also lead to some inconsistent results, as follows:
190 * - if someone does a concurrent update to both column families in question you may get a row
191 * that never existed, e.g. for { rowKey = 5, { cat_videos => 1 }, { video => "my cat" } }
192 * someone puts rowKey 5 with { cat_videos => 0 }, { video => "my dog" }, concurrent scan
193 * filtering on "cat_videos == 1" can get { rowKey = 5, { cat_videos => 1 },
194 * { video => "my dog" } }.
195 * - if there's a concurrent split and you have more than 2 column families, some rows may be
196 * missing some column families.
197 */
198 public Query setLoadColumnFamiliesOnDemand(boolean value) {
199 this.loadColumnFamiliesOnDemand = value;
200 return this;
201 }
202
203 /**
204 * Get the raw loadColumnFamiliesOnDemand setting; if it's not set, can be null.
205 */
206 public Boolean getLoadColumnFamiliesOnDemandValue() {
207 return this.loadColumnFamiliesOnDemand;
208 }
209
210 /**
211 * Get the logical value indicating whether on-demand CF loading should be allowed.
212 */
213 public boolean doLoadColumnFamiliesOnDemand() {
214 return (this.loadColumnFamiliesOnDemand != null) && this.loadColumnFamiliesOnDemand;
215 }
216
217 /**
218 * Get versions of columns only within the specified timestamp range,
219 * [minStamp, maxStamp) on a per CF bases. Note, default maximum versions to return is 1. If
220 * your time range spans more than one version and you want all versions
221 * returned, up the number of versions beyond the default.
222 * Column Family time ranges take precedence over the global time range.
223 *
224 * @param cf the column family for which you want to restrict
225 * @param minStamp minimum timestamp value, inclusive
226 * @param maxStamp maximum timestamp value, exclusive
227 * @return this
228 */
229
230 public Query setColumnFamilyTimeRange(byte[] cf, long minStamp, long maxStamp) {
231 try {
232 colFamTimeRangeMap.put(cf, new TimeRange(minStamp, maxStamp));
233 return this;
234 } catch (IOException ioe) {
235 throw new IllegalArgumentException(ioe);
236 }
237 }
238
239 /**
240 * @return Map<byte[], TimeRange> a map of column families to time ranges
241 */
242 public Map<byte[], TimeRange> getColumnFamilyTimeRange() {
243 return this.colFamTimeRangeMap;
244 }
245
246
247 }