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 this(tableName, rowspec, filter, caching, cacheBlocks, -1); 067 } 068 069 public ScannerResultGenerator(final String tableName, final RowSpec rowspec, final Filter filter, 070 final int caching, final boolean cacheBlocks, int limit) throws IOException { 071 Table table = RESTServlet.getInstance().getTable(tableName); 072 try { 073 Scan scan; 074 if (rowspec.hasEndRow()) { 075 scan = new Scan().withStartRow(rowspec.getStartRow()).withStopRow(rowspec.getEndRow()); 076 } else { 077 scan = new Scan().withStartRow(rowspec.getStartRow()); 078 } 079 if (rowspec.hasColumns()) { 080 byte[][] columns = rowspec.getColumns(); 081 for (byte[] column : columns) { 082 byte[][] split = CellUtil.parseColumn(column); 083 if (split.length == 1) { 084 scan.addFamily(split[0]); 085 } else if (split.length == 2) { 086 scan.addColumn(split[0], split[1]); 087 } else { 088 throw new IllegalArgumentException("Invalid familyAndQualifier provided."); 089 } 090 } 091 } 092 scan.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime()); 093 scan.readVersions(rowspec.getMaxVersions()); 094 if (filter != null) { 095 scan.setFilter(filter); 096 } 097 if (caching > 0) { 098 scan.setCaching(caching); 099 } 100 if (limit > 0) { 101 scan.setLimit(limit); 102 } 103 scan.setCacheBlocks(cacheBlocks); 104 if (rowspec.hasLabels()) { 105 scan.setAuthorizations(new Authorizations(rowspec.getLabels())); 106 } 107 scanner = table.getScanner(scan); 108 cached = null; 109 id = Long.toString(EnvironmentEdgeManager.currentTime()) 110 + Integer.toHexString(scanner.hashCode()); 111 } finally { 112 table.close(); 113 } 114 } 115 116 public String getID() { 117 return id; 118 } 119 120 @Override 121 public void close() { 122 if (scanner != null) { 123 scanner.close(); 124 scanner = null; 125 } 126 } 127 128 @Override 129 public boolean hasNext() { 130 if (cache != null) { 131 return true; 132 } 133 if (rowI != null && rowI.hasNext()) { 134 return true; 135 } 136 if (cached != null) { 137 return true; 138 } 139 try { 140 Result result = scanner.next(); 141 if (result != null && !result.isEmpty()) { 142 cached = result; 143 } 144 } catch (UnknownScannerException e) { 145 throw new IllegalArgumentException(e); 146 } catch (IOException e) { 147 LOG.error(StringUtils.stringifyException(e)); 148 } 149 return cached != null; 150 } 151 152 @Override 153 public Cell next() { 154 if (cache != null) { 155 Cell kv = cache; 156 cache = null; 157 return kv; 158 } 159 boolean loop; 160 do { 161 loop = false; 162 if (rowI != null) { 163 if (rowI.hasNext()) { 164 return rowI.next(); 165 } else { 166 rowI = null; 167 } 168 } 169 if (cached != null) { 170 rowI = cached.listCells().iterator(); 171 loop = true; 172 cached = null; 173 } else { 174 Result result = null; 175 try { 176 result = scanner.next(); 177 } catch (UnknownScannerException e) { 178 throw new IllegalArgumentException(e); 179 } catch (TableNotEnabledException tnee) { 180 throw new IllegalStateException(tnee); 181 } catch (TableNotFoundException tnfe) { 182 throw new IllegalArgumentException(tnfe); 183 } catch (IOException e) { 184 LOG.error(StringUtils.stringifyException(e)); 185 } 186 if (result != null && !result.isEmpty()) { 187 rowI = result.listCells().iterator(); 188 loop = true; 189 } 190 } 191 } while (loop); 192 return null; 193 } 194 195 @Override 196 public void putBack(Cell kv) { 197 this.cache = kv; 198 } 199 200 @Override 201 public void remove() { 202 throw new UnsupportedOperationException("remove not supported"); 203 } 204}