Skip to content

Commit

Permalink
refactor(engine): minor improvement
Browse files Browse the repository at this point in the history
* make it clear that there is at most one decision requirements event that should be written

(cherry picked from commit 011da00)
  • Loading branch information
saig0 authored and github-actions[bot] committed May 30, 2022
1 parent 9c5e430 commit f12e1d0
Showing 1 changed file with 44 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package io.camunda.zeebe.engine.processing.deployment.transform;

import static io.camunda.zeebe.util.buffer.BufferUtil.wrapString;
import static java.util.function.Predicate.not;

import io.camunda.zeebe.dmn.DecisionEngine;
import io.camunda.zeebe.dmn.DecisionEngineFactory;
Expand Down Expand Up @@ -68,8 +69,8 @@ public Either<Failure, Void> transformResource(
return checkForDuplicateIds(resource, parsedDrg, deployment)
.map(
noDuplicates -> {
appendMetadataToDeploymentEvent(resource, parsedDrg, deployment);
writeRecords(deployment, resource);
final var drgKey = appendMetadataToDeploymentEvent(resource, parsedDrg, deployment);
writeRecords(deployment, resource, drgKey);
return null;
});

Expand Down Expand Up @@ -148,7 +149,7 @@ private String findResourceName(
.orElse("<?>");
}

private void appendMetadataToDeploymentEvent(
private long appendMetadataToDeploymentEvent(
final DeploymentResource resource,
final ParsedDecisionRequirementsGraph parsedDrg,
final DeploymentRecord deploymentEvent) {
Expand Down Expand Up @@ -223,6 +224,8 @@ private void appendMetadataToDeploymentEvent(
.setDecisionKey(newDecisionKey.getAsLong())
.setVersion(INITIAL_VERSION));
});

return drgRecord.getDecisionRequirementsKey();
}

private boolean isDuplicate(
Expand All @@ -234,48 +237,44 @@ private boolean isDuplicate(
&& drg.getChecksum().equals(checksum);
}

private void writeRecords(final DeploymentRecord deployment, final DeploymentResource resource) {
final var metadataRecords =
deployment.decisionRequirementsMetadata().stream()
.filter(metadata -> metadata.getResourceName().equals(resource.getResourceName()))
.collect(Collectors.toList());

for (final DecisionRequirementsMetadataRecord drg : metadataRecords) {
if (!drg.isDuplicate()) {
stateWriter.appendFollowUpEvent(
drg.getDecisionRequirementsKey(),
DecisionRequirementsIntent.CREATED,
new DecisionRequirementsRecord()
.setDecisionRequirementsKey(drg.getDecisionRequirementsKey())
.setDecisionRequirementsId(drg.getDecisionRequirementsId())
.setDecisionRequirementsName(drg.getDecisionRequirementsName())
.setDecisionRequirementsVersion(drg.getDecisionRequirementsVersion())
.setNamespace(drg.getNamespace())
.setResourceName(drg.getResourceName())
.setChecksum(drg.getChecksumBuffer())
.setResource(resource.getResourceBuffer()));
}
}
private void writeRecords(
final DeploymentRecord deployment,
final DeploymentResource resource,
final long decisionRequirementsKey) {

final var decisionRequirementKeys =
metadataRecords.stream()
.map(DecisionRequirementsMetadataRecord::getDecisionRequirementsKey)
.collect(Collectors.toList());

for (final DecisionRecord decision : deployment.decisionsMetadata()) {
if (!decision.isDuplicate()
&& decisionRequirementKeys.contains(decision.getDecisionRequirementsKey())) {
stateWriter.appendFollowUpEvent(
decision.getDecisionKey(),
DecisionIntent.CREATED,
new DecisionRecord()
.setDecisionKey(decision.getDecisionKey())
.setDecisionId(decision.getDecisionId())
.setDecisionName(decision.getDecisionName())
.setVersion(decision.getVersion())
.setDecisionRequirementsId(decision.getDecisionRequirementsId())
.setDecisionRequirementsKey(decision.getDecisionRequirementsKey()));
}
}
deployment.decisionRequirementsMetadata().stream()
.filter(drg -> drg.getDecisionRequirementsKey() == decisionRequirementsKey)
.filter(not(DecisionRequirementsMetadataRecord::isDuplicate))
.findFirst()
.ifPresent(
drg ->
stateWriter.appendFollowUpEvent(
drg.getDecisionRequirementsKey(),
DecisionRequirementsIntent.CREATED,
new DecisionRequirementsRecord()
.setDecisionRequirementsKey(drg.getDecisionRequirementsKey())
.setDecisionRequirementsId(drg.getDecisionRequirementsId())
.setDecisionRequirementsName(drg.getDecisionRequirementsName())
.setDecisionRequirementsVersion(drg.getDecisionRequirementsVersion())
.setNamespace(drg.getNamespace())
.setResourceName(drg.getResourceName())
.setChecksum(drg.getChecksumBuffer())
.setResource(resource.getResourceBuffer())));

deployment.decisionsMetadata().stream()
.filter(decision -> decision.getDecisionRequirementsKey() == decisionRequirementsKey)
.filter(not(DecisionRecord::isDuplicate))
.forEach(
decision ->
stateWriter.appendFollowUpEvent(
decision.getDecisionKey(),
DecisionIntent.CREATED,
new DecisionRecord()
.setDecisionKey(decision.getDecisionKey())
.setDecisionId(decision.getDecisionId())
.setDecisionName(decision.getDecisionName())
.setVersion(decision.getVersion())
.setDecisionRequirementsId(decision.getDecisionRequirementsId())
.setDecisionRequirementsKey(decision.getDecisionRequirementsKey())));
}
}

0 comments on commit f12e1d0

Please sign in to comment.