001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.codec;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.OutputStream;
023
024import org.apache.hadoop.hbase.CellBuilderType;
025import org.apache.hadoop.hbase.ExtendedCellBuilderFactory;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.hadoop.hbase.io.ByteBuffInputStream;
028import org.apache.hadoop.hbase.nio.ByteBuff;
029import org.apache.hadoop.hbase.Cell;
030import org.apache.hadoop.hbase.ExtendedCellBuilder;
031import org.apache.hadoop.hbase.HBaseInterfaceAudience;
032import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations;
033import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
034import org.apache.hadoop.hbase.shaded.protobuf.generated.CellProtos;
035
036/**
037 * Codec that just writes out Cell as a protobuf Cell Message.  Does not write the mvcc stamp.
038 * Use a different codec if you want that in the stream.
039 */
040@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
041public class MessageCodec implements Codec {
042  static class MessageEncoder extends BaseEncoder {
043    MessageEncoder(final OutputStream out) {
044      super(out);
045    }
046
047    @Override
048    public void write(Cell cell) throws IOException {
049      checkFlushed();
050      CellProtos.Cell.Builder builder = CellProtos.Cell.newBuilder();
051      // This copies bytes from Cell to ByteString.  I don't see anyway around the copy.
052      // ByteString is final.
053      builder.setRow(UnsafeByteOperations.unsafeWrap(cell.getRowArray(), cell.getRowOffset(),
054          cell.getRowLength()));
055      builder.setFamily(UnsafeByteOperations.unsafeWrap(cell.getFamilyArray(),
056          cell.getFamilyOffset(),
057          cell.getFamilyLength()));
058      builder.setQualifier(UnsafeByteOperations.unsafeWrap(cell.getQualifierArray(),
059          cell.getQualifierOffset(), cell.getQualifierLength()));
060      builder.setTimestamp(cell.getTimestamp());
061      builder.setCellType(CellProtos.CellType.valueOf(cell.getTypeByte()));
062      builder.setValue(UnsafeByteOperations.unsafeWrap(cell.getValueArray(), cell.getValueOffset(),
063          cell.getValueLength()));
064      CellProtos.Cell pbcell = builder.build();
065      pbcell.writeDelimitedTo(this.out);
066    }
067  }
068
069  static class MessageDecoder extends BaseDecoder {
070    private final ExtendedCellBuilder cellBuilder = ExtendedCellBuilderFactory.create(CellBuilderType.SHALLOW_COPY);
071    MessageDecoder(final InputStream in) {
072      super(in);
073    }
074
075    @Override
076    protected Cell parseCell() throws IOException {
077      return ProtobufUtil.toCell(cellBuilder, CellProtos.Cell.parseDelimitedFrom(this.in));
078    }
079  }
080
081  @Override
082  public Decoder getDecoder(InputStream is) {
083    return new MessageDecoder(is);
084  }
085
086  @Override
087  public Decoder getDecoder(ByteBuff buf) {
088    return getDecoder(new ByteBuffInputStream(buf));
089  }
090
091  @Override
092  public Encoder getEncoder(OutputStream os) {
093    return new MessageEncoder(os);
094  }
095}