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