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.types;
019
020import java.io.IOException;
021import org.apache.hadoop.hbase.example.protobuf.generated.CellMessage;
022import org.apache.hadoop.hbase.util.PositionedByteRange;
023import org.apache.yetus.audience.InterfaceAudience;
024
025import org.apache.hbase.thirdparty.com.google.protobuf.CodedInputStream;
026import org.apache.hbase.thirdparty.com.google.protobuf.CodedOutputStream;
027
028/**
029 * An example for using protobuf objects with {@link DataType} API.
030 */
031@InterfaceAudience.Private
032public class PBCell extends PBType<CellMessage.Cell> {
033  @Override
034  public Class<CellMessage.Cell> encodedClass() {
035    return CellMessage.Cell.class;
036  }
037
038  @Override
039  public int skip(PositionedByteRange src) {
040    CellMessage.Cell.Builder builder = CellMessage.Cell.newBuilder();
041    CodedInputStream is = inputStreamFromByteRange(src);
042    is.setSizeLimit(src.getLength());
043    try {
044      builder.mergeFrom(is);
045      int consumed = is.getTotalBytesRead();
046      src.setPosition(src.getPosition() + consumed);
047      return consumed;
048    } catch (IOException e) {
049      throw new RuntimeException("Error while skipping type.", e);
050    }
051  }
052
053  @Override
054  public CellMessage.Cell decode(PositionedByteRange src) {
055    CellMessage.Cell.Builder builder = CellMessage.Cell.newBuilder();
056    CodedInputStream is = inputStreamFromByteRange(src);
057    is.setSizeLimit(src.getLength());
058    try {
059      CellMessage.Cell ret = builder.mergeFrom(is).build();
060      src.setPosition(src.getPosition() + is.getTotalBytesRead());
061      return ret;
062    } catch (IOException e) {
063      throw new RuntimeException("Error while decoding type.", e);
064    }
065  }
066
067  @Override
068  public int encode(PositionedByteRange dst, CellMessage.Cell val) {
069    CodedOutputStream os = outputStreamFromByteRange(dst);
070    try {
071      int before = os.spaceLeft(), after, written;
072      val.writeTo(os);
073      after = os.spaceLeft();
074      written = before - after;
075      dst.setPosition(dst.getPosition() + written);
076      return written;
077    } catch (IOException e) {
078      throw new RuntimeException("Error while encoding type.", e);
079    }
080  }
081}