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.client;
021
022import org.apache.hadoop.hbase.util.Bytes;
023import org.apache.yetus.audience.InterfaceAudience;
024
025import org.apache.hbase.thirdparty.com.google.common.base.Objects;
026import org.apache.hbase.thirdparty.com.google.protobuf.Descriptors.MethodDescriptor;
027import org.apache.hbase.thirdparty.com.google.protobuf.Message;
028
029
030/**
031 * Represents a coprocessor service method execution against a single region.  While coprocessor
032 * service calls are performed against a region, this class implements {@link Row} in order to
033 * make use of the AsyncProcess framework for batching multi-region calls per region server.
034 *
035 * <p><b>Note:</b> This class should not be instantiated directly.  Use
036 * HTable#batchCoprocessorService instead.</p>
037 */
038@InterfaceAudience.Private
039public class RegionCoprocessorServiceExec implements Row {
040
041  /*
042   * This duplicates region name in MultiAction, but allows us to easily access the region name in
043   * the AsyncProcessCallback context.
044   */
045  private final byte[] region;
046  private final byte[] startKey;
047  private final MethodDescriptor method;
048  private final Message request;
049
050  public RegionCoprocessorServiceExec(byte[] region, byte[] startKey,
051      MethodDescriptor method, Message request) {
052    this.region = region;
053    this.startKey = startKey;
054    this.method = method;
055    this.request = request;
056  }
057
058  @Override
059  public byte[] getRow() {
060    return startKey;
061  }
062
063  public byte[] getRegion() {
064    return region;
065  }
066
067  public MethodDescriptor getMethod() {
068    return method;
069  }
070
071  public Message getRequest() {
072    return request;
073  }
074
075  @Override
076  public int compareTo(Row o) {
077    int res = Bytes.compareTo(this.getRow(), o.getRow());
078    if ((o instanceof RegionCoprocessorServiceExec) && res == 0) {
079      RegionCoprocessorServiceExec exec = (RegionCoprocessorServiceExec) o;
080      res = method.getFullName().compareTo(exec.getMethod().getFullName());
081      if (res == 0) {
082        res = Bytes.compareTo(request.toByteArray(), exec.getRequest().toByteArray());
083      }
084    }
085    return res;
086  }
087
088  @Override
089  public int hashCode() {
090    return Objects.hashCode(Bytes.hashCode(this.getRow()), method.getFullName(), request);
091  }
092
093  @Override
094  public boolean equals(Object obj) {
095    if (this == obj) {
096      return true;
097    }
098    if (obj == null || getClass() != obj.getClass()) {
099      return false;
100    }
101    return compareTo((RegionCoprocessorServiceExec) obj) == 0;
102  }
103
104  @Override
105  public String toString() {
106    StringBuilder builder = new StringBuilder();
107    builder.append("region:")
108           .append(Bytes.toStringBinary(region))
109           .append(", startKey:")
110           .append(Bytes.toStringBinary(startKey))
111           .append(", method:")
112           .append(method.getFullName())
113           .append(", request:")
114           .append(request);
115    return builder.toString();
116  }
117}