Skip to content

Commit

Permalink
Refactor: simplify config_hash generation
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Jul 7, 2021
1 parent d10debb commit 9b9b7b1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
import org.logstash.RubyUtil;
import org.logstash.ackedqueue.QueueFactoryExt;
import org.logstash.ackedqueue.ext.JRubyAckedQueueExt;
Expand Down Expand Up @@ -148,19 +149,14 @@ public AbstractPipelineExt(final Ruby runtime, final RubyClass metaClass) {
@JRubyMethod
public final AbstractPipelineExt initialize(final ThreadContext context,
final IRubyObject pipelineConfig, final IRubyObject namespacedMetric,
final IRubyObject rubyLogger)
throws NoSuchAlgorithmException {
final IRubyObject rubyLogger) {
reporter = new PipelineReporterExt(
context.runtime, RubyUtil.PIPELINE_REPORTER_CLASS).initialize(context, rubyLogger, this
);
pipelineSettings = pipelineConfig;
configString = (RubyString) pipelineSettings.callMethod(context, "config_string");
configParts = pipelineSettings.toJava(PipelineConfig.class).getConfigParts();
configHash = context.runtime.newString(
Hex.encodeHexString(
MessageDigest.getInstance("SHA1").digest(configString.getBytes())
)
);
configHash = context.runtime.newString(Hex.encodeHexString(digestString(configString)));
settings = pipelineSettings.callMethod(context, "settings");
final IRubyObject id = getSetting(context, "pipeline.id");
if (id.isNil()) {
Expand Down Expand Up @@ -191,6 +187,19 @@ public final AbstractPipelineExt initialize(final ThreadContext context,
return this;
}

private static byte[] digestString(final RubyString content) {
final MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA1");
} catch (NoSuchAlgorithmException ex) {
throw new AssertionError(ex);
}

final ByteList bytes = content.getByteList();
digest.update(bytes.getUnsafeBytes(), bytes.getBegin(), bytes.getRealSize());
return digest.digest();
}

/**
* queue opening needs to happen out of the the initialize method because the
* AbstractPipeline is used for pipeline config validation and the queue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public JavaBasePipelineExt(final Ruby runtime, final RubyClass metaClass) {
}

@JRubyMethod(required = 4)
public JavaBasePipelineExt initialize(final ThreadContext context, final IRubyObject[] args)
throws IncompleteSourceWithMetadataException, NoSuchAlgorithmException {
public JavaBasePipelineExt initialize(final ThreadContext context, final IRubyObject[] args) {
initialize(context, args[0], args[1], args[2]);
lirExecution = new CompiledPipeline(
lir,
Expand Down

0 comments on commit 9b9b7b1

Please sign in to comment.