Refactor: avoid ThreadContext retrieval + use Ruby API

- thread-context isn't needed - only the runtime is really used
- native Ruby types do not need to pass through Java coercion

Fixes #11365
This commit is contained in:
Karol Bucek 2019-11-28 21:02:36 +01:00
parent ba51feea21
commit dfa8ea719f
3 changed files with 17 additions and 19 deletions

View file

@ -1,8 +1,9 @@
package org.logstash.ackedqueue;
import java.io.IOException;
import org.jruby.Ruby;
import org.jruby.RubyBoolean;
import org.jruby.RubyHash;
import org.jruby.runtime.ThreadContext;
import org.logstash.Event;
import org.logstash.ext.JrubyEventExtLibrary;
@ -15,11 +16,12 @@ public final class AckedBatch {
return ackedBatch;
}
public RubyHash toRubyHash(ThreadContext context) {
final RubyHash result = RubyHash.newHash(context.runtime);
this.batch.getElements().forEach(e -> result.put(
JrubyEventExtLibrary.RubyEvent.newRubyEvent(context.runtime, (Event) e),
context.tru
public RubyHash toRubyHash(final Ruby runtime) {
final RubyBoolean trueValue = runtime.getTrue();
final RubyHash result = RubyHash.newHash(runtime);
this.batch.getElements().forEach(e -> result.fastASet(
JrubyEventExtLibrary.RubyEvent.newRubyEvent(runtime, (Event) e),
trueValue
)
);
return result;

View file

@ -28,21 +28,20 @@ public final class AckedReadBatch implements QueueBatch {
}
private AckedReadBatch(final JRubyAckedQueueExt queue, final int size, final long timeout) {
ThreadContext context = RUBY.getCurrentContext();
AckedBatch batch = null;
AckedBatch batch;
try {
batch = queue.readBatch(size, timeout);
} catch (IOException e) {
throw new IllegalStateException(e);
}
if (batch == null) {
originals = RubyHash.newHash(context.runtime);
originals = RubyHash.newHash(RUBY);
ackedBatch = null;
} else {
ackedBatch = batch;
originals = ackedBatch.toRubyHash(context);
originals = ackedBatch.toRubyHash(RUBY);
}
generated = RubyHash.newHash(context.runtime);
generated = RubyHash.newHash(RUBY);
}
@Override
@ -55,18 +54,17 @@ public final class AckedReadBatch implements QueueBatch {
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public RubyArray to_a() {
ThreadContext context = RUBY.getCurrentContext();
final RubyArray result = context.runtime.newArray(filteredSize());
final RubyArray result = RUBY.newArray(filteredSize());
for (final JrubyEventExtLibrary.RubyEvent event
: (Collection<JrubyEventExtLibrary.RubyEvent>) originals.keys()) {
if (!MemoryReadBatch.isCancelled(event)) {
result.add(event);
result.append(event);
}
}
for (final JrubyEventExtLibrary.RubyEvent event
: (Collection<JrubyEventExtLibrary.RubyEvent>) generated.keys()) {
if (!MemoryReadBatch.isCancelled(event)) {
result.add(event);
result.append(event);
}
}
return result;

View file

@ -1,7 +1,6 @@
package org.logstash.execution;
import org.jruby.RubyArray;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.logstash.ext.JrubyEventExtLibrary;
@ -32,11 +31,10 @@ public final class MemoryReadBatch implements QueueBatch {
@Override
@SuppressWarnings({"rawtypes"})
public RubyArray to_a() {
ThreadContext context = RUBY.getCurrentContext();
final RubyArray result = context.runtime.newArray(events.size());
final RubyArray result = RUBY.newArray(events.size());
for (final IRubyObject event : events) {
if (!isCancelled(event)) {
result.add(event);
result.append(event);
}
}
return result;