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