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.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022 023import java.nio.ByteBuffer; 024import java.util.stream.Stream; 025import org.apache.hadoop.hbase.ByteBufferKeyValue; 026import org.apache.hadoop.hbase.CellUtil; 027import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate; 028import org.apache.hadoop.hbase.KeyValue; 029import org.apache.hadoop.hbase.KeyValueUtil; 030import org.apache.hadoop.hbase.filter.KeyOnlyFilter.KeyOnlyByteBufferExtendedCell; 031import org.apache.hadoop.hbase.filter.KeyOnlyFilter.KeyOnlyCell; 032import org.apache.hadoop.hbase.testclassification.MiscTests; 033import org.apache.hadoop.hbase.testclassification.SmallTests; 034import org.apache.hadoop.hbase.util.Bytes; 035import org.junit.jupiter.api.Tag; 036import org.junit.jupiter.api.TestTemplate; 037import org.junit.jupiter.params.provider.Arguments; 038 039@Tag(MiscTests.TAG) 040@Tag(SmallTests.TAG) 041@HBaseParameterizedTestTemplate 042public class TestKeyOnlyFilter { 043 044 public boolean lenAsVal; 045 046 public TestKeyOnlyFilter(boolean lenAsVal) { 047 this.lenAsVal = lenAsVal; 048 } 049 050 public static Stream<Arguments> parameters() { 051 return Stream.of(Arguments.of(true), Arguments.of(false)); 052 } 053 054 @TestTemplate 055 public void testKeyOnly() throws Exception { 056 byte[] r = Bytes.toBytes("row1"); 057 byte[] f = Bytes.toBytes("cf1"); 058 byte[] q = Bytes.toBytes("qual1"); 059 byte[] v = Bytes.toBytes("val1"); 060 byte[] tags = Bytes.toBytes("tag1"); 061 KeyValue kv = 062 new KeyValue(r, f, q, 0, q.length, 1234L, KeyValue.Type.Put, v, 0, v.length, tags); 063 064 ByteBuffer buffer = ByteBuffer.wrap(kv.getBuffer()); 065 ByteBufferKeyValue bbCell = new ByteBufferKeyValue(buffer, 0, buffer.remaining()); 066 067 // KV format: <keylen:4><valuelen:4><key:keylen><value:valuelen> 068 // Rebuild as: <keylen:4><0:4><key:keylen> 069 int dataLen = lenAsVal ? Bytes.SIZEOF_INT : 0; 070 int keyOffset = (2 * Bytes.SIZEOF_INT); 071 int keyLen = KeyValueUtil.keyLength(kv); 072 byte[] newBuffer = new byte[keyLen + keyOffset + dataLen]; 073 Bytes.putInt(newBuffer, 0, keyLen); 074 Bytes.putInt(newBuffer, Bytes.SIZEOF_INT, dataLen); 075 KeyValueUtil.appendKeyTo(kv, newBuffer, keyOffset); 076 if (lenAsVal) { 077 Bytes.putInt(newBuffer, newBuffer.length - dataLen, kv.getValueLength()); 078 } 079 KeyValue KeyOnlyKeyValue = new KeyValue(newBuffer); 080 081 KeyOnlyCell keyOnlyCell = new KeyOnlyCell(kv, lenAsVal); 082 KeyOnlyByteBufferExtendedCell keyOnlyByteBufferedCell = 083 new KeyOnlyByteBufferExtendedCell(bbCell, lenAsVal); 084 085 assertTrue(CellUtil.matchingRows(KeyOnlyKeyValue, keyOnlyCell)); 086 assertTrue(CellUtil.matchingRows(KeyOnlyKeyValue, keyOnlyByteBufferedCell)); 087 088 assertTrue(CellUtil.matchingFamily(KeyOnlyKeyValue, keyOnlyCell)); 089 assertTrue(CellUtil.matchingFamily(KeyOnlyKeyValue, keyOnlyByteBufferedCell)); 090 091 assertTrue(CellUtil.matchingQualifier(KeyOnlyKeyValue, keyOnlyCell)); 092 assertTrue(CellUtil.matchingQualifier(KeyOnlyKeyValue, keyOnlyByteBufferedCell)); 093 094 assertTrue(CellUtil.matchingValue(KeyOnlyKeyValue, keyOnlyCell)); 095 assertTrue(KeyOnlyKeyValue.getValueLength() == keyOnlyByteBufferedCell.getValueLength()); 096 assertEquals(8 + keyLen + (lenAsVal ? 4 : 0), KeyOnlyKeyValue.getSerializedSize()); 097 assertEquals(8 + keyLen + (lenAsVal ? 4 : 0), keyOnlyCell.getSerializedSize()); 098 if (keyOnlyByteBufferedCell.getValueLength() > 0) { 099 assertTrue(CellUtil.matchingValue(KeyOnlyKeyValue, keyOnlyByteBufferedCell)); 100 } 101 102 assertTrue(KeyOnlyKeyValue.getTimestamp() == keyOnlyCell.getTimestamp()); 103 assertTrue(KeyOnlyKeyValue.getTimestamp() == keyOnlyByteBufferedCell.getTimestamp()); 104 105 assertTrue(KeyOnlyKeyValue.getTypeByte() == keyOnlyCell.getTypeByte()); 106 assertTrue(KeyOnlyKeyValue.getTypeByte() == keyOnlyByteBufferedCell.getTypeByte()); 107 108 assertTrue(KeyOnlyKeyValue.getTagsLength() == keyOnlyCell.getTagsLength()); 109 assertTrue(KeyOnlyKeyValue.getTagsLength() == keyOnlyByteBufferedCell.getTagsLength()); 110 } 111 112}