View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.client;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  import java.util.TreeMap;
26  
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.util.Bytes;
30  
31  /**
32   * Container for Actions (i.e. Get, Delete, or Put), which are grouped by
33   * regionName. Intended to be used with HConnectionManager.processBatch()
34   */
35  @InterfaceAudience.Private
36  public final class MultiAction<R> {
37    // TODO: This class should not be visible outside of the client package.
38  
39    // map of regions to lists of puts/gets/deletes for that region.
40    public Map<byte[], List<Action<R>>> actions =
41      new TreeMap<byte[], List<Action<R>>>(Bytes.BYTES_COMPARATOR);
42  
43    private long nonceGroup = HConstants.NO_NONCE;
44  
45    public MultiAction() {
46      super();
47    }
48  
49    /**
50     * Get the total number of Actions
51     *
52     * @return total number of Actions for all groups in this container.
53     */
54    public int size() {
55      int size = 0;
56      for (List<?> l : actions.values()) {
57        size += l.size();
58      }
59      return size;
60    }
61  
62    /**
63     * Add an Action to this container based on it's regionName. If the regionName
64     * is wrong, the initial execution will fail, but will be automatically
65     * retried after looking up the correct region.
66     *
67     * @param regionName
68     * @param a
69     */
70    public void add(byte[] regionName, Action<R> a) {
71      List<Action<R>> rsActions = actions.get(regionName);
72      if (rsActions == null) {
73        rsActions = new ArrayList<Action<R>>();
74        actions.put(regionName, rsActions);
75      }
76      rsActions.add(a);
77    }
78  
79    public void setNonceGroup(long nonceGroup) {
80      this.nonceGroup = nonceGroup;
81    }
82  
83    public Set<byte[]> getRegions() {
84      return actions.keySet();
85    }
86  
87    public boolean hasNonceGroup() {
88      return nonceGroup != HConstants.NO_NONCE;
89    }
90  
91    public long getNonceGroup() {
92      return this.nonceGroup;
93    }
94  }