mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
DS-4194 Use lazy fetching iterator for long query results
This commit is contained in:
@@ -12,12 +12,14 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Stream;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Expression;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import com.google.common.collect.AbstractIterator;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.hibernate.Session;
|
||||
|
||||
@@ -269,8 +271,19 @@ public abstract class AbstractHibernateDAO<T> implements GenericDAO<T> {
|
||||
*/
|
||||
public Iterator<T> iterate(Query query) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Iterator<T> result = (Iterator<T>) query.getResultList().iterator();
|
||||
return result;
|
||||
org.hibernate.query.Query hquery = query.unwrap(org.hibernate.query.Query.class);
|
||||
Stream<T> stream = hquery.stream();
|
||||
Iterator<T> iter = stream.iterator();
|
||||
return new AbstractIterator<T> () {
|
||||
@Override
|
||||
protected T computeNext() {
|
||||
return iter.hasNext() ? iter.next() : endOfData();
|
||||
}
|
||||
@Override
|
||||
public void finalize() {
|
||||
stream.close();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user