De-serializes an hbase.client.Result array. @param buf The buffer that contains a serialized Result[]. @return The results parsed into a list of rows, where each row itself is a list of KeyValue objects.
(final ChannelBuffer buf)
| 1930 | * a list of {@link KeyValue} objects. |
| 1931 | */ |
| 1932 | private static |
| 1933 | ArrayList<ArrayList<KeyValue>> parseResults(final ChannelBuffer buf) { |
| 1934 | final byte version = buf.readByte(); |
| 1935 | if (version != 0x01) { |
| 1936 | LOG.warn("Received unsupported Result[] version: " + version); |
| 1937 | // Keep going anyway, just in case the new version is backwards |
| 1938 | // compatible somehow. If it's not, we'll just fail later. |
| 1939 | } |
| 1940 | final int nresults = buf.readInt(); |
| 1941 | if (nresults < 0) { |
| 1942 | badResponse("Negative number of results=" + nresults + " found in " |
| 1943 | + buf + '=' + Bytes.pretty(buf)); |
| 1944 | } else if (nresults == 0) { |
| 1945 | return null; |
| 1946 | } |
| 1947 | final int length = buf.readInt(); // Guaranteed > 0 as we have > 0 Result. |
| 1948 | HBaseRpc.checkNonEmptyArrayLength(buf, length); |
| 1949 | // Immediately try to "fault" if `length' bytes aren't available. |
| 1950 | ensureReadable(buf, length); |
| 1951 | //LOG.debug("total Result[] response length={}", length); |
| 1952 | //LOG.debug("Result[] "+nresults+" buf="+buf+'='+Bytes.pretty(buf)); |
| 1953 | |
| 1954 | final ArrayList<ArrayList<KeyValue>> results = |
| 1955 | new ArrayList<ArrayList<KeyValue>>(nresults); |
| 1956 | int bytes_read = 0; |
| 1957 | for (int i = 0; i < nresults; i++) { |
| 1958 | final int num_kv = buf.readInt(); |
| 1959 | //LOG.debug("num_kv="+num_kv); |
| 1960 | bytes_read += 4; |
| 1961 | if (num_kv < 0) { |
| 1962 | badResponse("Negative number of KeyValues=" + num_kv + " for Result[" |
| 1963 | + i + "] found in " + buf + '=' + Bytes.pretty(buf)); |
| 1964 | } else if (nresults == 0) { |
| 1965 | continue; |
| 1966 | } |
| 1967 | final ArrayList<KeyValue> result = new ArrayList<KeyValue>(num_kv); |
| 1968 | KeyValue kv = null; |
| 1969 | for (int j = 0; j < num_kv; j++) { |
| 1970 | final int kv_length = buf.readInt(); |
| 1971 | HBaseRpc.checkNonEmptyArrayLength(buf, kv_length); |
| 1972 | //LOG.debug("kv_length="+kv_length); |
| 1973 | kv = KeyValue.fromBuffer(buf, kv); |
| 1974 | result.add(kv); |
| 1975 | bytes_read += 4 + kv_length; |
| 1976 | } |
| 1977 | results.add(result); |
| 1978 | } |
| 1979 | if (length != bytes_read) { // Sanity check. |
| 1980 | badResponse("Result[" + nresults + "] was supposed to be " + length |
| 1981 | + " bytes, but we only read " + bytes_read + " bytes from " |
| 1982 | + buf + '=' + Bytes.pretty(buf)); |
| 1983 | } |
| 1984 | return results; |
| 1985 | } |
| 1986 | |
| 1987 | /** Throws an exception with the given error message. */ |
| 1988 | private static void badResponse(final String errmsg) { |
no test coverage detected