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 095 if (rowspec.isPartialTimeRange()) { 096 scan.setTimestamp(rowspec.getTimestamp()); 097 } else { 098 scan.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime()); 099 } 100 101 scan.readVersions(rowspec.getMaxVersions()); 102 if (filter != null) { 103 scan.setFilter(filter); 104 } 105 if (caching > 0) { 106 scan.setCaching(caching); 107 } 108 if (limit > 0) { 109 scan.setLimit(limit); 110 } 111 scan.setCacheBlocks(cacheBlocks); 112 if (rowspec.hasLabels()) { 113 scan.setAuthorizations(new Authorizations(rowspec.getLabels())); 114 } 115 scanner = table.getScanner(scan); 116 cached = null; 117 id = Long.toString(EnvironmentEdgeManager.currentTime()) 118 + Integer.toHexString(scanner.hashCode()); 119 } finally { 120 table.close(); 121 } 122 } 123 124 public String getID() { 125 return id; 126 } 127 128 @Override 129 public void close() { 130 if (scanner != null) { 131 scanner.close(); 132 scanner = null; 133 } 134 } 135 136 @Override 137 public boolean hasNext() { 138 if (cache != null) { 139 return true; 140 } 141 if (rowI != null && rowI.hasNext()) { 142 return true; 143 } 144 if (cached != null) { 145 return true; 146 } 147 try { 148 Result result = scanner.next(); 149 if (result != null && !result.isEmpty()) { 150 cached = result; 151 } 152 } catch (UnknownScannerException e) { 153 throw new IllegalArgumentException(e); 154 } catch (IOException e) { 155 LOG.error(StringUtils.stringifyException(e)); 156 } 157 return cached != null; 158 } 159 160 @Override 161 public Cell next() { 162 if (cache != null) { 163 Cell kv = cache; 164 cache = null; 165 return kv; 166 } 167 boolean loop; 168 do { 169 loop = false; 170 if (rowI != null) { 171 if (rowI.hasNext()) { 172 return rowI.next(); 173 } else { 174 rowI = null; 175 } 176 } 177 if (cached != null) { 178 rowI = cached.listCells().iterator(); 179 loop = true; 180 cached = null; 181 } else { 182 Result result = null; 183 try { 184 result = scanner.next(); 185 } catch (UnknownScannerException e) { 186 throw new IllegalArgumentException(e); 187 } catch (TableNotEnabledException tnee) { 188 throw new IllegalStateException(tnee); 189 } catch (TableNotFoundException tnfe) { 190 throw new IllegalArgumentException(tnfe); 191 } catch (IOException e) { 192 LOG.error(StringUtils.stringifyException(e)); 193 } 194 if (result != null && !result.isEmpty()) { 195 rowI = result.listCells().iterator(); 196 loop = true; 197 } 198 } 199 } while (loop); 200 return null; 201 } 202 203 @Override 204 public void putBack(Cell kv) { 205 this.cache = kv; 206 } 207 208 @Override 209 public void remove() { 210 throw new UnsupportedOperationException("remove not supported"); 211 } 212}