| 151 | } |
| 152 | |
| 153 | public Iterator<Tuple> iterator() throws IOException { |
| 154 | final LoadFunc p; |
| 155 | PigContext pigContext = ScriptState.get().getPigContext(); |
| 156 | if (pigContext == null || store == null) { |
| 157 | throw new IllegalArgumentException(); |
| 158 | } |
| 159 | try { |
| 160 | LoadFunc originalLoadFunc = (LoadFunc) PigContext |
| 161 | .instantiateFuncFromSpec(store.getSFile().getFuncSpec()); |
| 162 | |
| 163 | p = (LoadFunc) new ReadToEndLoader(originalLoadFunc, |
| 164 | ConfigurationUtil.toConfiguration(pigContext |
| 165 | .getProperties()), store.getSFile().getFileName(), |
| 166 | 0); |
| 167 | |
| 168 | } catch (Exception e) { |
| 169 | int errCode = 2088; |
| 170 | String msg = "Unable to get results for: " + store.getSFile(); |
| 171 | throw new ExecException(msg, errCode, PigException.BUG, e); |
| 172 | } |
| 173 | |
| 174 | return new Iterator<Tuple>() { |
| 175 | Tuple t; |
| 176 | boolean atEnd; |
| 177 | |
| 178 | @Override |
| 179 | public boolean hasNext() { |
| 180 | if (atEnd) return false; |
| 181 | try { |
| 182 | if (t == null) t = p.getNext(); |
| 183 | if (t == null) atEnd = true; |
| 184 | } catch (Exception e) { |
| 185 | LOG.error(e); |
| 186 | t = null; |
| 187 | atEnd = true; |
| 188 | throw new Error(e); |
| 189 | } |
| 190 | return !atEnd; |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public Tuple next() { |
| 195 | Tuple next = t; |
| 196 | if (next != null) { |
| 197 | t = null; |
| 198 | return next; |
| 199 | } |
| 200 | try { |
| 201 | next = p.getNext(); |
| 202 | } catch (Exception e) { |
| 203 | LOG.error(e); |
| 204 | } |
| 205 | if (next == null) |
| 206 | atEnd = true; |
| 207 | return next; |
| 208 | } |
| 209 | |
| 210 | @Override |