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 * An alternative to {@link OrderedBlob} for use by {@link Struct} fields that
027 * do not terminate the fields list. Built on
028 * {@link OrderedBytes#encodeBlobVar(PositionedByteRange, byte[], int, int, Order)}.
029 */
030@InterfaceAudience.Public
031public class OrderedBlobVar extends OrderedBytesBase<byte[]> {
032  /**
033   * @deprecated since 3.0.0 and will be removed in 4.0.0
034   */
035  @Deprecated
036  public static final OrderedBlobVar ASCENDING = new OrderedBlobVar(Order.ASCENDING);
037  /**
038   * @deprecated since 3.0.0 and will be removed in 4.0.0
039   */
040  @Deprecated
041  public static final OrderedBlobVar DESCENDING = new OrderedBlobVar(Order.DESCENDING);
042
043  public OrderedBlobVar(Order order) {
044    super(order);
045  }
046
047  @Override
048  public int encodedLength(byte[] val) {
049    return null == val ? 1 : OrderedBytes.blobVarEncodedLength(val.length);
050  }
051
052  @Override
053  public Class<byte[]> encodedClass() {
054    return byte[].class;
055  }
056
057  @Override
058  public byte[] decode(PositionedByteRange src) {
059    return OrderedBytes.decodeBlobVar(src);
060  }
061
062  @Override
063  public int encode(PositionedByteRange dst, byte[] val) {
064    return OrderedBytes.encodeBlobVar(dst, val, order);
065  }
066
067  /**
068   * Write a subset of {@code val} to {@code dst}.
069   *
070   * @param dst the {@link PositionedByteRange} to write to
071   * @param val  the value to write to {@code dst}
072   * @param voff the offset in {@code dst} where to write {@code val} to
073   * @param vlen the lenght of {@code val}
074   * @return the number of bytes written
075   */
076  public int encode(PositionedByteRange dst, byte[] val, int voff, int vlen) {
077    return OrderedBytes.encodeBlobVar(dst, val, voff, vlen, order);
078  }
079}