001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import java.util.Map;
021
022import org.apache.hbase.thirdparty.com.google.common.collect.Maps;
023import org.apache.yetus.audience.InterfaceAudience;
024import org.apache.hadoop.hbase.exceptions.DeserializationException;
025import org.apache.hadoop.hbase.filter.Filter;
026import org.apache.hadoop.hbase.io.TimeRange;
027import org.apache.hadoop.hbase.security.access.AccessControlConstants;
028import org.apache.hadoop.hbase.security.access.AccessControlUtil;
029import org.apache.hadoop.hbase.security.access.Permission;
030import org.apache.hadoop.hbase.security.visibility.Authorizations;
031import org.apache.hadoop.hbase.security.visibility.VisibilityConstants;
032import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
033
034import org.apache.hbase.thirdparty.com.google.common.collect.ArrayListMultimap;
035import org.apache.hbase.thirdparty.com.google.common.collect.ListMultimap;
036import org.apache.hadoop.hbase.util.Bytes;
037
038/**
039 * Base class for HBase read operations; e.g. Scan and Get.
040 */
041@InterfaceAudience.Public
042public abstract class Query extends OperationWithAttributes {
043  private static final String ISOLATION_LEVEL = "_isolationlevel_";
044  protected Filter filter = null;
045  protected int targetReplicaId = -1;
046  protected Consistency consistency = Consistency.STRONG;
047  protected Map<byte[], TimeRange> colFamTimeRangeMap = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
048  protected Boolean loadColumnFamiliesOnDemand = null;
049  /**
050   * @return Filter
051   */
052  public Filter getFilter() {
053    return filter;
054  }
055
056  /**
057   * Apply the specified server-side filter when performing the Query. Only
058   * {@link Filter#filterCell(org.apache.hadoop.hbase.Cell)} is called AFTER all tests for ttl,
059   * column match, deletes and column family's max versions have been run.
060   * @param filter filter to run on the server
061   * @return this for invocation chaining
062   */
063  public Query setFilter(Filter filter) {
064    this.filter = filter;
065    return this;
066  }
067
068  /**
069   * Sets the authorizations to be used by this Query
070   * @param authorizations
071   */
072  public Query setAuthorizations(Authorizations authorizations) {
073    this.setAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY, ProtobufUtil
074        .toAuthorizations(authorizations).toByteArray());
075    return this;
076  }
077
078  /**
079   * @return The authorizations this Query is associated with.
080   * @throws DeserializationException
081   */
082  public Authorizations getAuthorizations() throws DeserializationException {
083    byte[] authorizationsBytes = this.getAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY);
084    if (authorizationsBytes == null) return null;
085    return ProtobufUtil.toAuthorizations(authorizationsBytes);
086  }
087
088  /**
089   * @return The serialized ACL for this operation, or null if none
090   */
091  public byte[] getACL() {
092    return getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL);
093  }
094
095  /**
096   * @param user User short name
097   * @param perms Permissions for the user
098   */
099  public Query setACL(String user, Permission perms) {
100    setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
101      AccessControlUtil.toUsersAndPermissions(user, perms).toByteArray());
102    return this;
103  }
104
105  /**
106   * @param perms A map of permissions for a user or users
107   */
108  public Query setACL(Map<String, Permission> perms) {
109    ListMultimap<String, Permission> permMap = ArrayListMultimap.create();
110    for (Map.Entry<String, Permission> entry : perms.entrySet()) {
111      permMap.put(entry.getKey(), entry.getValue());
112    }
113    setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL,
114        AccessControlUtil.toUsersAndPermissions(permMap).toByteArray());
115    return this;
116  }
117
118  /**
119   * Returns the consistency level for this operation
120   * @return the consistency level
121   */
122  public Consistency getConsistency() {
123    return consistency;
124  }
125
126  /**
127   * Sets the consistency level for this operation
128   * @param consistency the consistency level
129   */
130  public Query setConsistency(Consistency consistency) {
131    this.consistency = consistency;
132    return this;
133  }
134
135  /**
136   * Specify region replica id where Query will fetch data from. Use this together with
137   * {@link #setConsistency(Consistency)} passing {@link Consistency#TIMELINE} to read data from
138   * a specific replicaId.
139   * <br><b> Expert: </b>This is an advanced API exposed. Only use it if you know what you are doing
140   * @param Id
141   */
142  public Query setReplicaId(int Id) {
143    this.targetReplicaId = Id;
144    return this;
145  }
146
147  /**
148   * Returns region replica id where Query will fetch data from.
149   * @return region replica id or -1 if not set.
150   */
151  public int getReplicaId() {
152    return this.targetReplicaId;
153  }
154
155  /**
156   * Set the isolation level for this query. If the
157   * isolation level is set to READ_UNCOMMITTED, then
158   * this query will return data from committed and
159   * uncommitted transactions. If the isolation level
160   * is set to READ_COMMITTED, then this query will return
161   * data from committed transactions only. If a isolation
162   * level is not explicitly set on a Query, then it
163   * is assumed to be READ_COMMITTED.
164   * @param level IsolationLevel for this query
165   */
166  public Query setIsolationLevel(IsolationLevel level) {
167    setAttribute(ISOLATION_LEVEL, level.toBytes());
168    return this;
169  }
170
171  /**
172   * @return The isolation level of this query.
173   * If no isolation level was set for this query object,
174   * then it returns READ_COMMITTED.
175   * @return The IsolationLevel for this query
176   */
177  public IsolationLevel getIsolationLevel() {
178    byte[] attr = getAttribute(ISOLATION_LEVEL);
179    return attr == null ? IsolationLevel.READ_COMMITTED :
180                          IsolationLevel.fromBytes(attr);
181  }
182
183  /**
184   * Set the value indicating whether loading CFs on demand should be allowed (cluster
185   * default is false). On-demand CF loading doesn't load column families until necessary, e.g.
186   * if you filter on one column, the other column family data will be loaded only for the rows
187   * that are included in result, not all rows like in normal case.
188   * With column-specific filters, like SingleColumnValueFilter w/filterIfMissing == true,
189   * this can deliver huge perf gains when there's a cf with lots of data; however, it can
190   * also lead to some inconsistent results, as follows:
191   * - if someone does a concurrent update to both column families in question you may get a row
192   *   that never existed, e.g. for { rowKey = 5, { cat_videos =&gt; 1 }, { video =&gt; "my cat" } }
193   *   someone puts rowKey 5 with { cat_videos =&gt; 0 }, { video =&gt; "my dog" }, concurrent scan
194   *   filtering on "cat_videos == 1" can get { rowKey = 5, { cat_videos =&gt; 1 },
195   *   { video =&gt; "my dog" } }.
196   * - if there's a concurrent split and you have more than 2 column families, some rows may be
197   *   missing some column families.
198   */
199  public Query setLoadColumnFamiliesOnDemand(boolean value) {
200    this.loadColumnFamiliesOnDemand = value;
201    return this;
202  }
203
204  /**
205   * Get the raw loadColumnFamiliesOnDemand setting; if it's not set, can be null.
206   */
207  public Boolean getLoadColumnFamiliesOnDemandValue() {
208    return this.loadColumnFamiliesOnDemand;
209  }
210
211  /**
212   * Get the logical value indicating whether on-demand CF loading should be allowed.
213   */
214  public boolean doLoadColumnFamiliesOnDemand() {
215    return (this.loadColumnFamiliesOnDemand != null)
216      && this.loadColumnFamiliesOnDemand;
217  }
218
219  /**
220   * Get versions of columns only within the specified timestamp range,
221   * [minStamp, maxStamp) on a per CF bases.  Note, default maximum versions to return is 1.  If
222   * your time range spans more than one version and you want all versions
223   * returned, up the number of versions beyond the default.
224   * Column Family time ranges take precedence over the global time range.
225   *
226   * @param cf       the column family for which you want to restrict
227   * @param minStamp minimum timestamp value, inclusive
228   * @param maxStamp maximum timestamp value, exclusive
229   * @return this
230   */
231
232  public Query setColumnFamilyTimeRange(byte[] cf, long minStamp, long maxStamp) {
233    colFamTimeRangeMap.put(cf, new TimeRange(minStamp, maxStamp));
234    return this;
235  }
236
237  /**
238   * @return A map of column families to time ranges
239   */
240  public Map<byte[], TimeRange> getColumnFamilyTimeRange() {
241    return this.colFamTimeRangeMap;
242  }
243}