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.assertTrue;
021
022import java.nio.ByteBuffer;
023import java.util.Collection;
024import org.apache.hadoop.hbase.ByteBufferKeyValue;
025import org.apache.hadoop.hbase.CellUtil;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
028import org.apache.hadoop.hbase.KeyValue;
029import org.apache.hadoop.hbase.KeyValue.Type;
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 HBaseCommonTestingUtility.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 = new KeyValue(r, f, q, 0, q.length, 1234L, Type.Put, v, 0,
067        v.length, tags);
068
069    ByteBuffer buffer = ByteBuffer.wrap(kv.getBuffer());
070    ByteBufferKeyValue bbCell = new ByteBufferKeyValue(buffer, 0,
071        buffer.remaining());
072
073    // KV format: <keylen:4><valuelen:4><key:keylen><value:valuelen>
074    // Rebuild as: <keylen:4><0:4><key:keylen>
075    int dataLen = lenAsVal ? Bytes.SIZEOF_INT : 0;
076    int keyOffset = (2 * Bytes.SIZEOF_INT);
077    int keyLen = KeyValueUtil.keyLength(kv);
078    byte[] newBuffer = new byte[keyLen + keyOffset + dataLen];
079    Bytes.putInt(newBuffer, 0, keyLen);
080    Bytes.putInt(newBuffer, Bytes.SIZEOF_INT, dataLen);
081    KeyValueUtil.appendKeyTo(kv, newBuffer, keyOffset);
082    if (lenAsVal) {
083      Bytes.putInt(newBuffer, newBuffer.length - dataLen, kv.getValueLength());
084    }
085    KeyValue KeyOnlyKeyValue = new KeyValue(newBuffer);
086
087    KeyOnlyCell keyOnlyCell = new KeyOnlyCell(kv, lenAsVal);
088    KeyOnlyByteBufferExtendedCell keyOnlyByteBufferedCell = new KeyOnlyByteBufferExtendedCell(
089        bbCell, lenAsVal);
090
091    assertTrue(CellUtil.matchingRows(KeyOnlyKeyValue, keyOnlyCell));
092    assertTrue(CellUtil.matchingRows(KeyOnlyKeyValue, keyOnlyByteBufferedCell));
093
094    assertTrue(CellUtil.matchingFamily(KeyOnlyKeyValue, keyOnlyCell));
095    assertTrue(CellUtil
096        .matchingFamily(KeyOnlyKeyValue, keyOnlyByteBufferedCell));
097
098    assertTrue(CellUtil.matchingQualifier(KeyOnlyKeyValue, keyOnlyCell));
099    assertTrue(CellUtil.matchingQualifier(KeyOnlyKeyValue,
100        keyOnlyByteBufferedCell));
101
102    assertTrue(CellUtil.matchingValue(KeyOnlyKeyValue, keyOnlyCell));
103    assertTrue(KeyOnlyKeyValue.getValueLength() == keyOnlyByteBufferedCell
104        .getValueLength());
105    if (keyOnlyByteBufferedCell.getValueLength() > 0) {
106      assertTrue(CellUtil.matchingValue(KeyOnlyKeyValue,
107          keyOnlyByteBufferedCell));
108    }
109
110    assertTrue(KeyOnlyKeyValue.getTimestamp() == keyOnlyCell.getTimestamp());
111    assertTrue(KeyOnlyKeyValue.getTimestamp() == keyOnlyByteBufferedCell
112        .getTimestamp());
113
114    assertTrue(KeyOnlyKeyValue.getTypeByte() == keyOnlyCell.getTypeByte());
115    assertTrue(KeyOnlyKeyValue.getTypeByte() == keyOnlyByteBufferedCell
116        .getTypeByte());
117
118    assertTrue(KeyOnlyKeyValue.getTagsLength() == keyOnlyCell.getTagsLength());
119    assertTrue(KeyOnlyKeyValue.getTagsLength() == keyOnlyByteBufferedCell
120        .getTagsLength());
121  }
122
123}