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 */
018package org.apache.hadoop.hbase.filter;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.nio.ByteBuffer;
025import org.apache.hadoop.hbase.ByteBufferKeyValue;
026import org.apache.hadoop.hbase.Cell;
027import org.apache.hadoop.hbase.KeyValue;
028import org.apache.hadoop.hbase.PrivateCellUtil;
029import org.apache.hadoop.hbase.testclassification.MiscTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.junit.jupiter.api.Tag;
033import org.junit.jupiter.api.Test;
034
035@Tag(MiscTests.TAG)
036@Tag(SmallTests.TAG)
037public class TestComparators {
038
039  @Test
040  public void testCellFieldsCompare() throws Exception {
041    byte[] r0 = Bytes.toBytes("row0");
042    byte[] r1 = Bytes.toBytes("row1");
043    byte[] r2 = Bytes.toBytes("row2");
044    byte[] f = Bytes.toBytes("cf1");
045    byte[] q1 = Bytes.toBytes("qual1");
046    byte[] q2 = Bytes.toBytes("qual2");
047    byte[] q3 = Bytes.toBytes("r");
048    long l1 = 1234L;
049    byte[] v1 = Bytes.toBytes(l1);
050    long l2 = 2000L;
051    byte[] v2 = Bytes.toBytes(l2);
052    // Row compare
053    KeyValue kv = new KeyValue(r1, f, q1, v1);
054    ByteBuffer buffer = ByteBuffer.wrap(kv.getBuffer());
055    Cell bbCell = new ByteBufferKeyValue(buffer, 0, buffer.remaining());
056    ByteArrayComparable comparable = new BinaryComparator(r1);
057    assertEquals(0, PrivateCellUtil.compareRow(bbCell, comparable));
058    assertEquals(0, PrivateCellUtil.compareRow(kv, comparable));
059    kv = new KeyValue(r0, f, q1, v1);
060    buffer = ByteBuffer.wrap(kv.getBuffer());
061    bbCell = new ByteBufferKeyValue(buffer, 0, buffer.remaining());
062    assertTrue(PrivateCellUtil.compareRow(bbCell, comparable) > 0);
063    assertTrue(PrivateCellUtil.compareRow(kv, comparable) > 0);
064    kv = new KeyValue(r2, f, q1, v1);
065    buffer = ByteBuffer.wrap(kv.getBuffer());
066    bbCell = new ByteBufferKeyValue(buffer, 0, buffer.remaining());
067    assertTrue(PrivateCellUtil.compareRow(bbCell, comparable) < 0);
068    assertTrue(PrivateCellUtil.compareRow(kv, comparable) < 0);
069    // Qualifier compare
070    comparable = new BinaryPrefixComparator(Bytes.toBytes("qual"));
071    assertEquals(0, PrivateCellUtil.compareQualifier(bbCell, comparable));
072    assertEquals(0, PrivateCellUtil.compareQualifier(kv, comparable));
073    kv = new KeyValue(r2, f, q2, v1);
074    buffer = ByteBuffer.wrap(kv.getBuffer());
075    bbCell = new ByteBufferKeyValue(buffer, 0, buffer.remaining());
076    assertEquals(0, PrivateCellUtil.compareQualifier(bbCell, comparable));
077    assertEquals(0, PrivateCellUtil.compareQualifier(kv, comparable));
078    kv = new KeyValue(r2, f, q3, v1);
079    buffer = ByteBuffer.wrap(kv.getBuffer());
080    bbCell = new ByteBufferKeyValue(buffer, 0, buffer.remaining());
081    assertTrue(PrivateCellUtil.compareQualifier(bbCell, comparable) < 0);
082    assertTrue(PrivateCellUtil.compareQualifier(kv, comparable) < 0);
083    // Value compare
084    comparable = new LongComparator(l1);
085    assertEquals(0, PrivateCellUtil.compareValue(bbCell, comparable));
086    assertEquals(0, PrivateCellUtil.compareValue(kv, comparable));
087    kv = new KeyValue(r1, f, q1, v2);
088    buffer = ByteBuffer.wrap(kv.getBuffer());
089    bbCell = new ByteBufferKeyValue(buffer, 0, buffer.remaining());
090    assertTrue(PrivateCellUtil.compareValue(bbCell, comparable) < 0);
091    assertTrue(PrivateCellUtil.compareValue(kv, comparable) < 0);
092    // Family compare
093    comparable = new SubstringComparator("cf");
094    assertEquals(0, PrivateCellUtil.compareFamily(bbCell, comparable));
095    assertEquals(0, PrivateCellUtil.compareFamily(kv, comparable));
096    // Qualifier starts with
097    kv = new KeyValue(r1, f, q1, v2);
098    assertTrue(PrivateCellUtil.qualifierStartsWith(kv, Bytes.toBytes("q")));
099    assertTrue(PrivateCellUtil.qualifierStartsWith(kv, q1));
100    assertFalse(PrivateCellUtil.qualifierStartsWith(kv, q2));
101    assertFalse(PrivateCellUtil.qualifierStartsWith(kv, Bytes.toBytes("longerthanthequalifier")));
102
103    // Binary component comparisons
104    byte[] val = Bytes.toBytes("abcd");
105    kv = new KeyValue(r0, f, q1, val);
106    buffer = ByteBuffer.wrap(kv.getBuffer());
107    bbCell = new ByteBufferKeyValue(buffer, 0, buffer.remaining());
108
109    // equality check
110    // row comparison
111    // row is "row0"(set by variable r0)
112    // and we are checking for equality to 'o' at position 1
113    // 'r' is at position 0.
114    byte[] component = Bytes.toBytes("o");
115    comparable = new BinaryComponentComparator(component, 1);
116    assertEquals(0, PrivateCellUtil.compareRow(bbCell, comparable));
117    assertEquals(0, PrivateCellUtil.compareRow(kv, comparable));
118    // value comparison
119    // value is "abcd"(set by variable val).
120    // and we are checking for equality to 'c' at position 2.
121    // 'a' is at position 0.
122    component = Bytes.toBytes("c");
123    comparable = new BinaryComponentComparator(component, 2);
124    assertEquals(0, PrivateCellUtil.compareValue(bbCell, comparable));
125    assertEquals(0, PrivateCellUtil.compareValue(kv, comparable));
126
127    // greater than
128    component = Bytes.toBytes("z");
129    // checking for greater than at position 1.
130    // for both row("row0") and value("abcd")
131    // 'z' > 'r'
132    comparable = new BinaryComponentComparator(component, 1);
133    // row comparison
134    assertTrue(PrivateCellUtil.compareRow(bbCell, comparable) > 0);
135    assertTrue(PrivateCellUtil.compareRow(kv, comparable) > 0);
136    // value comparison
137    // 'z' > 'a'
138    assertTrue(PrivateCellUtil.compareValue(bbCell, comparable) > 0);
139    assertTrue(PrivateCellUtil.compareValue(kv, comparable) > 0);
140
141    // less than
142    component = Bytes.toBytes("a");
143    // checking for less than at position 1 for row ("row0")
144    comparable = new BinaryComponentComparator(component, 1);
145    // row comparison
146    // 'a' < 'r'
147    assertTrue(PrivateCellUtil.compareRow(bbCell, comparable) < 0);
148    assertTrue(PrivateCellUtil.compareRow(kv, comparable) < 0);
149    // value comparison
150    // checking for less than at position 2 for value("abcd")
151    // 'a' < 'c'
152    comparable = new BinaryComponentComparator(component, 2);
153    assertTrue(PrivateCellUtil.compareValue(bbCell, comparable) < 0);
154    assertTrue(PrivateCellUtil.compareValue(kv, comparable) < 0);
155  }
156
157}