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.util.Map;
21  
22  import com.google.common.collect.Maps;
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.exceptions.DeserializationException;
26  import org.apache.hadoop.hbase.filter.Filter;
27  import org.apache.hadoop.hbase.io.TimeRange;
28  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
29  import org.apache.hadoop.hbase.security.access.AccessControlConstants;
30  import org.apache.hadoop.hbase.security.access.Permission;
31  import org.apache.hadoop.hbase.security.visibility.Authorizations;
32  import org.apache.hadoop.hbase.security.visibility.VisibilityConstants;
33  import com.google.common.collect.ArrayListMultimap;
34  import com.google.common.collect.ListMultimap;
35  import org.apache.hadoop.hbase.util.Bytes;
36  
37  @InterfaceAudience.Public
38  @InterfaceStability.Evolving
39  public abstract class Query extends OperationWithAttributes {
40    private static final String ISOLATION_LEVEL = "_isolationlevel_";
41    protected Filter filter = null;
42    protected int targetReplicaId = -1;
43    protected Consistency consistency = Consistency.STRONG;
44    protected Map<byte[], TimeRange> colFamTimeRangeMap = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
45  
46    /**
47     * @return Filter
48     */
49    public Filter getFilter() {
50      return filter;
51    }
52  
53    /**
54     * Apply the specified server-side filter when performing the Query.
55     * Only {@link Filter#filterKeyValue(Cell)} is called AFTER all tests
56     * for ttl, column match, deletes and max versions have been run.
57     * @param filter filter to run on the server
58     * @return this for invocation chaining
59     */
60    public Query setFilter(Filter filter) {
61      this.filter = filter;
62      return this;
63    }
64  
65    /**
66     * Sets the authorizations to be used by this Query
67     * @param authorizations
68     */
69    public Query setAuthorizations(Authorizations authorizations) {
70      this.setAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY, ProtobufUtil
71          .toAuthorizations(authorizations).toByteArray());
72      return this;
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 Query setACL(String user, Permission perms) {
97      setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
98        ProtobufUtil.toUsersAndPermissions(user, perms).toByteArray());
99      return this;
100   }
101 
102   /**
103    * @param perms A map of permissions for a user or users
104    */
105   public Query setACL(Map<String, Permission> perms) {
106     ListMultimap<String, Permission> permMap = ArrayListMultimap.create();
107     for (Map.Entry<String, Permission> entry : perms.entrySet()) {
108       permMap.put(entry.getKey(), entry.getValue());
109     }
110     setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
111       ProtobufUtil.toUsersAndPermissions(permMap).toByteArray());
112     return this;
113   }
114 
115   /**
116    * Returns the consistency level for this operation
117    * @return the consistency level
118    */
119   public Consistency getConsistency() {
120     return consistency;
121   }
122 
123   /**
124    * Sets the consistency level for this operation
125    * @param consistency the consistency level
126    */
127   public Query setConsistency(Consistency consistency) {
128     this.consistency = consistency;
129     return this;
130   }
131 
132   /**
133    * Specify region replica id where Query will fetch data from. Use this together with
134    * {@link #setConsistency(Consistency)} passing {@link Consistency#TIMELINE} to read data from
135    * a specific replicaId.
136    * <br><b> Expert: </b>This is an advanced API exposed. Only use it if you know what you are doing
137    * @param Id
138    */
139   public Query setReplicaId(int Id) {
140     this.targetReplicaId = Id;
141     return this;
142   }
143 
144   /**
145    * Returns region replica id where Query will fetch data from.
146    * @return region replica id or -1 if not set.
147    */
148   public int getReplicaId() {
149     return this.targetReplicaId;
150   }
151 
152   /**
153    * Set the isolation level for this query. If the
154    * isolation level is set to READ_UNCOMMITTED, then
155    * this query will return data from committed and
156    * uncommitted transactions. If the isolation level
157    * is set to READ_COMMITTED, then this query will return
158    * data from committed transactions only. If a isolation
159    * level is not explicitly set on a Query, then it
160    * is assumed to be READ_COMMITTED.
161    * @param level IsolationLevel for this query
162    */
163   public Query setIsolationLevel(IsolationLevel level) {
164     setAttribute(ISOLATION_LEVEL, level.toBytes());
165     return this;
166   }
167 
168   /**
169    * @return The isolation level of this query.
170    * If no isolation level was set for this query object,
171    * then it returns READ_COMMITTED.
172    * @return The IsolationLevel for this query
173    */
174   public IsolationLevel getIsolationLevel() {
175     byte[] attr = getAttribute(ISOLATION_LEVEL);
176     return attr == null ? IsolationLevel.READ_COMMITTED :
177                           IsolationLevel.fromBytes(attr);
178   }
179 
180 
181   /**
182    * Get versions of columns only within the specified timestamp range,
183    * [minStamp, maxStamp) on a per CF bases.  Note, default maximum versions to return is 1.  If
184    * your time range spans more than one version and you want all versions
185    * returned, up the number of versions beyond the default.
186    * Column Family time ranges take precedence over the global time range.
187    *
188    * @param cf       the column family for which you want to restrict
189    * @param minStamp minimum timestamp value, inclusive
190    * @param maxStamp maximum timestamp value, exclusive
191    * @return this
192    */
193 
194   public Query setColumnFamilyTimeRange(byte[] cf, long minStamp, long maxStamp) {
195     colFamTimeRangeMap.put(cf, new TimeRange(minStamp, maxStamp));
196     return this;
197   }
198 
199   /**
200    * @return Map<byte[], TimeRange> a map of column families to time ranges
201    */
202   public Map<byte[], TimeRange> getColumnFamilyTimeRange() {
203     return this.colFamTimeRangeMap;
204   }
205 }