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;
023import org.apache.hadoop.hbase.Cell;
024import org.apache.hadoop.hbase.CellBuilderType;
025import org.apache.hadoop.hbase.ExtendedCellBuilder;
026import org.apache.hadoop.hbase.ExtendedCellBuilderFactory;
027import org.apache.hadoop.hbase.HBaseInterfaceAudience;
028import org.apache.hadoop.hbase.io.ByteBuffInputStream;
029import org.apache.hadoop.hbase.nio.ByteBuff;
030import org.apache.yetus.audience.InterfaceAudience;
031
032import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations;
033
034import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
035import org.apache.hadoop.hbase.shaded.protobuf.generated.CellProtos;
036
037/**
038 * Codec that just writes out Cell as a protobuf Cell Message. Does not write the mvcc stamp. Use a
039 * different codec if you want that in the stream.
040 */
041@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
042public class MessageCodec implements Codec {
043  static class MessageEncoder extends BaseEncoder {
044    MessageEncoder(final OutputStream out) {
045      super(out);
046    }
047
048    @Override
049    public void write(Cell cell) throws IOException {
050      checkFlushed();
051      CellProtos.Cell.Builder builder = CellProtos.Cell.newBuilder();
052      // This copies bytes from Cell to ByteString. I don't see anyway around the copy.
053      // ByteString is final.
054      builder.setRow(UnsafeByteOperations.unsafeWrap(cell.getRowArray(), cell.getRowOffset(),
055        cell.getRowLength()));
056      builder.setFamily(UnsafeByteOperations.unsafeWrap(cell.getFamilyArray(),
057        cell.getFamilyOffset(), 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 =
071      ExtendedCellBuilderFactory.create(CellBuilderType.SHALLOW_COPY);
072
073    MessageDecoder(final InputStream in) {
074      super(in);
075    }
076
077    @Override
078    protected Cell parseCell() throws IOException {
079      return ProtobufUtil.toCell(cellBuilder, CellProtos.Cell.parseDelimitedFrom(this.in), false);
080    }
081  }
082
083  @Override
084  public Decoder getDecoder(InputStream is) {
085    return new MessageDecoder(is);
086  }
087
088  @Override
089  public Decoder getDecoder(ByteBuff buf) {
090    return getDecoder(new ByteBuffInputStream(buf));
091  }
092
093  @Override
094  public Encoder getEncoder(OutputStream os) {
095    return new MessageEncoder(os);
096  }
097}