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