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