package com.cloudera.flume.core;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.cloudera.flume.handlers.debug.NullSink;
import com.cloudera.flume.reporter.ReportEvent;
import com.cloudera.util.MultipleIOException;
import com.google.common.base.Preconditions;
public class DemuxSink<S
extends EventSink>
extends EventSink.Base {
String field;
Map<byte[], S> split;
final EventSink fallthrough;
public DemuxSink(String field, Map<
byte[], S> split, EventSink fallthrough) {
Preconditions.checkNotNull(field);
Preconditions.checkNotNull(split);
Preconditions.checkNotNull(fallthrough);
this.field = field;
this.split = split;
this.fallthrough = fallthrough;
}
public DemuxSink(String field, Map<
byte[], S> split) {
this(field, split, new NullSink());
}
@Override
public void append(Event e)
throws IOException {
byte[] val = e.get(field);
S handler = split.get(val);
if (handler == null) {
fallThrough(val, e);
return;
}
handler.append(e);
super.append(e);
}
public void fallThrough(
byte[] val, Event e)
throws IOException {
fallthrough.append(e);
super.append(e);
}
@Override
public void close()
throws IOException {
List<IOException> exs = new ArrayList<IOException>();
for (S snk : split.values()) {
try {
snk.close();
} catch (IOException ioe) {
exs.add(ioe);
}
}
if (!exs.isEmpty()) {
throw MultipleIOException.createIOException(exs);
}
}
@Override
public void open()
throws IOException {
List<IOException> exs = new ArrayList<IOException>();
for (S snk : split.values()) {
try {
snk.open();
} catch (IOException ioe) {
exs.add(ioe);
}
}
if (!exs.isEmpty()) {
throw MultipleIOException.createIOException(exs);
}
}
@Override
public void getReports(String namePrefix, Map<String, ReportEvent> reports) {
super.getReports(namePrefix, reports);
int count = 0;
for (S snk : split.values()) {
snk.getReports(namePrefix + getName() + "." + count + ".", reports);
count++;
}
fallthrough.getReports(namePrefix + getName() + ".fallthrough.", reports);
}
}