Skip to content

Commit

Permalink
Add field nflow_workflow_definition.definition_sha1.
Browse files Browse the repository at this point in the history
Field contains SHA1 digest of serialized workflow definition. The field
can be used e.g. quickly detect if definition has changed.
  • Loading branch information
jsyrjala committed Jan 16, 2015
1 parent c2a2356 commit 38392ba
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.nitorcreations.nflow.engine.internal.dao;

import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.sort;
import static org.slf4j.LoggerFactory.getLogger;
import static org.springframework.util.CollectionUtils.isEmpty;

import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
Expand Down Expand Up @@ -57,16 +62,19 @@ public void storeWorkflowDefinition(WorkflowDefinition<? extends WorkflowState>
StoredWorkflowDefinition storedDefinition = convert(definition);
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("type", definition.getType());
params.addValue("definition", serializeDefinition(storedDefinition));
String serializedDefinition = serializeDefinition(storedDefinition);
params.addValue("definition_sha1", sha1(serializedDefinition));
params.addValue("definition", serializedDefinition);
params.addValue("modified_by", executorInfo.getExecutorId());
params.addValue("executor_group", executorInfo.getExecutorGroup());

String sql = "update nflow_workflow_definition "
+ "set definition = :definition, modified_by = :modified_by "
+ "where type = :type and executor_group = :executor_group and definition <> :definition";
+ "set definition = :definition, modified_by = :modified_by, definition_sha1 = :definition_sha1 "
+ "where type = :type and executor_group = :executor_group and definition_sha1 <> :definition_sha1";
int updatedRows = namedJdbc.update(sql, params);
if (updatedRows == 0) {
sql = "insert into nflow_workflow_definition(type, definition, modified_by, executor_group) "
+ "values (:type, :definition, :modified_by, :executor_group)";
sql = "insert into nflow_workflow_definition(type, definition_sha1, definition, modified_by, executor_group) "
+ "values (:type, :definition_sha1, :definition, :modified_by, :executor_group)";
try {
namedJdbc.update(sql, params);
} catch (DataIntegrityViolationException dex) {
Expand All @@ -75,6 +83,16 @@ public void storeWorkflowDefinition(WorkflowDefinition<? extends WorkflowState>
}
}

private String sha1(String serializedDefinition) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.update(serializedDefinition.getBytes(UTF_8));
return format("%040x", new BigInteger(1, digest.digest()));
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("SHA1 not supported");
}
}

public List<StoredWorkflowDefinition> queryStoredWorkflowDefinitions(Collection<String> types) {
String sql = "select definition from nflow_workflow_definition where " + executorInfo.getExecutorGroupCondition();
MapSqlParameterSource params = new MapSqlParameterSource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ create table if not exists nflow_executor (

create table if not exists nflow_workflow_definition (
type varchar(64) not null,
definition_sha1 varchar(40) not null default 'n/a',
definition text not null,
created timestamp not null default current_timestamp,
modified timestamp not null default current_timestamp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ create table if not exists nflow_executor (

create table if not exists nflow_workflow_definition (
type varchar(64) not null,
definition_sha1 varchar(40) not null default 'n/a',
definition text not null,
created timestamp(3) not null default current_timestamp(3),
modified timestamp(3) not null default current_timestamp(3) on update current_timestamp(3),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ create table if not exists nflow_executor (

create table if not exists nflow_workflow_definition (
type varchar(64) not null,
definition_sha1 varchar(40) not null default 'n/a',
definition text not null,
modified timestamp not null default current_timestamp on update current_timestamp,
modified_by int not null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ create table if not exists nflow_executor (

create table if not exists nflow_workflow_definition (
type varchar(64) not null,
definition_sha1 varchar(40) not null default 'n/a',
definition text not null,
created timestamptz not null default current_timestamp,
modified timestamptz not null default current_timestamp,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

alter table nflow_workflow_definition add definition_sha1 varchar(40) not null default 'n/a';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

alter table nflow_workflow_definition add definition_sha1 varchar(40) not null default 'n/a';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

alter table nflow_workflow_definition add definition_sha1 varchar(40) not null default 'n/a';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

alter table nflow_workflow_definition add definition_sha1 varchar(40) not null default 'n/a';

0 comments on commit 38392ba

Please sign in to comment.