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.types; 19 20 import com.google.protobuf.CodedInputStream; 21 import com.google.protobuf.CodedOutputStream; 22 import com.google.protobuf.Message; 23 import org.apache.hadoop.hbase.util.Order; 24 import org.apache.hadoop.hbase.util.PositionedByteRange; 25 26 /** 27 * A base-class for {@link DataType} implementations backed by protobuf. See 28 * {@code PBKeyValue} in {@code hbase-examples} module. 29 */ 30 public abstract class PBType<T extends Message> implements DataType<T> { 31 @Override 32 public boolean isOrderPreserving() { 33 return false; 34 } 35 36 @Override 37 public Order getOrder() { 38 return null; 39 } 40 41 @Override 42 public boolean isNullable() { 43 return false; 44 } 45 46 @Override 47 public boolean isSkippable() { 48 return true; 49 } 50 51 @Override 52 public int encodedLength(T val) { 53 return val.getSerializedSize(); 54 } 55 56 /** 57 * Create a {@link CodedInputStream} from a {@link PositionedByteRange}. Be sure to update 58 * {@code src}'s position after consuming from the stream. 59 * <p>For example: 60 * <pre> 61 * Foo.Builder builder = ... 62 * CodedInputStream is = inputStreamFromByteRange(src); 63 * Foo ret = builder.mergeFrom(is).build(); 64 * src.setPosition(src.getPosition() + is.getTotalBytesRead()); 65 * </pre> 66 */ 67 public static CodedInputStream inputStreamFromByteRange(PositionedByteRange src) { 68 return CodedInputStream.newInstance( 69 src.getBytes(), 70 src.getOffset() + src.getPosition(), 71 src.getRemaining()); 72 } 73 74 /** 75 * Create a {@link CodedOutputStream} from a {@link PositionedByteRange}. Be sure to update 76 * {@code dst}'s position after writing to the stream. 77 * <p>For example: 78 * <pre> 79 * CodedOutputStream os = outputStreamFromByteRange(dst); 80 * int before = os.spaceLeft(), after, written; 81 * val.writeTo(os); 82 * after = os.spaceLeft(); 83 * written = before - after; 84 * dst.setPosition(dst.getPosition() + written); 85 * </pre> 86 */ 87 public static CodedOutputStream outputStreamFromByteRange(PositionedByteRange dst) { 88 return CodedOutputStream.newInstance( 89 dst.getBytes(), 90 dst.getOffset() + dst.getPosition(), 91 dst.getRemaining() 92 ); 93 } 94 }