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