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   * @deprecated Use MultiAction instead
38   * Data type class for putting multiple regions worth of puts in one RPC.
39   */
40  public class MultiPut implements Writable {
41    public HServerAddress address; // client code ONLY
42  
43    // map of regions to lists of puts for that region.
44    public Map<byte[], List<Put> > puts = new TreeMap<byte[], List<Put>>(Bytes.BYTES_COMPARATOR);
45  
46    /**
47     * Writable constructor only.
48     */
49    public MultiPut() {}
50  
51    /**
52     * MultiPut for putting multiple regions worth of puts in one RPC.
53     * @param a address
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 }