Skip to content

Commit

Permalink
JAVAFICATION: Cache filter callsite and dedup some method names
Browse files Browse the repository at this point in the history
  • Loading branch information
original-brownbear committed Jun 18, 2018
1 parent 52e4f49 commit c56d62f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
@JRubyClass(name = "AbstractOutputDelegator")
public abstract class AbstractOutputDelegatorExt extends RubyObject {

public static final String OUTPUT_METHOD_NAME = "multi_receive";

private AbstractMetricExt metric;

protected AbstractNamespacedMetricExt namespacedMetric;
Expand Down Expand Up @@ -86,7 +88,7 @@ public IRubyObject metricEvents() {
return metricEvents;
}

@JRubyMethod(name = "multi_receive")
@JRubyMethod(name = OUTPUT_METHOD_NAME)
public IRubyObject multiReceive(final IRubyObject events) {
final RubyArray batch = (RubyArray) events;
final int count = batch.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.logstash.RubyUtil;
Expand All @@ -23,9 +24,11 @@
@JRubyClass(name = "JavaFilterDelegator")
public final class FilterDelegatorExt extends RubyObject {

private static final String FILTER_METHOD_NAME = "multi_filter";

private static final long serialVersionUID = 1L;

private IRubyObject filterClass;
private RubyClass filterClass;

private IRubyObject filter;

Expand All @@ -37,6 +40,8 @@ public final class FilterDelegatorExt extends RubyObject {

private LongCounter eventMetricIn;

private DynamicMethod filterMethod;

private LongCounter eventMetricTime;

private boolean flushes;
Expand All @@ -46,7 +51,8 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject fil
final IRubyObject id) {
this.id = (RubyString) id;
this.filter = filter;
this.filterClass = filter.getSingletonClass().getRealClass();
filterClass = filter.getSingletonClass().getRealClass();
filterMethod = filterClass.searchMethod(FILTER_METHOD_NAME);
final AbstractNamespacedMetricExt namespacedMetric =
(AbstractNamespacedMetricExt) filter.callMethod(context, "metric");
metricEvents = namespacedMetric.namespace(context, MetricKeys.EVENTS_KEY);
Expand All @@ -66,6 +72,7 @@ public FilterDelegatorExt initForTesting(final IRubyObject filter) {
eventMetricIn = LongCounter.DUMMY_COUNTER;
eventMetricTime = LongCounter.DUMMY_COUNTER;
this.filter = filter;
filterMethod = filter.getMetaClass().searchMethod(FILTER_METHOD_NAME);
flushes = filter.respondsTo("flush");
return this;
}
Expand Down Expand Up @@ -118,8 +125,8 @@ public IRubyObject getId() {
public RubyArray multiFilter(final RubyArray batch) {
eventMetricIn.increment((long) batch.size());
final long start = System.nanoTime();
final RubyArray result = (RubyArray) filter.callMethod(
WorkerLoop.THREAD_CONTEXT.get(), "multi_filter", batch
final RubyArray result = (RubyArray) filterMethod.call(
WorkerLoop.THREAD_CONTEXT.get(), filter, filterClass, FILTER_METHOD_NAME, batch
);
eventMetricTime.increment(
TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,23 @@ public final IRubyObject doClose(final ThreadContext context) {
return close(context);
}

@JRubyMethod(name = "multi_receive")
@JRubyMethod(name = AbstractOutputDelegatorExt.OUTPUT_METHOD_NAME)
public final IRubyObject multiReceive(final ThreadContext context, final IRubyObject events)
throws InterruptedException {
return output(context, events);
}

protected final void initOutputCallsite(final RubyClass outputClass) {
outputMethod = outputClass.searchMethod("multi_receive");
outputMethod = outputClass.searchMethod(AbstractOutputDelegatorExt.OUTPUT_METHOD_NAME);
this.outputClass = outputClass;
}

protected final void invokeOutput(final ThreadContext context, final IRubyObject batch,
final IRubyObject pluginInstance) {
outputMethod.call(context, pluginInstance, outputClass, "multi_receive", batch);
outputMethod.call(
context, pluginInstance, outputClass, AbstractOutputDelegatorExt.OUTPUT_METHOD_NAME,
batch
);
}

protected abstract IRubyObject output(ThreadContext context, IRubyObject events)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public IRubyObject executionContext(IRubyObject args) {
return this;
}

@JRubyMethod(name = "multi_receive")
@JRubyMethod(name = AbstractOutputDelegatorExt.OUTPUT_METHOD_NAME)
public IRubyObject multiReceive(final IRubyObject args) {
multiReceiveCallCount++;
multiReceiveArgs = args;
Expand Down

0 comments on commit c56d62f

Please sign in to comment.