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.Bytes;
23  import org.apache.hadoop.hbase.util.Order;
24  import org.apache.hadoop.hbase.util.PositionedByteRange;
25  
26  /**
27   * An {@code DataType} for interacting with variable-length values
28   * encoded using {@link Bytes#putBytes(byte[], int, byte[], int, int)}.
29   * Intended to make it easier to transition away from direct use of
30   * {@link Bytes}.
31   * @see Bytes#putBytes(byte[], int, byte[], int, int)
32   * @see RawBytesTerminated
33   * @see RawBytesFixedLength
34   * @see OrderedBlob
35   * @see OrderedBlobVar
36   */
37  @InterfaceAudience.Public
38  @InterfaceStability.Evolving
39  public class RawBytes implements DataType<byte[]> {
40  
41    public static final RawBytes ASCENDING = new RawBytes(Order.ASCENDING);
42    public static final RawBytes DESCENDING = new RawBytes(Order.DESCENDING);
43  
44    protected final Order order;
45  
46    protected RawBytes() { this.order = Order.ASCENDING; }
47    protected RawBytes(Order order) { this.order = order; }
48  
49    @Override
50    public boolean isOrderPreserving() { return true; }
51  
52    @Override
53    public Order getOrder() { return order; }
54  
55    @Override
56    public boolean isNullable() { return false; }
57  
58    @Override
59    public boolean isSkippable() { return false; }
60  
61    @Override
62    public int skip(PositionedByteRange src) {
63      int skipped = src.getRemaining();
64      src.setPosition(src.getLength());
65      return skipped;
66    }
67  
68    @Override
69    public int encodedLength(byte[] val) { return val.length; }
70  
71    @Override
72    public Class<byte[]> encodedClass() { return byte[].class; }
73  
74    @Override
75    public byte[] decode(PositionedByteRange src) {
76      return decode(src, src.getRemaining());
77    }
78  
79    @Override
80    public int encode(PositionedByteRange dst, byte[] val) {
81      return encode(dst, val, 0, val.length);
82    }
83  
84    /**
85     * Read a {@code byte[]} from the buffer {@code src}.
86     */
87    public byte[] decode(PositionedByteRange src, int length) {
88      byte[] val = new byte[length];
89      src.get(val);
90      return val;
91    }
92  
93    /**
94     * Write {@code val} into {@code dst}, respecting {@code voff} and {@code vlen}.
95     * @return number of bytes written.
96     */
97    public int encode(PositionedByteRange dst, byte[] val, int voff, int vlen) {
98      Bytes.putBytes(dst.getBytes(), dst.getOffset() + dst.getPosition(), val, voff, vlen);
99      dst.setPosition(dst.getPosition() + vlen);
100     return vlen;
101   }
102 }