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  
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.Cell;
25  import org.apache.hadoop.hbase.coprocessor.ColumnInterpreter;
26  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.EmptyMsg;
27  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.LongMsg;
28  import org.apache.hadoop.hbase.util.Bytes;
29  
30  /**
31   * a concrete column interpreter implementation. The cell value is a Long value
32   * and its promoted data type is also a Long value. For computing aggregation
33   * function, this class is used to find the datatype of the cell value. Client
34   * is supposed to instantiate it and passed along as a parameter. See
35   * TestAggregateProtocol methods for its sample usage.
36   * Its methods handle null arguments gracefully. 
37   */
38  @InterfaceAudience.Private
39  public class LongColumnInterpreter extends ColumnInterpreter<Long, Long,
40                   EmptyMsg, LongMsg, LongMsg> {
41  
42    public Long getValue(byte[] colFamily, byte[] colQualifier, Cell kv)
43        throws IOException {
44      if (kv == null || kv.getValueLength() != Bytes.SIZEOF_LONG)
45        return null;
46      return Bytes.toLong(kv.getValueArray(), kv.getValueOffset());
47    }
48  
49     @Override
50    public Long add(Long l1, Long l2) {
51      if (l1 == null ^ l2 == null) {
52        return (l1 == null) ? l2 : l1; // either of one is null.
53      } else if (l1 == null) // both are null
54        return null;
55      return l1 + l2;
56    }
57  
58    @Override
59    public int compare(final Long l1, final Long l2) {
60      if (l1 == null ^ l2 == null) {
61        return l1 == null ? -1 : 1; // either of one is null.
62      } else if (l1 == null)
63        return 0; // both are null
64      return l1.compareTo(l2); // natural ordering.
65    }
66  
67    @Override
68    public Long getMaxValue() {
69      return Long.MAX_VALUE;
70    }
71  
72    @Override
73    public Long increment(Long o) {
74      return o == null ? null : (o + 1l);
75    }
76  
77    @Override
78    public Long multiply(Long l1, Long l2) {
79      return (l1 == null || l2 == null) ? null : l1 * l2;
80    }
81  
82    @Override
83    public Long getMinValue() {
84      return Long.MIN_VALUE;
85    }
86  
87    @Override
88    public double divideForAvg(Long l1, Long l2) {
89      return (l2 == null || l1 == null) ? Double.NaN : (l1.doubleValue() / l2
90          .doubleValue());
91    }
92  
93    @Override
94    public Long castToReturnType(Long o) {
95      return o;
96    }
97  
98    @Override
99    public Long castToCellType(Long l) {
100     return l;
101   }
102 
103   @Override
104   public EmptyMsg getRequestData() {
105     return EmptyMsg.getDefaultInstance();
106   }
107 
108   @Override
109   public void initialize(EmptyMsg msg) {
110     //nothing 
111   }
112 
113   @Override
114   public LongMsg getProtoForCellType(Long t) {
115     LongMsg.Builder builder = LongMsg.newBuilder();
116     return builder.setLongMsg(t).build();
117   }
118 
119   @Override
120   public LongMsg getProtoForPromotedType(Long s) {
121     LongMsg.Builder builder = LongMsg.newBuilder();
122     return builder.setLongMsg(s).build();
123   }
124 
125   @Override
126   public Long getPromotedValueFromProto(LongMsg r) {
127     return r.getLongMsg();
128   }
129 
130   @Override
131   public Long getCellValueFromProto(LongMsg q) {
132     return q.getLongMsg();
133   }
134 }