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.codec;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.io.ByteArrayInputStream;
025import java.io.ByteArrayOutputStream;
026import java.io.DataInputStream;
027import java.io.DataOutputStream;
028import java.io.IOException;
029import java.util.List;
030import org.apache.hadoop.hbase.ArrayBackedTag;
031import org.apache.hadoop.hbase.Cell;
032import org.apache.hadoop.hbase.CellUtil;
033import org.apache.hadoop.hbase.HBaseClassTestRule;
034import org.apache.hadoop.hbase.HConstants;
035import org.apache.hadoop.hbase.KeyValue;
036import org.apache.hadoop.hbase.PrivateCellUtil;
037import org.apache.hadoop.hbase.Tag;
038import org.apache.hadoop.hbase.testclassification.MiscTests;
039import org.apache.hadoop.hbase.testclassification.SmallTests;
040import org.apache.hadoop.hbase.util.Bytes;
041import org.junit.ClassRule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044
045import org.apache.hbase.thirdparty.com.google.common.io.CountingInputStream;
046import org.apache.hbase.thirdparty.com.google.common.io.CountingOutputStream;
047
048@Category({MiscTests.class, SmallTests.class})
049public class TestKeyValueCodecWithTags {
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052      HBaseClassTestRule.forClass(TestKeyValueCodecWithTags.class);
053
054  @Test
055  public void testKeyValueWithTag() throws IOException {
056    ByteArrayOutputStream baos = new ByteArrayOutputStream();
057    CountingOutputStream cos = new CountingOutputStream(baos);
058    DataOutputStream dos = new DataOutputStream(cos);
059    Codec codec = new KeyValueCodecWithTags();
060    Codec.Encoder encoder = codec.getEncoder(dos);
061    final KeyValue kv1 = new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("1"),
062        HConstants.LATEST_TIMESTAMP, Bytes.toBytes("1"), new Tag[] {
063          new ArrayBackedTag((byte) 1, Bytes.toBytes("teststring1")),
064          new ArrayBackedTag((byte) 2, Bytes.toBytes("teststring2")) });
065    final KeyValue kv2 = new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("2"),
066        HConstants.LATEST_TIMESTAMP, Bytes.toBytes("2"), new Tag[] { new ArrayBackedTag((byte) 1,
067            Bytes.toBytes("teststring3")), });
068    final KeyValue kv3 = new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("3"),
069        HConstants.LATEST_TIMESTAMP, Bytes.toBytes("3"), new Tag[] {
070          new ArrayBackedTag((byte) 2, Bytes.toBytes("teststring4")),
071          new ArrayBackedTag((byte) 2, Bytes.toBytes("teststring5")),
072          new ArrayBackedTag((byte) 1, Bytes.toBytes("teststring6")) });
073
074    encoder.write(kv1);
075    encoder.write(kv2);
076    encoder.write(kv3);
077    encoder.flush();
078    dos.close();
079    long offset = cos.getCount();
080    CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
081    DataInputStream dis = new DataInputStream(cis);
082    Codec.Decoder decoder = codec.getDecoder(dis);
083    assertTrue(decoder.advance());
084    Cell c = decoder.current();
085    assertTrue(CellUtil.equals(c, kv1));
086    List<Tag> tags =
087        PrivateCellUtil.getTags(c);
088    assertEquals(2, tags.size());
089    Tag tag = tags.get(0);
090    assertEquals(1, tag.getType());
091    assertTrue(Bytes.equals(Bytes.toBytes("teststring1"), Tag.cloneValue(tag)));
092    tag = tags.get(1);
093    assertEquals(2, tag.getType());
094    assertTrue(Bytes.equals(Bytes.toBytes("teststring2"), Tag.cloneValue(tag)));
095    assertTrue(decoder.advance());
096    c = decoder.current();
097    assertTrue(CellUtil.equals(c, kv2));
098    tags = PrivateCellUtil.getTags(c);
099    assertEquals(1, tags.size());
100    tag = tags.get(0);
101    assertEquals(1, tag.getType());
102    assertTrue(Bytes.equals(Bytes.toBytes("teststring3"), Tag.cloneValue(tag)));
103    assertTrue(decoder.advance());
104    c = decoder.current();
105    assertTrue(CellUtil.equals(c, kv3));
106    tags = PrivateCellUtil.getTags(c);
107    assertEquals(3, tags.size());
108    tag = tags.get(0);
109    assertEquals(2, tag.getType());
110    assertTrue(Bytes.equals(Bytes.toBytes("teststring4"), Tag.cloneValue(tag)));
111    tag = tags.get(1);
112    assertEquals(2, tag.getType());
113    assertTrue(Bytes.equals(Bytes.toBytes("teststring5"), Tag.cloneValue(tag)));
114    tag = tags.get(2);
115    assertEquals(1, tag.getType());
116    assertTrue(Bytes.equals(Bytes.toBytes("teststring6"), Tag.cloneValue(tag)));
117    assertFalse(decoder.advance());
118    dis.close();
119    assertEquals(offset, cis.getCount());
120  }
121}