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