View Javadoc

1   /*
2    * Copyright 2009 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.client;
22  
23  import org.apache.hadoop.io.Writable;
24  import org.apache.hadoop.hbase.io.HbaseObjectWritable;
25  import org.apache.hadoop.hbase.util.Bytes;
26  import org.apache.hadoop.hbase.util.Pair;
27  import org.apache.hadoop.hbase.HServerAddress;
28  import org.apache.hadoop.io.WritableUtils;
29  import org.apache.hadoop.util.StringUtils;
30  
31  import java.io.DataOutput;
32  import java.io.IOException;
33  import java.io.DataInput;
34  import java.lang.reflect.Constructor;
35  import java.lang.reflect.InvocationTargetException;
36  import java.util.Collection;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.ArrayList;
40  import java.util.TreeMap;
41  
42  /**
43   * A container for Result objects, grouped by regionName.
44   */
45  public class MultiResponse implements Writable {
46  
47    // map of regionName to list of (Results paired to the original index for that
48    // Result)
49    private Map<byte[], List<Pair<Integer, Object>>> results =
50        new TreeMap<byte[], List<Pair<Integer, Object>>>(Bytes.BYTES_COMPARATOR);
51  
52    public MultiResponse() {
53    }
54  
55    /**
56     * @return Number of pairs in this container
57     */
58    public int size() {
59      int size = 0;
60      for (Collection<?> c : results.values()) {
61        size += c.size();
62      }
63      return size;
64    }
65  
66    /**
67     * Add the pair to the container, grouped by the regionName
68     *
69     * @param regionName
70     * @param r
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, Pair<Integer, Object> r) {
76      List<Pair<Integer, Object>> rs = results.get(regionName);
77      if (rs == null) {
78        rs = new ArrayList<Pair<Integer, Object>>();
79        results.put(regionName, rs);
80      }
81      rs.add(r);
82    }
83  
84    public void add(byte []regionName, int originalIndex, Object resOrEx) {
85      add(regionName, new Pair<Integer,Object>(originalIndex, resOrEx));
86    }
87  
88    public Map<byte[], List<Pair<Integer, Object>>> getResults() {
89      return results;
90    }
91  
92    @Override
93    public void write(DataOutput out) throws IOException {
94      out.writeInt(results.size());
95      for (Map.Entry<byte[], List<Pair<Integer, Object>>> e : results.entrySet()) {
96        Bytes.writeByteArray(out, e.getKey());
97        List<Pair<Integer, Object>> lst = e.getValue();
98        out.writeInt(lst.size());
99        for (Pair<Integer, Object> r : lst) {
100         if (r == null) {
101           out.writeInt(-1); // Cant have index -1; on other side we recognize -1 as 'null'
102         } else {
103           out.writeInt(r.getFirst()); // Can this can npe!?!
104           Object obj = r.getSecond();
105           if (obj instanceof Throwable) {
106             out.writeBoolean(true); // true, Throwable/exception.
107 
108             Throwable t = (Throwable) obj;
109             // serialize exception
110             WritableUtils.writeString(out, t.getClass().getName());
111             WritableUtils.writeString(out,
112                 StringUtils.stringifyException(t));
113 
114           } else {
115             out.writeBoolean(false); // no exception
116 
117             if (! (obj instanceof Writable))
118               obj = null; // squash all non-writables to null.
119             HbaseObjectWritable.writeObject(out, r.getSecond(),
120                 obj != null ? obj.getClass() : Writable.class, null);
121           }
122         }
123       }
124     }
125   }
126 
127   @Override
128   public void readFields(DataInput in) throws IOException {
129     results.clear();
130     int mapSize = in.readInt();
131     for (int i = 0; i < mapSize; i++) {
132       byte[] key = Bytes.readByteArray(in);
133       int listSize = in.readInt();
134       List<Pair<Integer, Object>> lst = new ArrayList<Pair<Integer, Object>>(
135           listSize);
136       for (int j = 0; j < listSize; j++) {
137         Integer idx = in.readInt();
138         if (idx == -1) {
139           lst.add(null);
140         } else {
141           boolean isException = in.readBoolean();
142           Object o = null;
143           if (isException) {
144             String klass = WritableUtils.readString(in);
145             String desc = WritableUtils.readString(in);
146             try {
147               // the type-unsafe insertion, but since we control what klass is..
148               Class<? extends Throwable> c = (Class<? extends Throwable>) Class.forName(klass);
149               Constructor<? extends Throwable> cn = c.getDeclaredConstructor(String.class);
150               o = cn.newInstance(desc);
151             } catch (ClassNotFoundException ignored) {
152             } catch (NoSuchMethodException ignored) {
153             } catch (InvocationTargetException ignored) {
154             } catch (InstantiationException ignored) {
155             } catch (IllegalAccessException ignored) {
156             }
157           } else {
158             o = HbaseObjectWritable.readObject(in, null);
159           }
160           lst.add(new Pair<Integer, Object>(idx, o));
161         }
162       }
163       results.put(key, lst);
164     }
165   }
166 
167 }