View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.client;
21  
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.util.Bytes;
24  
25  import com.google.common.base.Objects;
26  import com.google.protobuf.Descriptors.MethodDescriptor;
27  import com.google.protobuf.Message;
28  
29  
30  /**
31   * Represents a coprocessor service method execution against a single region.  While coprocessor
32   * service calls are performed against a region, this class implements {@link Row} in order to
33   * make use of the {@link AsyncProcess} framework for batching multi-region calls per region server.
34   *
35   * <p><b>Note:</b> This class should not be instantiated directly.  Use 
36   * HTable#batchCoprocessorService instead.</p>
37   */
38  @InterfaceAudience.Private
39  public class RegionCoprocessorServiceExec implements Row {
40  
41    /*
42     * This duplicates region name in MultiAction, but allows us to easily access the region name in
43     * the AsyncProcessCallback context.
44     */
45    private final byte[] region;
46    private final byte[] startKey;
47    private final MethodDescriptor method;
48    private final Message request;
49  
50    public RegionCoprocessorServiceExec(byte[] region, byte[] startKey,
51        MethodDescriptor method, Message request) {
52      this.region = region;
53      this.startKey = startKey;
54      this.method = method;
55      this.request = request;
56    }
57  
58    @Override
59    public byte[] getRow() {
60      return startKey;
61    }
62  
63    public byte[] getRegion() {
64      return region;
65    }
66  
67    public MethodDescriptor getMethod() {
68      return method;
69    }
70  
71    public Message getRequest() {
72      return request;
73    }
74  
75    @Override
76    public int compareTo(Row o) {
77      int res = Bytes.compareTo(this.getRow(), o.getRow());
78      if ((o instanceof RegionCoprocessorServiceExec) && res == 0) {
79        RegionCoprocessorServiceExec exec = (RegionCoprocessorServiceExec) o;
80        res = method.getFullName().compareTo(exec.getMethod().getFullName());
81        if (res == 0) {
82          res = Bytes.compareTo(request.toByteArray(), exec.getRequest().toByteArray());
83        }
84      }
85      return res;
86    }
87  
88    @Override
89    public int hashCode() {
90      return Objects.hashCode(Bytes.hashCode(this.getRow()), method.getFullName(), request);
91    }
92  
93    @Override
94    public boolean equals(Object obj) {
95      if (this == obj) {
96        return true;
97      }
98      if (obj == null || getClass() != obj.getClass()) {
99        return false;
100     }
101     Row other = (Row) obj;
102     return compareTo(other) == 0;
103   }
104 }