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 java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.TreeMap;
28  
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.apache.hadoop.hbase.util.Pair;
32  
33  /**
34   * A container for Result objects, grouped by regionName.
35   */
36  @InterfaceAudience.Private
37  public class MultiResponse {
38  
39    // map of regionName to map of Results by the original index for that Result
40    private Map<byte[], Map<Integer, Object>> results =
41        new TreeMap<byte[], Map<Integer, Object>>(Bytes.BYTES_COMPARATOR);
42  
43    /**
44     * The server can send us a failure for the region itself, instead of individual failure.
45     * It's a part of the protobuf definition.
46     */
47    private Map<byte[], Throwable> exceptions =
48        new TreeMap<byte[], Throwable>(Bytes.BYTES_COMPARATOR);
49  
50    public MultiResponse() {
51      super();
52    }
53  
54    /**
55     * @return Number of pairs in this container
56     */
57    public int size() {
58      int size = 0;
59      for (Map<?,?> c : results.values()) {
60        size += c.size();
61      }
62      return size;
63    }
64  
65    /**
66     * Add the pair to the container, grouped by the regionName
67     *
68     * @param regionName
69     * @param originalIndex the original index of the Action (request).
70     * @param resOrEx the result or error; will be empty for successful Put and Delete actions.
71     */
72    public void add(byte[] regionName, int originalIndex, Object resOrEx) {
73      Map<Integer, Object> rs = results.get(regionName);
74      if (rs == null) {
75        rs = new HashMap<Integer, Object>();
76        results.put(regionName, rs);
77      }
78      rs.put(originalIndex, resOrEx);
79    }
80  
81    public Map<byte[], Map<Integer, Object>> getResults() {
82      return results;
83    }
84  
85    public void addException(byte []regionName, Throwable ie){
86      exceptions.put(regionName, ie);
87    }
88  
89    /**
90     * @return the exception for the region, if any. Null otherwise.
91     */
92    public Throwable getException(byte []regionName){
93      return exceptions.get(regionName);
94    }
95  
96    public Map<byte[], Throwable> getExceptions() {
97      return exceptions;
98    }
99  }