PERFORMANCE: Remove TimestampBiValue

Fixes #7937
This commit is contained in:
Armin 2017-08-08 11:05:00 +02:00 committed by Armin Braun
parent c9b495d55f
commit cbff08fd8b
9 changed files with 49 additions and 105 deletions

View file

@ -14,7 +14,6 @@ import org.jruby.RubySymbol;
import org.logstash.ackedqueue.Queueable;
import org.logstash.bivalues.BiValues;
import org.logstash.bivalues.NullBiValue;
import org.logstash.bivalues.TimestampBiValue;
import org.logstash.ext.JrubyTimestampExtLibrary;
import static org.logstash.ObjectMappers.CBOR_MAPPER;
@ -314,8 +313,6 @@ public final class Event implements Cloneable, Queueable {
return ((JrubyTimestampExtLibrary.RubyTimestamp) o).getTimestamp();
} else if (o instanceof Timestamp) {
return (Timestamp) o;
} else if (o instanceof TimestampBiValue) {
return ((TimestampBiValue) o).javaValue();
} else if (o instanceof DateTime) {
return new Timestamp((DateTime) o);
} else if (o instanceof Date) {

View file

@ -5,6 +5,7 @@ import org.jruby.RubyFloat;
import org.jruby.RubyString;
import org.logstash.bivalues.BiValue;
import org.logstash.bivalues.BiValues;
import org.logstash.ext.JrubyTimestampExtLibrary;
public class Javafier {
private static final String ERR_TEMPLATE = "Missing Ruby class handling for full class name=%s, simple name=%s";
@ -20,12 +21,16 @@ public class Javafier {
if (o instanceof RubyString) {
return o.toString();
}
if (o instanceof String || o instanceof Float || o instanceof Double) {
if (o instanceof String || o instanceof Float || o instanceof Double ||
o instanceof Timestamp) {
return o;
}
if (o instanceof RubyFloat) {
return ((RubyFloat) o).getDoubleValue();
}
if (o instanceof JrubyTimestampExtLibrary.RubyTimestamp) {
return ((JrubyTimestampExtLibrary.RubyTimestamp) o).getTimestamp();
}
if (o instanceof BiValue) {
return ((BiValue)o).javaValue();
} else if(o instanceof ConvertedMap) {

View file

@ -11,6 +11,7 @@ import org.jruby.RubyString;
import org.jruby.runtime.builtin.IRubyObject;
import org.logstash.bivalues.BiValue;
import org.logstash.bivalues.BiValues;
import org.logstash.ext.JrubyTimestampExtLibrary;
public final class Rubyfier {
private static final String ERR_TEMPLATE = "Missing Java class handling for full class name=%s, simple name=%s";
@ -27,12 +28,19 @@ public final class Rubyfier {
}
public static IRubyObject deep(Ruby runtime, final Object input) {
if (input instanceof RubyString) return (RubyString) input;
if (input instanceof RubyString || input instanceof RubyFloat
|| input instanceof JrubyTimestampExtLibrary.RubyTimestamp) {
return (IRubyObject) input;
}
if (input instanceof String) return BiValues.RUBY.newString((String) input);
if (input instanceof RubyFloat) return (RubyFloat) input;
if (input instanceof Double || input instanceof Float) {
return BiValues.RUBY.newFloat(((Number) input).doubleValue());
}
if (input instanceof Timestamp) {
return JrubyTimestampExtLibrary.RubyTimestamp.newRubyTimestamp(
BiValues.RUBY, (Timestamp) input
);
}
if (input instanceof BiValue) return ((BiValue) input).rubyValue(runtime);
if (input instanceof Map) return deepMap(runtime, (Map) input);
if (input instanceof List) return deepList(runtime, (List) input);

View file

@ -51,24 +51,33 @@ public final class Valuefier {
}
}
public static Object convert(Object o) {
if (o instanceof RubyString) {
public static Object convert(final Object o) {
if (o instanceof RubyString || o instanceof RubyFloat
|| o instanceof JrubyTimestampExtLibrary.RubyTimestamp
|| o instanceof ConvertedMap || o instanceof ConvertedList
|| o instanceof BiValue) {
return o;
}
if (o instanceof String) {
return BiValues.RUBY.newString((String) o);
}
if (o instanceof RubyFloat) {
return o;
}
if (o instanceof Float || o instanceof Double) {
return BiValues.RUBY.newFloat(((Number) o).doubleValue());
}
if (o instanceof ConvertedMap || o instanceof ConvertedList) {
return o;
if (o instanceof Timestamp) {
return JrubyTimestampExtLibrary.RubyTimestamp.newRubyTimestamp(
BiValues.RUBY, (Timestamp) o
);
}
if (o instanceof BiValue) {
return o;
if (o instanceof RubyTime) {
return JrubyTimestampExtLibrary.RubyTimestamp.newRubyTimestamp(
BiValues.RUBY, new Timestamp(((RubyTime) o).getDateTime())
);
}
if (o instanceof DateTime) {
return JrubyTimestampExtLibrary.RubyTimestamp.newRubyTimestamp(
BiValues.RUBY, new Timestamp((DateTime) o)
);
}
if (o instanceof RubyHash) {
return ConvertedMap.newFromRubyHash((RubyHash) o);
@ -88,16 +97,6 @@ public final class Valuefier {
if (o instanceof ArrayJavaProxy || o instanceof ConcreteJavaProxy){
return convertJavaProxy((JavaProxy) o);
}
if (o instanceof RubyTime) {
RubyTime time = (RubyTime) o;
Timestamp ts = new Timestamp(time.getDateTime());
JrubyTimestampExtLibrary.RubyTimestamp rts = JrubyTimestampExtLibrary.RubyTimestamp.newRubyTimestamp(time.getRuntime(), ts);
return convertNonCollection(rts);
}
if (o instanceof DateTime) {
Timestamp ts = new Timestamp((DateTime) o);
return convertNonCollection(ts);
}
return o == null ? BiValues.NULL_BI_VALUE : convertNonCollection(o);
}
}

View file

@ -11,12 +11,8 @@ import org.jruby.RubySymbol;
import org.jruby.ext.bigdecimal.RubyBigDecimal;
import org.jruby.java.proxies.JavaProxy;
import org.jruby.runtime.builtin.IRubyObject;
import org.logstash.Timestamp;
import org.logstash.ext.JrubyTimestampExtLibrary.RubyTimestamp;
public enum BiValues {
ORG_LOGSTASH_EXT_JRUBYTIMESTAMPEXTLIBRARY$RUBYTIMESTAMP(BiValueType.TIMESTAMP),
ORG_LOGSTASH_TIMESTAMP(BiValueType.TIMESTAMP),
JAVA_LANG_BOOLEAN(BiValueType.BOOLEAN),
JAVA_LANG_INTEGER(BiValueType.INT),
JAVA_LANG_LONG(BiValueType.LONG),
@ -36,8 +32,6 @@ public enum BiValues {
private static HashMap<String, String> initCache() {
HashMap<String, String> hm = new HashMap<>();
hm.put("org.logstash.Timestamp", "ORG_LOGSTASH_TIMESTAMP");
hm.put("org.logstash.ext.JrubyTimestampExtLibrary$RubyTimestamp", "ORG_LOGSTASH_EXT_JRUBYTIMESTAMPEXTLIBRARY$RUBYTIMESTAMP");
hm.put("java.lang.Boolean", "JAVA_LANG_BOOLEAN");
hm.put("java.lang.Integer", "JAVA_LANG_INTEGER");
hm.put("java.lang.Long", "JAVA_LANG_LONG");
@ -136,14 +130,6 @@ public enum BiValues {
return new BooleanBiValue((Boolean) value);
}
},
TIMESTAMP {
BiValue build(Object value) {
if (value instanceof IRubyObject) {
return new TimestampBiValue((RubyTimestamp) value);
}
return new TimestampBiValue((Timestamp) value);
}
},
NULL {
NullBiValue build(Object value) {
return NULL_BI_VALUE;

View file

@ -1,36 +0,0 @@
package org.logstash.bivalues;
import org.logstash.Timestamp;
import org.logstash.ext.JrubyTimestampExtLibrary.RubyTimestamp;
import org.jruby.Ruby;
import java.io.ObjectStreamException;
public class TimestampBiValue extends BiValue<RubyTimestamp, Timestamp> {
public TimestampBiValue(RubyTimestamp rubyValue) {
this.rubyValue = rubyValue;
javaValue = null;
}
public TimestampBiValue(Timestamp javaValue) {
this.javaValue = javaValue;
rubyValue = null;
}
private TimestampBiValue() {
}
protected void addRuby(Ruby runtime) {
rubyValue = RubyTimestamp.newRubyTimestamp(runtime, javaValue);
}
protected void addJava() {
javaValue = rubyValue.getTimestamp();
}
// Called when object is to be serialized on a stream to allow the object to substitute a proxy for itself.
private Object writeReplace() throws ObjectStreamException {
return newProxy(this);
}
}

View file

@ -6,13 +6,24 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.BeforeClass;
import org.junit.Test;
import org.logstash.bivalues.BiValues;
import static net.javacrumbs.jsonunit.JsonAssert.assertJsonEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
public class EventTest {
public final class EventTest {
/**
* Some of these tests require a fully initialized global {@link org.jruby.Ruby} instance
* so we force the creation of the "LogStash" module here.
*/
@BeforeClass
public static void before() {
BiValues.RUBY.getOrCreateModule("LogStash");
}
@Test
public void queueableInterfaceRoundTrip() throws Exception {

View file

@ -19,7 +19,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.logstash.bivalues.BiValue;
import org.logstash.bivalues.TimestampBiValue;
import org.logstash.ext.JrubyTimestampExtLibrary;
import static junit.framework.TestCase.assertEquals;
@ -63,7 +63,7 @@ public class ValuefierTest extends TestBase {
public void testRubyTime() {
RubyTime ro = RubyTime.newTime(ruby, DateTime.now());
Object result = Valuefier.convert(ro);
assertEquals(TimestampBiValue.class, result.getClass());
assertEquals(JrubyTimestampExtLibrary.RubyTimestamp.class, result.getClass());
}
@Test
@ -71,7 +71,7 @@ public class ValuefierTest extends TestBase {
DateTime jo = DateTime.now();
Object result = Valuefier.convert(jo);
assertEquals(TimestampBiValue.class, result.getClass());
assertEquals(JrubyTimestampExtLibrary.RubyTimestamp.class, result.getClass());
}
@Rule

View file

@ -12,8 +12,6 @@ import org.jruby.ext.bigdecimal.RubyBigDecimal;
import org.jruby.javasupport.JavaUtil;
import org.junit.Test;
import org.logstash.TestBase;
import org.logstash.Timestamp;
import org.logstash.ext.JrubyTimestampExtLibrary.RubyTimestamp;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -130,30 +128,6 @@ public class BiValuesTest extends TestBase {
assertEquals(ro.getClass(), subject.rubyValue(ruby).getClass());
}
@Test
public void testBiValuesTimestampRuby() {
Timestamp jo = new Timestamp("2014-09-23T00:00:00-0800");
RubyTimestamp ro = RubyTimestamp.newRubyTimestamp(ruby, jo);
BiValue subject = BiValues.newBiValue(ro);
assertEquals(ro, subject.rubyValueUnconverted());
assertEquals(ro.getClass(), subject.rubyValue(ruby).getClass());
assertEquals(jo, subject.javaValue());
assertEquals(Timestamp.class, subject.javaValue().getClass());
}
@Test
public void testBiValuesTimestampJava() {
Timestamp jo = new Timestamp("2014-09-23T00:00:00-0800");
RubyTimestamp ro = RubyTimestamp.newRubyTimestamp(ruby, jo);
BiValue subject = BiValues.newBiValue(jo);
assertEquals(jo, subject.javaValue());
assertEquals(Timestamp.class, subject.javaValue().getClass());
assertEquals(ro.toString(), subject.rubyValue(ruby).toString());
assertEquals(ro.getClass(), subject.rubyValue(ruby).getClass());
}
@Test
public void testBiValuesNilRuby() {
RubyNil ro = (RubyNil) ruby.getNil();