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