org.fiolino.common.processing.sink.MultiAggregatingSink Maven / Gradle / Ivy
Show all versions of commons Show documentation
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);
}
}