All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fiolino.common.processing.sink.MultiAggregatingSink Maven / Gradle / Ivy

Go to download

General structure to easily create dynamic logic via MethodHandles and others.

There is a newer version: 1.0.10
Show newest version
package org.fiolino.common.processing.sink;

import org.fiolino.common.container.Container;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Collects entries from multiple aggregations.
 * 

* Created by kuli on 27.03.16. */ public final class MultiAggregatingSink extends ThreadsafeChainedSink, List> { private final int chunkSize; private List list; private final Lock lock = new ReentrantLock(); public MultiAggregatingSink(ThreadsafeSink> target, int chunkSize) { super(target); this.chunkSize = chunkSize; initializeList(); } private void initializeList() { list = new ArrayList<>(chunkSize); } @Override public void accept(List values, Container metadata) throws Exception { if (values.isEmpty()) { return; } values = new ArrayList<>(values); lock.lock(); List toSend; try { list.addAll(values); if (list.size() < chunkSize) { return; } toSend = list; initializeList(); } finally { lock.unlock(); } getTarget().accept(toSend, metadata); } @Override public void commit(Container metadata) throws Exception { List toSend; lock.lock(); try { toSend = list; initializeList(); } finally { lock.unlock(); } if (!toSend.isEmpty()) { getTarget().accept(toSend, metadata); } super.commit(metadata); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy