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;
025
026import org.apache.hadoop.hbase.Cell;
027import org.apache.hadoop.hbase.CellUtil;
028import org.apache.hadoop.hbase.PrivateCellUtil;
029import org.apache.yetus.audience.InterfaceAudience;
030import org.apache.hadoop.hbase.exceptions.DeserializationException;
031import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
032import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations;
033import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos;
034import org.apache.hadoop.hbase.util.Bytes;
035
036/**
037 * This filter is used for selecting only those keys with columns that matches
038 * a particular prefix. For example, if prefix is 'an', it will pass keys will
039 * columns like 'and', 'anti' but not keys with columns like 'ball', 'act'.
040 */
041@InterfaceAudience.Public
042public class MultipleColumnPrefixFilter extends FilterBase {
043  protected byte [] hint = null;
044  protected TreeSet<byte []> sortedPrefixes = createTreeSet();
045  private final static int MAX_LOG_PREFIXES = 5;
046
047  public MultipleColumnPrefixFilter(final byte [][] prefixes) {
048    if (prefixes != null) {
049      for (int i = 0; i < prefixes.length; i++) {
050        if (!sortedPrefixes.add(prefixes[i]))
051          throw new IllegalArgumentException ("prefixes must be distinct");
052      }
053    }
054  }
055
056  public byte [][] getPrefix() {
057    int count = 0;
058    byte [][] temp = new byte [sortedPrefixes.size()][];
059    for (byte [] prefixes : sortedPrefixes) {
060      temp [count++] = prefixes;
061    }
062    return temp;
063  }
064
065  @Override
066  public boolean filterRowKey(Cell cell) throws IOException {
067    // Impl in FilterBase might do unnecessary copy for Off heap backed Cells.
068    return false;
069  }
070
071  @Deprecated
072  @Override
073  public ReturnCode filterKeyValue(final Cell c) {
074    return filterCell(c);
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  /**
120   * @return The filter serialized using pb
121   */
122  @Override
123  public byte [] toByteArray() {
124    FilterProtos.MultipleColumnPrefixFilter.Builder builder =
125      FilterProtos.MultipleColumnPrefixFilter.newBuilder();
126    for (byte [] element : sortedPrefixes) {
127      if (element != null) builder.addSortedPrefixes(UnsafeByteOperations.unsafeWrap(element));
128    }
129    return builder.build().toByteArray();
130  }
131
132  /**
133   * @param pbBytes A pb serialized {@link MultipleColumnPrefixFilter} instance
134   * @return An instance of {@link MultipleColumnPrefixFilter} made from <code>bytes</code>
135   * @throws DeserializationException
136   * @see #toByteArray
137   */
138  public static MultipleColumnPrefixFilter parseFrom(final byte [] pbBytes)
139  throws DeserializationException {
140    FilterProtos.MultipleColumnPrefixFilter proto;
141    try {
142      proto = FilterProtos.MultipleColumnPrefixFilter.parseFrom(pbBytes);
143    } catch (InvalidProtocolBufferException e) {
144      throw new DeserializationException(e);
145    }
146    int numPrefixes = proto.getSortedPrefixesCount();
147    byte [][] prefixes = new byte[numPrefixes][];
148    for (int i = 0; i < numPrefixes; ++i) {
149      prefixes[i] = proto.getSortedPrefixes(i).toByteArray();
150    }
151
152    return new MultipleColumnPrefixFilter(prefixes);
153  }
154
155  /**
156   * @param o the other filter to compare with
157   * @return true if and only if the fields of the filter that are serialized
158   * are equal to the corresponding fields in other.  Used for testing.
159   */
160  @Override
161  boolean areSerializedFieldsEqual(Filter o) {
162    if (o == this) return true;
163    if (!(o instanceof MultipleColumnPrefixFilter)) return false;
164
165    MultipleColumnPrefixFilter other = (MultipleColumnPrefixFilter)o;
166    return this.sortedPrefixes.equals(other.sortedPrefixes);
167  }
168
169  @Override
170  public Cell getNextCellHint(Cell cell) {
171    return PrivateCellUtil.createFirstOnRowCol(cell, hint, 0, hint.length);
172  }
173
174  public TreeSet<byte []> createTreeSet() {
175    return new TreeSet<>(new Comparator<Object>() {
176        @Override
177          public int compare (Object o1, Object o2) {
178          if (o1 == null || o2 == null)
179            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(),
209        count, 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}