From c9f265e9f5bb299b59f789dfd1a1c619e7188a17 Mon Sep 17 00:00:00 2001 From: Udai Bhan Kashyap Date: Tue, 10 Sep 2019 15:14:12 -0400 Subject: [PATCH] HBASE-22969 A new binary component comparator(BinaryComponentComparator) to perform comparison of arbitrary length and position --- .../filter/BinaryComponentComparator.java | 132 ++++++++++++++++++ .../hadoop/hbase/filter/TestComparators.java | 39 ++++++ .../src/main/protobuf/Comparator.proto | 5 + .../src/main/protobuf/Comparator.proto | 5 + 4 files changed, 181 insertions(+) create mode 100644 hbase-client/src/main/java/org/apache/hadoop/hbase/filter/BinaryComponentComparator.java diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/filter/BinaryComponentComparator.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/filter/BinaryComponentComparator.java new file mode 100644 index 0000000000..07c8a29cf5 --- /dev/null +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/filter/BinaryComponentComparator.java @@ -0,0 +1,132 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.filter; + +import com.google.protobuf.ByteString; +import com.google.protobuf.InvalidProtocolBufferException; +import org.apache.hadoop.hbase.exceptions.DeserializationException; +import org.apache.hadoop.hbase.protobuf.generated.ComparatorProtos; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.yetus.audience.InterfaceAudience; + +/** + * A comparator which compares against a specified byte array, but only + * compares specific portion of the byte array. For the rest it is similar to + * {@link BinaryComparator}. + */ +@InterfaceAudience.Public +@SuppressWarnings("ComparableType") +public class BinaryComponentComparator extends ByteArrayComparable { + /** + * offset of component from beginning. + */ + private int offset; + + /** + * Constructor + * + * @param value value of the component + * @param offset offset of the component from begining + */ + public BinaryComponentComparator(byte[] value, int offset) { + super(value); + this.offset = offset; + } + + @Override + public int compareTo(byte[] value) { + return compareTo(value, 0, value.length); + } + + @Override + public int compareTo(byte[] value, int offset, int length) { + + return Bytes.compareTo(this.value, 0, this.value.length, value, offset + this.offset, + this.value.length); + } + + @Override + public boolean equals(Object other) { + + if(other == this){ + return true; + } + if(!(other instanceof BinaryComponentComparator)){ + return false; + } + BinaryComponentComparator bcc = (BinaryComponentComparator)other; + return offset == bcc.offset && + (compareTo(bcc.value) == 0); + + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + offset; + return result; + } + + /** + * @return The comparator serialized using pb + */ + @Override + public byte[] toByteArray() { + ComparatorProtos.BinaryComponentComparator.Builder builder = + ComparatorProtos.BinaryComponentComparator.newBuilder(); + builder.setValue(ByteString.copyFrom(this.value)); + builder.setOffset(this.offset); + return builder.build().toByteArray(); + } + + /** + * @param pbBytes A pb serialized {@link BinaryComponentComparator} instance + * @return An instance of {@link BinaryComponentComparator} made from bytes + * @throws DeserializationException DeserializationException + * @see #toByteArray + */ + public static BinaryComponentComparator parseFrom(final byte[] pbBytes) + throws DeserializationException { + ComparatorProtos.BinaryComponentComparator proto; + try { + proto = ComparatorProtos.BinaryComponentComparator.parseFrom(pbBytes); + } catch (InvalidProtocolBufferException e) { + throw new DeserializationException(e); + } + return new BinaryComponentComparator(proto.getValue().toByteArray(), proto.getOffset()); + } + + /** + * @param other paramemter to compare against + * @return true if and only if the fields of the comparator that are + * serialized are equal to the corresponding fields in other. Used for testing. + */ + @Override + boolean areSerializedFieldsEqual(ByteArrayComparable other) { + if (other == this){ + return true; + } + if (!(other instanceof BinaryComponentComparator)){ + return false; + } + + return super.areSerializedFieldsEqual(other); + } +} diff --git a/hbase-client/src/test/java/org/apache/hadoop/hbase/filter/TestComparators.java b/hbase-client/src/test/java/org/apache/hadoop/hbase/filter/TestComparators.java index 7a4f97029e..15d13b156b 100644 --- a/hbase-client/src/test/java/org/apache/hadoop/hbase/filter/TestComparators.java +++ b/hbase-client/src/test/java/org/apache/hadoop/hbase/filter/TestComparators.java @@ -97,5 +97,44 @@ public class TestComparators { comparable = new SubstringComparator("cf"); assertEquals(0, PrivateCellUtil.compareFamily(bbCell, comparable)); assertEquals(0, PrivateCellUtil.compareFamily(kv, comparable)); + + //Binary component comparisons + byte[] val = Bytes.toBytes("abcd"); + kv = new KeyValue(r0, f, q1, val); + buffer = ByteBuffer.wrap(kv.getBuffer()); + bbCell = new ByteBufferKeyValue(buffer, 0, buffer.remaining()); + + //equality check + byte[] component = Bytes.toBytes("o"); + comparable = new BinaryComponentComparator(component,1); + //key comparison + assertEquals(0, PrivateCellUtil.compareRow(bbCell, comparable)); + assertEquals(0, PrivateCellUtil.compareRow(kv, comparable)); + component = Bytes.toBytes("c"); + comparable = new BinaryComponentComparator(component,2); + //value comparison + assertEquals(0,PrivateCellUtil.compareValue(bbCell, comparable)); + assertEquals(0,PrivateCellUtil.compareValue(kv, comparable)); + + //greater than + component = Bytes.toBytes("z"); + comparable = new BinaryComponentComparator(component,1); + //key comparison + assertTrue(PrivateCellUtil.compareRow(bbCell, comparable)>0); + assertTrue(PrivateCellUtil.compareRow(kv, comparable)>0); + //value comparison + assertTrue(PrivateCellUtil.compareValue(bbCell, comparable)>0); + assertTrue(PrivateCellUtil.compareValue(kv, comparable)>0); + + //less than + component = Bytes.toBytes("a"); + comparable = new BinaryComponentComparator(component,1); + //key comparison + assertTrue(PrivateCellUtil.compareRow(bbCell, comparable) < 0); + assertTrue(PrivateCellUtil.compareRow(kv, comparable) < 0); + comparable = new BinaryComponentComparator(component,2); + //value comparison + assertTrue(PrivateCellUtil.compareValue(bbCell, comparable) < 0); + assertTrue(PrivateCellUtil.compareValue(kv, comparable) < 0); } } diff --git a/hbase-protocol-shaded/src/main/protobuf/Comparator.proto b/hbase-protocol-shaded/src/main/protobuf/Comparator.proto index 55253aae5f..6a087d3fa6 100644 --- a/hbase-protocol-shaded/src/main/protobuf/Comparator.proto +++ b/hbase-protocol-shaded/src/main/protobuf/Comparator.proto @@ -77,3 +77,8 @@ message SubstringComparator { message BigDecimalComparator { required ByteArrayComparable comparable = 1; } + +message BinaryComponentComparator { + required bytes value = 1; + required uint32 offset = 2; +} diff --git a/hbase-protocol/src/main/protobuf/Comparator.proto b/hbase-protocol/src/main/protobuf/Comparator.proto index 878a179ef3..802021f7cc 100644 --- a/hbase-protocol/src/main/protobuf/Comparator.proto +++ b/hbase-protocol/src/main/protobuf/Comparator.proto @@ -76,3 +76,8 @@ message SubstringComparator { message BigDecimalComparator { required ByteArrayComparable comparable = 1; } + +message BinaryComponentComparator { + required bytes value = 1; + required uint32 offset = 2; +} -- 2.22.1