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