package com.cloudera.flume.core;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.Map.Entry;
import org.apache.commons.lang.StringEscapeUtils;
import com.cloudera.flume.conf.FlumeConfiguration;
import com.cloudera.util.Clock;
import com.cloudera.util.NetUtils;
import com.google.common.base.Preconditions;
public class EventImpl extends EventBaseImpl {
byte[] body;
long timestamp;
Priority pri;
long nanos;
String host;
final static long MAX_BODY_SIZE = FlumeConfiguration.get().getEventMaxSizeBytes();
this(new byte[0], 0, Priority.INFO, 0, "");
}
this(e.getBody(), e.getTimestamp(), e.getPriority(), e.getNanos(), e
.getHost(), new HashMap<String, byte[]>(e.getAttrs()));
}
this(s, Clock.unixTime(), Priority.INFO, Clock.nanos(), NetUtils
.localhost());
}
this(s, Clock.unixTime(), pri, Clock.nanos(), NetUtils.localhost());
}
public EventImpl(
byte[] s,
long timestamp, Priority pri,
long nanoTime,
String host) {
this(s, timestamp, pri, nanoTime, host, new HashMap<String, byte[]>());
}
public EventImpl(
byte[] s,
long timestamp, Priority pri,
long nanoTime,
String host, Map<String, byte[]> fields) {
super(fields);
Preconditions.checkNotNull(s);
Preconditions.checkArgument(s.length <= MAX_BODY_SIZE);
Preconditions.checkNotNull(pri);
this.body = s;
this.timestamp = timestamp;
this.pri = pri;
this.nanos = nanoTime;
this.host = host;
}
return body;
}
return pri;
}
this.pri = p;
}
return timestamp;
}
this.timestamp = stamp;
}
String mbody = StringEscapeUtils.escapeJava(new String(getBody()));
StringBuilder attrs = new StringBuilder();
SortedMap<String, byte[]> sorted = new TreeMap<String, byte[]>(this.fields);
for (Entry<String, byte[]> e : sorted.entrySet()) {
attrs.append("{ " + e.getKey() + " : ");
String o = Attributes.toString(this, e.getKey());
attrs.append(o + " } ");
}
return getHost() + " [" + getPriority().toString() + " "
+ new Date(getTimestamp()) + "] " + attrs.toString() + mbody;
}
@Override
return nanos;
}
@Override
return host;
}
public static Event
select(Event e, String... attrs) {
Event e2 = new EventImpl(e.getBody(), e.getTimestamp(), e.getPriority(), e
.getNanos(), e.getHost());
for (String a : attrs) {
byte[] data = e.get(a);
if (data == null) {
continue;
}
e2.set(a, data);
}
return e2;
}
public static Event
unselect(Event e, String... attrs) {
Event e2 = new EventImpl(e.getBody(), e.getTimestamp(), e.getPriority(), e
.getNanos(), e.getHost());
List<String> as = Arrays.asList(attrs);
for (Entry<String, byte[]> ent : e.getAttrs().entrySet()) {
String a = ent.getKey();
if (as.contains(a)) {
continue;
}
byte[] data = e.get(a);
e2.set(a, data);
}
return e2;
}
}