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