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 */
018
019package org.apache.hadoop.hbase.filter;
020
021import java.nio.ByteBuffer;
022
023import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
024
025import org.apache.yetus.audience.InterfaceAudience;
026import org.apache.hadoop.hbase.exceptions.DeserializationException;
027import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
028import org.apache.hadoop.hbase.shaded.protobuf.generated.ComparatorProtos;
029import org.apache.hadoop.hbase.util.ByteBufferUtils;
030import org.apache.hadoop.hbase.util.Bytes;
031
032
033/**
034 * A long comparator which numerical compares against the specified byte array
035 */
036@InterfaceAudience.Public
037@SuppressWarnings("ComparableType") // Should this move to Comparator usage?
038public class LongComparator extends ByteArrayComparable {
039  private long longValue;
040
041  public LongComparator(long value) {
042    super(Bytes.toBytes(value));
043    this.longValue = value;
044  }
045
046  @Override
047  public int compareTo(byte[] value, int offset, int length) {
048    long that = Bytes.toLong(value, offset, length);
049    return Long.compare(longValue, that);
050  }
051
052  @Override
053  public int compareTo(ByteBuffer value, int offset, int length) {
054    long that = ByteBufferUtils.toLong(value, offset);
055    return Long.compare(longValue, that);
056  }
057
058    /**
059     * @return The comparator serialized using pb
060     */
061    @Override
062    public byte [] toByteArray() {
063        ComparatorProtos.LongComparator.Builder builder =
064                ComparatorProtos.LongComparator.newBuilder();
065        builder.setComparable(ProtobufUtil.toByteArrayComparable(this.value));
066        return builder.build().toByteArray();
067    }
068
069    /**
070     * @param pbBytes A pb serialized {@link LongComparator} instance
071     * @return An instance of {@link LongComparator} made from <code>bytes</code>
072     * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
073     * @see #toByteArray
074     */
075    public static LongComparator parseFrom(final byte [] pbBytes)
076            throws DeserializationException {
077        ComparatorProtos.LongComparator proto;
078        try {
079            proto = ComparatorProtos.LongComparator.parseFrom(pbBytes);
080        } catch (InvalidProtocolBufferException e) {
081            throw new DeserializationException(e);
082        }
083        return new LongComparator(Bytes.toLong(proto.getComparable().getValue().toByteArray()));
084    }
085
086    /**
087     * @param other
088     * @return true if and only if the fields of the comparator that are serialized
089     * are equal to the corresponding fields in other.  Used for testing.
090     */
091    boolean areSerializedFieldsEqual(LongComparator other) {
092        if (other == this) return true;
093        return super.areSerializedFieldsEqual(other);
094    }
095}