View Javadoc

1   /*
2    * Copyright 2009 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  package org.apache.hadoop.hbase.client;
21  
22  import org.apache.hadoop.io.Writable;
23  import org.apache.hadoop.hbase.io.HbaseObjectWritable;
24  import org.apache.hadoop.hbase.util.Bytes;
25  
26  import java.io.DataOutput;
27  import java.io.IOException;
28  import java.io.DataInput;
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Set;
33  import java.util.TreeMap;
34  
35  /**
36   * Container for Actions (i.e. Get, Delete, or Put), which are grouped by
37   * regionName. Intended to be used with HConnectionManager.processBatch()
38   */
39  public final class MultiAction<R> implements Writable {
40  
41    // map of regions to lists of puts/gets/deletes for that region.
42    public Map<byte[], List<Action<R>>> actions =
43      new TreeMap<byte[], List<Action<R>>>(
44        Bytes.BYTES_COMPARATOR);
45  
46    public MultiAction() {
47    }
48  
49    /**
50     * Get the total number of Actions
51     *
52     * @return total number of Actions for all groups in this container.
53     */
54    public int size() {
55      int size = 0;
56      for (List l : actions.values()) {
57        size += l.size();
58      }
59      return size;
60    }
61  
62    /**
63     * Add an Action to this container based on it's regionName. If the regionName
64     * is wrong, the initial execution will fail, but will be automatically
65     * retried after looking up the correct region.
66     *
67     * @param regionName
68     * @param a
69     */
70    public void add(byte[] regionName, Action<R> a) {
71      List<Action<R>> rsActions = actions.get(regionName);
72      if (rsActions == null) {
73        rsActions = new ArrayList<Action<R>>();
74        actions.put(regionName, rsActions);
75      }
76      rsActions.add(a);
77    }
78  
79    public Set<byte[]> getRegions() {
80      return actions.keySet();
81    }
82  
83    /**
84     * @return All actions from all regions in this container
85     */
86    public List<Action<R>> allActions() {
87      List<Action<R>> res = new ArrayList<Action<R>>();
88      for (List<Action<R>> lst : actions.values()) {
89        res.addAll(lst);
90      }
91      return res;
92    }
93  
94    @Override
95    public void write(DataOutput out) throws IOException {
96      out.writeInt(actions.size());
97      for (Map.Entry<byte[], List<Action<R>>> e : actions.entrySet()) {
98        Bytes.writeByteArray(out, e.getKey());
99        List<Action<R>> lst = e.getValue();
100       out.writeInt(lst.size());
101       for (Action a : lst) {
102         HbaseObjectWritable.writeObject(out, a, a.getClass(), null);
103       }
104     }
105   }
106 
107   @Override
108   public void readFields(DataInput in) throws IOException {
109     actions.clear();
110     int mapSize = in.readInt();
111     for (int i = 0; i < mapSize; i++) {
112       byte[] key = Bytes.readByteArray(in);
113       int listSize = in.readInt();
114       List<Action<R>> lst = new ArrayList<Action<R>>(listSize);
115       for (int j = 0; j < listSize; j++) {
116         lst.add((Action) HbaseObjectWritable.readObject(in, null));
117       }
118       actions.put(key, lst);
119     }
120   }
121 
122 }