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 java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  
24  import org.apache.commons.io.IOUtils;
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.CellUtil;
27  import org.apache.hadoop.hbase.util.Bytes;
28  
29  /**
30   * Basic Cell codec that just writes out all the individual elements of a Cell.  Uses ints
31   * delimiting all lengths. Profligate. Needs tune up.  Does not write the mvcc stamp.
32   * Use a different codec if you want that in the stream.
33   */
34  public class CellCodec implements Codec {
35    static class CellEncoder extends BaseEncoder {
36      CellEncoder(final OutputStream out) {
37        super(out);
38      }
39  
40      @Override
41      public void write(Cell cell) throws IOException {
42        checkFlushed();
43        // Row
44        write(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
45        // Column family
46        write(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
47        // Qualifier
48        write(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
49        // Version
50        this.out.write(Bytes.toBytes(cell.getTimestamp()));
51        // Type
52        this.out.write(cell.getTypeByte());
53        // Value
54        write(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
55      }
56  
57      /**
58       * Write int length followed by array bytes.
59       * @param bytes
60       * @param offset
61       * @param length
62       * @throws IOException
63       */
64      private void write(final byte [] bytes, final int offset, final int length)
65      throws IOException {
66        this.out.write(Bytes.toBytes(length));
67        this.out.write(bytes, offset, length);
68      }
69    }
70  
71    static class CellDecoder extends BaseDecoder {
72      public CellDecoder(final InputStream in) {
73        super(in);
74      }
75  
76      protected Cell parseCell() throws IOException {
77        byte [] row = readByteArray(this.in);
78        byte [] family = readByteArray(in);
79        byte [] qualifier = readByteArray(in);
80        byte [] longArray = new byte[Bytes.SIZEOF_LONG];
81        IOUtils.readFully(this.in, longArray);
82        long timestamp = Bytes.toLong(longArray);
83        byte type = (byte) this.in.read();
84        byte [] value = readByteArray(in);
85        return CellUtil.createCell(row, family, qualifier, timestamp, type, value);
86      }
87  
88      /**
89       * @return Byte array read from the stream.
90       * @throws IOException
91       */
92      private byte [] readByteArray(final InputStream in) throws IOException {
93        byte [] intArray = new byte[Bytes.SIZEOF_INT];
94        IOUtils.readFully(in, intArray);
95        int length = Bytes.toInt(intArray);
96        byte [] bytes = new byte [length];
97        IOUtils.readFully(in, bytes);
98        return bytes;
99      }
100   }
101 
102   @Override
103   public Decoder getDecoder(InputStream is) {
104     return new CellDecoder(is);
105   }
106 
107   @Override
108   public Encoder getEncoder(OutputStream os) {
109     return new CellEncoder(os);
110   }
111 }