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