View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import org.apache.hadoop.hbase.KeyValue.KVComparator;
24  import org.apache.hadoop.hbase.KeyValue.Type;
25  import org.apache.hadoop.hbase.testclassification.SmallTests;
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.junit.Test;
28  import org.junit.experimental.categories.Category;
29  @Category(SmallTests.class)
30  public class TestCellComparator {
31  
32    byte[] row1 = Bytes.toBytes("row1");
33    byte[] row2 = Bytes.toBytes("row2");
34    byte[] row_1_0 = Bytes.toBytes("row10");
35  
36    byte[] fam1 = Bytes.toBytes("fam1");
37    byte[] fam2 = Bytes.toBytes("fam2");
38    byte[] fam_1_2 = Bytes.toBytes("fam12");
39  
40    byte[] qual1 = Bytes.toBytes("qual1");
41    byte[] qual2 = Bytes.toBytes("qual2");
42  
43    byte[] val = Bytes.toBytes("val");
44  
45    @Test
46    public void testCompareCells() {
47      KeyValue kv1 = new KeyValue(row1, fam1, qual1, val);
48      KeyValue kv2 = new KeyValue(row2, fam1, qual1, val);
49      assertTrue((CellComparator.compare(kv1, kv2, false)) < 0);
50  
51      kv1 = new KeyValue(row1, fam2, qual1, val);
52      kv2 = new KeyValue(row1, fam1, qual1, val);
53      assertTrue((CellComparator.compareFamilies(kv1, kv2) > 0));
54  
55      kv1 = new KeyValue(row1, fam1, qual1, 1l, val);
56      kv2 = new KeyValue(row1, fam1, qual1, 2l, val);
57      assertTrue((CellComparator.compare(kv1, kv2, false) > 0));
58  
59      kv1 = new KeyValue(row1, fam1, qual1, 1l, Type.Put);
60      kv2 = new KeyValue(row1, fam1, qual1, 1l, Type.Maximum);
61      assertTrue((CellComparator.compare(kv1, kv2, false) > 0));
62  
63      kv1 = new KeyValue(row1, fam1, qual1, 1l, Type.Put);
64      kv2 = new KeyValue(row1, fam_1_2, qual1, 1l, Type.Maximum);
65      assertTrue((CellComparator.compareCommonFamilyPrefix(kv1, kv2, 4) < 0));
66  
67      kv1 = new KeyValue(row1, fam1, qual1, 1l, Type.Put);
68      kv2 = new KeyValue(row_1_0, fam_1_2, qual1, 1l, Type.Maximum);
69      assertTrue((CellComparator.compareCommonRowPrefix(kv1, kv2, 4) < 0));
70  
71      kv1 = new KeyValue(row1, fam1, qual2, 1l, Type.Put);
72      kv2 = new KeyValue(row1, fam1, qual1, 1l, Type.Maximum);
73      assertTrue((CellComparator.compareCommonQualifierPrefix(kv1, kv2, 4) > 0));
74  
75      kv1 = new KeyValue(row1, fam1, qual1, 1l, Type.Put);
76      kv2 = new KeyValue(row1, fam1, qual1, 1l, Type.Put);
77      assertTrue((CellComparator.equals(kv1, kv2)));
78    }
79  
80    @Test
81    public void testGetShortMidpoint() {
82      KeyValue.KVComparator comparator = new KeyValue.KVComparator();
83  
84      Cell left = CellUtil.createCell(Bytes.toBytes("a"), Bytes.toBytes("a"), Bytes.toBytes("a"));
85      Cell right = CellUtil.createCell(Bytes.toBytes("a"), Bytes.toBytes("a"), Bytes.toBytes("a"));
86      Cell mid = CellComparator.getMidpoint(comparator, left, right);
87      assertTrue(CellComparator.compare(left, mid, true) <= 0);
88      assertTrue(CellComparator.compare(mid, right, true) <= 0);
89  
90      left = CellUtil.createCell(Bytes.toBytes("a"), Bytes.toBytes("a"), Bytes.toBytes("a"));
91      right = CellUtil.createCell(Bytes.toBytes("b"), Bytes.toBytes("a"), Bytes.toBytes("a"));
92      mid = CellComparator.getMidpoint(comparator, left, right);
93      assertTrue(CellComparator.compare(left, mid, true) < 0);
94      assertTrue(CellComparator.compare(mid, right, true) <= 0);
95  
96      left = CellUtil.createCell(Bytes.toBytes("g"), Bytes.toBytes("a"), Bytes.toBytes("a"));
97      right = CellUtil.createCell(Bytes.toBytes("i"), Bytes.toBytes("a"), Bytes.toBytes("a"));
98      mid = CellComparator.getMidpoint(comparator, left, right);
99      assertTrue(CellComparator.compare(left, mid, true) < 0);
100     assertTrue(CellComparator.compare(mid, right, true) <= 0);
101 
102     left = CellUtil.createCell(Bytes.toBytes("a"), Bytes.toBytes("a"), Bytes.toBytes("a"));
103     right = CellUtil.createCell(Bytes.toBytes("bbbbbbb"), Bytes.toBytes("a"), Bytes.toBytes("a"));
104     mid = CellComparator.getMidpoint(comparator, left, right);
105     assertTrue(CellComparator.compare(left, mid, true) < 0);
106     assertTrue(CellComparator.compare(mid, right, true) < 0);
107     assertEquals(1, (int)mid.getRowLength());
108 
109     left = CellUtil.createCell(Bytes.toBytes("a"), Bytes.toBytes("a"), Bytes.toBytes("a"));
110     right = CellUtil.createCell(Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("a"));
111     mid = CellComparator.getMidpoint(comparator, left, right);
112     assertTrue(CellComparator.compare(left, mid, true) < 0);
113     assertTrue(CellComparator.compare(mid, right, true) <= 0);
114 
115     left = CellUtil.createCell(Bytes.toBytes("a"), Bytes.toBytes("a"), Bytes.toBytes("a"));
116     right = CellUtil.createCell(Bytes.toBytes("a"), Bytes.toBytes("aaaaaaaa"), Bytes.toBytes("b"));
117     mid = CellComparator.getMidpoint(comparator, left, right);
118     assertTrue(CellComparator.compare(left, mid, true) < 0);
119     assertTrue(CellComparator.compare(mid, right, true) < 0);
120     assertEquals(2, (int)mid.getFamilyLength());
121 
122     left = CellUtil.createCell(Bytes.toBytes("a"), Bytes.toBytes("a"), Bytes.toBytes("a"));
123     right = CellUtil.createCell(Bytes.toBytes("a"), Bytes.toBytes("a"), Bytes.toBytes("aaaaaaaaa"));
124     mid = CellComparator.getMidpoint(comparator, left, right);
125     assertTrue(CellComparator.compare(left, mid, true) < 0);
126     assertTrue(CellComparator.compare(mid, right, true) < 0);
127     assertEquals(2, (int)mid.getQualifierLength());
128 
129     left = CellUtil.createCell(Bytes.toBytes("a"), Bytes.toBytes("a"), Bytes.toBytes("a"));
130     right = CellUtil.createCell(Bytes.toBytes("a"), Bytes.toBytes("a"), Bytes.toBytes("b"));
131     mid = CellComparator.getMidpoint(comparator, left, right);
132     assertTrue(CellComparator.compare(left, mid, true) < 0);
133     assertTrue(CellComparator.compare(mid, right, true) <= 0);
134     assertEquals(1, (int)mid.getQualifierLength());
135 
136     // Assert that if meta comparator, it returns the right cell -- i.e.  no optimization done.
137     left = CellUtil.createCell(Bytes.toBytes("g"), Bytes.toBytes("a"), Bytes.toBytes("a"));
138     right = CellUtil.createCell(Bytes.toBytes("i"), Bytes.toBytes("a"), Bytes.toBytes("a"));
139     mid = CellComparator.getMidpoint(new KeyValue.MetaComparator(), left, right);
140     assertTrue(CellComparator.compare(left, mid, true) < 0);
141     assertTrue(CellComparator.compare(mid, right, true) == 0);
142   }
143 }