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"),
063      new Tag[] { 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"),
067      new Tag[] { new ArrayBackedTag((byte) 1, 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"),
070      new Tag[] { 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 = PrivateCellUtil.getTags(c);
087    assertEquals(2, tags.size());
088    Tag tag = tags.get(0);
089    assertEquals(1, tag.getType());
090    assertTrue(Bytes.equals(Bytes.toBytes("teststring1"), Tag.cloneValue(tag)));
091    tag = tags.get(1);
092    assertEquals(2, tag.getType());
093    assertTrue(Bytes.equals(Bytes.toBytes("teststring2"), Tag.cloneValue(tag)));
094    assertTrue(decoder.advance());
095    c = decoder.current();
096    assertTrue(CellUtil.equals(c, kv2));
097    tags = PrivateCellUtil.getTags(c);
098    assertEquals(1, tags.size());
099    tag = tags.get(0);
100    assertEquals(1, tag.getType());
101    assertTrue(Bytes.equals(Bytes.toBytes("teststring3"), Tag.cloneValue(tag)));
102    assertTrue(decoder.advance());
103    c = decoder.current();
104    assertTrue(CellUtil.equals(c, kv3));
105    tags = PrivateCellUtil.getTags(c);
106    assertEquals(3, tags.size());
107    tag = tags.get(0);
108    assertEquals(2, tag.getType());
109    assertTrue(Bytes.equals(Bytes.toBytes("teststring4"), Tag.cloneValue(tag)));
110    tag = tags.get(1);
111    assertEquals(2, tag.getType());
112    assertTrue(Bytes.equals(Bytes.toBytes("teststring5"), Tag.cloneValue(tag)));
113    tag = tags.get(2);
114    assertEquals(1, tag.getType());
115    assertTrue(Bytes.equals(Bytes.toBytes("teststring6"), Tag.cloneValue(tag)));
116    assertFalse(decoder.advance());
117    dis.close();
118    assertEquals(offset, cis.getCount());
119  }
120}