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