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