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