001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.client.coprocessor; 019 020import com.google.protobuf.ByteString; 021import com.google.protobuf.Message; 022 023import java.io.IOException; 024import java.lang.reflect.InvocationTargetException; 025import java.lang.reflect.Method; 026import java.lang.reflect.ParameterizedType; 027import java.lang.reflect.Type; 028 029import org.apache.hadoop.hbase.HConstants; 030import org.apache.hadoop.hbase.client.Scan; 031import org.apache.hadoop.hbase.coprocessor.ColumnInterpreter; 032import org.apache.hadoop.hbase.protobuf.ProtobufUtil; 033import org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateRequest; 034import org.apache.hadoop.hbase.util.Bytes; 035import org.apache.yetus.audience.InterfaceAudience; 036 037/** 038 * Helper class for constructing aggregation request and response. 039 */ 040@InterfaceAudience.Private 041public final class AggregationHelper { 042 private AggregationHelper() {} 043 044 /** 045 * @param scan the HBase scan object to use to read data from HBase 046 * @param canFamilyBeAbsent whether column family can be absent in familyMap of scan 047 */ 048 private static void validateParameters(Scan scan, boolean canFamilyBeAbsent) throws IOException { 049 if (scan == null 050 || (Bytes.equals(scan.getStartRow(), scan.getStopRow()) 051 && !Bytes.equals(scan.getStartRow(), HConstants.EMPTY_START_ROW)) 052 || ((Bytes.compareTo(scan.getStartRow(), scan.getStopRow()) > 0) 053 && !Bytes.equals(scan.getStopRow(), HConstants.EMPTY_END_ROW))) { 054 throw new IOException("Agg client Exception: Startrow should be smaller than Stoprow"); 055 } else if (!canFamilyBeAbsent) { 056 if (scan.getFamilyMap().size() != 1) { 057 throw new IOException("There must be only one family."); 058 } 059 } 060 } 061 062 static <R, S, P extends Message, Q extends Message, T extends Message> AggregateRequest 063 validateArgAndGetPB(Scan scan, ColumnInterpreter<R, S, P, Q, T> ci, boolean canFamilyBeAbsent) 064 throws IOException { 065 validateParameters(scan, canFamilyBeAbsent); 066 final AggregateRequest.Builder requestBuilder = AggregateRequest.newBuilder(); 067 requestBuilder.setInterpreterClassName(ci.getClass().getCanonicalName()); 068 P columnInterpreterSpecificData = ci.getRequestData(); 069 if (columnInterpreterSpecificData != null) { 070 requestBuilder.setInterpreterSpecificBytes(columnInterpreterSpecificData.toByteString()); 071 } 072 requestBuilder.setScan(ProtobufUtil.toScan(scan)); 073 return requestBuilder.build(); 074 } 075 076 /** 077 * Get an instance of the argument type declared in a class's signature. The argument type is 078 * assumed to be a PB Message subclass, and the instance is created using parseFrom method on the 079 * passed ByteString. 080 * @param runtimeClass the runtime type of the class 081 * @param position the position of the argument in the class declaration 082 * @param b the ByteString which should be parsed to get the instance created 083 * @return the instance 084 * @throws IOException Either we couldn't instantiate the method object, or "parseFrom" failed. 085 */ 086 @SuppressWarnings("unchecked") 087 // Used server-side too by Aggregation Coprocesor Endpoint. Undo this interdependence. TODO. 088 public static <T extends Message> T getParsedGenericInstance(Class<?> runtimeClass, int position, 089 ByteString b) throws IOException { 090 Type type = runtimeClass.getGenericSuperclass(); 091 Type argType = ((ParameterizedType) type).getActualTypeArguments()[position]; 092 Class<T> classType = (Class<T>) argType; 093 T inst; 094 try { 095 Method m = classType.getMethod("parseFrom", ByteString.class); 096 inst = (T) m.invoke(null, b); 097 return inst; 098 } catch (SecurityException e) { 099 throw new IOException(e); 100 } catch (NoSuchMethodException e) { 101 throw new IOException(e); 102 } catch (IllegalArgumentException e) { 103 throw new IOException(e); 104 } catch (InvocationTargetException e) { 105 throw new IOException(e); 106 } catch (IllegalAccessException e) { 107 throw new IOException(e); 108 } 109 } 110}