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.io.Writable;
24 import org.apache.hadoop.hbase.io.HbaseObjectWritable;
25 import org.apache.hadoop.hbase.util.Bytes;
26 import org.apache.hadoop.hbase.util.Pair;
27 import org.apache.hadoop.hbase.HServerAddress;
28 import org.apache.hadoop.io.WritableUtils;
29 import org.apache.hadoop.util.StringUtils;
30
31 import java.io.DataOutput;
32 import java.io.IOException;
33 import java.io.DataInput;
34 import java.lang.reflect.Constructor;
35 import java.lang.reflect.InvocationTargetException;
36 import java.util.Collection;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.ArrayList;
40 import java.util.TreeMap;
41
42
43
44
45 public class MultiResponse implements Writable {
46
47
48
49 private Map<byte[], List<Pair<Integer, Object>>> results =
50 new TreeMap<byte[], List<Pair<Integer, Object>>>(Bytes.BYTES_COMPARATOR);
51
52 public MultiResponse() {
53 }
54
55
56
57
58 public int size() {
59 int size = 0;
60 for (Collection<?> c : results.values()) {
61 size += c.size();
62 }
63 return size;
64 }
65
66
67
68
69
70
71
72
73
74
75 public void add(byte[] regionName, Pair<Integer, Object> r) {
76 List<Pair<Integer, Object>> rs = results.get(regionName);
77 if (rs == null) {
78 rs = new ArrayList<Pair<Integer, Object>>();
79 results.put(regionName, rs);
80 }
81 rs.add(r);
82 }
83
84 public void add(byte []regionName, int originalIndex, Object resOrEx) {
85 add(regionName, new Pair<Integer,Object>(originalIndex, resOrEx));
86 }
87
88 public Map<byte[], List<Pair<Integer, Object>>> getResults() {
89 return results;
90 }
91
92 @Override
93 public void write(DataOutput out) throws IOException {
94 out.writeInt(results.size());
95 for (Map.Entry<byte[], List<Pair<Integer, Object>>> e : results.entrySet()) {
96 Bytes.writeByteArray(out, e.getKey());
97 List<Pair<Integer, Object>> lst = e.getValue();
98 out.writeInt(lst.size());
99 for (Pair<Integer, Object> r : lst) {
100 if (r == null) {
101 out.writeInt(-1);
102 } else {
103 out.writeInt(r.getFirst());
104 Object obj = r.getSecond();
105 if (obj instanceof Throwable) {
106 out.writeBoolean(true);
107
108 Throwable t = (Throwable) obj;
109
110 WritableUtils.writeString(out, t.getClass().getName());
111 WritableUtils.writeString(out,
112 StringUtils.stringifyException(t));
113
114 } else {
115 out.writeBoolean(false);
116
117 if (! (obj instanceof Writable))
118 obj = null;
119 HbaseObjectWritable.writeObject(out, obj, Result.class, null);
120 }
121 }
122 }
123 }
124 }
125
126 @Override
127 public void readFields(DataInput in) throws IOException {
128 results.clear();
129 int mapSize = in.readInt();
130 for (int i = 0; i < mapSize; i++) {
131 byte[] key = Bytes.readByteArray(in);
132 int listSize = in.readInt();
133 List<Pair<Integer, Object>> lst = new ArrayList<Pair<Integer, Object>>(
134 listSize);
135 for (int j = 0; j < listSize; j++) {
136 Integer idx = in.readInt();
137 if (idx == -1) {
138 lst.add(null);
139 } else {
140 boolean isException = in.readBoolean();
141 Object o = null;
142 if (isException) {
143 String klass = WritableUtils.readString(in);
144 String desc = WritableUtils.readString(in);
145 try {
146
147 Class<? extends Throwable> c = (Class<? extends Throwable>) Class.forName(klass);
148 Constructor<? extends Throwable> cn = c.getDeclaredConstructor(String.class);
149 o = cn.newInstance(desc);
150 } catch (ClassNotFoundException ignored) {
151 } catch (NoSuchMethodException ignored) {
152 } catch (InvocationTargetException ignored) {
153 } catch (InstantiationException ignored) {
154 } catch (IllegalAccessException ignored) {
155 }
156 } else {
157 o = HbaseObjectWritable.readObject(in, null);
158 }
159 lst.add(new Pair<Integer, Object>(idx, o));
160 }
161 }
162 results.put(key, lst);
163 }
164 }
165
166 }