-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from aol/auto-closing-experimental
auto-closing queues inside LazyFutureStreams
- Loading branch information
Showing
32 changed files
with
820 additions
and
159 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/com/aol/simple/react/async/AlwaysContinue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.aol.simple.react.async; | ||
|
||
public class AlwaysContinue implements Continueable{ | ||
|
||
public void closeQueueIfFinished(Queue queue){ | ||
|
||
} | ||
|
||
@Override | ||
public void addQueue(Queue queue) { | ||
|
||
|
||
} | ||
public void registerSkip(long skip){ | ||
|
||
} | ||
public void registerLimit(long limit){ | ||
|
||
} | ||
|
||
@Override | ||
public void closeAll() { | ||
|
||
|
||
} | ||
|
||
@Override | ||
public boolean closed() { | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public void closeQueueIfFinishedStateless(Queue queue) { | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/com/aol/simple/react/async/Continueable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.aol.simple.react.async; | ||
|
||
public interface Continueable { | ||
|
||
public void closeQueueIfFinished(Queue queue); | ||
|
||
public void addQueue(Queue queue); | ||
public void registerSkip(long skip); | ||
public void registerLimit(long limit); | ||
public void closeAll(); | ||
|
||
public boolean closed(); | ||
|
||
public void closeQueueIfFinishedStateless(Queue queue); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/main/java/com/aol/simple/react/async/Subscription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.aol.simple.react.async; | ||
|
||
|
||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import org.jooq.lambda.Seq; | ||
|
||
@Getter | ||
@Setter | ||
public class Subscription implements Continueable{ | ||
private final Map<Queue,AtomicLong> limits = new HashMap<>(); | ||
|
||
private final Map<Queue,AtomicBoolean> unlimited = new HashMap<>(); | ||
private final Map<Queue,AtomicLong> count = new HashMap<>(); | ||
private final List<Queue> queues = new LinkedList<>(); | ||
|
||
private final AtomicBoolean closed= new AtomicBoolean(false); | ||
|
||
public void registerSkip(long skip){ | ||
if(queues.size()>0) | ||
limits.get(currentQueue()).addAndGet(skip); | ||
} | ||
public void registerLimit(long limit){ | ||
|
||
if(queues.size()>0){ | ||
if(unlimited.get(currentQueue()).get()) | ||
limits.get(currentQueue()).set(0); | ||
|
||
limits.get(currentQueue()).addAndGet(limit); | ||
unlimited.get(currentQueue()).set(false); | ||
|
||
queues.stream().forEach(this::closeQueueIfFinishedStateless); | ||
|
||
} | ||
} | ||
private Queue currentQueue() { | ||
return queues.get(queues.size()-1); | ||
} | ||
public void addQueue(Queue q){ | ||
|
||
queues.add(q); | ||
limits.put(q, new AtomicLong(Long.MAX_VALUE-1)); | ||
unlimited.put(q, new AtomicBoolean(true)); | ||
count.put(q, new AtomicLong(0l)); | ||
|
||
} | ||
|
||
|
||
public void closeQueueIfFinished(Queue queue){ | ||
|
||
closeQueueIfFinished(queue,AtomicLong::incrementAndGet); | ||
|
||
} | ||
private void closeQueueIfFinished(Queue queue, Function<AtomicLong,Long> fn){ | ||
|
||
if(queues.size()==0) | ||
return; | ||
|
||
long queueCount = fn.apply(count.get(queue)); | ||
long limit = valuesToRight(queue).stream().reduce((acc,next)-> Math.min(acc, next)).get(); | ||
|
||
|
||
|
||
|
||
if(queueCount>=limit){ //last entry - close THIS queue only! | ||
|
||
queue.closeAndClear(); | ||
closed.set(true); | ||
} | ||
|
||
|
||
} | ||
public void closeQueueIfFinishedStateless(Queue queue){ | ||
|
||
closeQueueIfFinished(queue,AtomicLong::get); | ||
|
||
} | ||
private List<Long> valuesToRight(Queue queue) { | ||
return Seq.seq(queues.stream()).splitAt(findQueue(queue)).v2.map(limits::get).map(AtomicLong::get).collect(Collectors.toList()); | ||
|
||
} | ||
|
||
private int findQueue(Queue queue){ | ||
for(int i=0;i< queues.size();i++){ | ||
if(queues.get(i) == queue) | ||
return i; | ||
} | ||
return -1; | ||
} | ||
@Override | ||
public void closeAll() { | ||
closed.set(true); | ||
queues.stream().forEach(Queue::closeAndClear); | ||
|
||
} | ||
@Override | ||
public boolean closed() { | ||
return closed.get(); | ||
} | ||
} | ||
/** | ||
stream.map().iterator().limit(4).flatMap(..).limit(2).map(..).limit(8) | ||
subscription | ||
stream no limit | ||
q1:limit (4) | ||
q2:limit (2) | ||
q3:limit (8) | ||
**/ | ||
|
42 changes: 42 additions & 0 deletions
42
src/main/java/com/aol/simple/react/stream/BaseLazySimpleReact.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.aol.simple.react.stream; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Supplier; | ||
import java.util.function.UnaryOperator; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
import com.aol.simple.react.async.Subscription; | ||
import com.aol.simple.react.stream.traits.SimpleReactStream; | ||
|
||
public abstract class BaseLazySimpleReact extends BaseSimpleReact{ | ||
/** | ||
* Generate an infinite reactive flow. Requires a lazy flow. | ||
* | ||
* The flow will run indefinitely unless / until the provided Supplier throws an Exception | ||
* | ||
* @see com.aol.simple.react.async.Queue SimpleReact Queue for a way to create a more managable infinit flow | ||
* | ||
* @param s Supplier to generate the infinite flow | ||
* @return Next stage in the flow | ||
*/ | ||
public <U> SimpleReactStream< U> reactInfinitely(final Supplier<U> s) { | ||
if(isEager()) | ||
throw new InfiniteProcessingException("To reactInfinitely use a lazy stream"); | ||
Subscription sub = new Subscription(); | ||
SimpleReactStream stream = construct(StreamSupport.stream( | ||
new InfiniteClosingSpliterator(Long.MAX_VALUE, () -> CompletableFuture.completedFuture(s.get()),sub), false), | ||
this.getExecutor(),getRetrier(),false).withSubscription(sub); | ||
|
||
return stream; | ||
|
||
|
||
} | ||
|
||
public <U> SimpleReactStream<U> iterateInfinitely(final U seed, final UnaryOperator<U> f){ | ||
if(isEager()) | ||
throw new InfiniteProcessingException("To iterateInfinitely use a lazy stream"); | ||
return construct(Stream.iterate(seed, it -> f.apply(it)).map(it -> CompletableFuture.completedFuture(it)), | ||
this.getExecutor(),getRetrier(),false); | ||
} | ||
} |
Oops, something went wrong.