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  package org.apache.hadoop.hbase.client;
20  
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.HConstants;
23  
24  /**
25   * A Get, Put, Increment, Append, or Delete associated with it's region.  Used internally by  
26   * {@link HTable#batch} to associate the action with it's region and maintain
27   * the index from the original request. 
28   */
29  @InterfaceAudience.Private
30  //TODO: R is never used
31  public class Action<R> implements Comparable<R> {
32    // TODO: This class should not be visible outside of the client package.
33    private Row action;
34    private int originalIndex;
35    private long nonce = HConstants.NO_NONCE;
36    private int replicaId = RegionReplicaUtil.DEFAULT_REPLICA_ID;
37    private int priority;
38  
39    public Action(Row action, int originalIndex) {
40      this(action, originalIndex, HConstants.PRIORITY_UNSET);
41    }
42  
43    public Action(Row action, int originalIndex, int priority) {
44      super();
45      this.action = action;
46      this.originalIndex = originalIndex;
47      this.priority = priority;
48    }
49  
50    /**
51     * Creates an action for a particular replica from original action.
52     * @param action Original action.
53     * @param replicaId Replica id for the new action.
54     */
55    public Action(Action<R> action, int replicaId) {
56      super();
57      this.action = action.action;
58      this.nonce = action.nonce;
59      this.originalIndex = action.originalIndex;
60      this.replicaId = replicaId;
61    }
62  
63  
64    public void setNonce(long nonce) {
65      this.nonce = nonce;
66    }
67  
68    public boolean hasNonce() {
69      return nonce != HConstants.NO_NONCE;
70    }
71  
72    public Row getAction() {
73      return action;
74    }
75  
76    public int getOriginalIndex() {
77      return originalIndex;
78    }
79  
80    public int getReplicaId() {
81      return replicaId;
82    }
83  
84    public int getPriority() { return priority; }
85  
86    @SuppressWarnings("rawtypes")
87    @Override
88    public int compareTo(Object o) {
89      return action.compareTo(((Action) o).getAction());
90    }
91  
92    @Override
93    public int hashCode() {
94      return this.action.hashCode();
95    }
96  
97    @Override
98    public boolean equals(Object obj) {
99      if (this == obj) return true;
100     if (obj == null || getClass() != obj.getClass()) return false;
101     Action<?> other = (Action<?>) obj;
102     return compareTo(other) == 0;
103   }
104 
105   public long getNonce() {
106     return nonce;
107   }
108 }