001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.client.coprocessor;
020
021import java.io.IOException;
022
023import org.apache.hadoop.hbase.Cell;
024import org.apache.hadoop.hbase.HBaseInterfaceAudience;
025import org.apache.hadoop.hbase.PrivateCellUtil;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.yetus.audience.InterfaceStability;
028import org.apache.hadoop.hbase.coprocessor.ColumnInterpreter;
029import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.EmptyMsg;
030import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.LongMsg;
031import org.apache.hadoop.hbase.util.Bytes;
032
033/**
034 * a concrete column interpreter implementation. The cell value is a Long value
035 * and its promoted data type is also a Long value. For computing aggregation
036 * function, this class is used to find the datatype of the cell value. Client
037 * is supposed to instantiate it and passed along as a parameter. See
038 * TestAggregateProtocol methods for its sample usage.
039 * Its methods handle null arguments gracefully. 
040 */
041@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
042@InterfaceStability.Evolving
043public class LongColumnInterpreter extends ColumnInterpreter<Long, Long,
044                 EmptyMsg, LongMsg, LongMsg> {
045
046  @Override
047  public Long getValue(byte[] colFamily, byte[] colQualifier, Cell kv)
048      throws IOException {
049    if (kv == null || kv.getValueLength() != Bytes.SIZEOF_LONG)
050      return null;
051    return PrivateCellUtil.getValueAsLong(kv);
052  }
053
054  @Override
055  public Long add(Long l1, Long l2) {
056    if (l1 == null ^ l2 == null) {
057      return (l1 == null) ? l2 : l1; // either of one is null.
058    } else if (l1 == null) // both are null
059      return null;
060    return l1 + l2;
061  }
062
063  @Override
064  public int compare(final Long l1, final Long l2) {
065    if (l1 == null ^ l2 == null) {
066      return l1 == null ? -1 : 1; // either of one is null.
067    } else if (l1 == null)
068      return 0; // both are null
069    return l1.compareTo(l2); // natural ordering.
070  }
071
072  @Override
073  public Long getMaxValue() {
074    return Long.MAX_VALUE;
075  }
076
077  @Override
078  public Long increment(Long o) {
079    return o == null ? null : (o + 1l);
080  }
081
082  @Override
083  public Long multiply(Long l1, Long l2) {
084    return (l1 == null || l2 == null) ? null : l1 * l2;
085  }
086
087  @Override
088  public Long getMinValue() {
089    return Long.MIN_VALUE;
090  }
091
092  @Override
093  public double divideForAvg(Long l1, Long l2) {
094    return (l2 == null || l1 == null) ? Double.NaN : (l1.doubleValue() / l2
095        .doubleValue());
096  }
097
098  @Override
099  public Long castToReturnType(Long o) {
100    return o;
101  }
102
103  @Override
104  public Long castToCellType(Long l) {
105    return l;
106  }
107
108  @Override
109  public EmptyMsg getRequestData() {
110    return EmptyMsg.getDefaultInstance();
111  }
112
113  @Override
114  public void initialize(EmptyMsg msg) {
115    //nothing 
116  }
117
118  @Override
119  public LongMsg getProtoForCellType(Long t) {
120    LongMsg.Builder builder = LongMsg.newBuilder();
121    return builder.setLongMsg(t).build();
122  }
123
124  @Override
125  public LongMsg getProtoForPromotedType(Long s) {
126    LongMsg.Builder builder = LongMsg.newBuilder();
127    return builder.setLongMsg(s).build();
128  }
129
130  @Override
131  public Long getPromotedValueFromProto(LongMsg r) {
132    return r.getLongMsg();
133  }
134
135  @Override
136  public Long getCellValueFromProto(LongMsg q) {
137    return q.getLongMsg();
138  }
139}