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 com.google.common.base.Objects;
23  import com.google.protobuf.Descriptors.MethodDescriptor;
24  import com.google.protobuf.Message;
25  
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.client.coprocessor.Batch;
28  import org.apache.hadoop.hbase.util.Bytes;
29  
30  
31  /**
32   * Represents a coprocessor service method execution against a single region.  While coprocessor
33   * service calls are performed against a region, this class implements {@link Row} in order to
34   * make use of the {@link AsyncProcess} framework for batching multi-region calls per region server.
35   *
36   * <p><b>Note:</b> This class should not be instantiated directly.  Use 
37   * HTable#batchCoprocessorService instead.</p>
38   */
39  @InterfaceAudience.Private
40  public class RegionCoprocessorServiceExec implements Row {
41  
42    /*
43     * This duplicates region name in MultiAction, but allows us to easily access the region name in
44     * the AsyncProcessCallback context.
45     */
46    private final byte[] region;
47    private final byte[] startKey;
48    private final MethodDescriptor method;
49    private final Message request;
50  
51    public RegionCoprocessorServiceExec(byte[] region, byte[] startKey,
52        MethodDescriptor method, Message request) {
53      this.region = region;
54      this.startKey = startKey;
55      this.method = method;
56      this.request = request;
57    }
58  
59    @Override
60    public byte[] getRow() {
61      return startKey;
62    }
63  
64    public byte[] getRegion() {
65      return region;
66    }
67  
68    public MethodDescriptor getMethod() {
69      return method;
70    }
71  
72    public Message getRequest() {
73      return request;
74    }
75  
76    @Override
77    public int compareTo(Row o) {
78      int res = Bytes.compareTo(this.getRow(), o.getRow());
79      if ((o instanceof RegionCoprocessorServiceExec) && res == 0) {
80        RegionCoprocessorServiceExec exec = (RegionCoprocessorServiceExec) o;
81        res = method.getFullName().compareTo(exec.getMethod().getFullName());
82        if (res == 0) {
83          res = Bytes.compareTo(request.toByteArray(), exec.getRequest().toByteArray());
84        }
85      }
86      return res;
87    }
88  
89    @Override
90    public int hashCode() {
91      return Objects.hashCode(Bytes.hashCode(this.getRow()), method.getFullName(), request);
92    }
93  
94    @Override
95    public boolean equals(Object obj) {
96      if (this == obj) {
97        return true;
98      }
99      if (obj == null || getClass() != obj.getClass()) {
100       return false;
101     }
102     Row other = (Row) obj;
103     return compareTo(other) == 0;
104   }
105 }