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