View Javadoc

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  
47    /**
48     * @return Filter
49     */
50    public Filter getFilter() {
51      return filter;
52    }
53  
54    /**
55     * Apply the specified server-side filter when performing the Query.
56     * Only {@link Filter#filterKeyValue(Cell)} is called AFTER all tests
57     * for ttl, column match, deletes and max versions have been run.
58     * @param filter filter to run on the server
59     * @return this for invocation chaining
60     */
61    public Query setFilter(Filter filter) {
62      this.filter = filter;
63      return this;
64    }
65  
66    /**
67     * Sets the authorizations to be used by this Query
68     * @param authorizations
69     */
70    public void setAuthorizations(Authorizations authorizations) {
71      this.setAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY, ProtobufUtil
72          .toAuthorizations(authorizations).toByteArray());
73    }
74  
75    /**
76     * @return The authorizations this Query is associated with.
77     * @throws DeserializationException
78     */
79    public Authorizations getAuthorizations() throws DeserializationException {
80      byte[] authorizationsBytes = this.getAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY);
81      if (authorizationsBytes == null) return null;
82      return ProtobufUtil.toAuthorizations(authorizationsBytes);
83    }
84  
85    /**
86     * @return The serialized ACL for this operation, or null if none
87     */
88    public byte[] getACL() {
89      return getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL);
90    }
91  
92    /**
93     * @param user User short name
94     * @param perms Permissions for the user
95     */
96    public void setACL(String user, Permission perms) {
97      setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
98        ProtobufUtil.toUsersAndPermissions(user, perms).toByteArray());
99    }
100 
101   /**
102    * @param perms A map of permissions for a user or users
103    */
104   public void setACL(Map<String, Permission> perms) {
105     ListMultimap<String, Permission> permMap = ArrayListMultimap.create();
106     for (Map.Entry<String, Permission> entry : perms.entrySet()) {
107       permMap.put(entry.getKey(), entry.getValue());
108     }
109     setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
110       ProtobufUtil.toUsersAndPermissions(permMap).toByteArray());
111   }
112 
113   /**
114    * Returns the consistency level for this operation
115    * @return the consistency level
116    */
117   public Consistency getConsistency() {
118     return consistency;
119   }
120 
121   /**
122    * Sets the consistency level for this operation
123    * @param consistency the consistency level
124    */
125   public Query setConsistency(Consistency consistency) {
126     this.consistency = consistency;
127     return this;
128   }
129 
130   /**
131    * Specify region replica id where Query will fetch data from. Use this together with
132    * {@link #setConsistency(Consistency)} passing {@link Consistency#TIMELINE} to read data from
133    * a specific replicaId.
134    * <br><b> Expert: </b>This is an advanced API exposed. Only use it if you know what you are doing
135    * @param Id
136    */
137   public Query setReplicaId(int Id) {
138     this.targetReplicaId = Id;
139     return this;
140   }
141 
142   /**
143    * Returns region replica id where Query will fetch data from.
144    * @return region replica id or -1 if not set.
145    */
146   public int getReplicaId() {
147     return this.targetReplicaId;
148   }
149 
150   /**
151    * Set the isolation level for this query. If the
152    * isolation level is set to READ_UNCOMMITTED, then
153    * this query will return data from committed and
154    * uncommitted transactions. If the isolation level
155    * is set to READ_COMMITTED, then this query will return
156    * data from committed transactions only. If a isolation
157    * level is not explicitly set on a Query, then it
158    * is assumed to be READ_COMMITTED.
159    * @param level IsolationLevel for this query
160    */
161   public Query setIsolationLevel(IsolationLevel level) {
162     setAttribute(ISOLATION_LEVEL, level.toBytes());
163     return this;
164   }
165 
166   /**
167    * @return The isolation level of this query.
168    * If no isolation level was set for this query object,
169    * then it returns READ_COMMITTED.
170    * @return The IsolationLevel for this query
171    */
172   public IsolationLevel getIsolationLevel() {
173     byte[] attr = getAttribute(ISOLATION_LEVEL);
174     return attr == null ? IsolationLevel.READ_COMMITTED :
175                           IsolationLevel.fromBytes(attr);
176   }
177 
178 
179   /**
180    * Get versions of columns only within the specified timestamp range,
181    * [minStamp, maxStamp) on a per CF bases.  Note, default maximum versions to return is 1.  If
182    * your time range spans more than one version and you want all versions
183    * returned, up the number of versions beyond the default.
184    * Column Family time ranges take precedence over the global time range.
185    *
186    * @param cf       the column family for which you want to restrict
187    * @param minStamp minimum timestamp value, inclusive
188    * @param maxStamp maximum timestamp value, exclusive
189    * @return this
190    */
191 
192   public Query setColumnFamilyTimeRange(byte[] cf, long minStamp, long maxStamp) {
193     try {
194       colFamTimeRangeMap.put(cf, new TimeRange(minStamp, maxStamp));
195       return this;
196     } catch (IOException ioe) {
197       throw new IllegalArgumentException(ioe);
198     }
199   }
200 
201   /**
202    * @return Map<byte[], TimeRange> a map of column families to time ranges
203    */
204   public Map<byte[], TimeRange> getColumnFamilyTimeRange() {
205     return this.colFamTimeRangeMap;
206   }
207 
208 
209 }