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  
20  package org.apache.hadoop.hbase.coprocessor;
21  
22  import java.io.IOException;
23  
24  import org.apache.hadoop.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.KeyValue;
27  import org.apache.hadoop.hbase.KeyValueUtil;
28  import org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter;
29  
30  import com.google.protobuf.Message;
31  
32  /**
33   * Defines how value for specific column is interpreted and provides utility
34   * methods like compare, add, multiply etc for them. Takes column family, column
35   * qualifier and return the cell value. Its concrete implementation should
36   * handle null case gracefully. Refer to {@link LongColumnInterpreter} for an
37   * example.
38   * <p>
39   * Takes two generic parameters and three Message parameters. 
40   * The cell value type of the interpreter is <T>.
41   * During some computations like sum, average, the return type can be different
42   * than the cell value data type, for eg, sum of int cell values might overflow
43   * in case of a int result, we should use Long for its result. Therefore, this
44   * class mandates to use a different (promoted) data type for result of these
45   * computations <S>. All computations are performed on the promoted data type
46   * <S>. There is a conversion method
47   * {@link ColumnInterpreter#castToReturnType(Object)} which takes a <T> type and
48   * returns a <S> type.
49   * The AggregateImplementation uses PB messages to initialize the
50   * user's ColumnInterpreter implementation, and for sending the responses
51   * back to AggregationClient.
52   * @param <T> Cell value data type
53   * @param <S> Promoted data type
54   * @param <P> PB message that is used to transport initializer specific bytes
55   * @param <Q> PB message that is used to transport Cell (<T>) instance
56   * @param <R> PB message that is used to transport Promoted (<S>) instance
57   */
58  @InterfaceAudience.Private
59  public abstract class ColumnInterpreter<T, S, P extends Message, 
60  Q extends Message, R extends Message> {
61  
62    /**
63     * 
64     * @param colFamily
65     * @param colQualifier
66     * @param c
67     * @return value of type T
68     * @throws IOException
69     */
70    public abstract T getValue(byte[] colFamily, byte[] colQualifier, Cell c)
71        throws IOException;
72  
73    /**
74     * @param l1
75     * @param l2
76     * @return sum or non null value among (if either of them is null); otherwise
77     * returns a null.
78     */
79    public abstract S add(S l1, S l2);
80  
81    /**
82     * returns the maximum value for this type T
83     * @return max
84     */
85  
86    public abstract T getMaxValue();
87  
88    public abstract T getMinValue();
89  
90    /**
91     * @param o1
92     * @param o2
93     * @return multiplication
94     */
95    public abstract S multiply(S o1, S o2);
96  
97    /**
98     * @param o
99     * @return increment
100    */
101   public abstract S increment(S o);
102 
103   /**
104    * provides casting opportunity between the data types.
105    * @param o
106    * @return cast
107    */
108   public abstract S castToReturnType(T o);
109 
110   /**
111    * This takes care if either of arguments are null. returns 0 if they are
112    * equal or both are null;
113    * <ul>
114    * <li>>0 if l1 > l2 or l1 is not null and l2 is null.
115    * <li>< 0 if l1 < l2 or l1 is null and l2 is not null.
116    */
117   public abstract int compare(final T l1, final T l2);
118 
119   /**
120    * used for computing average of <S> data values. Not providing the divide
121    * method that takes two <S> values as it is not needed as of now.
122    * @param o
123    * @param l
124    * @return Average
125    */
126   public abstract double divideForAvg(S o, Long l);
127 
128   /**
129    * This method should return any additional data that is needed on the
130    * server side to construct the ColumnInterpreter. The server
131    * will pass this to the {@link #initialize}
132    * method. If there is no ColumnInterpreter specific data (for e.g.,
133    * {@link LongColumnInterpreter}) then null should be returned.
134    * @return the PB message
135    */
136   public abstract P getRequestData();
137 
138   /**
139    * This method should initialize any field(s) of the ColumnInterpreter with
140    * a parsing of the passed message bytes (used on the server side).
141    * @param msg
142    */
143   public abstract void initialize(P msg);
144   
145   /**
146    * This method gets the PB message corresponding to the cell type
147    * @param t
148    * @return the PB message for the cell-type instance
149    */
150   public abstract Q getProtoForCellType(T t);
151 
152   /**
153    * This method gets the PB message corresponding to the cell type
154    * @param q
155    * @return the cell-type instance from the PB message
156    */
157   public abstract T getCellValueFromProto(Q q);
158 
159   /**
160    * This method gets the PB message corresponding to the promoted type
161    * @param s
162    * @return the PB message for the promoted-type instance
163    */
164   public abstract R getProtoForPromotedType(S s);
165 
166   /**
167    * This method gets the promoted type from the proto message
168    * @param r
169    * @return the promoted-type instance from the PB message
170    */
171   public abstract S getPromotedValueFromProto(R r);
172 
173   /**
174    * The response message comes as type S. This will convert/cast it to T.
175    * In some sense, performs the opposite of {@link #castToReturnType(Object)}
176    * @param response
177    * @return cast
178    */
179   public abstract T castToCellType(S response);
180 }