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