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 variable-length values encoded using
027 * {@link Bytes#putBytes(byte[], int, byte[], int, int)}. Intended to make it easier to transition
028 * away from direct use of {@link Bytes}.
029 * @see Bytes#putBytes(byte[], int, byte[], int, int)
030 * @see RawBytesTerminated
031 * @see RawBytesFixedLength
032 * @see OrderedBlob
033 * @see OrderedBlobVar
034 */
035@InterfaceAudience.Public
036public class RawBytes implements DataType<byte[]> {
037  /**
038   * @deprecated since 3.0.0 and will be removed in 4.0.0
039   */
040  @Deprecated
041  public static final RawBytes ASCENDING = new RawBytes(Order.ASCENDING);
042  /**
043   * @deprecated since 3.0.0 and will be removed in 4.0.0
044   */
045  @Deprecated
046  public static final RawBytes DESCENDING = new RawBytes(Order.DESCENDING);
047
048  protected final Order order;
049
050  /**
051   * @deprecated since 3.0.0 and will be removed in 4.0.0
052   */
053  @Deprecated
054  public RawBytes() {
055    this.order = Order.ASCENDING;
056  }
057
058  /**
059   * Creates a new {@link DataType} with variable-length values.
060   * @param order the {@link Order} to use
061   */
062  public RawBytes(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(byte[] val) {
095    return val.length;
096  }
097
098  @Override
099  public Class<byte[]> encodedClass() {
100    return byte[].class;
101  }
102
103  @Override
104  public byte[] decode(PositionedByteRange src) {
105    return decode(src, src.getRemaining());
106  }
107
108  @Override
109  public int encode(PositionedByteRange dst, byte[] val) {
110    return encode(dst, val, 0, val.length);
111  }
112
113  /**
114   * Read a {@code byte[]} from the buffer {@code src}.
115   * @param src    the {@link PositionedByteRange} to read the {@code byte[]} from
116   * @param length the length to read from the buffer
117   * @return the {@code byte[]} read from the buffer
118   */
119  public byte[] decode(PositionedByteRange src, int length) {
120    byte[] val = new byte[length];
121    src.get(val);
122    return val;
123  }
124
125  /**
126   * Write {@code val} into {@code dst}, respecting {@code voff} and {@code vlen}.
127   * @param dst  the {@link PositionedByteRange} to write to
128   * @param val  the value to write to {@code dst}
129   * @param voff the offset in {@code dst} where to write {@code val} to
130   * @param vlen the length of {@code val}
131   * @return number of bytes written
132   */
133  public int encode(PositionedByteRange dst, byte[] val, int voff, int vlen) {
134    Bytes.putBytes(dst.getBytes(), dst.getOffset() + dst.getPosition(), val, voff, vlen);
135    dst.setPosition(dst.getPosition() + vlen);
136    return vlen;
137  }
138}