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.DoubleMsg;
030import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.EmptyMsg;
031
032/**
033 * a concrete column interpreter implementation. The cell value is a Double value and its promoted
034 * data type is also a Double 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 TestDoubleColumnInterpreter methods for its sample usage. Its methods handle null
037 * arguments gracefully.
038 */
039@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
040@InterfaceStability.Evolving
041public class DoubleColumnInterpreter
042  extends ColumnInterpreter<Double, Double, EmptyMsg, DoubleMsg, DoubleMsg> {
043
044  @Override
045  public Double getValue(byte[] colFamily, byte[] colQualifier, Cell c) throws IOException {
046    if (c == null || c.getValueLength() != Bytes.SIZEOF_DOUBLE) return null;
047    return PrivateCellUtil.getValueAsDouble(c);
048  }
049
050  @Override
051  public Double add(Double d1, Double d2) {
052    if (d1 == null || d2 == null) {
053      return (d1 == null) ? d2 : d1;
054    }
055    return d1 + d2;
056  }
057
058  @Override
059  public int compare(final Double d1, final Double d2) {
060    if (d1 == null ^ d2 == null) {
061      return d1 == null ? -1 : 1; // either of one is null.
062    } else if (d1 == null) return 0; // both are null
063    return d1.compareTo(d2); // natural ordering.
064  }
065
066  @Override
067  public Double getMaxValue() {
068    return Double.MAX_VALUE;
069  }
070
071  @Override
072  public Double increment(Double o) {
073    return o == null ? null : (o + 1.00d);
074  }
075
076  @Override
077  public Double multiply(Double d1, Double d2) {
078    return (d1 == null || d2 == null) ? null : d1 * d2;
079  }
080
081  @Override
082  public Double getMinValue() {
083    return Double.MIN_VALUE;
084  }
085
086  @Override
087  public double divideForAvg(Double d1, Long l2) {
088    return (l2 == null || d1 == null) ? Double.NaN : (d1.doubleValue() / l2.doubleValue());
089  }
090
091  @Override
092  public Double castToReturnType(Double o) {
093    return o;
094  }
095
096  @Override
097  public Double castToCellType(Double d) {
098    return d;
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 DoubleMsg getProtoForCellType(Double t) {
113    DoubleMsg.Builder builder = DoubleMsg.newBuilder();
114    return builder.setDoubleMsg(t).build();
115  }
116
117  @Override
118  public DoubleMsg getProtoForPromotedType(Double s) {
119    DoubleMsg.Builder builder = DoubleMsg.newBuilder();
120    return builder.setDoubleMsg(s).build();
121  }
122
123  @Override
124  public Double getPromotedValueFromProto(DoubleMsg r) {
125    return r.getDoubleMsg();
126  }
127
128  @Override
129  public Double getCellValueFromProto(DoubleMsg q) {
130    return q.getDoubleMsg();
131  }
132}