You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/queryalgebra/evaluation/src/main/java/org/eclipse/rdf4j/query/algebra/evaluation/iterator/PeekMarkIterator.java
+29-8Lines changed: 29 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -51,7 +51,6 @@ private void calculateNext() {
51
51
52
52
if (bufferIterator.hasNext()) {
53
53
next = bufferIterator.next();
54
-
assertresetPossible == 1;
55
54
} else {
56
55
if (!mark && resetPossible > -1) {
57
56
resetPossible--;
@@ -62,7 +61,7 @@ private void calculateNext() {
62
61
}
63
62
64
63
if (mark && next != null) {
65
-
assertresetPossible== 1;
64
+
assertresetPossible> 0;
66
65
buffer.add(next);
67
66
}
68
67
@@ -107,6 +106,10 @@ public E peek() {
107
106
returnnext;
108
107
}
109
108
109
+
/**
110
+
* Mark the current position so that the iterator can be reset to the current state. This will cause elements to be
111
+
* stored in memory until one of {@link #reset()}, {@link #unmark()} or {@link #mark()} is called
112
+
*/
110
113
publicvoidmark() {
111
114
if (closed) {
112
115
thrownewIllegalStateException("The iteration has been closed.");
@@ -120,38 +123,51 @@ public void mark() {
120
123
121
124
}
122
125
126
+
/**
127
+
* Reset the iterator to the marked position. Resetting an iterator multiple times will always reset to the same
128
+
* position. If the iterator was not marked, this will throw an exception.
129
+
*/
123
130
publicvoidreset() {
124
131
if (closed) {
125
132
thrownewIllegalStateException("The iteration has been closed.");
126
133
}
127
134
if (buffer == null) {
128
135
thrownewIllegalStateException("Mark never set");
129
136
}
137
+
if (resetPossible < 0) {
138
+
thrownewIllegalStateException("Reset not possible");
139
+
}
140
+
130
141
if (mark && bufferIterator.hasNext()) {
131
142
while (bufferIterator.hasNext()) {
132
143
buffer.add(bufferIterator.next());
133
144
}
134
145
}
135
146
136
-
mark = false;
137
-
if (resetPossible < 0) {
138
-
thrownewIllegalStateException("Reset not possible");
139
-
} elseif (resetPossible == 0) {
147
+
if (resetPossible == 0) {
148
+
assert !mark;
140
149
buffer.add(next);
141
150
next = null;
142
151
bufferIterator = buffer.iterator();
143
-
} elseif (resetPossible== 1) {
152
+
} elseif (resetPossible> 0) {
144
153
next = null;
145
154
bufferIterator = buffer.iterator();
146
155
}
147
156
157
+
mark = false;
148
158
resetPossible = 1;
149
159
}
150
160
161
+
/**
162
+
* @return true if the iterator is marked
163
+
*/
151
164
booleanisMarked() {
152
165
return !closed && mark;
153
166
}
154
167
168
+
/**
169
+
* @return true if {@link #reset()} can be called on this iterator
170
+
*/
155
171
booleanisResettable() {
156
172
return !closed && (mark || resetPossible >= 0);
157
173
}
@@ -160,11 +176,16 @@ boolean isResettable() {
160
176
publicvoidclose() {
161
177
this.closed = true;
162
178
iterator.close();
179
+
buffer = null;
163
180
}
164
181
165
-
// What will happen if we are iterating over the buffer at this point, then unmark is called followed by mark?
182
+
/**
183
+
* Unmark the iterator. This will cause the iterator to stop buffering elements. If the iterator was recently reset
184
+
* and there are still elements in the buffer, then these elements will still be returned by next().
0 commit comments