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