29
29
import com .arangodb .entity .CursorEntity .Warning ;
30
30
import com .arangodb .internal .ArangoCursorExecute ;
31
31
import com .arangodb .internal .InternalArangoDatabase ;
32
+ import org .slf4j .Logger ;
33
+ import org .slf4j .LoggerFactory ;
32
34
33
35
import java .util .ArrayList ;
34
36
import java .util .Collection ;
38
40
* @author Mark Vollmary
39
41
*/
40
42
public class ArangoCursorImpl <T > extends AbstractArangoIterable <T > implements ArangoCursor <T > {
43
+ private final static Logger LOG = LoggerFactory .getLogger (ArangoCursorImpl .class );
41
44
42
45
private final Class <T > type ;
43
46
protected final ArangoCursorIterator <T > iterator ;
44
47
private final String id ;
45
48
private final ArangoCursorExecute execute ;
46
- private final boolean isPontentialDirtyRead ;
49
+ private final boolean pontentialDirtyRead ;
50
+ private final boolean allowRetry ;
47
51
48
52
public ArangoCursorImpl (final InternalArangoDatabase <?, ?> db , final ArangoCursorExecute execute ,
49
53
final Class <T > type , final CursorEntity result ) {
@@ -52,7 +56,8 @@ public ArangoCursorImpl(final InternalArangoDatabase<?, ?> db, final ArangoCurso
52
56
this .type = type ;
53
57
iterator = createIterator (this , db , execute , result );
54
58
id = result .getId ();
55
- this .isPontentialDirtyRead = Boolean .parseBoolean (result .getMeta ().get ("X-Arango-Potential-Dirty-Read" ));
59
+ this .pontentialDirtyRead = Boolean .parseBoolean (result .getMeta ().get ("X-Arango-Potential-Dirty-Read" ));
60
+ this .allowRetry = result .getNextBatchId () != null ;
56
61
}
57
62
58
63
protected ArangoCursorIterator <T > createIterator (
@@ -98,7 +103,7 @@ public boolean isCached() {
98
103
99
104
@ Override
100
105
public void close () {
101
- if (id != null && hasNext ( )) {
106
+ if (getId () != null && ( allowRetry || iterator . getResult (). getHasMore () )) {
102
107
execute .close (id , iterator .getResult ().getMeta ());
103
108
}
104
109
}
@@ -119,12 +124,17 @@ public List<T> asListRemaining() {
119
124
while (hasNext ()) {
120
125
remaining .add (next ());
121
126
}
127
+ try {
128
+ close ();
129
+ } catch (final Exception e ) {
130
+ LOG .warn ("Could not close cursor: " , e );
131
+ }
122
132
return remaining ;
123
133
}
124
134
125
135
@ Override
126
136
public boolean isPotentialDirtyRead () {
127
- return isPontentialDirtyRead ;
137
+ return pontentialDirtyRead ;
128
138
}
129
139
130
140
@ Override
@@ -144,4 +154,8 @@ public void foreach(final Consumer<? super T> action) {
144
154
}
145
155
}
146
156
157
+ public String getNextBatchId () {
158
+ return iterator .getResult ().getNextBatchId ();
159
+ }
160
+
147
161
}
0 commit comments