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.List;
25 import java.util.Map;
26 import java.util.TreeMap;
27
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.hbase.util.Bytes;
30 import org.apache.hadoop.hbase.util.Pair;
31
32 /**
33 * A container for Result objects, grouped by regionName.
34 */
35 @InterfaceAudience.Private
36 public class MultiResponse {
37
38 // map of regionName to list of (Results paired to the original index for that
39 // Result)
40 private Map<byte[], List<Pair<Integer, Object>>> results =
41 new TreeMap<byte[], List<Pair<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 (Collection<?> 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 r
70 * First item in the pair is the original index of the Action
71 * (request). Second item is the Result. Result will be empty for
72 * successful Put and Delete actions.
73 */
74 public void add(byte[] regionName, Pair<Integer, Object> r) {
75 List<Pair<Integer, Object>> rs = results.get(regionName);
76 if (rs == null) {
77 rs = new ArrayList<Pair<Integer, Object>>();
78 results.put(regionName, rs);
79 }
80 rs.add(r);
81 }
82
83 public void add(byte []regionName, int originalIndex, Object resOrEx) {
84 add(regionName, new Pair<Integer,Object>(originalIndex, resOrEx));
85 }
86
87 public Map<byte[], List<Pair<Integer, Object>>> getResults() {
88 return results;
89 }
90
91 public void addException(byte []regionName, Throwable ie){
92 exceptions.put(regionName, ie);
93 }
94
95 /**
96 * @return the exception for the region, if any. Null otherwise.
97 */
98 public Throwable getException(byte []regionName){
99 return exceptions.get(regionName);
100 }
101
102 public Map<byte[], Throwable> getExceptions() {
103 return exceptions;
104 }
105 }