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