View Javadoc

1   /*
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.client.coprocessor;
21  
22  import java.io.DataInput;
23  import java.io.DataOutput;
24  import java.io.IOException;
25  
26  import org.apache.hadoop.hbase.KeyValue;
27  import org.apache.hadoop.hbase.coprocessor.ColumnInterpreter;
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  public class LongColumnInterpreter implements ColumnInterpreter<Long, Long> {
39  
40    public Long getValue(byte[] colFamily, byte[] colQualifier, KeyValue kv)
41        throws IOException {
42      if (kv == null || kv.getValueLength() != Bytes.SIZEOF_LONG)
43        return null;
44      return Bytes.toLong(kv.getBuffer(), kv.getValueOffset());
45    }
46  
47     @Override
48    public Long add(Long l1, Long l2) {
49      if (l1 == null ^ l2 == null) {
50        return (l1 == null) ? l2 : l1; // either of one is null.
51      } else if (l1 == null) // both are null
52        return null;
53      return l1 + l2;
54    }
55  
56    @Override
57    public int compare(final Long l1, final Long l2) {
58      if (l1 == null ^ l2 == null) {
59        return l1 == null ? -1 : 1; // either of one is null.
60      } else if (l1 == null)
61        return 0; // both are null
62      return l1.compareTo(l2); // natural ordering.
63    }
64  
65    @Override
66    public Long getMaxValue() {
67      return Long.MAX_VALUE;
68    }
69  
70    @Override
71    public Long increment(Long o) {
72      return o == null ? null : (o + 1l);
73    }
74  
75    @Override
76    public Long multiply(Long l1, Long l2) {
77      return (l1 == null || l2 == null) ? null : l1 * l2;
78    }
79  
80    @Override
81    public Long getMinValue() {
82      return Long.MIN_VALUE;
83    }
84  
85    @Override
86    public void readFields(DataInput arg0) throws IOException {
87      // nothing to serialize
88    }
89  
90    @Override
91    public void write(DataOutput arg0) throws IOException {
92       // nothing to serialize
93    }
94  
95    @Override
96    public double divideForAvg(Long l1, Long l2) {
97      return (l2 == null || l1 == null) ? Double.NaN : (l1.doubleValue() / l2
98          .doubleValue());
99    }
100 
101   @Override
102   public Long castToReturnType(Long o) {
103     return o;
104   }
105 
106 }