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