1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.codec;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.DataInputStream;
27 import java.io.DataOutputStream;
28 import java.io.IOException;
29 import java.util.List;
30
31 import org.apache.hadoop.hbase.Cell;
32 import org.apache.hadoop.hbase.CellComparator;
33 import org.apache.hadoop.hbase.HConstants;
34 import org.apache.hadoop.hbase.KeyValue;
35 import org.apache.hadoop.hbase.testclassification.SmallTests;
36 import org.apache.hadoop.hbase.Tag;
37 import org.apache.hadoop.hbase.util.Bytes;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
40
41 import com.google.common.io.CountingInputStream;
42 import com.google.common.io.CountingOutputStream;
43
44 @Category(SmallTests.class)
45 public class TestKeyValueCodecWithTags {
46 @Test
47 public void testKeyValueWithTag() throws IOException {
48 ByteArrayOutputStream baos = new ByteArrayOutputStream();
49 CountingOutputStream cos = new CountingOutputStream(baos);
50 DataOutputStream dos = new DataOutputStream(cos);
51 Codec codec = new KeyValueCodecWithTags();
52 Codec.Encoder encoder = codec.getEncoder(dos);
53 final KeyValue kv1 = new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("1"),
54 HConstants.LATEST_TIMESTAMP, Bytes.toBytes("1"), new Tag[] {
55 new Tag((byte) 1, Bytes.toBytes("teststring1")),
56 new Tag((byte) 2, Bytes.toBytes("teststring2")) });
57 final KeyValue kv2 = new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("2"),
58 HConstants.LATEST_TIMESTAMP, Bytes.toBytes("2"), new Tag[] { new Tag((byte) 1,
59 Bytes.toBytes("teststring3")), });
60 final KeyValue kv3 = new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("3"),
61 HConstants.LATEST_TIMESTAMP, Bytes.toBytes("3"), new Tag[] {
62 new Tag((byte) 2, Bytes.toBytes("teststring4")),
63 new Tag((byte) 2, Bytes.toBytes("teststring5")),
64 new Tag((byte) 1, Bytes.toBytes("teststring6")) });
65
66 encoder.write(kv1);
67 encoder.write(kv2);
68 encoder.write(kv3);
69 encoder.flush();
70 dos.close();
71 long offset = cos.getCount();
72 CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
73 DataInputStream dis = new DataInputStream(cis);
74 Codec.Decoder decoder = codec.getDecoder(dis);
75 assertTrue(decoder.advance());
76 Cell c = decoder.current();
77 assertTrue(CellComparator.equals(c, kv1));
78 List<Tag> tags = Tag.asList(c.getTagsArray(), c.getTagsOffset(), c.getTagsLength());
79 assertEquals(2, tags.size());
80 Tag tag = tags.get(0);
81 assertEquals(1, tag.getType());
82 assertTrue(Bytes.equals(Bytes.toBytes("teststring1"), tag.getValue()));
83 tag = tags.get(1);
84 assertEquals(2, tag.getType());
85 assertTrue(Bytes.equals(Bytes.toBytes("teststring2"), tag.getValue()));
86 assertTrue(decoder.advance());
87 c = decoder.current();
88 assertTrue(CellComparator.equals(c, kv2));
89 tags = Tag.asList(c.getTagsArray(), c.getTagsOffset(), c.getTagsLength());
90 assertEquals(1, tags.size());
91 tag = tags.get(0);
92 assertEquals(1, tag.getType());
93 assertTrue(Bytes.equals(Bytes.toBytes("teststring3"), tag.getValue()));
94 assertTrue(decoder.advance());
95 c = decoder.current();
96 assertTrue(CellComparator.equals(c, kv3));
97 tags = Tag.asList(c.getTagsArray(), c.getTagsOffset(), c.getTagsLength());
98 assertEquals(3, tags.size());
99 tag = tags.get(0);
100 assertEquals(2, tag.getType());
101 assertTrue(Bytes.equals(Bytes.toBytes("teststring4"), tag.getValue()));
102 tag = tags.get(1);
103 assertEquals(2, tag.getType());
104 assertTrue(Bytes.equals(Bytes.toBytes("teststring5"), tag.getValue()));
105 tag = tags.get(2);
106 assertEquals(1, tag.getType());
107 assertTrue(Bytes.equals(Bytes.toBytes("teststring6"), tag.getValue()));
108 assertFalse(decoder.advance());
109 dis.close();
110 assertEquals(offset, cis.getCount());
111 }
112 }