HBASE-25617 Revisit the span names (#2998)
[hbase.git] / hbase-client / src / main / java / org / apache / hadoop / hbase / client / MultiResponse.java
blob03f168893a71c1f14aeb856d32c60ce017459684
1 /*
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 package org.apache.hadoop.hbase.client;
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.TreeMap;
26 import org.apache.yetus.audience.InterfaceAudience;
27 import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos;
28 import org.apache.hadoop.hbase.util.Bytes;
30 /**
31 * A container for Result objects, grouped by regionName.
33 @InterfaceAudience.Private
34 public class MultiResponse extends AbstractResponse {
36 // map of regionName to map of Results by the original index for that Result
37 private Map<byte[], RegionResult> results = new TreeMap<>(Bytes.BYTES_COMPARATOR);
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.
43 private Map<byte[], Throwable> exceptions =
44 new TreeMap<>(Bytes.BYTES_COMPARATOR);
46 public MultiResponse() {
47 super();
50 /**
51 * @return Number of pairs in this container
53 public int size() {
54 int size = 0;
55 for (RegionResult result: results.values()) {
56 size += result.size();
58 return size;
61 /**
62 * Add the pair to the container, grouped by the regionName
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.
68 public void add(byte[] regionName, int originalIndex, Object resOrEx) {
69 getResult(regionName).addResult(originalIndex, resOrEx);
72 public void addException(byte []regionName, Throwable ie){
73 exceptions.put(regionName, ie);
76 /**
77 * @return the exception for the region, if any. Null otherwise.
79 public Throwable getException(byte []regionName){
80 return exceptions.get(regionName);
83 public Map<byte[], Throwable> getExceptions() {
84 return exceptions;
87 public void addStatistic(byte[] regionName, ClientProtos.RegionLoadStats stat) {
88 getResult(regionName).setStat(stat);
91 private RegionResult getResult(byte[] region){
92 RegionResult rs = results.get(region);
93 if (rs == null) {
94 rs = new RegionResult();
95 results.put(region, rs);
97 return rs;
100 public Map<byte[], RegionResult> getResults(){
101 return this.results;
104 @Override
105 public ResponseType type() {
106 return ResponseType.MULTI;
109 static class RegionResult{
110 Map<Integer, Object> result = new HashMap<>();
111 ClientProtos.RegionLoadStats stat;
113 public void addResult(int index, Object result){
114 this.result.put(index, result);
117 public void setStat(ClientProtos.RegionLoadStats stat){
118 this.stat = stat;
121 public int size() {
122 return this.result.size();
125 public ClientProtos.RegionLoadStats getStat() {
126 return this.stat;