Return-Path: X-Original-To: apmail-hbase-commits-archive@www.apache.org Delivered-To: apmail-hbase-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 69F83107EF for ; Mon, 8 Apr 2013 20:57:56 +0000 (UTC) Received: (qmail 44154 invoked by uid 500); 8 Apr 2013 20:57:56 -0000 Delivered-To: apmail-hbase-commits-archive@hbase.apache.org Received: (qmail 44067 invoked by uid 500); 8 Apr 2013 20:57:56 -0000 Mailing-List: contact commits-help@hbase.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@hbase.apache.org Delivered-To: mailing list commits@hbase.apache.org Received: (qmail 44045 invoked by uid 99); 8 Apr 2013 20:57:56 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Apr 2013 20:57:56 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Apr 2013 20:57:54 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id C103823888CD; Mon, 8 Apr 2013 20:57:34 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1465782 - in /hbase/trunk: hbase-client/src/main/java/org/apache/hadoop/hbase/client/coprocessor/BigDecimalColumnInterpreter.java hbase-server/src/main/java/org/apache/hadoop/hbase/client/coprocessor/BigDecimalColumnInterpreter.java Date: Mon, 08 Apr 2013 20:57:34 -0000 To: commits@hbase.apache.org From: eclark@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130408205734.C103823888CD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: eclark Date: Mon Apr 8 20:57:34 2013 New Revision: 1465782 URL: http://svn.apache.org/r1465782 Log: HBASE-8293 Move BigDecimalColumnInterpreter into hbase-client Added: hbase/trunk/hbase-client/src/main/java/org/apache/hadoop/hbase/client/coprocessor/BigDecimalColumnInterpreter.java Removed: hbase/trunk/hbase-server/src/main/java/org/apache/hadoop/hbase/client/coprocessor/BigDecimalColumnInterpreter.java Added: hbase/trunk/hbase-client/src/main/java/org/apache/hadoop/hbase/client/coprocessor/BigDecimalColumnInterpreter.java URL: http://svn.apache.org/viewvc/hbase/trunk/hbase-client/src/main/java/org/apache/hadoop/hbase/client/coprocessor/BigDecimalColumnInterpreter.java?rev=1465782&view=auto ============================================================================== --- hbase/trunk/hbase-client/src/main/java/org/apache/hadoop/hbase/client/coprocessor/BigDecimalColumnInterpreter.java (added) +++ hbase/trunk/hbase-client/src/main/java/org/apache/hadoop/hbase/client/coprocessor/BigDecimalColumnInterpreter.java Mon Apr 8 20:57:34 2013 @@ -0,0 +1,147 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.client.coprocessor; + +import java.io.IOException; +import java.math.BigDecimal; +import java.math.RoundingMode; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.KeyValue; +import org.apache.hadoop.hbase.coprocessor.ColumnInterpreter; +import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.BigDecimalMsg; +import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.EmptyMsg; +import org.apache.hadoop.hbase.util.Bytes; + +import com.google.protobuf.ByteString; + +/** + * ColumnInterpreter for doing Aggregation's with BigDecimal columns. This class + * is required at the RegionServer also. + * + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class BigDecimalColumnInterpreter extends ColumnInterpreter { + + @Override + public BigDecimal getValue(byte[] colFamily, byte[] colQualifier, KeyValue kv) + throws IOException { + if (kv == null || kv.getValue() == null) { + return null; + } + return Bytes.toBigDecimal(kv.getValue()).setScale(2, RoundingMode.HALF_EVEN); + } + + @Override + public BigDecimal add(BigDecimal bd1, BigDecimal bd2) { + if (bd1 == null ^ bd2 == null) { + return (bd1 == null) ? bd2 : bd1; // either of one is null. + } + if (bd1 == null) { + return null; + } + return bd1.add(bd2); + } + + @Override + public int compare(final BigDecimal bd1, final BigDecimal bd2) { + if (bd1 == null ^ bd2 == null) { + return bd1 == null ? -1 : 1; // either of one is null. + } + if (bd1 == null) { + return 0; // both are null + } + return bd1.compareTo(bd2); // natural ordering. + } + + @Override + public BigDecimal getMaxValue() { + return BigDecimal.valueOf(Double.MAX_VALUE); + } + + @Override + public BigDecimal increment(BigDecimal bd) { + return bd == null ? null : (bd.add(BigDecimal.ONE)); + } + + @Override + public BigDecimal multiply(BigDecimal bd1, BigDecimal bd2) { + return (bd1 == null || bd2 == null) ? null : bd1.multiply(bd2) + .setScale(2,RoundingMode.HALF_EVEN); + } + + @Override + public BigDecimal getMinValue() { + return BigDecimal.valueOf(Double.MIN_VALUE); + } + + @Override + public double divideForAvg(BigDecimal bd1, Long l2) { + return (l2 == null || bd1 == null) ? Double.NaN : (bd1.doubleValue() / l2 + .doubleValue()); + } + + @Override + public BigDecimal castToReturnType(BigDecimal bd) { + return bd; + } + + @Override + public BigDecimal castToCellType(BigDecimal bd) { + return bd; + } + + @Override + public EmptyMsg getRequestData() { + return EmptyMsg.getDefaultInstance(); + } + + @Override + public void initialize(EmptyMsg msg) { + //nothing + } + + private BigDecimalMsg getProtoForType(BigDecimal t) { + BigDecimalMsg.Builder builder = BigDecimalMsg.newBuilder(); + return builder.setBigdecimalMsg(ByteString.copyFrom(Bytes.toBytes(t))).build(); + } + + @Override + public BigDecimalMsg getProtoForCellType(BigDecimal t) { + return getProtoForType(t); + } + + @Override + public BigDecimalMsg getProtoForPromotedType(BigDecimal s) { + return getProtoForType(s); + } + + @Override + public BigDecimal getPromotedValueFromProto(BigDecimalMsg r) { + return Bytes.toBigDecimal(r.getBigdecimalMsg().toByteArray()); + } + + @Override + public BigDecimal getCellValueFromProto(BigDecimalMsg q) { + return Bytes.toBigDecimal(q.getBigdecimalMsg().toByteArray()); + } +}