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