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.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.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    @InterfaceAudience.Public
38    @InterfaceStability.Stable
39    public enum BitwiseOp {
40      /** and */
41      AND,
42      /** or */
43      OR,
44      /** xor */
45      XOR
46    }
47    protected BitwiseOp bitOperator;
48  
49    /**
50     * Constructor
51     * @param value value
52     * @param bitOperator operator to use on the bit comparison
53     */
54    public BitComparator(byte[] value, BitwiseOp bitOperator) {
55      super(value);
56      this.bitOperator = bitOperator;
57    }
58  
59    /**
60     * @return the bitwise operator
61     */
62    public BitwiseOp getOperator() {
63      return bitOperator;
64    }
65  
66    /**
67     * @return The comparator serialized using pb
68     */
69    public byte [] toByteArray() {
70      ComparatorProtos.BitComparator.Builder builder =
71        ComparatorProtos.BitComparator.newBuilder();
72      builder.setComparable(super.convert());
73      ComparatorProtos.BitComparator.BitwiseOp bitwiseOpPb =
74        ComparatorProtos.BitComparator.BitwiseOp.valueOf(bitOperator.name());
75      builder.setBitwiseOp(bitwiseOpPb);
76      return builder.build().toByteArray();
77    }
78  
79    /**
80     * @param pbBytes A pb serialized {@link BitComparator} instance
81     * @return An instance of {@link BitComparator} made from <code>bytes</code>
82     * @throws DeserializationException
83     * @see #toByteArray
84     */
85    public static BitComparator parseFrom(final byte [] pbBytes)
86    throws DeserializationException {
87      ComparatorProtos.BitComparator proto;
88      try {
89        proto = ComparatorProtos.BitComparator.parseFrom(pbBytes);
90      } catch (InvalidProtocolBufferException e) {
91        throw new DeserializationException(e);
92      }
93      BitwiseOp bitwiseOp = BitwiseOp.valueOf(proto.getBitwiseOp().name());
94      return new BitComparator(proto.getComparable().getValue().toByteArray(),bitwiseOp);
95    }
96  
97    /**
98     * @param other
99     * @return true if and only if the fields of the comparator that are serialized
100    * are equal to the corresponding fields in other.  Used for testing.
101    */
102   boolean areSerializedFieldsEqual(ByteArrayComparable other) {
103     if (other == this) return true;
104     if (!(other instanceof BitComparator)) return false;
105 
106     BitComparator comparator = (BitComparator)other;
107     return super.areSerializedFieldsEqual(other)
108       && this.getOperator().equals(comparator.getOperator());
109   }
110 
111   @Override
112   public int compareTo(byte[] value, int offset, int length) {
113     if (length != this.value.length) {
114       return 1;
115     }
116     int b = 0;
117     //Iterating backwards is faster because we can quit after one non-zero byte.
118     for (int i = length - 1; i >= 0 && b == 0; i--) {
119       switch (bitOperator) {
120         case AND:
121           b = (this.value[i] & value[i+offset]) & 0xff;
122           break;
123         case OR:
124           b = (this.value[i] | value[i+offset]) & 0xff;
125           break;
126         case XOR:
127           b = (this.value[i] ^ value[i+offset]) & 0xff;
128           break;
129       }
130     }
131     return b == 0 ? 1 : 0;
132   }
133 }
134