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.Arrays;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.TreeMap;
27  
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.util.Bytes;
31  
32  /**
33   * Container for Actions (i.e. Get, Delete, or Put), which are grouped by
34   * regionName. Intended to be used with HConnectionManager.processBatch()
35   */
36  @InterfaceAudience.Private
37  public final class MultiAction<R> {
38    // TODO: This class should not be visible outside of the client package.
39  
40    // map of regions to lists of puts/gets/deletes for that region.
41    public Map<byte[], List<Action<R>>> actions =
42      new TreeMap<byte[], List<Action<R>>>(Bytes.BYTES_COMPARATOR);
43  
44    private long nonceGroup = HConstants.NO_NONCE;
45  
46    public MultiAction() {
47      super();
48    }
49  
50    /**
51     * Get the total number of Actions
52     *
53     * @return total number of Actions for all groups in this container.
54     */
55    public int size() {
56      int size = 0;
57      for (List<?> l : actions.values()) {
58        size += l.size();
59      }
60      return size;
61    }
62  
63    /**
64     * Add an Action to this container based on it's regionName. If the regionName
65     * is wrong, the initial execution will fail, but will be automatically
66     * retried after looking up the correct region.
67     *
68     * @param regionName
69     * @param a
70     */
71    public void add(byte[] regionName, Action<R> a) {
72      add(regionName, Arrays.asList(a));
73    }
74  
75    /**
76     * Add an Action to this container based on it's regionName. If the regionName
77     * is wrong, the initial execution will fail, but will be automatically
78     * retried after looking up the correct region.
79     *
80     * @param regionName
81     * @param actionList list of actions to add for the region
82     */
83    public void add(byte[] regionName, List<Action<R>> actionList){
84      List<Action<R>> rsActions = actions.get(regionName);
85      if (rsActions == null) {
86        rsActions = new ArrayList<Action<R>>(actionList.size());
87        actions.put(regionName, rsActions);
88      }
89      rsActions.addAll(actionList);
90    }
91  
92    public void setNonceGroup(long nonceGroup) {
93      this.nonceGroup = nonceGroup;
94    }
95  
96    public Set<byte[]> getRegions() {
97      return actions.keySet();
98    }
99  
100   public boolean hasNonceGroup() {
101     return nonceGroup != HConstants.NO_NONCE;
102   }
103 
104   public long getNonceGroup() {
105     return this.nonceGroup;
106   }
107 }