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