/
BackgroundTupleResult.java
169 lines (151 loc) · 4.31 KB
/
BackgroundTupleResult.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*******************************************************************************
* Copyright (c) 2015 Eclipse RDF4J contributors, Aduna, and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*******************************************************************************/
package org.eclipse.rdf4j.http.client;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.QueryEvaluationException;
import org.eclipse.rdf4j.query.QueryResultHandlerException;
import org.eclipse.rdf4j.query.TupleQueryResultHandler;
import org.eclipse.rdf4j.query.TupleQueryResultHandlerException;
import org.eclipse.rdf4j.query.impl.IteratingTupleQueryResult;
import org.eclipse.rdf4j.query.resultio.QueryResultParseException;
import org.eclipse.rdf4j.query.resultio.TupleQueryResultParser;
/**
* Provides concurrent access to tuple results as they are being parsed.
*
* @author James Leigh
*/
public class BackgroundTupleResult extends IteratingTupleQueryResult
implements Runnable, TupleQueryResultHandler
{
private final TupleQueryResultParser parser;
private final InputStream in;
private final QueueCursor<BindingSet> queue;
private final List<String> bindingNames = new ArrayList<>();
private final CountDownLatch bindingNamesReady = new CountDownLatch(1);
public BackgroundTupleResult(TupleQueryResultParser parser, InputStream in) {
this(new QueueCursor<BindingSet>(10), parser, in);
}
public BackgroundTupleResult(QueueCursor<BindingSet> queue, TupleQueryResultParser parser,
InputStream in)
{
super(Collections.<String> emptyList(), queue);
this.queue = queue;
this.parser = parser;
this.in = in;
}
@Override
protected void handleClose()
throws QueryEvaluationException
{
try {
try {
super.handleClose();
}
finally {
try {
// After checking that we ourselves cannot possibly be generating an NPE ourselves,
// attempt to close the input stream we were given
InputStream toClose = in;
if (toClose != null) {
toClose.close();
}
}
catch (NullPointerException e) {
// Swallow NullPointerException that Apache HTTPClient is hiding behind a NotThreadSafe annotation
}
}
}
catch (IOException e) {
throw new QueryEvaluationException(e);
}
finally {
queue.close();
}
}
@Override
public List<String> getBindingNames() {
try {
bindingNamesReady.await();
queue.checkException();
return bindingNames;
}
catch (InterruptedException e) {
throw new UndeclaredThrowableException(e);
}
catch (QueryEvaluationException e) {
throw new UndeclaredThrowableException(e);
}
}
@Override
public void run() {
try {
parser.setQueryResultHandler(this);
parser.parseQueryResult(in);
}
catch (QueryResultHandlerException e) {
// parsing cancelled or interrupted
}
catch (QueryResultParseException e) {
queue.toss(e);
}
catch (IOException e) {
queue.toss(e);
}
finally {
queue.done();
bindingNamesReady.countDown();
}
}
@Override
public void startQueryResult(List<String> bindingNames)
throws TupleQueryResultHandlerException
{
this.bindingNames.addAll(bindingNames);
bindingNamesReady.countDown();
}
@Override
public void handleSolution(BindingSet bindingSet)
throws TupleQueryResultHandlerException
{
try {
queue.put(bindingSet);
}
catch (InterruptedException e) {
throw new TupleQueryResultHandlerException(e);
}
if (isClosed()) {
throw new TupleQueryResultHandlerException("Result closed");
}
}
@Override
public void endQueryResult()
throws TupleQueryResultHandlerException
{
// no-op
}
@Override
public void handleBoolean(boolean value)
throws QueryResultHandlerException
{
throw new UnsupportedOperationException("Cannot handle boolean results");
}
@Override
public void handleLinks(List<String> linkUrls)
throws QueryResultHandlerException
{
// ignore
}
}