View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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 TestCellCodecWithTags {
46  
47    @Test
48    public void testCellWithTag() throws IOException {
49      ByteArrayOutputStream baos = new ByteArrayOutputStream();
50      CountingOutputStream cos = new CountingOutputStream(baos);
51      DataOutputStream dos = new DataOutputStream(cos);
52      Codec codec = new CellCodecWithTags();
53      Codec.Encoder encoder = codec.getEncoder(dos);
54      final Cell cell1 = new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("1"),
55          HConstants.LATEST_TIMESTAMP, Bytes.toBytes("1"), new Tag[] {
56              new Tag((byte) 1, Bytes.toBytes("teststring1")),
57              new Tag((byte) 2, Bytes.toBytes("teststring2")) });
58      final Cell cell2 = new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("2"),
59          HConstants.LATEST_TIMESTAMP, Bytes.toBytes("2"), new Tag[] { new Tag((byte) 1,
60              Bytes.toBytes("teststring3")), });
61      final Cell cell3 = new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("3"),
62          HConstants.LATEST_TIMESTAMP, Bytes.toBytes("3"), new Tag[] {
63              new Tag((byte) 2, Bytes.toBytes("teststring4")),
64              new Tag((byte) 2, Bytes.toBytes("teststring5")),
65              new Tag((byte) 1, Bytes.toBytes("teststring6")) });
66  
67      encoder.write(cell1);
68      encoder.write(cell2);
69      encoder.write(cell3);
70      encoder.flush();
71      dos.close();
72      long offset = cos.getCount();
73      CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
74      DataInputStream dis = new DataInputStream(cis);
75      Codec.Decoder decoder = codec.getDecoder(dis);
76      assertTrue(decoder.advance());
77      Cell c = decoder.current();
78      assertTrue(CellComparator.equals(c, cell1));
79      List<Tag> tags = Tag.asList(c.getTagsArray(), c.getTagsOffset(), c.getTagsLength());
80      assertEquals(2, tags.size());
81      Tag tag = tags.get(0);
82      assertEquals(1, tag.getType());
83      assertTrue(Bytes.equals(Bytes.toBytes("teststring1"), tag.getValue()));
84      tag = tags.get(1);
85      assertEquals(2, tag.getType());
86      assertTrue(Bytes.equals(Bytes.toBytes("teststring2"), tag.getValue()));
87      assertTrue(decoder.advance());
88      c = decoder.current();
89      assertTrue(CellComparator.equals(c, cell2));
90      tags = Tag.asList(c.getTagsArray(), c.getTagsOffset(), c.getTagsLength());
91      assertEquals(1, tags.size());
92      tag = tags.get(0);
93      assertEquals(1, tag.getType());
94      assertTrue(Bytes.equals(Bytes.toBytes("teststring3"), tag.getValue()));
95      assertTrue(decoder.advance());
96      c = decoder.current();
97      assertTrue(CellComparator.equals(c, cell3));
98      tags = Tag.asList(c.getTagsArray(), c.getTagsOffset(), c.getTagsLength());
99      assertEquals(3, tags.size());
100     tag = tags.get(0);
101     assertEquals(2, tag.getType());
102     assertTrue(Bytes.equals(Bytes.toBytes("teststring4"), tag.getValue()));
103     tag = tags.get(1);
104     assertEquals(2, tag.getType());
105     assertTrue(Bytes.equals(Bytes.toBytes("teststring5"), tag.getValue()));
106     tag = tags.get(2);
107     assertEquals(1, tag.getType());
108     assertTrue(Bytes.equals(Bytes.toBytes("teststring6"), tag.getValue()));
109     assertFalse(decoder.advance());
110     dis.close();
111     assertEquals(offset, cis.getCount());
112   }
113 }