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 org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.classification.InterfaceStability;
24  import org.apache.hadoop.hbase.exceptions.DeserializationException;
25  import org.apache.hadoop.hbase.filter.Filter;
26  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
27  import org.apache.hadoop.hbase.security.access.AccessControlConstants;
28  import org.apache.hadoop.hbase.security.access.Permission;
29  import org.apache.hadoop.hbase.security.visibility.Authorizations;
30  import org.apache.hadoop.hbase.security.visibility.VisibilityConstants;
31  import com.google.common.collect.ArrayListMultimap;
32  import com.google.common.collect.ListMultimap;
33  
34  @InterfaceAudience.Public
35  @InterfaceStability.Evolving
36  public abstract class Query extends OperationWithAttributes {
37    private static final String ISOLATION_LEVEL = "_isolationlevel_";
38    protected Filter filter = null;
39    protected int targetReplicaId = -1;
40    protected Consistency consistency = Consistency.STRONG;
41  
42    /**
43     * @return Filter
44     */
45    public Filter getFilter() {
46      return filter;
47    }
48  
49    /**
50     * Apply the specified server-side filter when performing the Query.
51     * Only {@link Filter#filterKeyValue(Cell)} is called AFTER all tests
52     * for ttl, column match, deletes and max versions have been run.
53     * @param filter filter to run on the server
54     * @return this for invocation chaining
55     */
56    public Query setFilter(Filter filter) {
57      this.filter = filter;
58      return this;
59    }
60  
61    /**
62     * Sets the authorizations to be used by this Query
63     * @param authorizations
64     */
65    public Query setAuthorizations(Authorizations authorizations) {
66      this.setAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY, ProtobufUtil
67          .toAuthorizations(authorizations).toByteArray());
68      return this;
69    }
70  
71    /**
72     * @return The authorizations this Query is associated with.
73     * @throws DeserializationException
74     */
75    public Authorizations getAuthorizations() throws DeserializationException {
76      byte[] authorizationsBytes = this.getAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY);
77      if (authorizationsBytes == null) return null;
78      return ProtobufUtil.toAuthorizations(authorizationsBytes);
79    }
80  
81    /**
82     * @return The serialized ACL for this operation, or null if none
83     */
84    public byte[] getACL() {
85      return getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL);
86    }
87  
88    /**
89     * @param user User short name
90     * @param perms Permissions for the user
91     */
92    public Query setACL(String user, Permission perms) {
93      setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
94        ProtobufUtil.toUsersAndPermissions(user, perms).toByteArray());
95      return this;
96    }
97  
98    /**
99     * @param perms A map of permissions for a user or users
100    */
101   public Query setACL(Map<String, Permission> perms) {
102     ListMultimap<String, Permission> permMap = ArrayListMultimap.create();
103     for (Map.Entry<String, Permission> entry : perms.entrySet()) {
104       permMap.put(entry.getKey(), entry.getValue());
105     }
106     setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
107       ProtobufUtil.toUsersAndPermissions(permMap).toByteArray());
108     return this;
109   }
110 
111   /**
112    * Returns the consistency level for this operation
113    * @return the consistency level
114    */
115   public Consistency getConsistency() {
116     return consistency;
117   }
118 
119   /**
120    * Sets the consistency level for this operation
121    * @param consistency the consistency level
122    */
123   public Query setConsistency(Consistency consistency) {
124     this.consistency = consistency;
125     return this;
126   }
127 
128   /**
129    * Specify region replica id where Query will fetch data from. Use this together with
130    * {@link #setConsistency(Consistency)} passing {@link Consistency#TIMELINE} to read data from
131    * a specific replicaId.
132    * <br><b> Expert: </b>This is an advanced API exposed. Only use it if you know what you are doing
133    * @param Id
134    */
135   public Query setReplicaId(int Id) {
136     this.targetReplicaId = Id;
137     return this;
138   }
139 
140   /**
141    * Returns region replica id where Query will fetch data from.
142    * @return region replica id or -1 if not set.
143    */
144   public int getReplicaId() {
145     return this.targetReplicaId;
146   }
147 
148   /**
149    * Set the isolation level for this query. If the
150    * isolation level is set to READ_UNCOMMITTED, then
151    * this query will return data from committed and
152    * uncommitted transactions. If the isolation level
153    * is set to READ_COMMITTED, then this query will return
154    * data from committed transactions only. If a isolation
155    * level is not explicitly set on a Query, then it
156    * is assumed to be READ_COMMITTED.
157    * @param level IsolationLevel for this query
158    */
159   public Query setIsolationLevel(IsolationLevel level) {
160     setAttribute(ISOLATION_LEVEL, level.toBytes());
161     return this;
162   }
163 
164   /**
165    * @return The isolation level of this query.
166    * If no isolation level was set for this query object,
167    * then it returns READ_COMMITTED.
168    * @return The IsolationLevel for this query
169    */
170   public IsolationLevel getIsolationLevel() {
171     byte[] attr = getAttribute(ISOLATION_LEVEL);
172     return attr == null ? IsolationLevel.READ_COMMITTED :
173                           IsolationLevel.fromBytes(attr);
174   }
175 }