MINOR: Cleanup Event serialization and fix related compiler warnings

Fixes #7922
This commit is contained in:
Armin 2017-08-06 12:56:18 +02:00 committed by Armin Braun
parent 1b0158c2d5
commit d45b15a366
2 changed files with 27 additions and 25 deletions

View file

@ -1,5 +1,6 @@
package org.logstash; package org.logstash;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
@ -175,35 +176,25 @@ public final class Event implements Cloneable, Queueable {
} }
} }
private Map<String, Map<String, Object>> toSerializableMap() { private static Event fromSerializableMap(final byte[] source) throws IOException {
HashMap<String, Map<String, Object>> hashMap = new HashMap<>(); final Map<String, Map<String, Object>> representation =
hashMap.put(DATA_MAP_KEY, this.data); CBOR_MAPPER.readValue(source, ObjectMappers.EVENT_MAP_TYPE);
hashMap.put(META_MAP_KEY, this.metadata); if (representation == null) {
return hashMap; throw new IOException("incompatible from binary object type only HashMap is supported");
} }
final Map<String, Object> dataMap = representation.get(DATA_MAP_KEY);
private static Event fromSerializableMap(Map<String, Map<String, Object>> representation) throws IOException{ if (dataMap == null) {
if (!representation.containsKey(DATA_MAP_KEY)) {
throw new IOException("The deserialized Map must contain the \"DATA\" key"); throw new IOException("The deserialized Map must contain the \"DATA\" key");
} }
if (!representation.containsKey(META_MAP_KEY)) { final Map<String, Object> metaMap = representation.get(META_MAP_KEY);
if (metaMap == null) {
throw new IOException("The deserialized Map must contain the \"META\" key"); throw new IOException("The deserialized Map must contain the \"META\" key");
} }
Map<String, Object> dataMap = representation.get(DATA_MAP_KEY); dataMap.put(METADATA, metaMap);
dataMap.put(METADATA, representation.get(META_MAP_KEY));
return new Event(dataMap); return new Event(dataMap);
} }
private static Map<String, Map<String, Object>> fromBinaryToMap(byte[] source) throws IOException { public String toJson() throws JsonProcessingException {
Object o = CBOR_MAPPER.readValue(source, HashMap.class);
if (o == null) {
throw new IOException("incompatible from binary object type only HashMap is supported");
} else {
return (Map<String, Map<String, Object>>) o;
}
}
public String toJson() throws IOException {
return JSON_MAPPER.writeValueAsString(this.data); return JSON_MAPPER.writeValueAsString(this.data);
} }
@ -388,14 +379,17 @@ public final class Event implements Cloneable, Queueable {
} }
@Override @Override
public byte[] serialize() throws IOException { public byte[] serialize() throws JsonProcessingException {
return CBOR_MAPPER.writeValueAsBytes(toSerializableMap()); final Map<String, Map<String, Object>> map = new HashMap<>(2, 1.0F);
map.put(DATA_MAP_KEY, this.data);
map.put(META_MAP_KEY, this.metadata);
return CBOR_MAPPER.writeValueAsBytes(map);
} }
public static Event deserialize(byte[] data) throws IOException { public static Event deserialize(byte[] data) throws IOException {
if (data == null || data.length == 0) { if (data == null || data.length == 0) {
return new Event(); return new Event();
} }
return fromSerializableMap(fromBinaryToMap(data)); return fromSerializableMap(data);
} }
} }

View file

@ -1,6 +1,7 @@
package org.logstash; package org.logstash;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.module.SimpleModule;
@ -9,6 +10,7 @@ import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import com.fasterxml.jackson.dataformat.cbor.CBORGenerator; import com.fasterxml.jackson.dataformat.cbor.CBORGenerator;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule; import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import org.jruby.RubyFloat; import org.jruby.RubyFloat;
import org.jruby.RubyString; import org.jruby.RubyString;
@ -26,6 +28,12 @@ public final class ObjectMappers {
new CBORFactory().configure(CBORGenerator.Feature.WRITE_MINIMAL_INTS, false) new CBORFactory().configure(CBORGenerator.Feature.WRITE_MINIMAL_INTS, false)
).registerModule(new AfterburnerModule()).registerModule(RUBY_SERIALIZERS); ).registerModule(new AfterburnerModule()).registerModule(RUBY_SERIALIZERS);
/**
* {@link JavaType} for the {@link HashMap} that {@link Event} is serialized as.
*/
public static final JavaType EVENT_MAP_TYPE =
CBOR_MAPPER.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class);
private ObjectMappers() { private ObjectMappers() {
} }