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 values encoded using
28   * {@link Bytes#toBytes(String)}. Intended to make it easier to transition
29   * away from direct use of {@link Bytes}.
30   * @see Bytes#toBytes(String)
31   * @see Bytes#toString(byte[])
32   * @see RawStringTerminated
33   */
34  @InterfaceAudience.Public
35  @InterfaceStability.Evolving
36  public class RawString implements DataType<String> {
37  
38    public static final RawString ASCENDING = new RawString(Order.ASCENDING);
39    public static final RawString DESCENDING = new RawString(Order.DESCENDING);
40  
41    protected final Order order;
42  
43    protected RawString() { this.order = Order.ASCENDING; }
44    protected RawString(Order order) { this.order = order; }
45  
46    @Override
47    public boolean isOrderPreserving() { return true; }
48  
49    @Override
50    public Order getOrder() { return order; }
51  
52    @Override
53    public boolean isNullable() { return false; }
54  
55    @Override
56    public boolean isSkippable() { return false; }
57  
58    @Override
59    public int skip(PositionedByteRange src) {
60      int skipped = src.getRemaining();
61      src.setPosition(src.getLength());
62      return skipped;
63    }
64  
65    @Override
66    public int encodedLength(String val) { return Bytes.toBytes(val).length; }
67  
68    @Override
69    public Class<String> encodedClass() { return String.class; }
70  
71    @Override
72    public String decode(PositionedByteRange src) {
73      if (Order.ASCENDING == this.order) {
74        // avoid unnecessary array copy for ASC case.
75        String val =
76            Bytes.toString(src.getBytes(), src.getOffset() + src.getPosition(), src.getRemaining());
77        src.setPosition(src.getLength());
78        return val;
79      } else {
80        byte[] b = new byte[src.getRemaining()];
81        src.get(b);
82        order.apply(b, 0, b.length);
83        return Bytes.toString(b);
84      }
85    }
86  
87    @Override
88    public int encode(PositionedByteRange dst, String val) {
89      byte[] s = Bytes.toBytes(val);
90      order.apply(s);
91      dst.put(s);
92      return s.length;
93    }
94  }