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.classification.InterfaceAudience;
21  import org.apache.hadoop.hbase.coprocessor.ObserverContext;
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.Private
33  public class MiniBatchOperationInProgress<T> {
34    private final T[] operations;
35    private final OperationStatus[] retCodeDetails;
36    private final WALEdit[] walEditsFromCoprocessors;
37    private final int firstIndex;
38    private final int lastIndexExclusive;
39  
40    public MiniBatchOperationInProgress(T[] operations, OperationStatus[] retCodeDetails,
41        WALEdit[] walEditsFromCoprocessors, int firstIndex, int lastIndexExclusive) {
42      this.operations = operations;
43      this.retCodeDetails = retCodeDetails;
44      this.walEditsFromCoprocessors = walEditsFromCoprocessors;
45      this.firstIndex = firstIndex;
46      this.lastIndexExclusive = lastIndexExclusive;
47    }
48  
49    /**
50     * @return The number of operations(Mutations) involved in this batch.
51     */
52    public int size() {
53      return this.lastIndexExclusive - this.firstIndex;
54    }
55  
56    /**
57     * @param index
58     * @return The operation(Mutation) at the specified position.
59     */
60    public T getOperation(int index) {
61      return operations[getAbsoluteIndex(index)];
62    }
63  
64    /**
65     * Sets the status code for the operation(Mutation) at the specified position.
66     * By setting this status, {@link RegionObserver} can make HRegion to skip Mutations.
67     * @param index
68     * @param opStatus
69     */
70    public void setOperationStatus(int index, OperationStatus opStatus) {
71      this.retCodeDetails[getAbsoluteIndex(index)] = opStatus;
72    }
73  
74    /**
75     * @param index
76     * @return Gets the status code for the operation(Mutation) at the specified position.
77     */
78    public OperationStatus getOperationStatus(int index) {
79      return this.retCodeDetails[getAbsoluteIndex(index)];
80    }
81  
82    /**
83     * Sets the walEdit for the operation(Mutation) at the specified position.
84     * @param index
85     * @param walEdit
86     */
87    public void setWalEdit(int index, WALEdit walEdit) {
88      this.walEditsFromCoprocessors[getAbsoluteIndex(index)] = walEdit;
89    }
90  
91    /**
92     * @param index
93     * @return Gets the walEdit for the operation(Mutation) at the specified position.
94     */
95    public WALEdit getWalEdit(int index) {
96      return this.walEditsFromCoprocessors[getAbsoluteIndex(index)];
97    }
98  
99    private int getAbsoluteIndex(int index) {
100     if (index < 0 || this.firstIndex + index >= this.lastIndexExclusive) {
101       throw new ArrayIndexOutOfBoundsException(index);
102     }
103     return this.firstIndex + index;
104   }
105 }