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.regionserver;
019
020import org.apache.hadoop.hbase.HBaseInterfaceAudience;
021import org.apache.hadoop.hbase.client.Mutation;
022import org.apache.hadoop.hbase.wal.WALEdit;
023import org.apache.yetus.audience.InterfaceAudience;
024
025import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
026
027/**
028 * Wraps together the mutations which are applied as a batch to the region and their operation
029 * status and WALEdits.
030 * @see org.apache.hadoop.hbase.coprocessor.RegionObserver#preBatchMutate(
031 *      org.apache.hadoop.hbase.coprocessor.ObserverContext, MiniBatchOperationInProgress)
032 * @see org.apache.hadoop.hbase.coprocessor.RegionObserver#postBatchMutate(
033 *      org.apache.hadoop.hbase.coprocessor.ObserverContext, MiniBatchOperationInProgress)
034 * @param T Pair<Mutation, Integer> pair of Mutations and associated rowlock ids .
035 */
036@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
037public class MiniBatchOperationInProgress<T> {
038  private final T[] operations;
039  private Mutation[][] operationsFromCoprocessors;
040  private final OperationStatus[] retCodeDetails;
041  private final WALEdit[] walEditsFromCoprocessors;
042  private final int firstIndex;
043  private final int lastIndexExclusive;
044
045  private int readyToWriteCount = 0;
046  private int cellCount = 0;
047  private int numOfPuts = 0;
048  private int numOfDeletes = 0;
049  private int numOfIncrements = 0;
050  private int numOfAppends = 0;
051
052  public MiniBatchOperationInProgress(T[] operations, OperationStatus[] retCodeDetails,
053    WALEdit[] walEditsFromCoprocessors, int firstIndex, int lastIndexExclusive,
054    int readyToWriteCount) {
055    Preconditions.checkArgument(readyToWriteCount <= (lastIndexExclusive - firstIndex));
056    this.operations = operations;
057    this.retCodeDetails = retCodeDetails;
058    this.walEditsFromCoprocessors = walEditsFromCoprocessors;
059    this.firstIndex = firstIndex;
060    this.lastIndexExclusive = lastIndexExclusive;
061    this.readyToWriteCount = readyToWriteCount;
062  }
063
064  /** Returns The number of operations(Mutations) involved in this batch. */
065  public int size() {
066    return this.lastIndexExclusive - this.firstIndex;
067  }
068
069  /** Returns The operation(Mutation) at the specified position. */
070  public T getOperation(int index) {
071    return operations[getAbsoluteIndex(index)];
072  }
073
074  /**
075   * Sets the status code for the operation(Mutation) at the specified position. By setting this
076   * status, {@link org.apache.hadoop.hbase.coprocessor.RegionObserver} can make HRegion to skip
077   * Mutations.
078   */
079  public void setOperationStatus(int index, OperationStatus opStatus) {
080    this.retCodeDetails[getAbsoluteIndex(index)] = opStatus;
081  }
082
083  /** Returns Gets the status code for the operation(Mutation) at the specified position. */
084  public OperationStatus getOperationStatus(int index) {
085    return this.retCodeDetails[getAbsoluteIndex(index)];
086  }
087
088  /**
089   * Sets the walEdit for the operation(Mutation) at the specified position.
090   */
091  public void setWalEdit(int index, WALEdit walEdit) {
092    this.walEditsFromCoprocessors[getAbsoluteIndex(index)] = walEdit;
093  }
094
095  /** Returns Gets the walEdit for the operation(Mutation) at the specified position. */
096  public WALEdit getWalEdit(int index) {
097    return this.walEditsFromCoprocessors[getAbsoluteIndex(index)];
098  }
099
100  private int getAbsoluteIndex(int index) {
101    if (index < 0 || this.firstIndex + index >= this.lastIndexExclusive) {
102      throw new ArrayIndexOutOfBoundsException(index);
103    }
104    return this.firstIndex + index;
105  }
106
107  /**
108   * Add more Mutations corresponding to the Mutation at the given index to be committed atomically
109   * in the same batch. These mutations are applied to the WAL and applied to the memstore as well.
110   * The timestamp of the cells in the given Mutations MUST be obtained from the original mutation.
111   * <b>Note:</b> The durability from CP will be replaced by the durability of corresponding
112   * mutation. <b>Note:</b> Currently only supports Put and Delete operations.
113   * @param index         the index that corresponds to the original mutation index in the batch
114   * @param newOperations the Mutations to add
115   */
116  public void addOperationsFromCP(int index, Mutation[] newOperations) {
117    if (this.operationsFromCoprocessors == null) {
118      // lazy allocation to save on object allocation in case this is not used
119      this.operationsFromCoprocessors = new Mutation[operations.length][];
120    }
121    this.operationsFromCoprocessors[getAbsoluteIndex(index)] = newOperations;
122  }
123
124  public Mutation[] getOperationsFromCoprocessors(int index) {
125    return operationsFromCoprocessors == null
126      ? null
127      : operationsFromCoprocessors[getAbsoluteIndex(index)];
128  }
129
130  public int getReadyToWriteCount() {
131    return readyToWriteCount;
132  }
133
134  public int getLastIndexExclusive() {
135    return lastIndexExclusive;
136  }
137
138  public int getCellCount() {
139    return cellCount;
140  }
141
142  public void addCellCount(int cellCount) {
143    this.cellCount += cellCount;
144  }
145
146  public int getNumOfPuts() {
147    return numOfPuts;
148  }
149
150  public void incrementNumOfPuts() {
151    this.numOfPuts += 1;
152  }
153
154  public int getNumOfDeletes() {
155    return numOfDeletes;
156  }
157
158  public void incrementNumOfDeletes() {
159    this.numOfDeletes += 1;
160  }
161
162  public int getNumOfIncrements() {
163    return numOfIncrements;
164  }
165
166  public void incrementNumOfIncrements() {
167    this.numOfIncrements += 1;
168  }
169
170  public int getNumOfAppends() {
171    return numOfAppends;
172  }
173
174  public void incrementNumOfAppends() {
175    this.numOfAppends += 1;
176  }
177}