/** * Returns a Consumer representing an evict determiner that evict all tuples * from the window. * @param <T> Tuple type * @param <K> Key type * @param <L> List type for the partition contents. * @return An evict determiner that evicts all tuples. */ public static <T, K, L extends List<T>> Consumer<Partition<T, K, L> > evictAll(){ return partition -> partition.getContents().clear(); }
/** * Returns an evict determiner that evicts the oldest tuple. * @param <T> Tuple type * @param <K> Key type * @param <L> List type for the partition contents. * @return A evict determiner that evicts the oldest tuple. */ public static <T, K, L extends List<T>> Consumer<Partition<T, K, L> > evictOldest(){ return partition -> partition.getContents().remove(0); }
/** * Returns a Consumer representing an evict determiner that evict all tuples * from the window. * @param <T> Tuple type * @param <K> Key type * @param <L> List type for the partition contents. * @return An evict determiner that evicts all tuples. */ public static <T, K, L extends List<T>> Consumer<Partition<T, K, L> > evictAll(){ return partition -> partition.getContents().clear(); }
/** * Returns an evict determiner that evicts the oldest tuple. * @param <T> Tuple type * @param <K> Key type * @param <L> List type for the partition contents. * @return A evict determiner that evicts the oldest tuple. */ public static <T, K, L extends List<T>> Consumer<Partition<T, K, L> > evictOldest(){ return partition -> partition.getContents().remove(0); }
/** * Returns a count-based contents policy. * If, when called, the number of tuples in the partition is * greater than equal to {@code count} then {@code partition.evict()} * is called. * @param <T> Tuple type * @param <K> Key type * @param <L> List type for the partition contents. * @param count the count * @return A count-based contents policy. */ public static <T, K, L extends List<T>> BiConsumer<Partition<T, K, L>, T> countContentsPolicy(final int count){ return (partition, tuple) -> { if (partition.getContents().size() >= count) partition.evict(); }; }
/** * Returns a count-based contents policy. * If, when called, the number of tuples in the partition is * greater than equal to {@code count} then {@code partition.evict()} * is called. * @param <T> Tuple type * @param <K> Key type * @param <L> List type for the partition contents. * @param count the count * @return A count-based contents policy. */ public static <T, K, L extends List<T>> BiConsumer<Partition<T, K, L>, T> countContentsPolicy(final int count){ return (partition, tuple) -> { if (partition.getContents().size() >= count) partition.evict(); }; }
/** * Returns a trigger policy that triggers when the size of a partition * equals or exceeds a value, and then evicts its contents. * @param <T> Tuple type * @param <K> Key type * @param <L> List type for the partition contents. * @param size partition size * @return A trigger policy that triggers processing when the size of * the partition equals or exceets a value. */ public static <T, K, L extends List<T>> BiConsumer<Partition<T, K, L>, T> processWhenFullAndEvict(final int size){ return (partition, tuple) -> { if(partition.getContents().size() >= size){ partition.process(); partition.evict(); } }; }
/** * Returns a trigger policy that triggers when the size of a partition * equals or exceeds a value, and then evicts its contents. * @param <T> Tuple type * @param <K> Key type * @param <L> List type for the partition contents. * @param size partition size * @return A trigger policy that triggers processing when the size of * the partition equals or exceets a value. */ public static <T, K, L extends List<T>> BiConsumer<Partition<T, K, L>, T> processWhenFullAndEvict(final int size){ return (partition, tuple) -> { if(partition.getContents().size() >= size){ partition.process(); partition.evict(); } }; }
/** * A policy which schedules a future partition eviction if the partition is empty. * This can be used as a contents policy that is scheduling the eviction of * the tuple just about to be inserted. * @param <T> Tuple Type * @param <K> Key type * @param <L> List type for the partition contents. * @param time The time span in which tuple are permitted in the partition. * @param unit The units of time. * @return The time-based contents policy. */ public static <T, K, L extends List<T>> BiConsumer<Partition<T, K, L>, T> scheduleEvictIfEmpty(long time, TimeUnit unit){ return (partition, tuple) -> { if(partition.getContents().isEmpty()){ ScheduledExecutorService ses = partition.getWindow().getScheduledExecutorService(); ses.schedule(() -> partition.evict(), time, unit); } }; }
/** * A policy which schedules a future partition eviction if the partition is empty. * This can be used as a contents policy that is scheduling the eviction of * the tuple just about to be inserted. * @param <T> Tuple Type * @param <K> Key type * @param <L> List type for the partition contents. * @param time The time span in which tuple are permitted in the partition. * @param unit The units of time. * @return The time-based contents policy. */ public static <T, K, L extends List<T>> BiConsumer<Partition<T, K, L>, T> scheduleEvictIfEmpty(long time, TimeUnit unit){ return (partition, tuple) -> { if(partition.getContents().isEmpty()){ ScheduledExecutorService ses = partition.getWindow().getScheduledExecutorService(); ses.schedule(() -> partition.evict(), time, unit); } }; }
/** * An eviction policy which processes the window, evicts all tuples, and * schedules the next eviction after the appropriate interval. * @param <T> Tuple Type * @param <K> Key type * @param time The timespan in which tuple are permitted in the partition. * @param unit The units of time. * @return The time-based eviction policy. */ public static <T, K> Consumer<Partition<T, K, List<T>> > evictAllAndScheduleEvictWithProcess(long time, TimeUnit unit){ long timeMs = TimeUnit.MILLISECONDS.convert(time, unit); return (partition) -> { ScheduledExecutorService ses = partition.getWindow().getScheduledExecutorService(); List<T> tuples = partition.getContents(); partition.process(); tuples.clear(); ses.schedule(() -> partition.evict(), timeMs, TimeUnit.MILLISECONDS); }; }
/** * An eviction policy which processes the window, evicts all tuples, and * schedules the next eviction after the appropriate interval. * @param <T> Tuple Type * @param <K> Key type * @param time The timespan in which tuple are permitted in the partition. * @param unit The units of time. * @return The time-based eviction policy. */ public static <T, K> Consumer<Partition<T, K, List<T>> > evictAllAndScheduleEvictWithProcess(long time, TimeUnit unit){ long timeMs = TimeUnit.MILLISECONDS.convert(time, unit); return (partition) -> { ScheduledExecutorService ses = partition.getWindow().getScheduledExecutorService(); List<T> tuples = partition.getContents(); partition.process(); tuples.clear(); ses.schedule(() -> partition.evict(), timeMs, TimeUnit.MILLISECONDS); }; }
/** * An eviction policy which evicts all tuples that are older than a specified time. * If any tuples remain in the partition, it schedules their eviction after * an appropriate interval. * @param <T> Tuple Type * @param <K> Key type * @param time The timespan in which tuple are permitted in the partition. * @param unit The units of time. * @return The time-based eviction policy. */ public static <T, K> Consumer<Partition<T, K, InsertionTimeList<T>> > evictOlderWithProcess(long time, TimeUnit unit){ long timeMs = TimeUnit.MILLISECONDS.convert(time, unit); return (partition) -> { ScheduledExecutorService ses = partition.getWindow().getScheduledExecutorService(); InsertionTimeList<T> tuples = partition.getContents(); long evictTime = System.currentTimeMillis() - timeMs; tuples.evictOlderThan(evictTime); partition.process(); if(!tuples.isEmpty()){ ses.schedule(() -> partition.evict(), tuples.nextEvictDelay(timeMs), TimeUnit.MILLISECONDS); } }; }
/** * An eviction policy which evicts all tuples that are older than a specified time. * If any tuples remain in the partition, it schedules their eviction after * an appropriate interval. * @param <T> Tuple Type * @param <K> Key type * @param time The timespan in which tuple are permitted in the partition. * @param unit The units of time. * @return The time-based eviction policy. */ public static <T, K> Consumer<Partition<T, K, InsertionTimeList<T>> > evictOlderWithProcess(long time, TimeUnit unit){ long timeMs = TimeUnit.MILLISECONDS.convert(time, unit); return (partition) -> { ScheduledExecutorService ses = partition.getWindow().getScheduledExecutorService(); InsertionTimeList<T> tuples = partition.getContents(); long evictTime = System.currentTimeMillis() - timeMs; tuples.evictOlderThan(evictTime); partition.process(); if(!tuples.isEmpty()){ ses.schedule(() -> partition.evict(), tuples.nextEvictDelay(timeMs), TimeUnit.MILLISECONDS); } }; }
@Override public <J, U, K> TStream<J> joinLast(Function<T, K> keyer, TStream<U> lastStream, Function<U, K> lastStreamKeyer, BiFunction<T, U, J> joiner) { BiFunction<List<U>,K, Object> processor = Functions.synchronizedBiFunction((list, key) -> null); Window<U, K, LinkedList<U>> window = Windows.lastNProcessOnInsert(1, lastStreamKeyer); Aggregate<U,Object,K> op = new Aggregate<U,Object,K>(window, processor); lastStream.pipe(op); return this.map((tuple) -> { Partition<U, K, ? extends List<U>> part = window.getPartitions().get(keyer.apply(tuple)); if(part == null) return null; J ret; synchronized (part) { U last = part.getContents().get(0); ret = joiner.apply(tuple, last); } return ret; }); }
@Override public <J, U, K> TStream<J> joinLast(Function<T, K> keyer, TStream<U> lastStream, Function<U, K> lastStreamKeyer, BiFunction<T, U, J> joiner) { BiFunction<List<U>,K, Object> processor = Functions.synchronizedBiFunction((list, key) -> null); Window<U, K, LinkedList<U>> window = Windows.lastNProcessOnInsert(1, lastStreamKeyer); Aggregate<U,Object,K> op = new Aggregate<U,Object,K>(window, processor); lastStream.pipe(op); return this.map((tuple) -> { Partition<U, K, ? extends List<U>> part = window.getPartitions().get(keyer.apply(tuple)); if(part == null) return null; J ret; synchronized (part) { U last = part.getContents().get(0); ret = joiner.apply(tuple, last); } return ret; }); }
partition.getContents().clear(); }, Policies.processOnInsert(),
J ret; synchronized (part) { List<U> last = part.getContents(); ret = joiner.apply(tuple, last);
J ret; synchronized (part) { List<U> last = part.getContents(); ret = joiner.apply(tuple, last);