This commit is contained in:
Lucio Lelii 2014-12-03 16:34:30 +00:00
parent 44044943e6
commit f650e1f7da
5 changed files with 73 additions and 57 deletions

View File

@ -9,7 +9,7 @@
<groupId>org.gcube.data.access</groupId> <groupId>org.gcube.data.access</groupId>
<artifactId>streams</artifactId> <artifactId>streams</artifactId>
<version>2.0.1-SNAPSHOT</version> <version>2.0.2-SNAPSHOT</version>
<name>Stream Library</name> <name>Stream Library</name>

View File

@ -7,7 +7,6 @@ import gr.uoa.di.madgik.grs.record.Record;
import gr.uoa.di.madgik.grs.record.exception.GRS2UncheckedException; import gr.uoa.di.madgik.grs.record.exception.GRS2UncheckedException;
import java.net.URI; import java.net.URI;
import java.util.Iterator;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.gcube.data.streams.LookAheadStream; import org.gcube.data.streams.LookAheadStream;
@ -43,7 +42,8 @@ public class ResultsetStream<E extends Record> extends LookAheadStream<E> {
private RuntimeException lookAheadFailure; private RuntimeException lookAheadFailure;
private ForwardReader<E> reader; private ForwardReader<E> reader;
private Iterator<E> iterator;
private E record;
/** /**
@ -77,7 +77,7 @@ public class ResultsetStream<E extends Record> extends LookAheadStream<E> {
throw lookAheadFailure; throw lookAheadFailure;
else else
try { try {
return iterator.next(); return record;
} }
catch(GRS2UncheckedException e) { catch(GRS2UncheckedException e) {
@ -106,28 +106,32 @@ public class ResultsetStream<E extends Record> extends LookAheadStream<E> {
try { try {
reader = new ForwardReader<E>(locator); reader = new ForwardReader<E>(locator);
reader.setIteratorTimeout(timeout);
reader.setIteratorTimeUnit(timeoutUnit);
} }
catch (Throwable t) { catch (Throwable t) {
lookAheadFailure= new StreamOpenException("cannot open resultset "+locator,t); lookAheadFailure= new StreamOpenException("cannot open resultset "+locator,t);
return true; return true;
} }
iterator = reader.iterator();
log.info("initialised resultset at "+locator); log.info("initialised resultset at "+locator);
open=true; open=true;
} }
//infer outage if reader has been dismissed remotely try {
if (reader.getStatus()==Status.Dispose && !closed) record = reader.get(timeout, timeoutUnit );
lookAheadFailure= new RuntimeException("unrecoverable failure in resultset "); } catch (GRS2ReaderException e) {
lookAheadFailure = new RuntimeException(e);
lookAheadFailure.printStackTrace();
}
boolean hasNext = iterator.hasNext(); if (reader.getStatus()!=Status.Close && record == null) {
if (reader.getStatus()==Status.Open)
lookAheadFailure = new RuntimeException("Timeout occurred reading the resultSet");
else if (reader.getStatus()==Status.Dispose)
lookAheadFailure = new RuntimeException("ResultSet disposed");
return true;
} else return record!=null;
return hasNext;
} }
@ -159,7 +163,7 @@ public class ResultsetStream<E extends Record> extends LookAheadStream<E> {
@Override @Override
public void remove() { public void remove() {
iterator.remove(); record = null;
} }
@Override @Override

View File

@ -1,10 +1,12 @@
package org.gcube.data.streams.dsl; package org.gcube.data.streams.dsl;
import static java.util.Arrays.*; import static java.util.Arrays.asList;
import gr.uoa.di.madgik.grs.record.Record;
import java.net.URI; import java.net.URI;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
import org.gcube.data.streams.Stream; import org.gcube.data.streams.Stream;
import org.gcube.data.streams.adapters.IteratorAdapter; import org.gcube.data.streams.adapters.IteratorAdapter;
@ -28,8 +30,6 @@ import org.gcube.data.streams.handlers.StopFastHandler;
import org.gcube.data.streams.publishers.RsStringRecordFactory; import org.gcube.data.streams.publishers.RsStringRecordFactory;
import org.gcube.data.streams.test.FallibleIterator; import org.gcube.data.streams.test.FallibleIterator;
import gr.uoa.di.madgik.grs.record.Record;
/** /**
* *
* The definitions of an eDSL of stream and stream-related expressions. * The definitions of an eDSL of stream and stream-related expressions.
@ -127,9 +127,17 @@ public class Streams {
* @return the stream * @return the stream
*/ */
public static Stream<String> stringsIn(URI locator) { public static Stream<String> stringsIn(URI locator) {
return convert(locator).ofStrings().withDefaults(); return convert(locator).ofStrings().withTimeout(5, TimeUnit.MINUTES);
} }
/**
* Returns a {@link Stream} of strings extracted from a resultset of {@link RsStringRecordFactory#STRING_RECORD}s.
* @param locator the locator of the resultset
* @return the stream
*/
public static Stream<String> stringsIn(URI locator, int timeout, TimeUnit timeUnit) {
return convert(locator).ofStrings().withTimeout(timeout, timeUnit);
}
// PIPE // PIPE

View File

@ -281,11 +281,15 @@ public class RsPublisher<E> implements StreamPublisher {
//private helper: publish a record //private helper: publish a record
private void publish(RecordWriter<Record> writer, Record record) throws GRS2WriterException { private void publish(RecordWriter<Record> writer, Record record) throws GRS2WriterException {
if (writer.getStatus() == Status.Open) if (writer.getStatus() == Status.Open){
if (!writer.put(record, timeout, timeoutUnit)) { if (!writer.put(record, timeout, timeoutUnit)) {
log.trace("client is not consuming resulset, stop publishing"); log.trace("client is not consuming resulset, stop publishing");
throw new GRS2WriterException(); throw new GRS2WriterException();
} }
} else{
log.warn("Writer not open, actual status is {}",writer.getStatus());
throw new GRS2WriterException("writer closed or disposed");
}
} }