View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.client.coprocessor;
20  
21  import java.io.IOException;
22  
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.coprocessor.ColumnInterpreter;
27  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.EmptyMsg;
28  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.DoubleMsg;
29  import org.apache.hadoop.hbase.util.Bytes;
30  
31  /**
32   * a concrete column interpreter implementation. The cell value is a Double value
33   * and its promoted data type is also a Double value. For computing aggregation
34   * function, this class is used to find the datatype of the cell value. Client
35   * is supposed to instantiate it and passed along as a parameter. See
36   * TestDoubleColumnInterpreter methods for its sample usage.
37   * Its methods handle null arguments gracefully. 
38   */
39  @InterfaceAudience.Public
40  @InterfaceStability.Evolving
41  public class DoubleColumnInterpreter extends ColumnInterpreter<Double, Double, 
42        EmptyMsg, DoubleMsg, DoubleMsg>{
43   
44    @Override
45    public Double getValue(byte[] colFamily, byte[] colQualifier, Cell c)
46        throws IOException {
47      if (c == null || c.getValueLength() != Bytes.SIZEOF_DOUBLE)
48        return null;
49      return Bytes.toDouble(c.getValueArray(), c.getValueOffset());
50    }
51  
52    @Override
53    public Double add(Double d1, Double d2) {
54      if (d1 == null || d2 == null) {
55        return (d1 == null) ? d2 : d1; 
56      }
57      return d1 + d2;
58    }
59  
60    @Override
61    public int compare(final Double d1, final Double d2) {
62      if (d1 == null ^ d2 == null) {
63        return d1 == null ? -1 : 1; // either of one is null.
64      } else if (d1 == null)
65        return 0; // both are null
66      return d1.compareTo(d2); // natural ordering.
67    }
68  
69    @Override
70    public Double getMaxValue() {
71      return Double.MAX_VALUE;
72    }
73  
74    @Override
75    public Double increment(Double o) {
76      return o == null ? null : (o + 1.00d);
77    }
78  
79    @Override
80    public Double multiply(Double d1, Double d2) {
81      return (d1 == null || d2 == null) ? null : d1 * d2;
82    }
83  
84    @Override
85    public Double getMinValue() {
86      return Double.MIN_VALUE;
87    }
88  
89    @Override
90    public double divideForAvg(Double d1, Long l2) {
91      return (l2 == null || d1 == null) ? Double.NaN : (d1.doubleValue() / l2
92          .doubleValue());
93    }
94  
95    @Override
96    public Double castToReturnType(Double o) {
97      return o;
98    }
99  
100   @Override
101   public Double castToCellType(Double d) {
102     return d;
103   }
104 
105   @Override
106   public EmptyMsg getRequestData() {
107     return EmptyMsg.getDefaultInstance();
108   }
109 
110   @Override
111   public void initialize(EmptyMsg msg) {
112     //nothing 
113   }
114 
115   @Override
116   public DoubleMsg getProtoForCellType(Double t) {
117     DoubleMsg.Builder builder = DoubleMsg.newBuilder();
118     return builder.setDoubleMsg(t).build();
119   }
120 
121   @Override
122   public DoubleMsg getProtoForPromotedType(Double s) {
123     DoubleMsg.Builder builder = DoubleMsg.newBuilder();
124     return builder.setDoubleMsg(s).build();
125   }
126 
127   @Override
128   public Double getPromotedValueFromProto(DoubleMsg r) {
129     return r.getDoubleMsg();
130   }
131 
132   @Override
133   public Double getCellValueFromProto(DoubleMsg q) {
134     return q.getDoubleMsg();
135   }
136 }