/** {@inheritDoc} */ @Override public void seek(long pos) throws IOException { if (pos < 0) throw new IOException("Seek position cannot be negative: " + pos); is.seek(start + pos); this.pos = pos; }
/** * Constructor. * * @param is Base input stream. * @param start Start position. * @param maxLen Maximum stream length. * @throws IOException In case of exception. */ public IgfsRangeInputStream(IgfsInputStream is, long start, long maxLen) throws IOException { if (is == null) throw new IllegalArgumentException("Input stream cannot be null."); if (start < 0) throw new IllegalArgumentException("Start position cannot be negative."); if (start >= is.length()) throw new IllegalArgumentException("Start position cannot be greater that file length."); if (maxLen < 0) throw new IllegalArgumentException("Length cannot be negative."); if (start + maxLen > is.length()) throw new IllegalArgumentException("Sum of start position and length cannot be greater than file length."); this.is = is; this.start = start; this.maxLen = maxLen; is.seek(start); }
/** * Find first delimiter. In order to achieve this we have to rewind the stream until we find the delimiter * which stands at least [maxDelimLen] from the start search position or until we faced stream start. * Otherwise we cannot be sure that delimiter position is determined correctly. * * @param stream IGFS input stream. * @param startPos Start search position. * @return The first found delimiter. * @throws IOException In case of IO exception. */ @Nullable private IgniteBiTuple<State, Delimiter> findFirstDelimiter(IgfsInputStream stream, long startPos) throws IOException { State state; Delimiter delim; long curPos = Math.max(0, startPos - maxDelimLen); while (true) { stream.seek(curPos); state = new State(); delim = nextDelimiter(stream, state); if (curPos == 0 || delim == null || delim.start - curPos > maxDelimLen - 1) break; else curPos = Math.max(0, curPos - maxDelimLen); } return F.t(state, delim); }
/** {@inheritDoc} */ @Override public final Object execute(IgniteFileSystem igfs, IgfsFileRange range, IgfsInputStream in) throws IgniteException, IOException { in.seek(range.start()); return execute(igfs, new IgfsRangeInputStream(in, range)); }
@Override public Object call() throws Exception { IgfsInputStream in0 = igfs.open(FILE); in0.seek(blockSize * 2); try { in0.read(readBuf); } finally { U.closeQuiet(in0); } return null; } }, IOException.class,
/** {@inheritDoc} */ @Override public Object execute(IgniteFileSystem igfs, IgfsFileRange range, IgfsInputStream in) throws IOException { assert ignite != null; assert ses != null; assert ctx != null; in.seek(range.start()); byte[] buf = new byte[(int)range.length()]; int totalRead = 0; while (totalRead < buf.length) { int b = in.read(); assert b != -1; buf[totalRead++] = (byte)b; } String str = new String(buf); String[] chunks = str.split(" "); int ctr = 0; for (String chunk : chunks) { if (!chunk.isEmpty()) ctr++; } return F.t(range.length(), ctr); }
/** {@inheritDoc} */ @Override public Object execute() { IgniteFileSystem fs = ignite.fileSystem(igfsName); try (IgfsInputStream in = fs.open(path)) { IgfsFileRange split = new IgfsFileRange(path, start, len); if (rslvr != null) { split = rslvr.resolveRecords(fs, in, split); if (split == null) { log.warning("No data found for split on local node after resolver is applied " + "[igfsName=" + igfsName + ", path=" + path + ", start=" + start + ", len=" + len + ']'); return null; } } in.seek(split.start()); return job.execute(fs, new IgfsFileRange(path, split.start(), split.length()), in); } catch (IOException e) { throw new IgniteException("Failed to execute IGFS job for file split [igfsName=" + igfsName + ", path=" + path + ", start=" + start + ", len=" + len + ']', e); } }
/** {@inheritDoc} */ @Override public void seek(long pos) throws IOException { if (pos < 0) throw new IOException("Seek position cannot be negative: " + pos); is.seek(start + pos); this.pos = pos; }
/** * Constructor. * * @param is Base input stream. * @param start Start position. * @param maxLen Maximum stream length. * @throws IOException In case of exception. */ public IgfsRangeInputStream(IgfsInputStream is, long start, long maxLen) throws IOException { if (is == null) throw new IllegalArgumentException("Input stream cannot be null."); if (start < 0) throw new IllegalArgumentException("Start position cannot be negative."); if (start >= is.length()) throw new IllegalArgumentException("Start position cannot be greater that file length."); if (maxLen < 0) throw new IllegalArgumentException("Length cannot be negative."); if (start + maxLen > is.length()) throw new IllegalArgumentException("Sum of start position and length cannot be greater than file length."); this.is = is; this.start = start; this.maxLen = maxLen; is.seek(start); }
/** * Find first delimiter. In order to achieve this we have to rewind the stream until we find the delimiter * which stands at least [maxDelimLen] from the start search position or until we faced stream start. * Otherwise we cannot be sure that delimiter position is determined correctly. * * @param stream IGFS input stream. * @param startPos Start search position. * @return The first found delimiter. * @throws IOException In case of IO exception. */ @Nullable private IgniteBiTuple<State, Delimiter> findFirstDelimiter(IgfsInputStream stream, long startPos) throws IOException { State state; Delimiter delim; long curPos = Math.max(0, startPos - maxDelimLen); while (true) { stream.seek(curPos); state = new State(); delim = nextDelimiter(stream, state); if (curPos == 0 || delim == null || delim.start - curPos > maxDelimLen - 1) break; else curPos = Math.max(0, curPos - maxDelimLen); } return F.t(state, delim); }
/** {@inheritDoc} */ @Override public final Object execute(IgniteFileSystem igfs, IgfsFileRange range, IgfsInputStream in) throws IgniteException, IOException { in.seek(range.start()); return execute(igfs, new IgfsRangeInputStream(in, range)); }
/** {@inheritDoc} */ @Override public Object execute() { IgniteFileSystem fs = ignite.fileSystem(igfsName); try (IgfsInputStream in = fs.open(path)) { IgfsFileRange split = new IgfsFileRange(path, start, len); if (rslvr != null) { split = rslvr.resolveRecords(fs, in, split); if (split == null) { log.warning("No data found for split on local node after resolver is applied " + "[igfsName=" + igfsName + ", path=" + path + ", start=" + start + ", len=" + len + ']'); return null; } } in.seek(split.start()); return job.execute(fs, new IgfsFileRange(path, split.start(), split.length()), in); } catch (IOException e) { throw new IgniteException("Failed to execute IGFS job for file split [igfsName=" + igfsName + ", path=" + path + ", start=" + start + ", len=" + len + ']', e); } }