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 org.apache.hadoop.classification.InterfaceAudience;
22  import org.apache.hadoop.classification.InterfaceStability;
23  import org.apache.hadoop.hbase.KeyValue;
24  import org.apache.hadoop.hbase.coprocessor.ColumnInterpreter;
25  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.EmptyMsg;
26  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.LongMsg;
27  import org.apache.hadoop.hbase.util.Bytes;
28  
29  import java.io.IOException;
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.Public
40  @InterfaceStability.Evolving
41  public class LongColumnInterpreter extends ColumnInterpreter<Long, Long,
42                   EmptyMsg, LongMsg, LongMsg> {
43  
44    public Long getValue(byte[] colFamily, byte[] colQualifier, KeyValue kv)
45        throws IOException {
46      if (kv == null || kv.getValueLength() != Bytes.SIZEOF_LONG)
47        return null;
48      return Bytes.toLong(kv.getBuffer(), kv.getValueOffset());
49    }
50  
51     @Override
52    public Long add(Long l1, Long l2) {
53      if (l1 == null ^ l2 == null) {
54        return (l1 == null) ? l2 : l1; // either of one is null.
55      } else if (l1 == null) // both are null
56        return null;
57      return l1 + l2;
58    }
59  
60    @Override
61    public int compare(final Long l1, final Long l2) {
62      if (l1 == null ^ l2 == null) {
63        return l1 == null ? -1 : 1; // either of one is null.
64      } else if (l1 == null)
65        return 0; // both are null
66      return l1.compareTo(l2); // natural ordering.
67    }
68  
69    @Override
70    public Long getMaxValue() {
71      return Long.MAX_VALUE;
72    }
73  
74    @Override
75    public Long increment(Long o) {
76      return o == null ? null : (o + 1l);
77    }
78  
79    @Override
80    public Long multiply(Long l1, Long l2) {
81      return (l1 == null || l2 == null) ? null : l1 * l2;
82    }
83  
84    @Override
85    public Long getMinValue() {
86      return Long.MIN_VALUE;
87    }
88  
89    @Override
90    public double divideForAvg(Long l1, Long l2) {
91      return (l2 == null || l1 == null) ? Double.NaN : (l1.doubleValue() / l2
92          .doubleValue());
93    }
94  
95    @Override
96    public Long castToReturnType(Long o) {
97      return o;
98    }
99  
100   @Override
101   public Long castToCellType(Long l) {
102     return l;
103   }
104 
105   @Override
106   public EmptyMsg getRequestData() {
107     return EmptyMsg.getDefaultInstance();
108   }
109 
110   @Override
111   public void initialize(EmptyMsg msg) {
112     //nothing 
113   }
114 
115   @Override
116   public LongMsg getProtoForCellType(Long t) {
117     LongMsg.Builder builder = LongMsg.newBuilder();
118     return builder.setLongMsg(t).build();
119   }
120 
121   @Override
122   public LongMsg getProtoForPromotedType(Long s) {
123     LongMsg.Builder builder = LongMsg.newBuilder();
124     return builder.setLongMsg(s).build();
125   }
126 
127   @Override
128   public Long getPromotedValueFromProto(LongMsg r) {
129     return r.getLongMsg();
130   }
131 
132   @Override
133   public Long getCellValueFromProto(LongMsg q) {
134     return q.getLongMsg();
135   }
136 }