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  import java.math.BigDecimal;
23  import java.math.RoundingMode;
24  
25  import org.apache.hadoop.hbase.util.ByteStringer;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.classification.InterfaceStability;
28  import org.apache.hadoop.hbase.Cell;
29  import org.apache.hadoop.hbase.CellUtil;
30  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
31  import org.apache.hadoop.hbase.coprocessor.ColumnInterpreter;
32  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.BigDecimalMsg;
33  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.EmptyMsg;
34  import org.apache.hadoop.hbase.util.Bytes;
35  
36  /**
37   * ColumnInterpreter for doing Aggregation's with BigDecimal columns. This class
38   * is required at the RegionServer also.
39   *
40   */
41  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
42  @InterfaceStability.Evolving
43  public class BigDecimalColumnInterpreter extends ColumnInterpreter<BigDecimal, BigDecimal,
44    EmptyMsg, BigDecimalMsg, BigDecimalMsg> {
45  
46    @Override
47    public BigDecimal getValue(byte[] colFamily, byte[] colQualifier, Cell kv)
48        throws IOException {
49      if (kv == null || CellUtil.cloneValue(kv) == null) {
50        return null;
51      }
52      return Bytes.toBigDecimal(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength()).
53          setScale(2, RoundingMode.HALF_EVEN);
54    }
55  
56    @Override
57    public BigDecimal add(BigDecimal bd1, BigDecimal bd2) {
58      if (bd1 == null ^ bd2 == null) {
59        return (bd1 == null) ? bd2 : bd1; // either of one is null.
60      }
61      if (bd1 == null) {
62        return null;
63      }
64      return bd1.add(bd2);
65    }
66  
67    @Override
68    public int compare(final BigDecimal bd1, final BigDecimal bd2) {
69      if (bd1 == null ^ bd2 == null) {
70        return bd1 == null ? -1 : 1; // either of one is null.
71      }
72      if (bd1 == null) {
73        return 0; // both are null
74      }
75      return bd1.compareTo(bd2); // natural ordering.
76    }
77  
78    @Override
79    public BigDecimal getMaxValue() {
80      return BigDecimal.valueOf(Double.MAX_VALUE);
81    }
82  
83    @Override
84    public BigDecimal increment(BigDecimal bd) {
85      return bd == null ? null : (bd.add(BigDecimal.ONE));
86    }
87  
88    @Override
89    public BigDecimal multiply(BigDecimal bd1, BigDecimal bd2) {
90      return (bd1 == null || bd2 == null) ? null : bd1.multiply(bd2)
91          .setScale(2,RoundingMode.HALF_EVEN);
92    }
93  
94    @Override
95    public BigDecimal getMinValue() {
96      return BigDecimal.valueOf(Double.MIN_VALUE);
97    }
98  
99    @Override
100   public double divideForAvg(BigDecimal bd1, Long l2) {
101     return (l2 == null || bd1 == null) ? Double.NaN : (bd1.doubleValue() / l2
102         .doubleValue());
103   }
104 
105   @Override
106   public BigDecimal castToReturnType(BigDecimal bd) {
107     return bd;
108   }
109 
110   @Override
111   public BigDecimal castToCellType(BigDecimal bd) {
112     return bd;
113   }
114 
115   @Override
116   public EmptyMsg getRequestData() {
117     return EmptyMsg.getDefaultInstance();
118   }
119 
120   @Override
121   public void initialize(EmptyMsg msg) {
122     //nothing
123   }
124 
125   private BigDecimalMsg getProtoForType(BigDecimal t) {
126     BigDecimalMsg.Builder builder = BigDecimalMsg.newBuilder();
127     return builder.setBigdecimalMsg(ByteStringer.wrap(Bytes.toBytes(t))).build();
128   }
129   
130   @Override
131   public BigDecimalMsg getProtoForCellType(BigDecimal t) {
132     return getProtoForType(t);
133   }
134 
135   @Override
136   public BigDecimalMsg getProtoForPromotedType(BigDecimal s) {
137     return getProtoForType(s);
138   }
139 
140   @Override
141   public BigDecimal getPromotedValueFromProto(BigDecimalMsg r) {
142     return Bytes.toBigDecimal(r.getBigdecimalMsg().toByteArray());
143   }
144 
145   @Override
146   public BigDecimal getCellValueFromProto(BigDecimalMsg q) {
147     return Bytes.toBigDecimal(q.getBigdecimalMsg().toByteArray());
148   }
149 }