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