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