de.julielab.xmlData.dataBase.DBCThreadedIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of costosys Show documentation
Show all versions of costosys Show documentation
A utility for managing documents stored in a PostgreSQL database. The documents are imported into a
PostgreSQL DB as full texts with the goal to be able to retrieve the documents by their PubMedID efficiently.
For more sophisticated tasks, a user configuration file can be delivered which can take control of the table
schema to use, the PostgreSQL schema to use and the actual database server to connect to as well as the concrete
database.
package de.julielab.xmlData.dataBase;
import de.julielab.xmlData.dataBase.util.CoStoSysSQLRuntimeException;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Exchanger;
/**
*
* This iterator class employs multiple Threads for database queries. While the
* iterator offers access to retrieved values, additional values are
* concurrently retrieved from the database by another thread.
*
*
* The iterator uses the {@link Exchanger} class to communicate between threads.
*
*
* @author hellrich/faessler
*
* @param
*/
public abstract class DBCThreadedIterator extends DBCIterator {
// Exchangers switch results between 2 threads as needed
protected Exchanger> listExchanger = new Exchanger<>();
protected ConnectionClosable backgroundThread;
private Iterator currentListIter;
private boolean hasNext = true;
public boolean hasNext() {
return hasNext;
}
@Override
public E next() {
E next = currentListIter.next();
if (!currentListIter.hasNext())
update();
return next;
}
/**
* unsupported
*/
public void remove() {
throw new UnsupportedOperationException();
}
protected void update() {
try {
List list = listExchanger.exchange(null);
while (list != null && list.isEmpty()) {
list = listExchanger.exchange(null);
}
// list full or null
if (list == null) {
hasNext = false;
} else
currentListIter = list.iterator();
} catch (InterruptedException e) {
throw new CoStoSysSQLRuntimeException(e);
}
}
@Override
public abstract void close();
public abstract void join() throws InterruptedException;
}