1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.client;
22
23 import org.apache.hadoop.hbase.HRegionInfo;
24 import org.apache.hadoop.hbase.HServerAddress;
25 import org.apache.hadoop.hbase.util.Bytes;
26 import org.apache.hadoop.io.Writable;
27
28 import java.io.DataInput;
29 import java.io.DataOutput;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38 import java.util.TreeMap;
39 import java.util.TreeSet;
40
41
42
43
44
45 public class MultiPut extends Operation implements Writable {
46 public HServerAddress address;
47
48
49 public static final int DEFAULT_MAX_PUT_OUTPUT = 10;
50
51
52 public Map<byte[], List<Put> > puts = new TreeMap<byte[], List<Put>>(Bytes.BYTES_COMPARATOR);
53
54
55
56
57 public MultiPut() {}
58
59
60
61
62
63 public MultiPut(HServerAddress a) {
64 address = a;
65 }
66
67 public int size() {
68 int size = 0;
69 for( List<Put> l : puts.values()) {
70 size += l.size();
71 }
72 return size;
73 }
74
75 public void add(byte[] regionName, Put aPut) {
76 List<Put> rsput = puts.get(regionName);
77 if (rsput == null) {
78 rsput = new ArrayList<Put>();
79 puts.put(regionName, rsput);
80 }
81 rsput.add(aPut);
82 }
83
84 public Collection<Put> allPuts() {
85 List<Put> res = new ArrayList<Put>();
86 for ( List<Put> pp : puts.values() ) {
87 res.addAll(pp);
88 }
89 return res;
90 }
91
92
93
94
95
96
97
98 @Override
99 public Map<String, Object> getFingerprint() {
100 Map<String, Object> map = new HashMap<String, Object>();
101
102
103 Map<String, Map> tableInfo =
104 new HashMap<String, Map>();
105 map.put("tables", tableInfo);
106 for (Map.Entry<byte[], List<Put>> entry : puts.entrySet()) {
107
108
109 Set<String> familySet;
110 try {
111
112
113
114 String tableName = Bytes.toStringBinary(
115 HRegionInfo.parseRegionName(entry.getKey())[0]);
116 if (tableInfo.get(tableName) == null) {
117 Map<String, Object> table = new HashMap<String, Object>();
118 familySet = new TreeSet<String>();
119 table.put("families", familySet);
120 tableInfo.put(tableName, table);
121 } else {
122 familySet = (Set<String>) tableInfo.get(tableName).get("families");
123 }
124 } catch (IOException ioe) {
125
126 Map<String, Object> table = new HashMap<String, Object>();
127 familySet = new TreeSet<String>();
128 table.put("families", familySet);
129 tableInfo.put(Bytes.toStringBinary(entry.getKey()), table);
130 }
131
132
133 for (Put p : entry.getValue()) {
134 for (byte[] fam : p.getFamilyMap().keySet()) {
135 familySet.add(Bytes.toStringBinary(fam));
136 }
137 }
138 }
139 return map;
140 }
141
142
143
144
145
146
147
148
149 @Override
150 public Map<String, Object> toMap(int maxCols) {
151 Map<String, Object> map = getFingerprint();
152 Map<String, Object> tableInfo = (Map<String, Object>) map.get("tables");
153 int putCount = 0;
154 for (Map.Entry<byte[], List<Put>> entry : puts.entrySet()) {
155
156 if (putCount >= DEFAULT_MAX_PUT_OUTPUT) {
157 putCount += entry.getValue().size();
158 continue;
159 }
160 List<Put> regionPuts = entry.getValue();
161 List<Map<String, Object>> putSummaries =
162 new ArrayList<Map<String, Object>>();
163
164
165 int regionPutsToAdd = regionPuts.size();
166 putCount += regionPutsToAdd;
167 if (putCount > DEFAULT_MAX_PUT_OUTPUT) {
168 regionPutsToAdd -= putCount - DEFAULT_MAX_PUT_OUTPUT;
169 }
170 for (Iterator<Put> iter = regionPuts.iterator(); regionPutsToAdd-- > 0;) {
171 putSummaries.add(iter.next().toMap(maxCols));
172 }
173
174 String tableName = "";
175 try {
176 tableName = Bytes.toStringBinary(
177 HRegionInfo.parseRegionName(entry.getKey())[0]);
178 } catch (IOException ioe) {
179
180 tableName = Bytes.toStringBinary(entry.getKey());
181 }
182
183
184
185
186 Map<String, Object> table =
187 (Map<String, Object>) tableInfo.get(tableName);
188 if (table == null) {
189
190 table = new HashMap<String, Object>();
191 tableInfo.put(tableName, table);
192 table.put("puts", putSummaries);
193 } else if (table.get("puts") == null) {
194 table.put("puts", putSummaries);
195 } else {
196 ((List<Map<String, Object>>) table.get("puts")).addAll(putSummaries);
197 }
198 }
199 map.put("totalPuts", putCount);
200 return map;
201 }
202
203 @Override
204 public void write(DataOutput out) throws IOException {
205 out.writeInt(puts.size());
206 for( Map.Entry<byte[],List<Put>> e : puts.entrySet()) {
207 Bytes.writeByteArray(out, e.getKey());
208
209 List<Put> ps = e.getValue();
210 out.writeInt(ps.size());
211 for( Put p : ps ) {
212 p.write(out);
213 }
214 }
215 }
216
217 @Override
218 public void readFields(DataInput in) throws IOException {
219 puts.clear();
220
221 int mapSize = in.readInt();
222
223 for (int i = 0 ; i < mapSize; i++) {
224 byte[] key = Bytes.readByteArray(in);
225
226 int listSize = in.readInt();
227 List<Put> ps = new ArrayList<Put>(listSize);
228 for ( int j = 0 ; j < listSize; j++ ) {
229 Put put = new Put();
230 put.readFields(in);
231 ps.add(put);
232 }
233 puts.put(key, ps);
234 }
235 }
236 }