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.*;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.DataInputStream;
25  import java.io.DataOutputStream;
26  import java.io.IOException;
27  
28  import org.apache.hadoop.hbase.Cell;
29  import org.apache.hadoop.hbase.CellComparator;
30  import org.apache.hadoop.hbase.KeyValue;
31  import org.apache.hadoop.hbase.SmallTests;
32  import org.apache.hadoop.hbase.codec.CellCodec;
33  import org.apache.hadoop.hbase.codec.Codec;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  import com.google.common.io.CountingInputStream;
39  import com.google.common.io.CountingOutputStream;
40  
41  @Category(SmallTests.class)
42  public class TestCellCodec {
43  
44    @Test
45    public void testEmptyWorks() throws IOException {
46      ByteArrayOutputStream baos = new ByteArrayOutputStream();
47      CountingOutputStream cos = new CountingOutputStream(baos);
48      DataOutputStream dos = new DataOutputStream(cos);
49      Codec codec = new CellCodec();
50      Codec.Encoder encoder = codec.getEncoder(dos);
51      encoder.flush();
52      dos.close();
53      long offset = cos.getCount();
54      assertEquals(0, offset);
55      CountingInputStream cis =
56        new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
57      DataInputStream dis = new DataInputStream(cis);
58      Codec.Decoder decoder = codec.getDecoder(dis);
59      assertFalse(decoder.advance());
60      dis.close();
61      assertEquals(0, cis.getCount());
62    }
63  
64    @Test
65    public void testOne() throws IOException {
66      ByteArrayOutputStream baos = new ByteArrayOutputStream();
67      CountingOutputStream cos = new CountingOutputStream(baos);
68      DataOutputStream dos = new DataOutputStream(cos);
69      Codec codec = new CellCodec();
70      Codec.Encoder encoder = codec.getEncoder(dos);
71      final KeyValue kv =
72        new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("q"), Bytes.toBytes("v"));
73      kv.setMvccVersion(Long.MAX_VALUE);
74      encoder.write(kv);
75      encoder.flush();
76      dos.close();
77      long offset = cos.getCount();
78      CountingInputStream cis =
79        new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
80      DataInputStream dis = new DataInputStream(cis);
81      Codec.Decoder decoder = codec.getDecoder(dis);
82      assertTrue(decoder.advance()); // First read should pull in the KV
83      // Second read should trip over the end-of-stream marker and return false
84      assertFalse(decoder.advance());
85      dis.close();
86      assertEquals(offset, cis.getCount());
87    }
88  
89    @Test
90    public void testThree() throws IOException {
91      ByteArrayOutputStream baos = new ByteArrayOutputStream();
92      CountingOutputStream cos = new CountingOutputStream(baos);
93      DataOutputStream dos = new DataOutputStream(cos);
94      Codec codec = new CellCodec();
95      Codec.Encoder encoder = codec.getEncoder(dos);
96      final KeyValue kv1 =
97        new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("1"), Bytes.toBytes("1"));
98      final KeyValue kv2 =
99        new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("2"), Bytes.toBytes("2"));
100     final KeyValue kv3 =
101       new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("3"), Bytes.toBytes("3"));
102     encoder.write(kv1);
103     encoder.write(kv2);
104     encoder.write(kv3);
105     encoder.flush();
106     dos.close();
107     long offset = cos.getCount();
108     CountingInputStream cis =
109       new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
110     DataInputStream dis = new DataInputStream(cis);
111     Codec.Decoder decoder = codec.getDecoder(dis);
112     assertTrue(decoder.advance());
113     Cell c = decoder.current();
114     assertTrue(CellComparator.equals(c, kv1));
115     assertTrue(decoder.advance());
116     c = decoder.current();
117     assertTrue(CellComparator.equals(c, kv2));
118     assertTrue(decoder.advance());
119     c = decoder.current();
120     assertTrue(CellComparator.equals(c, kv3));
121     assertFalse(decoder.advance());
122     dis.close();
123     assertEquals(offset, cis.getCount());
124   }
125 }