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 com.google.protobuf.Message;
023import org.apache.hadoop.hbase.util.Order;
024import org.apache.hadoop.hbase.util.PositionedByteRange;
025import org.apache.yetus.audience.InterfaceAudience;
026
027/**
028 * A base-class for {@link DataType} implementations backed by protobuf. See {@code PBKeyValue} in
029 * {@code hbase-examples} module.
030 * @deprecated Will be removed in 3.0.0 without replacement. It should not be a public API as it
031 *             exposes the protobuf stuff. Users who depend on this class should just copy the code
032 *             your own code base.
033 */
034@Deprecated
035@InterfaceAudience.Public
036public abstract class PBType<T extends Message> implements DataType<T> {
037  @Override
038  public boolean isOrderPreserving() {
039    return false;
040  }
041
042  @Override
043  public Order getOrder() {
044    return null;
045  }
046
047  @Override
048  public boolean isNullable() {
049    return false;
050  }
051
052  @Override
053  public boolean isSkippable() {
054    return true;
055  }
056
057  @Override
058  public int encodedLength(T val) {
059    return val.getSerializedSize();
060  }
061
062  /**
063   * Create a {@link CodedInputStream} from a {@link PositionedByteRange}. Be sure to update
064   * {@code src}'s position after consuming from the stream.
065   * <p>For example:
066   * <pre>
067   * Foo.Builder builder = ...
068   * CodedInputStream is = inputStreamFromByteRange(src);
069   * Foo ret = builder.mergeFrom(is).build();
070   * src.setPosition(src.getPosition() + is.getTotalBytesRead());
071   * </pre>
072   */
073  public static CodedInputStream inputStreamFromByteRange(PositionedByteRange src) {
074    return CodedInputStream.newInstance(
075      src.getBytes(),
076      src.getOffset() + src.getPosition(),
077      src.getRemaining());
078  }
079
080  /**
081   * Create a {@link CodedOutputStream} from a {@link PositionedByteRange}. Be sure to update
082   * {@code dst}'s position after writing to the stream.
083   * <p>For example:
084   * <pre>
085   * CodedOutputStream os = outputStreamFromByteRange(dst);
086   * int before = os.spaceLeft(), after, written;
087   * val.writeTo(os);
088   * after = os.spaceLeft();
089   * written = before - after;
090   * dst.setPosition(dst.getPosition() + written);
091   * </pre>
092   */
093  public static CodedOutputStream outputStreamFromByteRange(PositionedByteRange dst) {
094    return CodedOutputStream.newInstance(
095      dst.getBytes(),
096      dst.getOffset() + dst.getPosition(),
097      dst.getRemaining()
098    );
099  }
100}