001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.client;
020
021import org.apache.hadoop.hbase.HConstants;
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * A Get, Put, Increment, Append, or Delete associated with it's region.  Used internally by
026 * {@link Table#batch} to associate the action with it's region and maintain
027 * the index from the original request.
028 */
029@InterfaceAudience.Private
030public class Action implements Comparable<Action> {
031  private final Row action;
032  private final int originalIndex;
033  private long nonce = HConstants.NO_NONCE;
034  private int replicaId = RegionReplicaUtil.DEFAULT_REPLICA_ID;
035  private int priority;
036
037  public Action(Row action, int originalIndex) {
038    this(action, originalIndex, HConstants.PRIORITY_UNSET);
039  }
040
041  public Action(Row action, int originalIndex, int priority) {
042    this.action = action;
043    this.originalIndex = originalIndex;
044    this.priority = priority;
045  }
046
047  /**
048   * Creates an action for a particular replica from original action.
049   * @param action Original action.
050   * @param replicaId Replica id for the new action.
051   */
052  public Action(Action action, int replicaId) {
053    this.action = action.action;
054    this.nonce = action.nonce;
055    this.originalIndex = action.originalIndex;
056    this.replicaId = replicaId;
057  }
058
059  public void setNonce(long nonce) {
060    this.nonce = nonce;
061  }
062
063  public boolean hasNonce() {
064    return nonce != HConstants.NO_NONCE;
065  }
066
067  public Row getAction() {
068    return action;
069  }
070
071  public int getOriginalIndex() {
072    return originalIndex;
073  }
074
075  public int getReplicaId() {
076    return replicaId;
077  }
078
079  public int getPriority() { return priority; }
080
081  @Override
082  public int compareTo(Action other) {
083    return action.compareTo(other.getAction());
084  }
085
086  @Override
087  public int hashCode() {
088    return this.action.hashCode();
089  }
090
091  @Override
092  public boolean equals(Object obj) {
093    if (this == obj) return true;
094    if (obj instanceof Action) {
095      return compareTo((Action) obj) == 0;
096    }
097    return false;
098  }
099
100  public long getNonce() {
101    return nonce;
102  }
103}