View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.client;
21  
22  import org.apache.hadoop.classification.InterfaceAudience;
23  import org.apache.hadoop.classification.InterfaceStability;
24  import org.apache.hadoop.hbase.util.Bytes;
25  import org.apache.hadoop.hbase.util.Pair;
26  
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.TreeMap;
32  
33  /**
34   * A container for Result objects, grouped by regionName.
35   */
36  @InterfaceAudience.Public
37  @InterfaceStability.Evolving
38  public class MultiResponse {
39  
40    // map of regionName to list of (Results paired to the original index for that
41    // Result)
42    private Map<byte[], List<Pair<Integer, Object>>> results =
43        new TreeMap<byte[], List<Pair<Integer, Object>>>(Bytes.BYTES_COMPARATOR);
44  
45    public MultiResponse() {
46      super();
47    }
48  
49    /**
50     * @return Number of pairs in this container
51     */
52    public int size() {
53      int size = 0;
54      for (Collection<?> c : results.values()) {
55        size += c.size();
56      }
57      return size;
58    }
59  
60    /**
61     * Add the pair to the container, grouped by the regionName
62     *
63     * @param regionName
64     * @param r
65     *          First item in the pair is the original index of the Action
66     *          (request). Second item is the Result. Result will be empty for
67     *          successful Put and Delete actions.
68     */
69    public void add(byte[] regionName, Pair<Integer, Object> r) {
70      List<Pair<Integer, Object>> rs = results.get(regionName);
71      if (rs == null) {
72        rs = new ArrayList<Pair<Integer, Object>>();
73        results.put(regionName, rs);
74      }
75      rs.add(r);
76    }
77  
78    public void add(byte []regionName, int originalIndex, Object resOrEx) {
79      add(regionName, new Pair<Integer,Object>(originalIndex, resOrEx));
80    }
81  
82    public Map<byte[], List<Pair<Integer, Object>>> getResults() {
83      return results;
84    }
85  }