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.assertTrue; 022 023import java.nio.ByteBuffer; 024import org.apache.hadoop.hbase.ByteBufferKeyValue; 025import org.apache.hadoop.hbase.Cell; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 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.ClassRule; 033import org.junit.Test; 034import org.junit.experimental.categories.Category; 035 036@Category({MiscTests.class, SmallTests.class}) 037public class TestComparators { 038 039 @ClassRule 040 public static final HBaseClassTestRule CLASS_RULE = 041 HBaseClassTestRule.forClass(TestComparators.class); 042 043 @Test 044 public void testCellFieldsCompare() throws Exception { 045 byte[] r0 = Bytes.toBytes("row0"); 046 byte[] r1 = Bytes.toBytes("row1"); 047 byte[] r2 = Bytes.toBytes("row2"); 048 byte[] f = Bytes.toBytes("cf1"); 049 byte[] q1 = Bytes.toBytes("qual1"); 050 byte[] q2 = Bytes.toBytes("qual2"); 051 byte[] q3 = Bytes.toBytes("r"); 052 long l1 = 1234L; 053 byte[] v1 = Bytes.toBytes(l1); 054 long l2 = 2000L; 055 byte[] v2 = Bytes.toBytes(l2); 056 // Row compare 057 KeyValue kv = new KeyValue(r1, f, q1, v1); 058 ByteBuffer buffer = ByteBuffer.wrap(kv.getBuffer()); 059 Cell bbCell = new ByteBufferKeyValue(buffer, 0, buffer.remaining()); 060 ByteArrayComparable comparable = new BinaryComparator(r1); 061 assertEquals(0, PrivateCellUtil.compareRow(bbCell, comparable)); 062 assertEquals(0, PrivateCellUtil.compareRow(kv, comparable)); 063 kv = new KeyValue(r0, f, q1, v1); 064 buffer = ByteBuffer.wrap(kv.getBuffer()); 065 bbCell = new ByteBufferKeyValue(buffer, 0, buffer.remaining()); 066 assertTrue(PrivateCellUtil.compareRow(bbCell, comparable) > 0); 067 assertTrue(PrivateCellUtil.compareRow(kv, comparable) > 0); 068 kv = new KeyValue(r2, f, q1, v1); 069 buffer = ByteBuffer.wrap(kv.getBuffer()); 070 bbCell = new ByteBufferKeyValue(buffer, 0, buffer.remaining()); 071 assertTrue(PrivateCellUtil.compareRow(bbCell, comparable) < 0); 072 assertTrue(PrivateCellUtil.compareRow(kv, comparable) < 0); 073 // Qualifier compare 074 comparable = new BinaryPrefixComparator(Bytes.toBytes("qual")); 075 assertEquals(0, PrivateCellUtil.compareQualifier(bbCell, comparable)); 076 assertEquals(0, PrivateCellUtil.compareQualifier(kv, comparable)); 077 kv = new KeyValue(r2, f, q2, v1); 078 buffer = ByteBuffer.wrap(kv.getBuffer()); 079 bbCell = new ByteBufferKeyValue(buffer, 0, buffer.remaining()); 080 assertEquals(0, PrivateCellUtil.compareQualifier(bbCell, comparable)); 081 assertEquals(0, PrivateCellUtil.compareQualifier(kv, comparable)); 082 kv = new KeyValue(r2, f, q3, v1); 083 buffer = ByteBuffer.wrap(kv.getBuffer()); 084 bbCell = new ByteBufferKeyValue(buffer, 0, buffer.remaining()); 085 assertTrue(PrivateCellUtil.compareQualifier(bbCell, comparable) < 0); 086 assertTrue(PrivateCellUtil.compareQualifier(kv, comparable) < 0); 087 // Value compare 088 comparable = new LongComparator(l1); 089 assertEquals(0, PrivateCellUtil.compareValue(bbCell, comparable)); 090 assertEquals(0, PrivateCellUtil.compareValue(kv, comparable)); 091 kv = new KeyValue(r1, f, q1, v2); 092 buffer = ByteBuffer.wrap(kv.getBuffer()); 093 bbCell = new ByteBufferKeyValue(buffer, 0, buffer.remaining()); 094 assertTrue(PrivateCellUtil.compareValue(bbCell, comparable) < 0); 095 assertTrue(PrivateCellUtil.compareValue(kv, comparable) < 0); 096 // Family compare 097 comparable = new SubstringComparator("cf"); 098 assertEquals(0, PrivateCellUtil.compareFamily(bbCell, comparable)); 099 assertEquals(0, PrivateCellUtil.compareFamily(kv, comparable)); 100 } 101}