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 109}