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 java.io.IOException;
021import java.util.ArrayList;
022import java.util.Comparator;
023import java.util.Objects;
024import java.util.TreeSet;
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;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
035import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations;
036
037import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos;
038
039/**
040 * This filter is used for selecting only those keys with columns that matches a particular prefix.
041 * For example, if prefix is 'an', it will pass keys will columns like 'and', 'anti' but not keys
042 * with columns like 'ball', 'act'.
043 */
044@InterfaceAudience.Public
045public class MultipleColumnPrefixFilter extends FilterBase {
046  private static final Logger LOG = LoggerFactory.getLogger(MultipleColumnPrefixFilter.class);
047  protected byte[] hint = null;
048  protected TreeSet<byte[]> sortedPrefixes = createTreeSet();
049  private final static int MAX_LOG_PREFIXES = 5;
050
051  public MultipleColumnPrefixFilter(final byte[][] prefixes) {
052    if (prefixes != null) {
053      for (byte[] prefix : prefixes) {
054        if (!sortedPrefixes.add(prefix)) {
055          LOG.error("prefix {} is repeated", Bytes.toString(prefix));
056          throw new IllegalArgumentException("prefixes must be distinct");
057        }
058      }
059    }
060  }
061
062  public byte[][] getPrefix() {
063    int count = 0;
064    byte[][] temp = new byte[sortedPrefixes.size()][];
065    for (byte[] prefixes : sortedPrefixes) {
066      temp[count++] = prefixes;
067    }
068    return temp;
069  }
070
071  @Override
072  public boolean filterRowKey(Cell cell) throws IOException {
073    // Impl in FilterBase might do unnecessary copy for Off heap backed Cells.
074    return false;
075  }
076
077  @Override
078  public ReturnCode filterCell(final Cell c) {
079    if (sortedPrefixes.isEmpty()) {
080      return ReturnCode.INCLUDE;
081    } else {
082      return filterColumn(c);
083    }
084  }
085
086  public ReturnCode filterColumn(Cell cell) {
087    byte[] qualifier = CellUtil.cloneQualifier(cell);
088    TreeSet<byte[]> lesserOrEqualPrefixes =
089      (TreeSet<byte[]>) sortedPrefixes.headSet(qualifier, true);
090
091    if (lesserOrEqualPrefixes.size() != 0) {
092      byte[] largestPrefixSmallerThanQualifier = lesserOrEqualPrefixes.last();
093
094      if (Bytes.startsWith(qualifier, largestPrefixSmallerThanQualifier)) {
095        return ReturnCode.INCLUDE;
096      }
097
098      if (lesserOrEqualPrefixes.size() == sortedPrefixes.size()) {
099        return ReturnCode.NEXT_ROW;
100      } else {
101        hint = sortedPrefixes.higher(largestPrefixSmallerThanQualifier);
102        return ReturnCode.SEEK_NEXT_USING_HINT;
103      }
104    } else {
105      hint = sortedPrefixes.first();
106      return ReturnCode.SEEK_NEXT_USING_HINT;
107    }
108  }
109
110  public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) {
111    byte[][] prefixes = new byte[filterArguments.size()][];
112    for (int i = 0; i < filterArguments.size(); i++) {
113      byte[] columnPrefix = ParseFilter.removeQuotesFromByteArray(filterArguments.get(i));
114      prefixes[i] = columnPrefix;
115    }
116    return new MultipleColumnPrefixFilter(prefixes);
117  }
118
119  /** Returns The filter serialized using pb */
120  @Override
121  public byte[] toByteArray() {
122    FilterProtos.MultipleColumnPrefixFilter.Builder builder =
123      FilterProtos.MultipleColumnPrefixFilter.newBuilder();
124    for (byte[] element : sortedPrefixes) {
125      if (element != null) builder.addSortedPrefixes(UnsafeByteOperations.unsafeWrap(element));
126    }
127    return builder.build().toByteArray();
128  }
129
130  /**
131   * Parse a serialized representation of {@link MultipleColumnPrefixFilter}
132   * @param pbBytes A pb serialized {@link MultipleColumnPrefixFilter} instance
133   * @return An instance of {@link MultipleColumnPrefixFilter} made from <code>bytes</code>
134   * @throws DeserializationException if an error occurred
135   * @see #toByteArray
136   */
137  public static MultipleColumnPrefixFilter parseFrom(final byte[] pbBytes)
138    throws DeserializationException {
139    FilterProtos.MultipleColumnPrefixFilter proto;
140    try {
141      proto = FilterProtos.MultipleColumnPrefixFilter.parseFrom(pbBytes);
142    } catch (InvalidProtocolBufferException e) {
143      throw new DeserializationException(e);
144    }
145    int numPrefixes = proto.getSortedPrefixesCount();
146    byte[][] prefixes = new byte[numPrefixes][];
147    for (int i = 0; i < numPrefixes; ++i) {
148      prefixes[i] = proto.getSortedPrefixes(i).toByteArray();
149    }
150
151    return new MultipleColumnPrefixFilter(prefixes);
152  }
153
154  /**
155   * Returns true if and only if the fields of the filter that are serialized are equal to the
156   * corresponding fields in other. Used for testing.
157   */
158  @Override
159  boolean areSerializedFieldsEqual(Filter o) {
160    if (o == this) {
161      return true;
162    }
163    if (!(o instanceof MultipleColumnPrefixFilter)) {
164      return false;
165    }
166    MultipleColumnPrefixFilter other = (MultipleColumnPrefixFilter) o;
167    return this.sortedPrefixes.equals(other.sortedPrefixes);
168  }
169
170  @Override
171  public Cell getNextCellHint(Cell cell) {
172    return PrivateCellUtil.createFirstOnRowCol(cell, hint, 0, hint.length);
173  }
174
175  public TreeSet<byte[]> createTreeSet() {
176    return new TreeSet<>(new Comparator<Object>() {
177      @Override
178      public int compare(Object o1, Object o2) {
179        if (o1 == null || o2 == null) throw new IllegalArgumentException("prefixes can't be null");
180
181        byte[] b1 = (byte[]) o1;
182        byte[] b2 = (byte[]) o2;
183        return Bytes.compareTo(b1, 0, b1.length, b2, 0, b2.length);
184      }
185    });
186  }
187
188  @Override
189  public String toString() {
190    return toString(MAX_LOG_PREFIXES);
191  }
192
193  protected String toString(int maxPrefixes) {
194    StringBuilder prefixes = new StringBuilder();
195
196    int count = 0;
197    for (byte[] ba : this.sortedPrefixes) {
198      if (count >= maxPrefixes) {
199        break;
200      }
201      ++count;
202      prefixes.append(Bytes.toStringBinary(ba));
203      if (count < this.sortedPrefixes.size() && count < maxPrefixes) {
204        prefixes.append(", ");
205      }
206    }
207
208    return String.format("%s (%d/%d): [%s]", this.getClass().getSimpleName(), count,
209      this.sortedPrefixes.size(), prefixes.toString());
210  }
211
212  @Override
213  public boolean equals(Object obj) {
214    return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
215  }
216
217  @Override
218  public int hashCode() {
219    return Objects.hash(this.sortedPrefixes);
220  }
221}