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.filter;
019
020import static org.apache.hadoop.hbase.util.Bytes.len;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.Objects;
025import org.apache.hadoop.hbase.Cell;
026import org.apache.hadoop.hbase.CellUtil;
027import org.apache.hadoop.hbase.PrivateCellUtil;
028import org.apache.hadoop.hbase.exceptions.DeserializationException;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.apache.yetus.audience.InterfaceAudience;
031
032import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
033import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
034import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations;
035
036import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos;
037
038/**
039 * This filter is used for selecting only those keys with columns that are between minColumn to
040 * maxColumn. For example, if minColumn is 'an', and maxColumn is 'be', it will pass keys with
041 * columns like 'ana', 'bad', but not keys with columns like 'bed', 'eye' If minColumn is null,
042 * there is no lower bound. If maxColumn is null, there is no upper bound. minColumnInclusive and
043 * maxColumnInclusive specify if the ranges are inclusive or not.
044 */
045@InterfaceAudience.Public
046public class ColumnRangeFilter extends FilterBase {
047  protected byte[] minColumn = null;
048  protected boolean minColumnInclusive = true;
049  protected byte[] maxColumn = null;
050  protected boolean maxColumnInclusive = false;
051
052  /**
053   * Create a filter to select those keys with columns that are between minColumn and maxColumn.
054   * @param minColumn          minimum value for the column range. If if it's null, there is no
055   *                           lower bound.
056   * @param minColumnInclusive if true, include minColumn in the range.
057   * @param maxColumn          maximum value for the column range. If it's null,
058   * @param maxColumnInclusive if true, include maxColumn in the range. there is no upper bound.
059   */
060  public ColumnRangeFilter(final byte[] minColumn, boolean minColumnInclusive,
061    final byte[] maxColumn, boolean maxColumnInclusive) {
062    this.minColumn = minColumn;
063    this.minColumnInclusive = minColumnInclusive;
064    this.maxColumn = maxColumn;
065    this.maxColumnInclusive = maxColumnInclusive;
066  }
067
068  /** Returns if min column range is inclusive. */
069  public boolean isMinColumnInclusive() {
070    return minColumnInclusive;
071  }
072
073  /** Returns if max column range is inclusive. */
074  public boolean isMaxColumnInclusive() {
075    return maxColumnInclusive;
076  }
077
078  /** Returns the min column range for the filter */
079  public byte[] getMinColumn() {
080    return this.minColumn;
081  }
082
083  /** Returns true if min column is inclusive, false otherwise */
084  public boolean getMinColumnInclusive() {
085    return this.minColumnInclusive;
086  }
087
088  /** Returns the max column range for the filter */
089  public byte[] getMaxColumn() {
090    return this.maxColumn;
091  }
092
093  /** Returns true if max column is inclusive, false otherwise */
094  public boolean getMaxColumnInclusive() {
095    return this.maxColumnInclusive;
096  }
097
098  @Override
099  public boolean filterRowKey(Cell cell) throws IOException {
100    // Impl in FilterBase might do unnecessary copy for Off heap backed Cells.
101    return false;
102  }
103
104  @Override
105  @Deprecated
106  public ReturnCode filterKeyValue(final Cell c) {
107    return filterCell(c);
108  }
109
110  @Override
111  public ReturnCode filterCell(final Cell c) {
112    int cmpMin = 1;
113
114    if (this.minColumn != null) {
115      cmpMin = CellUtil.compareQualifiers(c, this.minColumn, 0, this.minColumn.length);
116    }
117
118    if (cmpMin < 0) {
119      return ReturnCode.SEEK_NEXT_USING_HINT;
120    }
121
122    if (!this.minColumnInclusive && cmpMin == 0) {
123      return ReturnCode.NEXT_COL;
124    }
125
126    if (this.maxColumn == null) {
127      return ReturnCode.INCLUDE;
128    }
129
130    int cmpMax = CellUtil.compareQualifiers(c, this.maxColumn, 0, this.maxColumn.length);
131
132    if ((this.maxColumnInclusive && cmpMax <= 0) || (!this.maxColumnInclusive && cmpMax < 0)) {
133      return ReturnCode.INCLUDE;
134    }
135
136    return ReturnCode.NEXT_ROW;
137  }
138
139  public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) {
140    Preconditions.checkArgument(filterArguments.size() == 4, "Expected 4 but got: %s",
141      filterArguments.size());
142    byte[] minColumn = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
143    boolean minColumnInclusive = ParseFilter.convertByteArrayToBoolean(filterArguments.get(1));
144    byte[] maxColumn = ParseFilter.removeQuotesFromByteArray(filterArguments.get(2));
145    boolean maxColumnInclusive = ParseFilter.convertByteArrayToBoolean(filterArguments.get(3));
146
147    if (minColumn.length == 0) minColumn = null;
148    if (maxColumn.length == 0) maxColumn = null;
149    return new ColumnRangeFilter(minColumn, minColumnInclusive, maxColumn, maxColumnInclusive);
150  }
151
152  /** Returns The filter serialized using pb */
153  @Override
154  public byte[] toByteArray() {
155    FilterProtos.ColumnRangeFilter.Builder builder = FilterProtos.ColumnRangeFilter.newBuilder();
156    if (this.minColumn != null)
157      builder.setMinColumn(UnsafeByteOperations.unsafeWrap(this.minColumn));
158    builder.setMinColumnInclusive(this.minColumnInclusive);
159    if (this.maxColumn != null)
160      builder.setMaxColumn(UnsafeByteOperations.unsafeWrap(this.maxColumn));
161    builder.setMaxColumnInclusive(this.maxColumnInclusive);
162    return builder.build().toByteArray();
163  }
164
165  /**
166   * Parse a serialized representation of {@link ColumnRangeFilter}
167   * @param pbBytes A pb serialized {@link ColumnRangeFilter} instance
168   * @return An instance of {@link ColumnRangeFilter} made from <code>bytes</code>
169   * @throws DeserializationException if an error occurred
170   * @see #toByteArray
171   */
172  public static ColumnRangeFilter parseFrom(final byte[] pbBytes) throws DeserializationException {
173    FilterProtos.ColumnRangeFilter proto;
174    try {
175      proto = FilterProtos.ColumnRangeFilter.parseFrom(pbBytes);
176    } catch (InvalidProtocolBufferException e) {
177      throw new DeserializationException(e);
178    }
179    return new ColumnRangeFilter(proto.hasMinColumn() ? proto.getMinColumn().toByteArray() : null,
180      proto.getMinColumnInclusive(),
181      proto.hasMaxColumn() ? proto.getMaxColumn().toByteArray() : null,
182      proto.getMaxColumnInclusive());
183  }
184
185  /**
186   * Returns true if and only if the fields of the filter that are serialized are equal to the
187   * corresponding fields in other. Used for testing.
188   */
189  @Override
190  boolean areSerializedFieldsEqual(Filter o) {
191    if (o == this) {
192      return true;
193    }
194    if (!(o instanceof ColumnRangeFilter)) {
195      return false;
196    }
197    ColumnRangeFilter other = (ColumnRangeFilter) o;
198    return Bytes.equals(this.getMinColumn(), other.getMinColumn())
199      && this.getMinColumnInclusive() == other.getMinColumnInclusive()
200      && Bytes.equals(this.getMaxColumn(), other.getMaxColumn())
201      && this.getMaxColumnInclusive() == other.getMaxColumnInclusive();
202  }
203
204  @Override
205  public Cell getNextCellHint(Cell cell) {
206    return PrivateCellUtil.createFirstOnRowCol(cell, this.minColumn, 0, len(this.minColumn));
207  }
208
209  @Override
210  public String toString() {
211    return this.getClass().getSimpleName() + " " + (this.minColumnInclusive ? "[" : "(")
212      + Bytes.toStringBinary(this.minColumn) + ", " + Bytes.toStringBinary(this.maxColumn)
213      + (this.maxColumnInclusive ? "]" : ")");
214  }
215
216  @Override
217  public boolean equals(Object obj) {
218    return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
219  }
220
221  @Override
222  public int hashCode() {
223    return Objects.hash(Bytes.hashCode(getMinColumn()), getMinColumnInclusive(),
224      Bytes.hashCode(getMaxColumn()), getMaxColumnInclusive());
225  }
226}