Skip to content

Commit 154b4b3

Browse files
author
James Leigh
committed
Merge branch 'master' of github.com:eclipse/rdf4j into develop
2 parents 8767cd5 + 8cb3bd4 commit 154b4b3

File tree

4 files changed

+62
-141
lines changed

4 files changed

+62
-141
lines changed

core/query/src/main/java/org/eclipse/rdf4j/query/impl/BackgroundGraphResult.java

Lines changed: 25 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77
*******************************************************************************/
88
package org.eclipse.rdf4j.query.impl;
99

10-
import java.io.IOException;
1110
import java.io.InputStream;
1211
import java.io.InputStreamReader;
13-
import java.lang.reflect.UndeclaredThrowableException;
1412
import java.nio.charset.Charset;
1513
import java.util.Collections;
1614
import java.util.Map;
17-
import java.util.NoSuchElementException;
1815
import java.util.concurrent.ConcurrentHashMap;
1916
import java.util.concurrent.CountDownLatch;
2017

@@ -46,6 +43,8 @@ public class BackgroundGraphResult extends IterationWrapper<Statement, QueryEval
4643

4744
private final CountDownLatch namespacesReady = new CountDownLatch(1);
4845

46+
private final CountDownLatch finishedParsing = new CountDownLatch(1);
47+
4948
private final Map<String, String> namespaces = new ConcurrentHashMap<String, String>();
5049

5150
private final QueueCursor<Statement> queue;
@@ -66,118 +65,47 @@ public BackgroundGraphResult(QueueCursor<Statement> queue, RDFParser parser, Inp
6665
}
6766

