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