Skip to content

Commit

Permalink
GH-4809 refactor and optimize iterators and iterations
Browse files Browse the repository at this point in the history
  • Loading branch information
hmottestad committed Oct 15, 2023
1 parent 1e4ec0f commit 885bb08
Show file tree
Hide file tree
Showing 80 changed files with 738 additions and 458 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,5 @@ public final void close() {
*
* @throws X
*/
protected void handleClose() {
}
abstract protected void handleClose();
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ public void remove() {

iter.remove();
}

@Override
protected void handleClose() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ public void remove() {
*/
@Override
protected void handleClose() {
try {
super.handleClose();
} finally {
iter.close();
}
iter.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,8 @@ public void remove() {
*/
@Override
protected void handleClose() {
try {
super.handleClose();
} finally {
if (iter != null) {
iter.close();
}
if (iter != null) {
iter.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ protected boolean accept(E object) {
}
}

@Override
protected void handleClose() {

}

/**
* @param object
* @return true if the object is in the excludeSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package org.eclipse.rdf4j.common.iteration;

import java.util.NoSuchElementException;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
Expand All @@ -21,22 +22,11 @@
* CloseableIteration directly.
*/
@Deprecated(since = "4.1.0")
public final class EmptyIteration<E> extends AbstractCloseableIteration<E> {
public final class EmptyIteration<E> implements CloseableIteration<E> {

/*--------------*
* Constructors *
*--------------*/

/**
* Creates a new EmptyIteration.
*/
public EmptyIteration() {
}

/*---------*
* Methods *
*---------*/

@Override
public boolean hasNext() {
return false;
Expand All @@ -57,4 +47,13 @@ public Stream<E> stream() {
return Stream.empty();
}

@Override
public void close() {

}

@Override
public void forEachRemaining(Consumer<? super E> action) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,9 @@ public void remove() {
@Override
protected void handleClose() {
try {
super.handleClose();
} finally {
try {
iter.close();
} catch (RuntimeException e) {
throw convert(e);
}
iter.close();
} catch (RuntimeException e) {
throw convert(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@
* must implement the <var>accept</var> method to indicate which objects should be returned.
*/
@Deprecated(since = "4.1.0")
public abstract class FilterIteration<E> extends IterationWrapper<E> {
public abstract class FilterIteration<E> implements CloseableIteration<E> {
/**
* The wrapped Iteration.
*
* @deprecated This will be changed to private, possibly with an accessor in future. Do not rely on it.
*/
protected final CloseableIteration<? extends E> wrappedIter;

/*-----------*
* Variables *
*-----------*/

private E nextElement;
/**
* Flag indicating whether this iteration has been closed.
*/
private boolean closed = false;

/*--------------*
* Constructors *
Expand All @@ -34,7 +44,8 @@ public abstract class FilterIteration<E> extends IterationWrapper<E> {
* @param iter
*/
protected FilterIteration(CloseableIteration<? extends E> iter) {
super(iter);
assert iter != null;
this.wrappedIter = iter;
}

/*---------*
Expand Down Expand Up @@ -74,9 +85,36 @@ public E next() {
}

private void findNextElement() {
if (nextElement != null) {
return;
}

try {
while (!isClosed() && nextElement == null && super.hasNext()) {
E candidate = super.next();
if (!isClosed()) {
if (Thread.currentThread().isInterrupted()) {
close();
return;
} else {
boolean result = wrappedIter.hasNext();
if (!result) {
close();
return;
}
}
}
while (nextElement == null && wrappedIter.hasNext()) {
E result;
if (Thread.currentThread().isInterrupted()) {
close();
return;
}
try {
result = wrappedIter.next();
} catch (NoSuchElementException e) {
close();
throw e;
}
E candidate = result;

if (accept(candidate)) {
nextElement = candidate;
Expand All @@ -99,12 +137,51 @@ private void findNextElement() {
*/
protected abstract boolean accept(E object);

/**
* Removes the last element that has been returned from the wrapped Iteration.
*
* @throws UnsupportedOperationException If the wrapped Iteration does not support the <var>remove</var> operation.
* @throws IllegalStateException if the Iteration has been closed, or if {@link #next} has not yet been
* called, or {@link #remove} has already been called after the last call to
* {@link #next}.
*/
@Override
protected void handleClose() {
public void remove() {
if (isClosed()) {
throw new IllegalStateException("The iteration has been closed.");
} else if (Thread.currentThread().isInterrupted()) {
close();
throw new IllegalStateException("The iteration has been interrupted.");
}
try {
super.handleClose();
} finally {
nextElement = null;
wrappedIter.remove();
} catch (IllegalStateException e) {
close();
throw e;
}
}

private boolean isClosed() {
return closed;
}

/**
* Closes this Iteration and also closes the wrapped Iteration if it is a {@link CloseableIteration}.
*/
abstract protected void handleClose();

/**
* Calls {@link #handleClose()} upon first call and makes sure the resource closures are only executed once.
*/
@Override
public final void close() {
if (!closed) {
closed = true;
try {
wrappedIter.close();
} finally {
handleClose();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,8 @@ protected Set<E> makeSet() {

@Override
protected void handleClose() {
try {
super.handleClose();
} finally {
if (arg2 != null) {
arg2.close();
}
if (arg2 != null) {
arg2.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ public void remove() {
*/
@Override
protected void handleClose() {
try {
super.handleClose();
} finally {
wrappedIter.close();
}
wrappedIter.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,4 @@ public void remove() {
throw new UnsupportedOperationException();
}

@Override
protected void handleClose() {
nextElement = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,8 @@ protected boolean accept(E object) {

@Override
protected void handleClose() {
try {
super.handleClose();
} finally {
if (rightArg != null)
rightArg.close();
if (rightArg != null) {
rightArg.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ protected boolean accept(E object) {
return true;
}
}

@Override
protected void handleClose() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,11 @@ public E getNextElement() {

@Override
public void handleClose() {
try {
super.handleClose();
} finally {
done.set(true);
do {
queue.clear(); // ensure extra room is available
} while (!queue.offer(afterLast));
checkException();
}
done.set(true);
do {
queue.clear(); // ensure extra room is available
} while (!queue.offer(afterLast));
checkException();
}

public void checkException() throws T {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ protected boolean accept(E nextObject) {
return true;
}
}

@Override
protected void handleClose() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,30 +92,25 @@ protected E getNextElement() {
@Override
protected void handleClose() {
try {
// Close this iteration, this will prevent lookAhead() from calling
// getNextElement() again
super.handleClose();
} finally {
try {
List<Throwable> collectedExceptions = new ArrayList<>();
while (argIter.hasNext()) {
try {
CloseableIteration<? extends E> next = argIter.next();
if (next != null) {
next.close();
}
} catch (Throwable e) {
collectedExceptions.add(e);
List<Throwable> collectedExceptions = new ArrayList<>();
while (argIter.hasNext()) {
try {
CloseableIteration<? extends E> next = argIter.next();
if (next != null) {
next.close();
}
} catch (Throwable e) {
collectedExceptions.add(e);
}
if (!collectedExceptions.isEmpty()) {
throw new UndeclaredThrowableException(collectedExceptions.get(0));
}
} finally {
if (currentIter != null) {
currentIter.close();
}
}
if (!collectedExceptions.isEmpty()) {
throw new UndeclaredThrowableException(collectedExceptions.get(0));
}
} finally {
if (currentIter != null) {
currentIter.close();
}
}

}
}
Loading

0 comments on commit 885bb08

Please sign in to comment.