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.client.coprocessor; 019 020import java.io.IOException; 021import org.apache.hadoop.hbase.Cell; 022import org.apache.hadoop.hbase.HBaseInterfaceAudience; 023import org.apache.hadoop.hbase.PrivateCellUtil; 024import org.apache.hadoop.hbase.coprocessor.ColumnInterpreter; 025import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.EmptyMsg; 026import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.LongMsg; 027import org.apache.hadoop.hbase.util.Bytes; 028import org.apache.yetus.audience.InterfaceAudience; 029import org.apache.yetus.audience.InterfaceStability; 030 031/** 032 * a concrete column interpreter implementation. The cell value is a Long value and its promoted 033 * data type is also a Long value. For computing aggregation function, this class is used to find 034 * the datatype of the cell value. Client is supposed to instantiate it and passed along as a 035 * parameter. See TestAggregateProtocol methods for its sample usage. Its methods handle null 036 * arguments gracefully. 037 */ 038@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC) 039@InterfaceStability.Evolving 040public class LongColumnInterpreter 041 extends ColumnInterpreter<Long, Long, EmptyMsg, LongMsg, LongMsg> { 042 043 @Override 044 public Long getValue(byte[] colFamily, byte[] colQualifier, Cell kv) throws IOException { 045 if (kv == null || kv.getValueLength() != Bytes.SIZEOF_LONG) return null; 046 return PrivateCellUtil.getValueAsLong(kv); 047 } 048 049 @Override 050 public Long add(Long l1, Long l2) { 051 if (l1 == null ^ l2 == null) { 052 return (l1 == null) ? l2 : l1; // either of one is null. 053 } else if (l1 == null) // both are null 054 return null; 055 return l1 + l2; 056 } 057 058 @Override 059 public int compare(final Long l1, final Long l2) { 060 if (l1 == null ^ l2 == null) { 061 return l1 == null ? -1 : 1; // either of one is null. 062 } else if (l1 == null) return 0; // both are null 063 return l1.compareTo(l2); // natural ordering. 064 } 065 066 @Override 067 public Long getMaxValue() { 068 return Long.MAX_VALUE; 069 } 070 071 @Override 072 public Long increment(Long o) { 073 return o == null ? null : (o + 1l); 074 } 075 076 @Override 077 public Long multiply(Long l1, Long l2) { 078 return (l1 == null || l2 == null) ? null : l1 * l2; 079 } 080 081 @Override 082 public Long getMinValue() { 083 return Long.MIN_VALUE; 084 } 085 086 @Override 087 public double divideForAvg(Long l1, Long l2) { 088 return (l2 == null || l1 == null) ? Double.NaN : (l1.doubleValue() / l2.doubleValue()); 089 } 090 091 @Override 092 public Long castToReturnType(Long o) { 093 return o; 094 } 095 096 @Override 097 public Long castToCellType(Long l) { 098 return l; 099 } 100 101 @Override 102 public EmptyMsg getRequestData() { 103 return EmptyMsg.getDefaultInstance(); 104 } 105 106 @Override 107 public void initialize(EmptyMsg msg) { 108 // nothing 109 } 110 111 @Override 112 public LongMsg getProtoForCellType(Long t) { 113 LongMsg.Builder builder = LongMsg.newBuilder(); 114 return builder.setLongMsg(t).build(); 115 } 116 117 @Override 118 public LongMsg getProtoForPromotedType(Long s) { 119 LongMsg.Builder builder = LongMsg.newBuilder(); 120 return builder.setLongMsg(s).build(); 121 } 122 123 @Override 124 public Long getPromotedValueFromProto(LongMsg r) { 125 return r.getLongMsg(); 126 } 127 128 @Override 129 public Long getCellValueFromProto(LongMsg q) { 130 return q.getLongMsg(); 131 } 132}