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 org.apache.hadoop.hbase.util.Bytes;
021import org.apache.hadoop.hbase.util.Order;
022import org.apache.hadoop.hbase.util.PositionedByteRange;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * An {@code DataType} for interacting with values encoded using
027 * {@link Bytes#toBytes(String)}. Intended to make it easier to transition
028 * away from direct use of {@link Bytes}.
029 * @see Bytes#toBytes(String)
030 * @see Bytes#toString(byte[])
031 * @see RawStringTerminated
032 */
033@InterfaceAudience.Public
034public class RawString implements DataType<String> {
035  /**
036   * @deprecated since 3.0.0 and will be removed in 4.0.0
037   */
038  @Deprecated
039  public static final RawString ASCENDING = new RawString(Order.ASCENDING);
040  /**
041   * @deprecated since 3.0.0 and will be removed in 4.0.0
042   */
043  @Deprecated
044  public static final RawString DESCENDING = new RawString(Order.DESCENDING);
045
046  protected final Order order;
047
048  /**
049   * @deprecated since 3.0.0 and will be removed in 4.0.0
050   */
051  @Deprecated
052  public RawString() {
053    this.order = Order.ASCENDING;
054  }
055
056  /**
057   * Creates a new {@link DataType} for interacting with values encoded using
058   * {@link Bytes#toBytes(String)}.
059   *
060   * @param order the {@link Order} to use
061   */
062  public RawString(Order order) {
063    this.order = order;
064  }
065
066  @Override
067  public boolean isOrderPreserving() {
068    return true;
069  }
070
071  @Override
072  public Order getOrder() {
073    return order;
074  }
075
076  @Override
077  public boolean isNullable() {
078    return false;
079  }
080
081  @Override
082  public boolean isSkippable() {
083    return false;
084  }
085
086  @Override
087  public int skip(PositionedByteRange src) {
088    int skipped = src.getRemaining();
089    src.setPosition(src.getLength());
090    return skipped;
091  }
092
093  @Override
094  public int encodedLength(String val) {
095    return Bytes.toBytes(val).length;
096  }
097
098  @Override
099  public Class<String> encodedClass() {
100    return String.class;
101  }
102
103  @Override
104  public String decode(PositionedByteRange src) {
105    if (Order.ASCENDING == this.order) {
106      // avoid unnecessary array copy for ASC case.
107      String val =
108          Bytes.toString(src.getBytes(), src.getOffset() + src.getPosition(), src.getRemaining());
109      src.setPosition(src.getLength());
110      return val;
111    } else {
112      byte[] b = new byte[src.getRemaining()];
113      src.get(b);
114      order.apply(b, 0, b.length);
115      return Bytes.toString(b);
116    }
117  }
118
119  @Override
120  public int encode(PositionedByteRange dst, String val) {
121    byte[] s = Bytes.toBytes(val);
122    order.apply(s);
123    dst.put(s);
124    return s.length;
125  }
126}