View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
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   * Data type class for putting multiple regions worth of puts in one RPC.
38   */
39  public class MultiPut implements Writable {
40    public HServerAddress address; // client code ONLY
41  
42    // map of regions to lists of puts for that region.
43    public Map<byte[], List<Put> > puts = new TreeMap<byte[], List<Put>>(Bytes.BYTES_COMPARATOR);
44  
45    /**
46     * Writable constructor only.
47     */
48    public MultiPut() {}
49  
50    /**
51     * MultiPut for putting multiple regions worth of puts in one RPC.
52     * @param a address
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 }