/** * Read elements from a {@link Source.Reader} until n elements are read. * * <p>There must be at least n elements remaining in the reader, except for the case when n is * {@code Integer.MAX_VALUE}, which means "read all remaining elements". */ private static <T> List<T> readNItemsFromReader(Source.Reader<T> reader, int n, boolean started) throws IOException { List<T> res = new ArrayList<>(); for (int i = 0; i < n; i++) { boolean shouldStart = (i == 0 && !started); boolean more = shouldStart ? reader.start() : reader.advance(); if (n != Integer.MAX_VALUE) { assertTrue(more); } if (!more) { break; } res.add(reader.getCurrent()); } return res; }
private <T> List<T> readEverythingFromReader(Reader<T> reader) throws IOException { List<T> results = new ArrayList<>(); for (boolean available = reader.start(); available; available = reader.advance()) { T train = reader.getCurrent(); results.add(train); } return results; }
@Override public void open(TaskContext context, Instant startTime) { try { PipelineOptions options = serializedOptions.get(); this.reader = createReader(options); this.available = reader.start(); } catch (Exception e) { close(); throw new RuntimeException(e); } }
@Test public void testReadXMLInvalidRecordClassWithCustomEventHandler() throws IOException { File file = tempFolder.newFile("trainXMLSmall"); Files.write(file.toPath(), trainXML.getBytes(StandardCharsets.UTF_8)); ValidationEventHandler validationEventHandler = event -> { throw new RuntimeException("MyCustomValidationEventHandler failure mesage"); }; BoundedSource<WrongTrainType> source = XmlIO.<WrongTrainType>read() .from(file.toPath().toString()) .withRootElement("trains") .withRecordElement("train") .withRecordClass(WrongTrainType.class) .withValidationEventHandler(validationEventHandler) .createSource(); exception.expect(RuntimeException.class); // JAXB internationalizes the error message. So this is all we can match for. exception.expectMessage("MyCustomValidationEventHandler failure mesage"); try (Reader<WrongTrainType> reader = source.createReader(null)) { List<WrongTrainType> results = new ArrayList<>(); for (boolean available = reader.start(); available; available = reader.advance()) { WrongTrainType train = reader.getCurrent(); results.add(train); } } }
public boolean invokeStart(ReaderT reader) throws IOException { if (enableMetrics) { try (Closeable ignored = MetricsEnvironment.scopedMetricsContainer(container.getMetricsContainer(stepName))) { boolean result = reader.start(); container.updateMetrics(); return result; } } else { return reader.start(); } } public boolean invokeAdvance(ReaderT reader) throws IOException {
public boolean invokeStart(ReaderT reader) throws IOException { if (enableMetrics) { try (Closeable ignored = MetricsEnvironment.scopedMetricsContainer(container.getMetricsContainer(stepName))) { boolean result = reader.start(); container.updateMetrics(stepName); return result; } } else { return reader.start(); } }
private boolean seekNext() throws IOException { if (!started) { started = true; return reader.start(); } else { return !closed && reader.advance(); } }
public boolean invokeStart(ReaderT reader) throws IOException { if (enableMetrics) { try (Closeable ignored = MetricsEnvironment.scopedMetricsContainer(container.getMetricsContainer(stepName))) { boolean result = reader.start(); container.updateMetrics(stepName); return result; } } else { return reader.start(); } }