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 org.apache.hadoop.classification.InterfaceAudience;
22 import org.apache.hadoop.classification.InterfaceStability;
23 import org.apache.hadoop.hbase.util.Bytes;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.TreeMap;
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.Public
36 @InterfaceStability.Evolving
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 public MultiAction() {
45 super();
46 }
47
48 /**
49 * Get the total number of Actions
50 *
51 * @return total number of Actions for all groups in this container.
52 */
53 public int size() {
54 int size = 0;
55 for (List<?> l : actions.values()) {
56 size += l.size();
57 }
58 return size;
59 }
60
61 /**
62 * Add an Action to this container based on it's regionName. If the regionName
63 * is wrong, the initial execution will fail, but will be automatically
64 * retried after looking up the correct region.
65 *
66 * @param regionName
67 * @param a
68 */
69 public void add(byte[] regionName, Action<R> a) {
70 List<Action<R>> rsActions = actions.get(regionName);
71 if (rsActions == null) {
72 rsActions = new ArrayList<Action<R>>();
73 actions.put(regionName, rsActions);
74 }
75 rsActions.add(a);
76 }
77
78 public Set<byte[]> getRegions() {
79 return actions.keySet();
80 }
81
82 /**
83 * @return All actions from all regions in this container
84 */
85 public List<Action<R>> allActions() {
86 List<Action<R>> res = new ArrayList<Action<R>>();
87 for (List<Action<R>> lst : actions.values()) {
88 res.addAll(lst);
89 }
90 return res;
91 }
92 }