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