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 org.apache.hadoop.hbase.Cell;
023import org.apache.hadoop.hbase.CellUtil;
024import org.apache.hadoop.hbase.TableNotEnabledException;
025import org.apache.hadoop.hbase.TableNotFoundException;
026import org.apache.hadoop.hbase.UnknownScannerException;
027import org.apache.hadoop.hbase.client.Result;
028import org.apache.hadoop.hbase.client.ResultScanner;
029import org.apache.hadoop.hbase.client.Scan;
030import org.apache.hadoop.hbase.client.Table;
031import org.apache.hadoop.hbase.filter.Filter;
032import org.apache.hadoop.hbase.rest.model.ScannerModel;
033import org.apache.hadoop.hbase.security.visibility.Authorizations;
034import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
035import org.apache.hadoop.util.StringUtils;
036import org.apache.yetus.audience.InterfaceAudience;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040@InterfaceAudience.Private
041public class ScannerResultGenerator extends ResultGenerator {
042
043  private static final Logger LOG = LoggerFactory.getLogger(ScannerResultGenerator.class);
044
045  public static Filter buildFilterFromModel(final ScannerModel model) throws Exception {
046    String filter = model.getFilter();
047    if (filter == null || filter.length() == 0) {
048      return null;
049    }
050    return buildFilter(filter);
051  }
052
053  private String id;
054  private Iterator<Cell> rowI;
055  private Cell cache;
056  private ResultScanner scanner;
057  private Result cached;
058
059  public ScannerResultGenerator(final String tableName, final RowSpec rowspec, final Filter filter,
060    final boolean cacheBlocks) throws IllegalArgumentException, IOException {
061    this(tableName, rowspec, filter, -1, cacheBlocks);
062  }
063
064  public ScannerResultGenerator(final String tableName, final RowSpec rowspec, final Filter filter,
065    final int caching, final boolean cacheBlocks) throws IllegalArgumentException, IOException {
066    Table table = RESTServlet.getInstance().getTable(tableName);
067    try {
068      Scan scan;
069      if (rowspec.hasEndRow()) {
070        scan = new Scan(rowspec.getStartRow(), rowspec.getEndRow());
071      } else {
072        scan = new Scan(rowspec.getStartRow());
073      }
074      if (rowspec.hasColumns()) {
075        byte[][] columns = rowspec.getColumns();
076        for (byte[] column : columns) {
077          byte[][] split = CellUtil.parseColumn(column);
078          if (split.length == 1) {
079            scan.addFamily(split[0]);
080          } else if (split.length == 2) {
081            scan.addColumn(split[0], split[1]);
082          } else {
083            throw new IllegalArgumentException("Invalid familyAndQualifier provided.");
084          }
085        }
086      }
087      scan.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime());
088      scan.setMaxVersions(rowspec.getMaxVersions());
089      if (filter != null) {
090        scan.setFilter(filter);
091      }
092      if (caching > 0) {
093        scan.setCaching(caching);
094      }
095      scan.setCacheBlocks(cacheBlocks);
096      if (rowspec.hasLabels()) {
097        scan.setAuthorizations(new Authorizations(rowspec.getLabels()));
098      }
099      scanner = table.getScanner(scan);
100      cached = null;
101      id = Long.toString(EnvironmentEdgeManager.currentTime())
102        + Integer.toHexString(scanner.hashCode());
103    } finally {
104      table.close();
105    }
106  }
107
108  public String getID() {
109    return id;
110  }
111
112  @Override
113  public void close() {
114    if (scanner != null) {
115      scanner.close();
116      scanner = null;
117    }
118  }
119
120  @Override
121  public boolean hasNext() {
122    if (cache != null) {
123      return true;
124    }
125    if (rowI != null && rowI.hasNext()) {
126      return true;
127    }
128    if (cached != null) {
129      return true;
130    }
131    try {
132      Result result = scanner.next();
133      if (result != null && !result.isEmpty()) {
134        cached = result;
135      }
136    } catch (UnknownScannerException e) {
137      throw new IllegalArgumentException(e);
138    } catch (IOException e) {
139      LOG.error(StringUtils.stringifyException(e));
140    }
141    return cached != null;
142  }
143
144  @Override
145  public Cell next() {
146    if (cache != null) {
147      Cell kv = cache;
148      cache = null;
149      return kv;
150    }
151    boolean loop;
152    do {
153      loop = false;
154      if (rowI != null) {
155        if (rowI.hasNext()) {
156          return rowI.next();
157        } else {
158          rowI = null;
159        }
160      }
161      if (cached != null) {
162        rowI = cached.listCells().iterator();
163        loop = true;
164        cached = null;
165      } else {
166        Result result = null;
167        try {
168          result = scanner.next();
169        } catch (UnknownScannerException e) {
170          throw new IllegalArgumentException(e);
171        } catch (TableNotEnabledException tnee) {
172          throw new IllegalStateException(tnee);
173        } catch (TableNotFoundException tnfe) {
174          throw new IllegalArgumentException(tnfe);
175        } catch (IOException e) {
176          LOG.error(StringUtils.stringifyException(e));
177        }
178        if (result != null && !result.isEmpty()) {
179          rowI = result.listCells().iterator();
180          loop = true;
181        }
182      }
183    } while (loop);
184    return null;
185  }
186
187  @Override
188  public void putBack(Cell kv) {
189    this.cache = kv;
190  }
191
192  @Override
193  public void remove() {
194    throw new UnsupportedOperationException("remove not supported");
195  }
196}