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