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 values encoded using
027 * {@link Bytes#putLong(byte[], int, long)}. Intended to make it easier to transition away from
028 * direct use of {@link Bytes}.
029 * @see Bytes#putLong(byte[], int, long)
030 * @see Bytes#toLong(byte[])
031 */
032@InterfaceAudience.Public
033public class RawLong implements DataType<Long> {
034
035  @Override
036  public boolean isOrderPreserving() {
037    return false;
038  }
039
040  @Override
041  public Order getOrder() {
042    return null;
043  }
044
045  @Override
046  public boolean isNullable() {
047    return false;
048  }
049
050  @Override
051  public boolean isSkippable() {
052    return true;
053  }
054
055  @Override
056  public int encodedLength(Long val) {
057    return Bytes.SIZEOF_LONG;
058  }
059
060  @Override
061  public Class<Long> encodedClass() {
062    return Long.class;
063  }
064
065  @Override
066  public int skip(PositionedByteRange src) {
067    src.setPosition(src.getPosition() + Bytes.SIZEOF_LONG);
068    return Bytes.SIZEOF_LONG;
069  }
070
071  @Override
072  public Long decode(PositionedByteRange src) {
073    long val = Bytes.toLong(src.getBytes(), src.getOffset() + src.getPosition());
074    skip(src);
075    return val;
076  }
077
078  @Override
079  public int encode(PositionedByteRange dst, Long val) {
080    Bytes.putLong(dst.getBytes(), dst.getOffset() + dst.getPosition(), val);
081    return skip(dst);
082  }
083
084  /**
085   * Read a {@code long} value from the buffer {@code buff}.
086   */
087  public long decodeLong(byte[] buff, int offset) {
088    return Bytes.toLong(buff, offset);
089  }
090
091  /**
092   * Write instance {@code val} into buffer {@code buff}.
093   */
094  public int encodeLong(byte[] buff, int offset, long val) {
095    return Bytes.putLong(buff, offset, val);
096  }
097}