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.classification.InterfaceAudience;
24  import org.apache.hadoop.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.KeyValue;
27  import org.apache.hadoop.hbase.coprocessor.ColumnInterpreter;
28  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.EmptyMsg;
29  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.DoubleMsg;
30  import org.apache.hadoop.hbase.util.Bytes;
31  
32  /**
33   * a concrete column interpreter implementation. The cell value is a Double value
34   * and its promoted data type is also a Double value. For computing aggregation
35   * function, this class is used to find the datatype of the cell value. Client
36   * is supposed to instantiate it and passed along as a parameter. See
37   * TestDoubleColumnInterpreter methods for its sample usage.
38   * Its methods handle null arguments gracefully. 
39   */
40  @InterfaceAudience.Public
41  @InterfaceStability.Evolving
42  public class DoubleColumnInterpreter extends ColumnInterpreter<Double, Double, 
43        EmptyMsg, DoubleMsg, DoubleMsg>{
44   
45    @Override
46    public Double getValue(byte[] colFamily, byte[] colQualifier, Cell c)
47        throws IOException {
48      if (c == null || c.getValueLength() != Bytes.SIZEOF_DOUBLE)
49        return null;
50      return Bytes.toDouble(c.getValueArray(), c.getValueOffset());
51    }
52  
53    @Override
54    public Double add(Double d1, Double d2) {
55      if (d1 == null || d2 == null) {
56        return (d1 == null) ? d2 : d1; 
57      }
58      return d1 + d2;
59    }
60  
61    @Override
62    public int compare(final Double d1, final Double d2) {
63      if (d1 == null ^ d2 == null) {
64        return d1 == null ? -1 : 1; // either of one is null.
65      } else if (d1 == null)
66        return 0; // both are null
67      return d1.compareTo(d2); // natural ordering.
68    }
69  
70    @Override
71    public Double getMaxValue() {
72      return Double.MAX_VALUE;
73    }
74  
75    @Override
76    public Double increment(Double o) {
77      return o == null ? null : (o + 1.00d);
78    }
79  
80    @Override
81    public Double multiply(Double d1, Double d2) {
82      return (d1 == null || d2 == null) ? null : d1 * d2;
83    }
84  
85    @Override
86    public Double getMinValue() {
87      return Double.MIN_VALUE;
88    }
89  
90    @Override
91    public double divideForAvg(Double d1, Long l2) {
92      return (l2 == null || d1 == null) ? Double.NaN : (d1.doubleValue() / l2
93          .doubleValue());
94    }
95  
96    @Override
97    public Double castToReturnType(Double o) {
98      return o;
99    }
100 
101   @Override
102   public Double castToCellType(Double d) {
103     return d;
104   }
105 
106   @Override
107   public EmptyMsg getRequestData() {
108     return EmptyMsg.getDefaultInstance();
109   }
110 
111   @Override
112   public void initialize(EmptyMsg msg) {
113     //nothing 
114   }
115 
116   @Override
117   public DoubleMsg getProtoForCellType(Double t) {
118     DoubleMsg.Builder builder = DoubleMsg.newBuilder();
119     return builder.setDoubleMsg(t).build();
120   }
121 
122   @Override
123   public DoubleMsg getProtoForPromotedType(Double s) {
124     DoubleMsg.Builder builder = DoubleMsg.newBuilder();
125     return builder.setDoubleMsg(s).build();
126   }
127 
128   @Override
129   public Double getPromotedValueFromProto(DoubleMsg r) {
130     return r.getDoubleMsg();
131   }
132 
133   @Override
134   public Double getCellValueFromProto(DoubleMsg q) {
135     return q.getDoubleMsg();
136   }
137 }