001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.rest;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.List;
024import org.apache.hadoop.hbase.CellUtil;
025import org.apache.hadoop.hbase.DoNotRetryIOException;
026import org.apache.hadoop.hbase.client.Get;
027import org.apache.hadoop.hbase.client.Result;
028import org.apache.hadoop.hbase.client.Table;
029import org.apache.hadoop.hbase.filter.Filter;
030import org.apache.hadoop.hbase.security.AccessDeniedException;
031import org.apache.hadoop.util.StringUtils;
032import org.apache.yetus.audience.InterfaceAudience;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036@InterfaceAudience.Private
037public class MultiRowResultReader {
038
039  private static final Logger LOG = LoggerFactory.getLogger(MultiRowResultReader.class);
040
041  private Result[] results;
042
043  public MultiRowResultReader(final String tableName, final Collection<RowSpec> rowspecs,
044    final Filter filter, final boolean cacheBlocks) throws IOException {
045    try (Table table = RESTServlet.getInstance().getTable(tableName)) {
046      List<Get> gets = new ArrayList<>(rowspecs.size());
047      for (RowSpec rowspec : rowspecs) {
048        Get get = new Get(rowspec.getRow());
049        if (rowspec.hasColumns()) {
050          for (byte[] col : rowspec.getColumns()) {
051            byte[][] split = CellUtil.parseColumn(col);
052            if (split.length == 1) {
053              get.addFamily(split[0]);
054            } else if (split.length == 2) {
055              get.addColumn(split[0], split[1]);
056            } else {
057              throw new IllegalArgumentException("Invalid column specifier.");
058            }
059          }
060        }
061        get.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime());
062        get.readVersions(rowspec.getMaxVersions());
063        if (filter != null) {
064          get.setFilter(filter);
065        }
066        get.setCacheBlocks(cacheBlocks);
067        gets.add(get);
068      }
069      results = table.get(gets);
070    } catch (DoNotRetryIOException e) {
071      // TODO this is copied from RowResultGenerator, but we probably shouldn't swallow
072      // every type of exception but AccessDeniedException
073      LOG.warn(StringUtils.stringifyException(e));
074      // Lets get the exception rethrown to get a more meaningful error message than 404
075      if (e instanceof AccessDeniedException) {
076        throw e;
077      }
078    }
079  }
080
081  public Result[] getResults() {
082    return results;
083  }
084
085}