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