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 */
018
019package org.apache.hadoop.hbase.filter;
020
021import java.util.Objects;
022import java.util.Set;
023import java.util.TreeSet;
024
025import org.apache.hadoop.hbase.Cell;
026import org.apache.hadoop.hbase.CellUtil;
027import org.apache.yetus.audience.InterfaceAudience;
028import org.apache.hadoop.hbase.exceptions.DeserializationException;
029import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos;
030import org.apache.hadoop.hbase.util.Bytes;
031
032import org.apache.hbase.thirdparty.com.google.protobuf.ByteString;
033import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
034import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations;
035
036/**
037 * The filter looks for the given columns in KeyValue. Once there is a match for
038 * any one of the columns, it returns ReturnCode.NEXT_ROW for remaining
039 * KeyValues in the row.
040 * <p>
041 * Note : It may emit KVs which do not have the given columns in them, if
042 * these KVs happen to occur before a KV which does have a match. Given this
043 * caveat, this filter is only useful for special cases
044 * like org.apache.hadoop.hbase.mapreduce.RowCounter.
045 * <p>
046 * @deprecated Deprecated in 2.0.0 and will be removed in 3.0.0.
047 * @see <a href="https://issues.apache.org/jira/browse/HBASE-13347">HBASE-13347</a>
048 */
049@InterfaceAudience.Public
050@Deprecated
051public class FirstKeyValueMatchingQualifiersFilter extends FirstKeyOnlyFilter {
052
053  private Set<byte []> qualifiers;
054
055  /**
056   * Constructor which takes a set of columns. As soon as first KeyValue
057   * matching any of these columns is found, filter moves to next row.
058   * 
059   * @param qualifiers the set of columns to me matched.
060   */
061  public FirstKeyValueMatchingQualifiersFilter(Set<byte []> qualifiers) {
062    this.qualifiers = qualifiers;
063  }
064
065  @Deprecated
066  @Override
067  public ReturnCode filterKeyValue(final Cell c) {
068    return filterCell(c);
069  }
070
071  @Override
072  public ReturnCode filterCell(final Cell c) {
073    if (hasFoundKV()) {
074      return ReturnCode.NEXT_ROW;
075    } else if (hasOneMatchingQualifier(c)) {
076      setFoundKV(true);
077    }
078    return ReturnCode.INCLUDE;
079  }
080
081  private boolean hasOneMatchingQualifier(Cell c) {
082    for (byte[] q : qualifiers) {
083      if (CellUtil.matchingQualifier(c, q)) {
084        return true;
085      }
086    }
087    return false;
088  }
089
090  /**
091   * @return The filter serialized using pb
092   */
093  @Override
094  public byte [] toByteArray() {
095    FilterProtos.FirstKeyValueMatchingQualifiersFilter.Builder builder =
096      FilterProtos.FirstKeyValueMatchingQualifiersFilter.newBuilder();
097    for (byte[] qualifier : qualifiers) {
098      if (qualifier != null) builder.addQualifiers(UnsafeByteOperations.unsafeWrap(qualifier));
099    }
100    return builder.build().toByteArray();
101  }
102
103  /**
104   * @param pbBytes A pb serialized {@link FirstKeyValueMatchingQualifiersFilter} instance
105   * @return An instance of {@link FirstKeyValueMatchingQualifiersFilter} made from <code>bytes</code>
106   * @throws DeserializationException
107   * @see #toByteArray
108   */
109  public static FirstKeyValueMatchingQualifiersFilter parseFrom(final byte [] pbBytes)
110  throws DeserializationException {
111    FilterProtos.FirstKeyValueMatchingQualifiersFilter proto;
112    try {
113      proto = FilterProtos.FirstKeyValueMatchingQualifiersFilter.parseFrom(pbBytes);
114    } catch (InvalidProtocolBufferException e) {
115      throw new DeserializationException(e);
116    }
117
118    TreeSet<byte []> qualifiers = new TreeSet<>(Bytes.BYTES_COMPARATOR);
119    for (ByteString qualifier : proto.getQualifiersList()) {
120      qualifiers.add(qualifier.toByteArray());
121    }
122    return new FirstKeyValueMatchingQualifiersFilter(qualifiers);
123  }
124
125  /**
126   * @param o the other filter to compare with
127   * @return true if and only if the fields of the filter that are serialized
128   * are equal to the corresponding fields in other.  Used for testing.
129   */
130  @Override
131  boolean areSerializedFieldsEqual(Filter o) {
132    if (o == this) return true;
133    if (!(o instanceof FirstKeyValueMatchingQualifiersFilter)) return false;
134
135    FirstKeyValueMatchingQualifiersFilter other = (FirstKeyValueMatchingQualifiersFilter)o;
136    return this.qualifiers.equals(other.qualifiers);
137  }
138
139  @Override
140  public boolean equals(Object obj) {
141    return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj);
142  }
143
144  @Override
145  public int hashCode() {
146    return Objects.hash(this.qualifiers);
147  }
148}