/** * 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); }; }
/** * 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 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 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(); }
/** * 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); } }; }
if (partition.getKey().equals(1) || partition.getKey().equals(3)) { return false; partition.getContents().clear(); }, Policies.processOnInsert(),
/** * Returns a trigger policy that triggers * processing on every insert. * @param <T> Tuple type * @param <K> Key type * @param <L> List type for the partition contents. * @return A trigger policy that triggers processing on every insert. */ public static <T, K, L extends List<T>> BiConsumer<Partition<T, K, L>, T> processOnInsert(){ return (partition, tuple) -> partition.process(); }
@Override public boolean insert(T tuple) { K key = keyFunction.apply(tuple); Partition<T, K, L> partition; synchronized (partitions) { partition = partitions.get(key); if (partition == null) { partition = new PartitionImpl<T, K, L>(this, listSupplier.get(), key); partitions.put(key, partition); } } return partition.insert(tuple); }
/** * 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 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); } }; }
/** * 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 * processing on every insert. * @param <T> Tuple type * @param <K> Key type * @param <L> List type for the partition contents. * @return A trigger policy that triggers processing on every insert. */ public static <T, K, L extends List<T>> BiConsumer<Partition<T, K, L>, T> processOnInsert(){ return (partition, tuple) -> partition.process(); }
@Override public boolean insert(T tuple) { K key = keyFunction.apply(tuple); Partition<T, K, L> partition; synchronized (partitions) { partition = partitions.get(key); if (partition == null) { partition = new PartitionImpl<T, K, L>(this, listSupplier.get(), key); partitions.put(key, partition); } } return partition.insert(tuple); }
/** * 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); }; }
/** * 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(); }
/** * 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); } }; }