package com.cloudera.flume.handlers.debug;
import java.io.IOException;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cloudera.flume.conf.Context;
import com.cloudera.flume.conf.SinkFactory.SinkDecoBuilder;
import com.cloudera.flume.core.Event;
import com.cloudera.flume.core.EventImpl;
import com.cloudera.flume.core.EventSink;
import com.cloudera.flume.core.EventSinkDecorator;
import com.cloudera.util.bloom.BloomSet;
import com.google.common.base.Preconditions;
public static final Logger LOG = LoggerFactory.getLogger(BloomGeneratorDeco.class);
protected BloomSet bloom;
final int size;
final int hashes;
public final static String A_BLOOMSETDATA = "bloomSetData";
super(s);
this.size = size;
this.hashes = hashes;
}
this(null, size, hashes);
}
@Override
public void open()
throws IOException {
bloom = new BloomSet(size, hashes);
super.open();
}
@Override
public void append(Event e)
throws IOException {
includeEvent(bloom, e);
super.append(e);
}
@Override
public void close()
throws IOException {
EventImpl e = new EventImpl(new byte[0]);
addBloom(bloom, e);
super.append(e);
super.close();
}
int hash = Arrays.hashCode(e.getBody());
bloom.addInt(hash);
}
static void addBloom(BloomSet bloom, Event e)
throws IOException {
e.set(A_BLOOMSETDATA, bloom.getBytes());
}
public static SinkDecoBuilder
builder() {
return new SinkDecoBuilder() {
@Override
public EventSinkDecorator<EventSink>
build(Context ctx, String... argv) {
Preconditions.checkArgument(argv.length <= 2,
"usage: bloomCheck[(sz[,hashes])]");
int sz = 100000000;
int hashes = 2;
if (argv.length >= 1) {
sz = Integer.parseInt(argv[0]);
}
if (argv.length >= 2) {
hashes = Integer.parseInt(argv[1]);
}
return new BloomGeneratorDeco(sz, hashes);
}
};
}
}