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.Iterator;
022import java.util.NoSuchElementException;
023import org.apache.hadoop.hbase.Cell;
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 RowResultGenerator extends ResultGenerator {
038  private static final Logger LOG = LoggerFactory.getLogger(RowResultGenerator.class);
039
040  private Iterator<Cell> valuesI;
041  private Cell cache;
042
043  public RowResultGenerator(final String tableName, final RowSpec rowspec, final Filter filter,
044    final boolean cacheBlocks) throws IllegalArgumentException, IOException {
045    try (Table table = RESTServlet.getInstance().getTable(tableName)) {
046      Get get = new Get(rowspec.getRow());
047      if (rowspec.hasColumns()) {
048        for (byte[] col : rowspec.getColumns()) {
049          byte[][] split = CellUtil.parseColumn(col);
050          if (split.length == 1) {
051            get.addFamily(split[0]);
052          } else if (split.length == 2) {
053            get.addColumn(split[0], split[1]);
054          } else {
055            throw new IllegalArgumentException("Invalid column specifier.");
056          }
057        }
058      }
059      if (rowspec.isPartialTimeRange()) {
060        get.setTimestamp(rowspec.getTimestamp());
061      } else {
062        get.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime());
063      }
064
065      get.readVersions(rowspec.getMaxVersions());
066      if (filter != null) {
067        get.setFilter(filter);
068      }
069      get.setCacheBlocks(cacheBlocks);
070      Result result = table.get(get);
071      if (result != null && !result.isEmpty()) {
072        valuesI = result.listCells().iterator();
073      }
074    } catch (DoNotRetryIOException e) {
075      // Warn here because Stargate will return 404 in the case if multiple
076      // column families were specified but one did not exist -- currently
077      // HBase will fail the whole Get.
078      // Specifying multiple columns in a URI should be uncommon usage but
079      // help to avoid confusion by leaving a record of what happened here in
080      // the log.
081      LOG.warn(StringUtils.stringifyException(e));
082      // Lets get the exception rethrown to get a more meaningful error message than 404
083      if (e instanceof AccessDeniedException) {
084        throw e;
085      }
086    }
087  }
088
089  @Override
090  public void close() {
091  }
092
093  @Override
094  public boolean hasNext() {
095    if (cache != null) {
096      return true;
097    }
098    if (valuesI == null) {
099      return false;
100    }
101    return valuesI.hasNext();
102  }
103
104  @Override
105  public Cell next() {
106    if (cache != null) {
107      Cell kv = cache;
108      cache = null;
109      return kv;
110    }
111    if (valuesI == null) {
112      return null;
113    }
114    try {
115      return valuesI.next();
116    } catch (NoSuchElementException e) {
117      return null;
118    }
119  }
120
121  @Override
122  public void putBack(Cell kv) {
123    this.cache = kv;
124  }
125
126  @Override
127  public void remove() {
128    throw new UnsupportedOperationException("remove not supported");
129  }
130}