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 org.apache.hadoop.hbase.util.Bytes;
021import org.apache.yetus.audience.InterfaceAudience;
022
023import org.apache.hbase.thirdparty.com.google.common.base.Objects;
024import org.apache.hbase.thirdparty.com.google.protobuf.Descriptors.MethodDescriptor;
025import org.apache.hbase.thirdparty.com.google.protobuf.Message;
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  public int compareTo(Row o) {
074    int res = Row.COMPARATOR.compare(this, o);
075    if ((o instanceof RegionCoprocessorServiceExec) && res == 0) {
076      RegionCoprocessorServiceExec exec = (RegionCoprocessorServiceExec) o;
077      res = method.getFullName().compareTo(exec.getMethod().getFullName());
078      if (res == 0) {
079        res = Bytes.compareTo(request.toByteArray(), exec.getRequest().toByteArray());
080      }
081    }
082    return res;
083  }
084
085  @Override
086  public int hashCode() {
087    return Objects.hashCode(Bytes.hashCode(this.getRow()), method.getFullName(), request);
088  }
089
090  @Override
091  public boolean equals(Object obj) {
092    if (this == obj) {
093      return true;
094    }
095    if (obj == null || getClass() != obj.getClass()) {
096      return false;
097    }
098    return compareTo((RegionCoprocessorServiceExec) obj) == 0;
099  }
100
101  @Override
102  public String toString() {
103    StringBuilder builder = new StringBuilder();
104    builder.append("region:").append(Bytes.toStringBinary(region)).append(", startKey:")
105      .append(Bytes.toStringBinary(startKey)).append(", method:").append(method.getFullName())
106      .append(", request:").append(request);
107    return builder.toString();
108  }
109}