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