001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.rest;
021
022import java.io.IOException;
023import java.util.Iterator;
024import java.util.NoSuchElementException;
025
026import org.apache.hadoop.hbase.Cell;
027import org.apache.hadoop.hbase.CellUtil;
028import org.apache.hadoop.hbase.DoNotRetryIOException;
029import org.apache.hadoop.hbase.security.AccessDeniedException;
030import org.apache.hadoop.hbase.client.Get;
031import org.apache.hadoop.hbase.client.Result;
032import org.apache.hadoop.hbase.client.Table;
033import org.apache.hadoop.hbase.filter.Filter;
034import org.apache.hadoop.util.StringUtils;
035import org.apache.yetus.audience.InterfaceAudience;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039@InterfaceAudience.Private
040public class RowResultGenerator extends ResultGenerator {
041  private static final Logger LOG = LoggerFactory.getLogger(RowResultGenerator.class);
042
043  private Iterator<Cell> valuesI;
044  private Cell cache;
045
046  public RowResultGenerator(final String tableName, final RowSpec rowspec,
047      final Filter filter, final boolean cacheBlocks)
048      throws IllegalArgumentException, IOException {
049    Table table = RESTServlet.getInstance().getTable(tableName);
050    try {
051      Get get = new Get(rowspec.getRow());
052      if (rowspec.hasColumns()) {
053        for (byte[] col: rowspec.getColumns()) {
054          byte[][] split = CellUtil.parseColumn(col);
055          if (split.length == 1) {
056            get.addFamily(split[0]);
057          } else if (split.length == 2) {
058            get.addColumn(split[0], split[1]);
059          } else {
060            throw new IllegalArgumentException("Invalid column specifier.");
061          }
062        }
063      }
064      get.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime());
065      get.setMaxVersions(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    } finally {
087      table.close();
088    }
089  }
090
091  @Override
092  public void close() {
093  }
094
095  @Override
096  public boolean hasNext() {
097    if (cache != null) {
098      return true;
099    }
100    if (valuesI == null) {
101      return false;
102    }
103    return valuesI.hasNext();
104  }
105
106  @Override
107  public Cell next() {
108    if (cache != null) {
109      Cell kv = cache;
110      cache = null;
111      return kv;
112    }
113    if (valuesI == null) {
114      return null;
115    }
116    try {
117      return valuesI.next();
118    } catch (NoSuchElementException e) {
119      return null;
120    }
121  }
122
123  @Override
124  public void putBack(Cell kv) {
125    this.cache = kv;
126  }
127
128  @Override
129  public void remove() {
130    throw new UnsupportedOperationException("remove not supported");
131  }
132}