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   * @param order the {@link Order} to use
046   */
047  public OrderedBlob(Order order) {
048    super(order);
049  }
050
051  @Override
052  public boolean isSkippable() {
053    return false;
054  }
055
056  @Override
057  public int encodedLength(byte[] val) {
058    return null == val
059      ? (Order.ASCENDING == order ? 1 : 2)
060      : (Order.ASCENDING == order ? val.length + 1 : val.length + 2);
061  }
062
063  @Override
064  public Class<byte[]> encodedClass() {
065    return byte[].class;
066  }
067
068  @Override
069  public byte[] decode(PositionedByteRange src) {
070    return OrderedBytes.decodeBlobCopy(src);
071  }
072
073  @Override
074  public int encode(PositionedByteRange dst, byte[] val) {
075    return OrderedBytes.encodeBlobCopy(dst, val, order);
076  }
077
078  /**
079   * Write a subset of {@code val} to {@code dst}.
080   * @param dst  the {@link PositionedByteRange} to write to
081   * @param val  the value to write to {@code dst}
082   * @param voff the offset in {@code dst} where to write {@code val} to
083   * @param vlen the lenght of {@code val}
084   * @return the number of bytes written
085   */
086  public int encode(PositionedByteRange dst, byte[] val, int voff, int vlen) {
087    return OrderedBytes.encodeBlobCopy(dst, val, voff, vlen, order);
088  }
089}