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 051 @ClassRule 052 public static final HBaseClassTestRule CLASS_RULE = 053 HBaseClassTestRule.forClass(TestKeyValueCodecWithTags.class); 054 055 @Test 056 public void testKeyValueWithTag() throws IOException { 057 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 058 CountingOutputStream cos = new CountingOutputStream(baos); 059 DataOutputStream dos = new DataOutputStream(cos); 060 Codec codec = new KeyValueCodecWithTags(); 061 Codec.Encoder encoder = codec.getEncoder(dos); 062 final KeyValue kv1 = new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("1"), 063 HConstants.LATEST_TIMESTAMP, Bytes.toBytes("1"), new Tag[] { 064 new ArrayBackedTag((byte) 1, Bytes.toBytes("teststring1")), 065 new ArrayBackedTag((byte) 2, Bytes.toBytes("teststring2")) }); 066 final KeyValue kv2 = new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("2"), 067 HConstants.LATEST_TIMESTAMP, Bytes.toBytes("2"), new Tag[] { new ArrayBackedTag((byte) 1, 068 Bytes.toBytes("teststring3")), }); 069 final KeyValue kv3 = new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("3"), 070 HConstants.LATEST_TIMESTAMP, Bytes.toBytes("3"), new Tag[] { 071 new ArrayBackedTag((byte) 2, Bytes.toBytes("teststring4")), 072 new ArrayBackedTag((byte) 2, Bytes.toBytes("teststring5")), 073 new ArrayBackedTag((byte) 1, Bytes.toBytes("teststring6")) }); 074 075 encoder.write(kv1); 076 encoder.write(kv2); 077 encoder.write(kv3); 078 encoder.flush(); 079 dos.close(); 080 long offset = cos.getCount(); 081 CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(baos.toByteArray())); 082 DataInputStream dis = new DataInputStream(cis); 083 Codec.Decoder decoder = codec.getDecoder(dis); 084 assertTrue(decoder.advance()); 085 Cell c = decoder.current(); 086 assertTrue(CellUtil.equals(c, kv1)); 087 List<Tag> tags = 088 PrivateCellUtil.getTags(c); 089 assertEquals(2, tags.size()); 090 Tag tag = tags.get(0); 091 assertEquals(1, tag.getType()); 092 assertTrue(Bytes.equals(Bytes.toBytes("teststring1"), Tag.cloneValue(tag))); 093 tag = tags.get(1); 094 assertEquals(2, tag.getType()); 095 assertTrue(Bytes.equals(Bytes.toBytes("teststring2"), Tag.cloneValue(tag))); 096 assertTrue(decoder.advance()); 097 c = decoder.current(); 098 assertTrue(CellUtil.equals(c, kv2)); 099 tags = PrivateCellUtil.getTags(c); 100 assertEquals(1, tags.size()); 101 tag = tags.get(0); 102 assertEquals(1, tag.getType()); 103 assertTrue(Bytes.equals(Bytes.toBytes("teststring3"), Tag.cloneValue(tag))); 104 assertTrue(decoder.advance()); 105 c = decoder.current(); 106 assertTrue(CellUtil.equals(c, kv3)); 107 tags = PrivateCellUtil.getTags(c); 108 assertEquals(3, tags.size()); 109 tag = tags.get(0); 110 assertEquals(2, tag.getType()); 111 assertTrue(Bytes.equals(Bytes.toBytes("teststring4"), Tag.cloneValue(tag))); 112 tag = tags.get(1); 113 assertEquals(2, tag.getType()); 114 assertTrue(Bytes.equals(Bytes.toBytes("teststring5"), Tag.cloneValue(tag))); 115 tag = tags.get(2); 116 assertEquals(1, tag.getType()); 117 assertTrue(Bytes.equals(Bytes.toBytes("teststring6"), Tag.cloneValue(tag))); 118 assertFalse(decoder.advance()); 119 dis.close(); 120 assertEquals(offset, cis.getCount()); 121 } 122}