001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.coprocessor;
021
022import java.io.IOException;
023
024import org.apache.hadoop.hbase.Cell;
025import org.apache.hadoop.hbase.HBaseInterfaceAudience;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.yetus.audience.InterfaceStability;
028
029import com.google.protobuf.Message;
030
031/**
032 * Defines how value for specific column is interpreted and provides utility
033 * methods like compare, add, multiply etc for them. Takes column family, column
034 * qualifier and return the cell value. Its concrete implementation should
035 * handle null case gracefully. 
036 * Refer to {@link org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter} 
037 * for an example.
038 * <p>
039 * Takes two generic parameters and three Message parameters. 
040 * The cell value type of the interpreter is &lt;T&gt;.
041 * During some computations like sum, average, the return type can be different
042 * than the cell value data type, for eg, sum of int cell values might overflow
043 * in case of a int result, we should use Long for its result. Therefore, this
044 * class mandates to use a different (promoted) data type for result of these
045 * computations &lt;S&gt;. All computations are performed on the promoted data type
046 * &lt;S&gt;. There is a conversion method
047 * {@link ColumnInterpreter#castToReturnType(Object)} which takes a &lt;T&gt; type and
048 * returns a &lt;S&gt; type.
049 * The AggregateIm&gt;lementation uses PB messages to initialize the
050 * user's ColumnInterpreter implementation, and for sending the responses
051 * back to AggregationClient.
052 * @param T Cell value data type
053 * @param S Promoted data type
054 * @param P PB message that is used to transport initializer specific bytes
055 * @param Q PB message that is used to transport Cell (&lt;T&gt;) instance
056 * @param R PB message that is used to transport Promoted (&lt;S&gt;) instance
057 */
058@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
059@InterfaceStability.Evolving
060public abstract class ColumnInterpreter<T, S, P extends Message, 
061Q extends Message, R extends Message> {
062
063  /**
064   * 
065   * @param colFamily
066   * @param colQualifier
067   * @param c
068   * @return value of type T
069   * @throws IOException
070   */
071  public abstract T getValue(byte[] colFamily, byte[] colQualifier, Cell c)
072      throws IOException;
073
074  /**
075   * @param l1
076   * @param l2
077   * @return sum or non null value among (if either of them is null); otherwise
078   * returns a null.
079   */
080  public abstract S add(S l1, S l2);
081
082  /**
083   * returns the maximum value for this type T
084   * @return max
085   */
086
087  public abstract T getMaxValue();
088
089  public abstract T getMinValue();
090
091  /**
092   * @param o1
093   * @param o2
094   * @return multiplication
095   */
096  public abstract S multiply(S o1, S o2);
097
098  /**
099   * @param o
100   * @return increment
101   */
102  public abstract S increment(S o);
103
104  /**
105   * provides casting opportunity between the data types.
106   * @param o
107   * @return cast
108   */
109  public abstract S castToReturnType(T o);
110
111  /**
112   * This takes care if either of arguments are null. returns 0 if they are
113   * equal or both are null;
114   * <ul>
115   * <li>&gt; 0 if l1 &gt; l2 or l1 is not null and l2 is null.</li>
116   * <li>&lt; 0 if l1 &lt; l2 or l1 is null and l2 is not null.</li>
117   * </ul>
118   */
119  public abstract int compare(final T l1, final T l2);
120
121  /**
122   * used for computing average of &lt;S&gt; data values. Not providing the divide
123   * method that takes two &lt;S&gt; values as it is not needed as of now.
124   * @param o
125   * @param l
126   * @return Average
127   */
128  public abstract double divideForAvg(S o, Long l);
129
130  /**
131   * This method should return any additional data that is needed on the
132   * server side to construct the ColumnInterpreter. The server
133   * will pass this to the {@link #initialize}
134   * method. If there is no ColumnInterpreter specific data (for e.g.,
135   * {@link org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter})
136   *  then null should be returned.
137   * @return the PB message
138   */
139  public abstract P getRequestData();
140
141  /**
142   * This method should initialize any field(s) of the ColumnInterpreter with
143   * a parsing of the passed message bytes (used on the server side).
144   * @param msg
145   */
146  public abstract void initialize(P msg);
147  
148  /**
149   * This method gets the PB message corresponding to the cell type
150   * @param t
151   * @return the PB message for the cell-type instance
152   */
153  public abstract Q getProtoForCellType(T t);
154
155  /**
156   * This method gets the PB message corresponding to the cell type
157   * @param q
158   * @return the cell-type instance from the PB message
159   */
160  public abstract T getCellValueFromProto(Q q);
161
162  /**
163   * This method gets the PB message corresponding to the promoted type
164   * @param s
165   * @return the PB message for the promoted-type instance
166   */
167  public abstract R getProtoForPromotedType(S s);
168
169  /**
170   * This method gets the promoted type from the proto message
171   * @param r
172   * @return the promoted-type instance from the PB message
173   */
174  public abstract S getPromotedValueFromProto(R r);
175
176  /**
177   * The response message comes as type S. This will convert/cast it to T.
178   * In some sense, performs the opposite of {@link #castToReturnType(Object)}
179   * @param response
180   * @return cast
181   */
182  public abstract T castToCellType(S response);
183}