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  
32  import com.google.common.collect.ArrayListMultimap;
33  import com.google.common.collect.ListMultimap;
34  
35  @InterfaceAudience.Public
36  @InterfaceStability.Evolving
37  public abstract class Query extends OperationWithAttributes {
38    private static final String ISOLATION_LEVEL = "_isolationlevel_";
39    protected Filter filter = null;
40  
41    /**
42     * @return Filter
43     */
44    public Filter getFilter() {
45      return filter;
46    }
47  
48    /**
49     * Apply the specified server-side filter when performing the Query.
50     * Only {@link Filter#filterKeyValue(Cell)} is called AFTER all tests
51     * for ttl, column match, deletes and max versions have been run.
52     * @param filter filter to run on the server
53     * @return this for invocation chaining
54     */
55    public Query setFilter(Filter filter) {
56      this.filter = filter;
57      return this;
58    }
59  
60    /**
61     * Sets the authorizations to be used by this Query
62     * @param authorizations
63     */
64    @InterfaceStability.Unstable
65    public void setAuthorizations(Authorizations authorizations) {
66      this.setAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY, ProtobufUtil
67          .toAuthorizations(authorizations).toByteArray());
68    }
69  
70    /**
71     * @return The authorizations this Query is associated with.
72     * @throws DeserializationException
73     */
74    @InterfaceStability.Unstable
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    @InterfaceStability.Unstable
85    public byte[] getACL() {
86      return getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL);
87    }
88  
89    /**
90     * @param user User short name
91     * @param perms Permissions for the user
92     */
93    @InterfaceStability.Unstable
94    public void setACL(String user, Permission perms) {
95      setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
96        ProtobufUtil.toUsersAndPermissions(user, perms).toByteArray());
97    }
98  
99    /**
100    * @param perms A map of permissions for a user or users
101    */
102   @InterfaceStability.Unstable
103   public void setACL(Map<String, Permission> perms) {
104     ListMultimap<String, Permission> permMap = ArrayListMultimap.create();
105     for (Map.Entry<String, Permission> entry : perms.entrySet()) {
106       permMap.put(entry.getKey(), entry.getValue());
107     }
108     setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
109       ProtobufUtil.toUsersAndPermissions(permMap).toByteArray());
110   }
111 
112   /**
113    * @deprecated No effect
114    */
115   @Deprecated
116   public boolean getACLStrategy() {
117     return false;
118   }
119 
120   /**
121    * @deprecated No effect
122    */
123   @Deprecated
124   public void setACLStrategy(boolean cellFirstStrategy) {
125   }
126 
127   /**
128    * Set the isolation level for this query. If the
129    * isolation level is set to READ_UNCOMMITTED, then
130    * this query will return data from committed and
131    * uncommitted transactions. If the isolation level
132    * is set to READ_COMMITTED, then this query will return
133    * data from committed transactions only. If a isolation
134    * level is not explicitly set on a Query, then it
135    * is assumed to be READ_COMMITTED.
136    * @param level IsolationLevel for this query
137    */
138   public void setIsolationLevel(IsolationLevel level) {
139     setAttribute(ISOLATION_LEVEL, level.toBytes());
140   }
141 
142   /**
143    * @return The isolation level of this scan.
144    * If no isolation level was set for this scan object,
145    * then it returns READ_COMMITTED.
146    * @return The IsolationLevel for this scan
147    */
148   public IsolationLevel getIsolationLevel() {
149     byte[] attr = getAttribute(ISOLATION_LEVEL);
150     return attr == null ? IsolationLevel.READ_COMMITTED :
151                           IsolationLevel.fromBytes(attr);
152   }
153 }