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.HServerAddress;
24 import org.apache.hadoop.hbase.util.Bytes;
25 import org.apache.hadoop.io.Writable;
26
27 import java.io.DataInput;
28 import java.io.DataOutput;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.TreeMap;
35
36
37
38
39 public class MultiPut implements Writable {
40 public HServerAddress address;
41
42
43 public Map<byte[], List<Put> > puts = new TreeMap<byte[], List<Put>>(Bytes.BYTES_COMPARATOR);
44
45
46
47
48 public MultiPut() {}
49
50
51
52
53
54 public MultiPut(HServerAddress a) {
55 address = a;
56 }
57
58 public int size() {
59 int size = 0;
60 for( List<Put> l : puts.values()) {
61 size += l.size();
62 }
63 return size;
64 }
65
66 public void add(byte[] regionName, Put aPut) {
67 List<Put> rsput = puts.get(regionName);
68 if (rsput == null) {
69 rsput = new ArrayList<Put>();
70 puts.put(regionName, rsput);
71 }
72 rsput.add(aPut);
73 }
74
75 public Collection<Put> allPuts() {
76 List<Put> res = new ArrayList<Put>();
77 for ( List<Put> pp : puts.values() ) {
78 res.addAll(pp);
79 }
80 return res;
81 }
82
83
84 @Override
85 public void write(DataOutput out) throws IOException {
86 out.writeInt(puts.size());
87 for( Map.Entry<byte[],List<Put>> e : puts.entrySet()) {
88 Bytes.writeByteArray(out, e.getKey());
89
90 List<Put> ps = e.getValue();
91 out.writeInt(ps.size());
92 for( Put p : ps ) {
93 p.write(out);
94 }
95 }
96 }
97
98 @Override
99 public void readFields(DataInput in) throws IOException {
100 puts.clear();
101
102 int mapSize = in.readInt();
103
104 for (int i = 0 ; i < mapSize; i++) {
105 byte[] key = Bytes.readByteArray(in);
106
107 int listSize = in.readInt();
108 List<Put> ps = new ArrayList<Put>(listSize);
109 for ( int j = 0 ; j < listSize; j++ ) {
110 Put put = new Put();
111 put.readFields(in);
112 ps.add(put);
113 }
114 puts.put(key, ps);
115 }
116 }
117 }