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