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.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 org.apache.hadoop.hbase.coprocessor.
27   *      RegionObserver#preBatchMutate(ObserverContext, MiniBatchOperationInProgress)
28   * @see org.apache.hadoop.hbase.coprocessor.
29   *      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 org.apache.hadoop.hbase.coprocessor.RegionObserver} 
67     * 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 }