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.protobuf.generated.ClientProtos;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.apache.hadoop.hbase.util.Pair;
33  
34  /**
35   * A container for Result objects, grouped by regionName.
36   */
37  @InterfaceAudience.Private
38  public class MultiResponse {
39  
40  
41    // map of regionName to map of Results by the original index for that Result
42    private Map<byte[], RegionResult> results =
43      new TreeMap<byte[], RegionResult>(Bytes.BYTES_COMPARATOR);
44  
45    /**
46     * The server can send us a failure for the region itself, instead of individual failure.
47     * It's a part of the protobuf definition.
48     */
49    private Map<byte[], Throwable> exceptions =
50        new TreeMap<byte[], Throwable>(Bytes.BYTES_COMPARATOR);
51  
52    public MultiResponse() {
53      super();
54    }
55  
56    /**
57     * @return Number of pairs in this container
58     */
59    public int size() {
60      int size = 0;
61      for (RegionResult result: results.values()) {
62        size += result.size();
63      }
64      return size;
65    }
66  
67    /**
68     * Add the pair to the container, grouped by the regionName
69     *
70     * @param regionName
71     *          First item in the pair is the original index of the Action
72     *          (request). Second item is the Result. Result will be empty for
73     *          successful Put and Delete actions.
74     */
75    public void add(byte[] regionName, int originalIndex, Object resOrEx) {
76      getResult(regionName).addResult(originalIndex, resOrEx);
77    }
78  
79    public void addException(byte []regionName, Throwable ie){
80      exceptions.put(regionName, ie);
81    }
82  
83    /**
84     * @return the exception for the region, if any. Null otherwise.
85     */
86    public Throwable getException(byte []regionName){
87      return exceptions.get(regionName);
88    }
89  
90    public Map<byte[], Throwable> getExceptions() {
91      return exceptions;
92    }
93  
94    public void addStatistic(byte[] regionName, ClientProtos.RegionLoadStats stat) {
95      getResult(regionName).setStat(stat);
96    }
97  
98    private RegionResult getResult(byte[] region){
99      RegionResult rs = results.get(region);
100     if (rs == null) {
101       rs = new RegionResult();
102       results.put(region, rs);
103     }
104     return rs;
105   }
106 
107   public Map<byte[], RegionResult> getResults(){
108     return this.results;
109   }
110 
111   static class RegionResult{
112     Map<Integer, Object> result = new HashMap<Integer, Object>();
113     ClientProtos.RegionLoadStats stat;
114 
115     public void addResult(int index, Object result){
116       this.result.put(index, result);
117     }
118 
119     public void setStat(ClientProtos.RegionLoadStats stat){
120       this.stat = stat;
121     }
122 
123     public int size() {
124       return this.result.size();
125     }
126 
127     public ClientProtos.RegionLoadStats getStat() {
128       return this.stat;
129     }
130   }
131 }