Skip to content

Commit 5e57e62

Browse files
author
Burak Serdar
committed
Add rewindable() support to stream
1 parent a308640 commit 5e57e62

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

crud/src/main/java/com/redhat/lightblue/crud/DocumentStream.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ public interface DocumentStream<T> extends Iterator<T>{
3131
*/
3232
void close();
3333

34+
/**
35+
* returns true if rewind works
36+
*/
37+
default boolean canRewind() { return false; }
38+
39+
/**
40+
* returns a new stream that starts the same resultset from the beginning. Only works if canRewind() is true
41+
*/
42+
default DocumentStream<T> rewind() { throw new UnsupportedOperationException(); }
43+
3444
public static <S,D> DocumentStream<D> map(final DocumentStream<S> source,final Function<S,D> map) {
3545
return new DocumentStream<D>() {
3646
@Override

crud/src/main/java/com/redhat/lightblue/crud/ListDocumentStream.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ public ListDocumentStream(List<T> list) {
3333
this.documents=list;
3434
}
3535

36-
/**
37-
* Return the underlying list
38-
*/
39-
public List<T> getDocuments() {
40-
return documents;
36+
@Override
37+
public boolean canRewind() {
38+
return true;
39+
}
40+
41+
@Override
42+
public DocumentStream<T> rewind() {
43+
return new ListDocumentStream<T>(documents);
4144
}
4245

4346
@Override

crud/src/main/java/com/redhat/lightblue/hooks/HookManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import com.redhat.lightblue.crud.CrudConstants;
3434
import com.redhat.lightblue.crud.DocCtx;
3535
import com.redhat.lightblue.crud.DocumentStream;
36-
import com.redhat.lightblue.crud.ListDocumentStream;
3736
import com.redhat.lightblue.eval.Projector;
3837
import com.redhat.lightblue.mediator.OperationContext;
3938
import com.redhat.lightblue.metadata.EntityMetadata;
@@ -228,7 +227,7 @@ private void queueHooks(CRUDOperationContext ctx, boolean mediatorHooks) {
228227
LOGGER.debug("Hooks are resolved: {}", hooks);
229228
if (!hooks.isEmpty()) {
230229
DocumentStream<DocCtx> documents=ctx.getDocumentStream();
231-
if(documents instanceof ListDocumentStream) {
230+
if(documents.canRewind()) {
232231
// If this is the case, then we have access to all the documents, and
233232
// we can call hooks at once
234233

@@ -242,7 +241,8 @@ private void queueHooks(CRUDOperationContext ctx, boolean mediatorHooks) {
242241
// list where each element gives a hook, and all the
243242
// documents that will be passed to that hook.
244243
List<DocHooks> docHooksList = new ArrayList<>();
245-
for (DocCtx doc:((ListDocumentStream<DocCtx>)documents).getDocuments()) {
244+
for (DocumentStream<DocCtx> stream=documents.rewind();stream.hasNext();) {
245+
DocCtx doc=stream.next();
246246
if(!doc.hasErrors()) {
247247
if (doc.getCRUDOperationPerformed() != null) {
248248
Map<Hook, CRUDHook> hooksList = null;

0 commit comments

Comments
 (0)