View Javadoc

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