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.classification.InterfaceAudience;
21 import org.apache.hadoop.classification.InterfaceStability;
22 import org.apache.hadoop.hbase.coprocessor.RegionObserver;
23 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
24
25 /**
26 * Wraps together the mutations which are applied as a batch to the region and their operation
27 * status and WALEdits.
28 * @see RegionObserver#preBatchMutate(ObserverContext, MiniBatchOperationInProgress)
29 * @see RegionObserver#postBatchMutate(ObserverContext, MiniBatchOperationInProgress)
30 * @param <T> Pair<Mutation, Integer> pair of Mutations and associated rowlock ids .
31 */
32 @InterfaceAudience.Public
33 @InterfaceStability.Evolving
34 public class MiniBatchOperationInProgress<T> {
35 private final T[] operations;
36 private final OperationStatus[] retCodeDetails;
37 private final WALEdit[] walEditsFromCoprocessors;
38 private final int firstIndex;
39 private final int lastIndexExclusive;
40
41 public MiniBatchOperationInProgress(T[] operations, OperationStatus[] retCodeDetails,
42 WALEdit[] walEditsFromCoprocessors, int firstIndex, int lastIndexExclusive) {
43 this.operations = operations;
44 this.retCodeDetails = retCodeDetails;
45 this.walEditsFromCoprocessors = walEditsFromCoprocessors;
46 this.firstIndex = firstIndex;
47 this.lastIndexExclusive = lastIndexExclusive;
48 }
49
50 /**
51 * @return The number of operations(Mutations) involved in this batch.
52 */
53 public int size() {
54 return this.lastIndexExclusive - this.firstIndex;
55 }
56
57 /**
58 * @param index
59 * @return The operation(Mutation) at the specified position.
60 */
61 public T getOperation(int index) {
62 return operations[getAbsoluteIndex(index)];
63 }
64
65 /**
66 * Sets the status code for the operation(Mutation) at the specified position.
67 * By setting this status, {@link RegionObserver} can make HRegion to skip Mutations.
68 * @param index
69 * @param opStatus
70 */
71 public void setOperationStatus(int index, OperationStatus opStatus) {
72 this.retCodeDetails[getAbsoluteIndex(index)] = opStatus;
73 }
74
75 /**
76 * @param index
77 * @return Gets the status code for the operation(Mutation) at the specified position.
78 */
79 public OperationStatus getOperationStatus(int index) {
80 return this.retCodeDetails[getAbsoluteIndex(index)];
81 }
82
83 /**
84 * Sets the walEdit for the operation(Mutation) at the specified position.
85 * @param index
86 * @param walEdit
87 */
88 public void setWalEdit(int index, WALEdit walEdit) {
89 this.walEditsFromCoprocessors[getAbsoluteIndex(index)] = walEdit;
90 }
91
92 /**
93 * @param index
94 * @return Gets the walEdit for the operation(Mutation) at the specified position.
95 */
96 public WALEdit getWalEdit(int index) {
97 return this.walEditsFromCoprocessors[getAbsoluteIndex(index)];
98 }
99
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 }