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.Order;
021import org.apache.hadoop.hbase.util.OrderedBytes;
022import org.apache.hadoop.hbase.util.PositionedByteRange;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * A {@code byte[]} of variable-length. Build on
027 * {@link OrderedBytes#encodeBlobCopy(PositionedByteRange, byte[], int, int, Order)}.
028 */
029@InterfaceAudience.Public
030public class OrderedBlob extends OrderedBytesBase<byte[]> {
031
032  /**
033   * @deprecated since 3.0.0 and will be removed in 4.0.0
034   */
035  @Deprecated
036  public static final OrderedBlob ASCENDING = new OrderedBlob(Order.ASCENDING);
037  /**
038   * @deprecated since 3.0.0 and will b removed in 4.0.0
039   */
040  @Deprecated
041  public static final OrderedBlob DESCENDING = new OrderedBlob(Order.DESCENDING);
042
043  /**
044   * Creates a new {@code byte[]} with variable length.
045   *
046   * @param order the {@link Order} to use
047   */
048  public OrderedBlob(Order order) {
049    super(order);
050  }
051
052  @Override
053  public boolean isSkippable() {
054    return false;
055  }
056
057  @Override
058  public int encodedLength(byte[] val) {
059    return null == val ?
060      (Order.ASCENDING == order ? 1 : 2) :
061      (Order.ASCENDING == order ? val.length + 1 : val.length + 2);
062  }
063
064  @Override
065  public Class<byte[]> encodedClass() {
066    return byte[].class;
067  }
068
069  @Override
070  public byte[] decode(PositionedByteRange src) {
071    return OrderedBytes.decodeBlobCopy(src);
072  }
073
074  @Override
075  public int encode(PositionedByteRange dst, byte[] val) {
076    return OrderedBytes.encodeBlobCopy(dst, val, order);
077  }
078
079  /**
080   * Write a subset of {@code val} to {@code dst}.
081   *
082   * @param dst the {@link PositionedByteRange} to write to
083   * @param val  the value to write to {@code dst}
084   * @param voff the offset in {@code dst} where to write {@code val} to
085   * @param vlen the lenght of {@code val}
086   * @return the number of bytes written
087   */
088  public int encode(PositionedByteRange dst, byte[] val, int voff, int vlen) {
089    return OrderedBytes.encodeBlobCopy(dst, val, voff, vlen, order);
090  }
091}