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.HConstants;
021import org.apache.yetus.audience.InterfaceAudience;
022
023/**
024 * A Get, Put, Increment, Append, or Delete associated with it's region. Used internally by
025 * {@link Table#batch} to associate the action with it's region and maintain the index from the
026 * original request.
027 */
028@InterfaceAudience.Private
029public class Action implements Comparable<Action> {
030  private final Row action;
031  private final int originalIndex;
032  private long nonce = HConstants.NO_NONCE;
033  private int replicaId = RegionReplicaUtil.DEFAULT_REPLICA_ID;
034  private int priority;
035
036  public Action(Row action, int originalIndex) {
037    this(action, originalIndex, HConstants.PRIORITY_UNSET);
038  }
039
040  public Action(Row action, int originalIndex, int priority) {
041    this.action = action;
042    this.originalIndex = originalIndex;
043    this.priority = priority;
044  }
045
046  /**
047   * Creates an action for a particular replica from original action.
048   * @param action    Original action.
049   * @param replicaId Replica id for the new action.
050   */
051  public Action(Action action, int replicaId) {
052    this.action = action.action;
053    this.nonce = action.nonce;
054    this.originalIndex = action.originalIndex;
055    this.replicaId = replicaId;
056  }
057
058  public void setNonce(long nonce) {
059    this.nonce = nonce;
060  }
061
062  public boolean hasNonce() {
063    return nonce != HConstants.NO_NONCE;
064  }
065
066  public Row getAction() {
067    return action;
068  }
069
070  public int getOriginalIndex() {
071    return originalIndex;
072  }
073
074  public int getReplicaId() {
075    return replicaId;
076  }
077
078  public int getPriority() {
079    return priority;
080  }
081
082  @Override
083  public int compareTo(Action other) {
084    return Row.COMPARATOR.compare(action, other.getAction());
085  }
086
087  @Override
088  public int hashCode() {
089    return this.action.hashCode();
090  }
091
092  @Override
093  public boolean equals(Object obj) {
094    if (this == obj) return true;
095    if (obj instanceof Action) {
096      return compareTo((Action) obj) == 0;
097    }
098    return false;
099  }
100
101  public long getNonce() {
102    return nonce;
103  }
104}