1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.client;
21
22 import org.apache.hadoop.io.Writable;
23 import org.apache.hadoop.hbase.io.HbaseObjectWritable;
24 import org.apache.hadoop.hbase.util.Bytes;
25
26 import java.io.DataOutput;
27 import java.io.IOException;
28 import java.io.DataInput;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.TreeMap;
34
35
36
37
38
39 public final class MultiAction<R> implements Writable {
40
41
42 public Map<byte[], List<Action<R>>> actions =
43 new TreeMap<byte[], List<Action<R>>>(
44 Bytes.BYTES_COMPARATOR);
45
46 public MultiAction() {
47 }
48
49
50
51
52
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
64
65
66
67
68
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 Set<byte[]> getRegions() {
80 return actions.keySet();
81 }
82
83
84
85
86 public List<Action<R>> allActions() {
87 List<Action<R>> res = new ArrayList<Action<R>>();
88 for (List<Action<R>> lst : actions.values()) {
89 res.addAll(lst);
90 }
91 return res;
92 }
93
94 @Override
95 public void write(DataOutput out) throws IOException {
96 out.writeInt(actions.size());
97 for (Map.Entry<byte[], List<Action<R>>> e : actions.entrySet()) {
98 Bytes.writeByteArray(out, e.getKey());
99 List<Action<R>> lst = e.getValue();
100 out.writeInt(lst.size());
101 for (Action a : lst) {
102 HbaseObjectWritable.writeObject(out, a, a.getClass(), null);
103 }
104 }
105 }
106
107 @Override
108 public void readFields(DataInput in) throws IOException {
109 actions.clear();
110 int mapSize = in.readInt();
111 for (int i = 0; i < mapSize; i++) {
112 byte[] key = Bytes.readByteArray(in);
113 int listSize = in.readInt();
114 List<Action<R>> lst = new ArrayList<Action<R>>(listSize);
115 for (int j = 0; j < listSize; j++) {
116 lst.add((Action) HbaseObjectWritable.readObject(in, null));
117 }
118 actions.put(key, lst);
119 }
120 }
121
122 }