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    @Test
47    public void testCellWithTag() throws IOException {
48      ByteArrayOutputStream baos = new ByteArrayOutputStream();
49      CountingOutputStream cos = new CountingOutputStream(baos);
50      DataOutputStream dos = new DataOutputStream(cos);
51      Codec codec = new CellCodecWithTags();
52      Codec.Encoder encoder = codec.getEncoder(dos);
53      final Cell cell1 = 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 Cell cell2 = 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 Cell cell3 = 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(cell1);
67      encoder.write(cell2);
68      encoder.write(cell3);
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, cell1));
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, cell2));
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, cell3));
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 }