@Test public void testGetKeyMethods() throws Exception { KeyValue kvCell = new KeyValue(row1, fam1, qual1, 0l, Type.Put, row1, tags); ByteBuffer buf = ByteBuffer.allocateDirect(kvCell.getKeyLength()); ByteBufferUtils.copyFromArrayToBuffer( buf, kvCell.getBuffer(), kvCell.getKeyOffset(), kvCell.getKeyLength()); ByteBufferedCell offheapKeyOnlyKV = new ByteBufferedKeyOnlyKeyValue(buf, 0, buf.capacity()); assertEquals( ROW1, ByteBufferUtils.toStringBinary( offheapKeyOnlyKV.getRowByteBuffer(), offheapKeyOnlyKV.getRowPositionInByteBuffer(), offheapKeyOnlyKV.getRowLength())); assertEquals( FAM1, ByteBufferUtils.toStringBinary( offheapKeyOnlyKV.getFamilyByteBuffer(), offheapKeyOnlyKV.getFamilyPositionInByteBuffer(), offheapKeyOnlyKV.getFamilyLength())); assertEquals( QUAL1, ByteBufferUtils.toStringBinary( offheapKeyOnlyKV.getQualifierByteBuffer(), offheapKeyOnlyKV.getQualifierPositionInByteBuffer(), offheapKeyOnlyKV.getQualifierLength())); assertEquals(0L, offheapKeyOnlyKV.getTimestamp()); assertEquals(Type.Put.getCode(), offheapKeyOnlyKV.getTypeByte()); }
@Test public void testFindCommonPrefixInFlatKey() { // The whole key matching case KeyValue kv1 = new KeyValue("r1".getBytes(), "f1".getBytes(), "q1".getBytes(), null); Assert.assertEquals( kv1.getKeyLength(), CellUtil.findCommonPrefixInFlatKey(kv1, kv1, true, true)); Assert.assertEquals( kv1.getKeyLength(), CellUtil.findCommonPrefixInFlatKey(kv1, kv1, false, true)); Assert.assertEquals( kv1.getKeyLength() - KeyValue.TIMESTAMP_TYPE_SIZE, CellUtil.findCommonPrefixInFlatKey(kv1, kv1, true, false)); // The rk length itself mismatch KeyValue kv2 = new KeyValue("r12".getBytes(), "f1".getBytes(), "q1".getBytes(), null); Assert.assertEquals(1, CellUtil.findCommonPrefixInFlatKey(kv1, kv2, true, true)); // part of rk is same KeyValue kv3 = new KeyValue("r14".getBytes(), "f1".getBytes(), "q1".getBytes(), null); Assert.assertEquals( KeyValue.ROW_LENGTH_SIZE + "r1".getBytes().length, CellUtil.findCommonPrefixInFlatKey(kv2, kv3, true, true)); // entire rk is same but different cf name KeyValue kv4 = new KeyValue("r14".getBytes(), "f2".getBytes(), "q1".getBytes(), null); Assert.assertEquals( KeyValue.ROW_LENGTH_SIZE + kv3.getRowLength() + KeyValue.FAMILY_LENGTH_SIZE + "f".getBytes().length, CellUtil.findCommonPrefixInFlatKey(kv3, kv4, false, true)); // rk and family are same and part of qualifier KeyValue kv5 = new KeyValue("r14".getBytes(), "f2".getBytes(), "q123".getBytes(), null); Assert.assertEquals( KeyValue.ROW_LENGTH_SIZE + kv3.getRowLength() + KeyValue.FAMILY_LENGTH_SIZE + kv4.getFamilyLength() + kv4.getQualifierLength(), CellUtil.findCommonPrefixInFlatKey(kv4, kv5, true, true)); // rk, cf and q are same. ts differs KeyValue kv6 = new KeyValue("rk".getBytes(), 1234L); KeyValue kv7 = new KeyValue("rk".getBytes(), 1235L); // only last byte out of 8 ts bytes in ts part differs Assert.assertEquals( KeyValue.ROW_LENGTH_SIZE + kv6.getRowLength() + KeyValue.FAMILY_LENGTH_SIZE + kv6.getFamilyLength() + kv6.getQualifierLength() + 7, CellUtil.findCommonPrefixInFlatKey(kv6, kv7, true, true)); // rk, cf, q and ts are same. Only type differs KeyValue kv8 = new KeyValue("rk".getBytes(), 1234L, Type.Delete); Assert.assertEquals( KeyValue.ROW_LENGTH_SIZE + kv6.getRowLength() + KeyValue.FAMILY_LENGTH_SIZE + kv6.getFamilyLength() + kv6.getQualifierLength() + KeyValue.TIMESTAMP_SIZE, CellUtil.findCommonPrefixInFlatKey(kv6, kv8, true, true)); // With out TS_TYPE check Assert.assertEquals( KeyValue.ROW_LENGTH_SIZE + kv6.getRowLength() + KeyValue.FAMILY_LENGTH_SIZE + kv6.getFamilyLength() + kv6.getQualifierLength(), CellUtil.findCommonPrefixInFlatKey(kv6, kv8, true, false)); }