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
027 * encoded using {@link Bytes#putBytes(byte[], int, byte[], int, int)}.
028 * Intended to make it easier to transition away from direct use of
029 * {@link Bytes}.
030 * @see Bytes#putBytes(byte[], int, byte[], int, int)
031 * @see RawBytesTerminated
032 * @see RawBytesFixedLength
033 * @see OrderedBlob
034 * @see OrderedBlobVar
035 */
036@InterfaceAudience.Public
037public class RawBytes implements DataType<byte[]> {
038
039  public static final RawBytes ASCENDING = new RawBytes(Order.ASCENDING);
040  public static final RawBytes DESCENDING = new RawBytes(Order.DESCENDING);
041
042  protected final Order order;
043
044  protected RawBytes() {
045    this.order = Order.ASCENDING;
046  }
047
048  protected RawBytes(Order order) {
049    this.order = order;
050  }
051
052  @Override
053  public boolean isOrderPreserving() {
054    return true;
055  }
056
057  @Override
058  public Order getOrder() {
059    return order;
060  }
061
062  @Override
063  public boolean isNullable() {
064    return false;
065  }
066
067  @Override
068  public boolean isSkippable() {
069    return false;
070  }
071
072  @Override
073  public int skip(PositionedByteRange src) {
074    int skipped = src.getRemaining();
075    src.setPosition(src.getLength());
076    return skipped;
077  }
078
079  @Override
080  public int encodedLength(byte[] val) {
081    return val.length;
082  }
083
084  @Override
085  public Class<byte[]> encodedClass() {
086    return byte[].class;
087  }
088
089  @Override
090  public byte[] decode(PositionedByteRange src) {
091    return decode(src, src.getRemaining());
092  }
093
094  @Override
095  public int encode(PositionedByteRange dst, byte[] val) {
096    return encode(dst, val, 0, val.length);
097  }
098
099  /**
100   * Read a {@code byte[]} from the buffer {@code src}.
101   */
102  public byte[] decode(PositionedByteRange src, int length) {
103    byte[] val = new byte[length];
104    src.get(val);
105    return val;
106  }
107
108  /**
109   * Write {@code val} into {@code dst}, respecting {@code voff} and {@code vlen}.
110   * @return number of bytes written.
111   */
112  public int encode(PositionedByteRange dst, byte[] val, int voff, int vlen) {
113    Bytes.putBytes(dst.getBytes(), dst.getOffset() + dst.getPosition(), val, voff, vlen);
114    dst.setPosition(dst.getPosition() + vlen);
115    return vlen;
116  }
117}