View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver;
19  
20  import org.apache.hadoop.hbase.coprocessor.RegionObserver;
21  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
22  
23  /**
24   * Wraps together the mutations which are applied as a batch to the region and their operation
25   * status and WALEdits. 
26   * @see RegionObserver#preBatchMutate(ObserverContext, MiniBatchOperationInProgress)
27   * @see RegionObserver#postBatchMutate(ObserverContext, MiniBatchOperationInProgress)
28   * @param <T> Pair<Mutation, Integer> pair of Mutations and associated rowlock ids.
29   */
30  public class MiniBatchOperationInProgress<T> {
31    private final T[] operations;
32    private final OperationStatus[] retCodeDetails;
33    private final WALEdit[] walEditsFromCoprocessors;
34    private final int firstIndex;
35    private final int lastIndexExclusive;
36  
37    public MiniBatchOperationInProgress(T[] operations, OperationStatus[] retCodeDetails,
38        WALEdit[] walEditsFromCoprocessors, int firstIndex, int lastIndexExclusive) {
39      this.operations = operations;
40      this.retCodeDetails = retCodeDetails;
41      this.walEditsFromCoprocessors = walEditsFromCoprocessors;
42      this.firstIndex = firstIndex;
43      this.lastIndexExclusive = lastIndexExclusive;
44    }
45  
46    /**
47     * @return The number of operations(Mutations) involved in this batch.
48     */
49    public int size() {
50      return this.lastIndexExclusive - this.firstIndex;
51    }
52  
53    /**
54     * @param index
55     * @return The operation(Mutation) at the specified position.
56     */
57    public T getOperation(int index) {
58      return operations[getAbsoluteIndex(index)];
59    }
60  
61    /**
62     * Sets the status code for the operation(Mutation) at the specified position.
63     * By setting this status, {@link RegionObserver} can make HRegion to skip Mutations.
64     * @param index
65     * @param opStatus
66     */
67    public void setOperationStatus(int index, OperationStatus opStatus) {
68      this.retCodeDetails[getAbsoluteIndex(index)] = opStatus;
69    }
70  
71    /**
72     * @param index
73     * @return Gets the status code for the operation(Mutation) at the specified position.
74     */
75    public OperationStatus getOperationStatus(int index) {
76      return this.retCodeDetails[getAbsoluteIndex(index)];
77    }
78  
79    /**
80     * Sets the walEdit for the operation(Mutation) at the specified position.
81     * @param index
82     * @param walEdit
83     */
84    public void setWalEdit(int index, WALEdit walEdit) {
85      this.walEditsFromCoprocessors[getAbsoluteIndex(index)] = walEdit;
86    }
87  
88    /**
89     * @param index
90     * @return Gets the walEdit for the operation(Mutation) at the specified position.
91     */
92    public WALEdit getWalEdit(int index) {
93      return this.walEditsFromCoprocessors[getAbsoluteIndex(index)];
94    }
95  
96    private int getAbsoluteIndex(int index) {
97      if (index < 0 || this.firstIndex + index >= this.lastIndexExclusive) {
98        throw new ArrayIndexOutOfBoundsException(index);
99      }
100     return this.firstIndex + index;
101   }
102 }