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