View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.filter;
21  
22  import com.google.protobuf.InvalidProtocolBufferException;
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.exceptions.DeserializationException;
26  import org.apache.hadoop.hbase.protobuf.generated.ComparatorProtos;
27  
28  /**
29   * A bit comparator which performs the specified bitwise operation on each of the bytes
30   * with the specified byte array. Then returns whether the result is non-zero.
31   */
32  @InterfaceAudience.Public
33  @InterfaceStability.Stable
34  public class BitComparator extends ByteArrayComparable {
35  
36    /** Bit operators. */
37    public enum BitwiseOp {
38      /** and */
39      AND,
40      /** or */
41      OR,
42      /** xor */
43      XOR
44    }
45    protected BitwiseOp bitOperator;
46  
47    /**
48     * Constructor
49     * @param value value
50     * @param bitOperator operator to use on the bit comparison
51     */
52    public BitComparator(byte[] value, BitwiseOp bitOperator) {
53      super(value);
54      this.bitOperator = bitOperator;
55    }
56  
57    /**
58     * @return the bitwise operator
59     */
60    public BitwiseOp getOperator() {
61      return bitOperator;
62    }
63  
64    /**
65     * @return The comparator serialized using pb
66     */
67    public byte [] toByteArray() {
68      ComparatorProtos.BitComparator.Builder builder =
69        ComparatorProtos.BitComparator.newBuilder();
70      builder.setComparable(super.convert());
71      ComparatorProtos.BitComparator.BitwiseOp bitwiseOpPb =
72        ComparatorProtos.BitComparator.BitwiseOp.valueOf(bitOperator.name());
73      builder.setBitwiseOp(bitwiseOpPb);
74      return builder.build().toByteArray();
75    }
76  
77    /**
78     * @param pbBytes A pb serialized {@link BitComparator} instance
79     * @return An instance of {@link BitComparator} made from <code>bytes</code>
80     * @throws DeserializationException
81     * @see #toByteArray
82     */
83    public static BitComparator parseFrom(final byte [] pbBytes)
84    throws DeserializationException {
85      ComparatorProtos.BitComparator proto;
86      try {
87        proto = ComparatorProtos.BitComparator.parseFrom(pbBytes);
88      } catch (InvalidProtocolBufferException e) {
89        throw new DeserializationException(e);
90      }
91      BitwiseOp bitwiseOp = BitwiseOp.valueOf(proto.getBitwiseOp().name());
92      return new BitComparator(proto.getComparable().getValue().toByteArray(),bitwiseOp);
93    }
94  
95    /**
96     * @param other
97     * @return true if and only if the fields of the comparator that are serialized
98     * are equal to the corresponding fields in other.  Used for testing.
99     */
100   boolean areSerializedFieldsEqual(ByteArrayComparable other) {
101     if (other == this) return true;
102     if (!(other instanceof BitComparator)) return false;
103 
104     BitComparator comparator = (BitComparator)other;
105     return super.areSerializedFieldsEqual(other)
106       && this.getOperator().equals(comparator.getOperator());
107   }
108 
109   @Override
110   public int compareTo(byte[] value, int offset, int length) {
111     if (length != this.value.length) {
112       return 1;
113     }
114     int b = 0;
115     //Iterating backwards is faster because we can quit after one non-zero byte.
116     for (int i = length - 1; i >= 0 && b == 0; i--) {
117       switch (bitOperator) {
118         case AND:
119           b = (this.value[i] & value[i+offset]) & 0xff;
120           break;
121         case OR:
122           b = (this.value[i] | value[i+offset]) & 0xff;
123           break;
124         case XOR:
125           b = (this.value[i] ^ value[i+offset]) & 0xff;
126           break;
127       }
128     }
129     return b == 0 ? 1 : 0;
130   }
131 }
132