6867
@Override
69-
public boolean hasNext()
70-
throws QueryEvaluationException
71-
{
72-
if (isClosed()) {
73-
return false;
74-
}
75-
if (Thread.currentThread().isInterrupted()) {
76-
close();
77-
return false;
78-
}
79-
80-
boolean result = queue.hasNext();
81-
if (!result) {
82-
close();
83-
}
84-
return result;
85-
}
86-
87-
@Override
88-
public Statement next()
68+
protected void handleClose()
8969
throws QueryEvaluationException
9070
{
91-
if (isClosed()) {
92-
throw new NoSuchElementException("The iteration has been closed.");
93-
}
94-
if (Thread.currentThread().isInterrupted()) {
95-
close();
96-
throw new NoSuchElementException("The iteration has been closed.");
97-
}
98-
9971
try {
100-
return queue.next();
101-
}
102-
catch (NoSuchElementException e) {
103-
close();
104-
throw e;
72+
super.handleClose();
10573
}
106-
}
107-
108-
@Override
109-
public void remove()
110-
throws QueryEvaluationException
111-
{
112-
if (isClosed()) {
113-
throw new IllegalStateException("The iteration has been closed.");
114-
}
115-
if (Thread.currentThread().isInterrupted()) {
116-
close();
117-
throw new IllegalStateException("The iteration has been closed.");
74+
finally {
75+
queue.done();
11876
}
119-
12077
try {
121-
queue.remove();
78+
finishedParsing.await();
12279
}
123-
catch (IllegalStateException e) {
124-
close();
125-
throw e;
80+
catch (InterruptedException e) {
81+
Thread.currentThread().interrupt();
82+
} finally {
83+
queue.checkException();
12684
}
12785
}
12886

12987
@Override
130-
protected void handleClose()
131-
throws QueryEvaluationException
132-
{
88+
public void run() {
13389
try {
13490
try {
135-
super.handleClose();
136-
}
137-
finally {
138-
try {
139-
// After checking that we ourselves cannot possibly be generating an NPE ourselves,
140-
// attempt to close the input stream we were given
141-
InputStream toClose = in;
142-
if (toClose != null) {
143-
toClose.close();
144-
}
91+
parser.setRDFHandler(this);
92+
if (charset == null) {
93+
parser.parse(in, baseURI);
14594
}
146-
catch (NullPointerException e) {
147-
// Swallow NullPointerException that Apache HTTPClient is hiding behind a NotThreadSafe annotation
95+
else {
96+
parser.parse(new InputStreamReader(in, charset), baseURI);
14897
}
98+
} finally {
99+
in.close();
149100
}
150101
}
151-
catch (IOException e) {
152-
throw new QueryEvaluationException(e);
153-
}
154-
finally {
155-
queue.close();
156-
}
157-
}
158-
159-
@Override
160-
public void run() {
161-
try {
162-
parser.setRDFHandler(this);
163-
if (charset == null) {
164-
parser.parse(in, baseURI);
165-
}
166-
else {
167-
parser.parse(new InputStreamReader(in, charset), baseURI);
168-
}
169-
}
170-
catch (RDFHandlerException e) {
171-
// parsing was cancelled or interrupted
172-
close();
173-
}
174102
catch (Exception e) {
175103
queue.toss(e);
176-
close();
177104
}
178105
finally {
179106
queue.done();
180107
namespacesReady.countDown();
108+
finishedParsing.countDown();
181109
}
182110
}
183111

@@ -197,8 +125,9 @@ public Map<String, String> getNamespaces() {
197125
}
198126
catch (InterruptedException e) {
199127
Thread.currentThread().interrupt();
200-
close();
201-
throw new UndeclaredThrowableException(e);
128+
return Collections.emptyMap();
129+
} finally {
130+
queue.checkException();
202131
}
203132
}
204133

@@ -226,11 +155,8 @@ public void handleStatement(Statement st)
226155
}
227156
catch (InterruptedException e) {
228157
Thread.currentThread().interrupt();
229-
close();
230-
throw new RDFHandlerException(e);
231-
}
232-
if (isClosed()) {
233-
throw new RDFHandlerException("Result closed");
158+
queue.toss(e);
159+
queue.done();
234160
}
235161
}
236162

core/queryresultio/api/src/main/java/org/eclipse/rdf4j/query/resultio/helpers/BackgroundTupleResult.java

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
*******************************************************************************/
88
package org.eclipse.rdf4j.query.resultio.helpers;
99

10-
import java.io.IOException;
1110
import java.io.InputStream;
12-
import java.lang.reflect.UndeclaredThrowableException;
1311
import java.util.ArrayList;
1412
import java.util.Collections;
1513
import java.util.List;
@@ -43,6 +41,8 @@ public class BackgroundTupleResult extends IteratingTupleQueryResult
4341

4442
private final CountDownLatch bindingNamesReady = new CountDownLatch(1);
4543

44+
private final CountDownLatch finishedParsing = new CountDownLatch(1);
45+
4646
public BackgroundTupleResult(TupleQueryResultParser parser, InputStream in) {
4747
this(new QueueCursor<BindingSet>(10), parser, in);
4848
}
@@ -61,66 +61,52 @@ protected void handleClose()
6161
throws QueryEvaluationException
6262
{
6363
try {
64-
try {
65-
super.handleClose();
66-
}
67-
finally {
68-
try {
69-
// After checking that we ourselves cannot possibly be generating an NPE ourselves,
70-
// attempt to close the input stream we were given
71-
InputStream toClose = in;
72-
if (toClose != null) {
73-
toClose.close();
74-
}
75-
}
76-
catch (NullPointerException e) {
77-
// Swallow NullPointerException that Apache HTTPClient is hiding behind a NotThreadSafe annotation
78-
}
79-
}
80-
}
81-
catch (IOException e) {
82-
throw new QueryEvaluationException(e);
64+
super.handleClose();
8365
}
8466
finally {
85-
queue.close();
67+
queue.done();
68+
}
69+
try {
70+
finishedParsing.await();
71+
}
72+
catch (InterruptedException e) {
73+
Thread.currentThread().interrupt();
74+
} finally {
75+
queue.checkException();
8676
}
8777
}
8878

8979
@Override
9080
public List<String> getBindingNames() {
9181
try {
9282
bindingNamesReady.await();
93-
queue.checkException();
9483
return bindingNames;
9584
}
9685
catch (InterruptedException e) {
9786
Thread.currentThread().interrupt();
98-
close();
99-
throw new UndeclaredThrowableException(e);
100-
}
101-
catch (QueryEvaluationException e) {
102-
close();
103-
throw new UndeclaredThrowableException(e);
87+
return Collections.emptyList();
88+
} finally {
89+
queue.checkException();
10490
}
10591
}
10692

10793
@Override
10894
public void run() {
10995
try {
110-
parser.setQueryResultHandler(this);
111-
parser.parseQueryResult(in);
112-
}
113-
catch (QueryResultHandlerException e) {
114-
// parsing cancelled or interrupted
115-
close();
96+
try {
97+
parser.setQueryResultHandler(this);
98+
parser.parseQueryResult(in);
99+
} finally {
100+
in.close();
101+
}
116102
}
117103
catch (Exception e) {
118104
queue.toss(e);
119-
close();
120105
}
121106
finally {
122107
queue.done();
123108
bindingNamesReady.countDown();
109+
finishedParsing.countDown();
124110
}
125111
}
126112

@@ -141,11 +127,8 @@ public void handleSolution(BindingSet bindingSet)
141127
}
142128
catch (InterruptedException e) {
143129
Thread.currentThread().interrupt();
144-
close();
145-
throw new TupleQueryResultHandlerException(e);
146-
}
147-
if (isClosed()) {
148-
throw new TupleQueryResultHandlerException("Result closed");
130+
queue.toss(e);
131+
queue.done();
149132
}
150133
}
151134

core/util/src/main/java/org/eclipse/rdf4j/common/iteration/IterationWrapper.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public boolean hasNext()
6060
if (isClosed()) {
6161
return false;
6262
}
63+
else if (Thread.currentThread().isInterrupted()) {
64+
close();
65+
return false;
66+
}
6367
boolean result = wrappedIter.hasNext();
6468
if (!result) {
6569
close();
@@ -79,6 +83,10 @@ public E next()
7983
if (isClosed()) {
8084
throw new NoSuchElementException("The iteration has been closed.");
8185
}
86+
else if (Thread.currentThread().isInterrupted()) {
87+
close();
88+
throw new NoSuchElementException("The iteration has been interrupted.");
89+
}
8290
try {
8391
return wrappedIter.next();
8492
}
@@ -103,6 +111,10 @@ public void remove()
103111
if (isClosed()) {
104112
throw new IllegalStateException("The iteration has been closed.");
105113
}
114+
else if (Thread.currentThread().isInterrupted()) {
115+
close();
116+
throw new IllegalStateException("The iteration has been interrupted.");
117+
}
106118
try {
107119
wrappedIter.remove();
108120
}

core/util/src/main/java/org/eclipse/rdf4j/common/iteration/QueueIteration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ public E getNextElement()
142142
}
143143
}
144144
if (isAfterLast(take)) {
145-
checkException();
146145
done(); // put afterLast back for others
146+
checkException();
147147
return null;
148148
}
149149
checkException();

0 commit comments

Comments
 (0)