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.hadoop.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.CellUtil;
27  import org.apache.hadoop.hbase.codec.BaseDecoder;
28  import org.apache.hadoop.hbase.codec.BaseEncoder;
29  import org.apache.hadoop.hbase.codec.Codec;
30  import org.apache.hadoop.hbase.codec.CodecException;
31  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
32  
33  import com.google.protobuf.ByteString;
34  import org.apache.hadoop.classification.InterfaceStability;
35  
36  /**
37   * Codec that just writes out Cell as a protobuf Cell Message.  Does not write the mvcc stamp.
38   * Use a different codec if you want that in the stream.
39   */
40  @InterfaceAudience.Public
41  @InterfaceStability.Evolving
42  public class MessageCodec implements Codec {
43    static class MessageEncoder extends BaseEncoder {
44      MessageEncoder(final OutputStream out) {
45        super(out);
46      }
47  
48      @Override
49      public void write(Cell cell) throws IOException {
50        checkFlushed();
51        HBaseProtos.Cell.Builder builder = HBaseProtos.Cell.newBuilder();
52        // This copies bytes from Cell to ByteString.  I don't see anyway around the copy.
53        // ByteString is final.
54        builder.setRow(ByteString.copyFrom(cell.getRowArray(), cell.getRowOffset(),
55            cell.getRowLength()));
56        builder.setFamily(ByteString.copyFrom(cell.getFamilyArray(), cell.getFamilyOffset(),
57            cell.getFamilyLength()));
58        builder.setQualifier(ByteString.copyFrom(cell.getQualifierArray(), cell.getQualifierOffset(),
59            cell.getQualifierLength()));
60        builder.setTimestamp(cell.getTimestamp());
61        builder.setCellType(HBaseProtos.CellType.valueOf(cell.getTypeByte()));
62        builder.setValue(ByteString.copyFrom(cell.getValueArray(), cell.getValueOffset(),
63            cell.getValueLength()));
64        HBaseProtos.Cell pbcell = builder.build();
65        try {
66          pbcell.writeDelimitedTo(this.out);
67        } catch (IOException e) {
68          throw new CodecException(e);
69        }
70      }
71    }
72  
73    static class MessageDecoder extends BaseDecoder {
74      MessageDecoder(final InputStream in) {
75        super(in);
76      }
77  
78      protected Cell parseCell() throws IOException {
79        HBaseProtos.Cell pbcell = HBaseProtos.Cell.parseDelimitedFrom(this.in);
80        return CellUtil.createCell(pbcell.getRow().toByteArray(),
81          pbcell.getFamily().toByteArray(), pbcell.getQualifier().toByteArray(),
82          pbcell.getTimestamp(), (byte)pbcell.getCellType().getNumber(),
83          pbcell.getValue().toByteArray());
84      }
85    }
86  
87    @Override
88    public Decoder getDecoder(InputStream is) {
89      return new MessageDecoder(is);
90    }
91  
92    @Override
93    public Encoder getEncoder(OutputStream os) {
94      return new MessageEncoder(os);
95    }
96  }