Skip to content

Commit

Permalink
Refactor RuleConfigurationEventBuilder‘s impl (apache#26641)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Jun 27, 2023
1 parent c6cf11f commit 7fd45e3
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,18 @@ public final class BroadcastRuleConfigurationEventBuilder implements RuleConfigu

@Override
public Optional<GovernanceEvent> build(final String databaseName, final DataChangedEvent event) {
if (!broadcastRuleNodePath.getRoot().isValidatedPath(event.getKey()) || Strings.isNullOrEmpty(event.getValue())) {
return Optional.empty();
}
if (broadcastRuleNodePath.getUniqueItem(BroadcastNodePath.TABLES).isActiveVersionPath(event.getKey()) && !Strings.isNullOrEmpty(event.getValue())) {
return createBroadcastConfigEvent(databaseName, event);
}
return Optional.empty();
return Strings.isNullOrEmpty(event.getValue()) || !broadcastRuleNodePath.getUniqueItem(BroadcastNodePath.TABLES).isActiveVersionPath(event.getKey())
? Optional.empty()
: Optional.of(createBroadcastConfigEvent(databaseName, event));
}

private Optional<GovernanceEvent> createBroadcastConfigEvent(final String databaseName, final DataChangedEvent event) {
private GovernanceEvent createBroadcastConfigEvent(final String databaseName, final DataChangedEvent event) {
if (Type.ADDED == event.getType()) {
return Optional.of(new AddBroadcastTableEvent(databaseName, event.getKey(), event.getValue()));
return new AddBroadcastTableEvent(databaseName, event.getKey(), event.getValue());
}
if (Type.UPDATED == event.getType()) {
return Optional.of(new AlterBroadcastTableEvent(databaseName, event.getKey(), event.getValue()));
return new AlterBroadcastTableEvent(databaseName, event.getKey(), event.getValue());
}
return Optional.of(new DeleteBroadcastTableEvent(databaseName));
return new DeleteBroadcastTableEvent(databaseName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,27 @@ public Optional<GovernanceEvent> build(final String databaseName, final DataChan
return Optional.empty();
}
Optional<String> tableName = encryptRuleNodePath.getNamedItem(CompatibleEncryptNodePath.TABLES).getNameByActiveVersion(event.getKey());
if (tableName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
return createEncryptConfigEvent(databaseName, tableName.get(), event);
if (tableName.isPresent()) {
return Optional.of(createEncryptConfigEvent(databaseName, tableName.get(), event));
}
Optional<String> encryptorName = encryptRuleNodePath.getNamedItem(CompatibleEncryptNodePath.ENCRYPTORS).getNameByActiveVersion(event.getKey());
if (encryptorName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
return createEncryptorEvent(databaseName, encryptorName.get(), event);
}
return Optional.empty();
return encryptorName.map(optional -> createEncryptorEvent(databaseName, optional, event));
}

private Optional<GovernanceEvent> createEncryptConfigEvent(final String databaseName, final String groupName, final DataChangedEvent event) {
private GovernanceEvent createEncryptConfigEvent(final String databaseName, final String groupName, final DataChangedEvent event) {
if (Type.ADDED == event.getType()) {
return Optional.of(new AddCompatibleEncryptTableEvent(databaseName, event.getKey(), event.getValue()));
return new AddCompatibleEncryptTableEvent(databaseName, event.getKey(), event.getValue());
}
if (Type.UPDATED == event.getType()) {
return Optional.of(new AlterCompatibleEncryptTableEvent(databaseName, groupName, event.getKey(), event.getValue()));
return new AlterCompatibleEncryptTableEvent(databaseName, groupName, event.getKey(), event.getValue());
}
return Optional.of(new DeleteCompatibleEncryptTableEvent(databaseName, groupName));
return new DeleteCompatibleEncryptTableEvent(databaseName, groupName);
}

private Optional<GovernanceEvent> createEncryptorEvent(final String databaseName, final String encryptorName, final DataChangedEvent event) {
private GovernanceEvent createEncryptorEvent(final String databaseName, final String encryptorName, final DataChangedEvent event) {
if (Type.ADDED == event.getType() || Type.UPDATED == event.getType()) {
return Optional.of(new AlterCompatibleEncryptorEvent(databaseName, encryptorName, event.getKey(), event.getValue()));
return new AlterCompatibleEncryptorEvent(databaseName, encryptorName, event.getKey(), event.getValue());
}
return Optional.of(new DeleteCompatibleEncryptorEvent(databaseName, encryptorName));
return new DeleteCompatibleEncryptorEvent(databaseName, encryptorName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,27 @@ public Optional<GovernanceEvent> build(final String databaseName, final DataChan
return Optional.empty();
}
Optional<String> tableName = encryptRuleNodePath.getNamedItem(EncryptNodePath.TABLES).getNameByActiveVersion(event.getKey());
if (tableName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
return createEncryptConfigEvent(databaseName, tableName.get(), event);
if (tableName.isPresent()) {
return Optional.of(createEncryptConfigEvent(databaseName, tableName.get(), event));
}
Optional<String> encryptorName = encryptRuleNodePath.getNamedItem(EncryptNodePath.ENCRYPTORS).getNameByActiveVersion(event.getKey());
if (encryptorName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
return createEncryptorEvent(databaseName, encryptorName.get(), event);
}
return Optional.empty();
return encryptorName.flatMap(optional -> Optional.of(createEncryptorEvent(databaseName, optional, event)));
}

private Optional<GovernanceEvent> createEncryptConfigEvent(final String databaseName, final String groupName, final DataChangedEvent event) {
private GovernanceEvent createEncryptConfigEvent(final String databaseName, final String groupName, final DataChangedEvent event) {
if (Type.ADDED == event.getType()) {
return Optional.of(new AddEncryptTableEvent(databaseName, event.getKey(), event.getValue()));
return new AddEncryptTableEvent(databaseName, event.getKey(), event.getValue());
}
if (Type.UPDATED == event.getType()) {
return Optional.of(new AlterEncryptTableEvent(databaseName, groupName, event.getKey(), event.getValue()));
return new AlterEncryptTableEvent(databaseName, groupName, event.getKey(), event.getValue());
}
return Optional.of(new DeleteEncryptTableEvent(databaseName, groupName));
return new DeleteEncryptTableEvent(databaseName, groupName);
}

private Optional<GovernanceEvent> createEncryptorEvent(final String databaseName, final String encryptorName, final DataChangedEvent event) {
private GovernanceEvent createEncryptorEvent(final String databaseName, final String encryptorName, final DataChangedEvent event) {
if (Type.ADDED == event.getType() || Type.UPDATED == event.getType()) {
return Optional.of(new AlterEncryptorEvent(databaseName, encryptorName, event.getKey(), event.getValue()));
return new AlterEncryptorEvent(databaseName, encryptorName, event.getKey(), event.getValue());
}
return Optional.of(new DeleteEncryptorEvent(databaseName, encryptorName));
return new DeleteEncryptorEvent(databaseName, encryptorName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,27 @@ public Optional<GovernanceEvent> build(final String databaseName, final DataChan
return Optional.empty();
}
Optional<String> tableName = maskRuleNodePath.getNamedItem(MaskNodePath.TABLES).getNameByActiveVersion(event.getKey());
if (tableName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
return createMaskConfigEvent(databaseName, tableName.get(), event);
if (tableName.isPresent()) {
return Optional.of(createMaskConfigEvent(databaseName, tableName.get(), event));
}
Optional<String> algorithmName = maskRuleNodePath.getNamedItem(MaskNodePath.ALGORITHMS).getNameByActiveVersion(event.getKey());
if (algorithmName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
return createMaskAlgorithmEvent(databaseName, algorithmName.get(), event);
}
return Optional.empty();
return algorithmName.flatMap(optional -> Optional.of(createMaskAlgorithmEvent(databaseName, optional, event)));
}

private Optional<GovernanceEvent> createMaskConfigEvent(final String databaseName, final String tableName, final DataChangedEvent event) {
private GovernanceEvent createMaskConfigEvent(final String databaseName, final String tableName, final DataChangedEvent event) {
if (Type.ADDED == event.getType()) {
return Optional.of(new AddMaskTableEvent(databaseName, event.getKey(), event.getValue()));
return new AddMaskTableEvent(databaseName, event.getKey(), event.getValue());
}
if (Type.UPDATED == event.getType()) {
return Optional.of(new AlterMaskTableEvent(databaseName, tableName, event.getKey(), event.getValue()));
return new AlterMaskTableEvent(databaseName, tableName, event.getKey(), event.getValue());
}
return Optional.of(new DeleteMaskTableEvent(databaseName, tableName));
return new DeleteMaskTableEvent(databaseName, tableName);
}

private Optional<GovernanceEvent> createMaskAlgorithmEvent(final String databaseName, final String algorithmName, final DataChangedEvent event) {
private GovernanceEvent createMaskAlgorithmEvent(final String databaseName, final String algorithmName, final DataChangedEvent event) {
if (Type.ADDED == event.getType() || Type.UPDATED == event.getType()) {
return Optional.of(new AlterMaskAlgorithmEvent(databaseName, algorithmName, event.getKey(), event.getValue()));
return new AlterMaskAlgorithmEvent(databaseName, algorithmName, event.getKey(), event.getValue());
}
return Optional.of(new DeleteMaskAlgorithmEvent(databaseName, algorithmName));
return new DeleteMaskAlgorithmEvent(databaseName, algorithmName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,27 @@ public Optional<GovernanceEvent> build(final String databaseName, final DataChan
return Optional.empty();
}
Optional<String> groupName = readwriteSplittingRuleNodePath.getNamedItem(ReadwriteSplittingNodePath.DATA_SOURCES).getNameByActiveVersion(event.getKey());
if (groupName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
return createReadwriteSplittingConfigEvent(databaseName, groupName.get(), event);
if (groupName.isPresent()) {
return Optional.of(createReadwriteSplittingConfigEvent(databaseName, groupName.get(), event));
}
Optional<String> loadBalancerName = readwriteSplittingRuleNodePath.getNamedItem(ReadwriteSplittingNodePath.LOAD_BALANCERS).getNameByActiveVersion(event.getKey());
if (loadBalancerName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
return createLoadBalanceEvent(databaseName, loadBalancerName.get(), event);
}
return Optional.empty();
return loadBalancerName.flatMap(optional -> Optional.of(createLoadBalanceEvent(databaseName, optional, event)));
}

private Optional<GovernanceEvent> createReadwriteSplittingConfigEvent(final String databaseName, final String groupName, final DataChangedEvent event) {
private GovernanceEvent createReadwriteSplittingConfigEvent(final String databaseName, final String groupName, final DataChangedEvent event) {
if (Type.ADDED == event.getType()) {
return Optional.of(new AddReadwriteSplittingDataSourceEvent(databaseName, groupName, event.getKey(), event.getValue()));
return new AddReadwriteSplittingDataSourceEvent(databaseName, groupName, event.getKey(), event.getValue());
}
if (Type.UPDATED == event.getType()) {
return Optional.of(new AlterReadwriteSplittingDataSourceEvent(databaseName, groupName, event.getKey(), event.getValue()));
return new AlterReadwriteSplittingDataSourceEvent(databaseName, groupName, event.getKey(), event.getValue());
}
return Optional.of(new DeleteReadwriteSplittingDataSourceEvent(databaseName, groupName));
return new DeleteReadwriteSplittingDataSourceEvent(databaseName, groupName);
}

private Optional<GovernanceEvent> createLoadBalanceEvent(final String databaseName, final String loadBalancerName, final DataChangedEvent event) {
private GovernanceEvent createLoadBalanceEvent(final String databaseName, final String loadBalancerName, final DataChangedEvent event) {
if (Type.ADDED == event.getType() || Type.UPDATED == event.getType()) {
return Optional.of(new AlterLoadBalanceEvent(databaseName, loadBalancerName, event.getKey(), event.getValue()));
return new AlterLoadBalanceEvent(databaseName, loadBalancerName, event.getKey(), event.getValue());
}
return Optional.of(new DeleteLoadBalanceEvent(databaseName, loadBalancerName));
return new DeleteLoadBalanceEvent(databaseName, loadBalancerName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,54 +50,54 @@ public Optional<GovernanceEvent> build(final String databaseName, final DataChan
return Optional.empty();
}
Optional<String> dataSourceName = shadowRuleNodePath.getNamedItem(ShadowNodePath.DATA_SOURCES).getNameByActiveVersion(event.getKey());
if (dataSourceName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
return createShadowConfigEvent(databaseName, dataSourceName.get(), event);
if (dataSourceName.isPresent()) {
return Optional.of(createShadowConfigEvent(databaseName, dataSourceName.get(), event));
}
Optional<String> tableName = shadowRuleNodePath.getNamedItem(ShadowNodePath.TABLES).getNameByActiveVersion(event.getKey());
if (tableName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
return createShadowTableConfigEvent(databaseName, tableName.get(), event);
if (tableName.isPresent()) {
return Optional.of(createShadowTableConfigEvent(databaseName, tableName.get(), event));
}
Optional<String> algorithmName = shadowRuleNodePath.getNamedItem(ShadowNodePath.ALGORITHMS).getNameByActiveVersion(event.getKey());
if (algorithmName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
return createShadowAlgorithmEvent(databaseName, algorithmName.get(), event);
if (algorithmName.isPresent()) {
return Optional.of(createShadowAlgorithmEvent(databaseName, algorithmName.get(), event));
}
if (shadowRuleNodePath.getUniqueItem(ShadowNodePath.DEFAULT_ALGORITHM).isActiveVersionPath(event.getKey()) && !Strings.isNullOrEmpty(event.getValue())) {
return createDefaultShadowAlgorithmNameEvent(databaseName, event);
if (shadowRuleNodePath.getUniqueItem(ShadowNodePath.DEFAULT_ALGORITHM).isActiveVersionPath(event.getKey())) {
return Optional.of(createDefaultShadowAlgorithmNameEvent(databaseName, event));
}
return Optional.empty();
}

private Optional<GovernanceEvent> createShadowConfigEvent(final String databaseName, final String dataSourceName, final DataChangedEvent event) {
private GovernanceEvent createShadowConfigEvent(final String databaseName, final String dataSourceName, final DataChangedEvent event) {
if (Type.ADDED == event.getType()) {
return Optional.of(new AddShadowDataSourceEvent(databaseName, dataSourceName, event.getKey(), event.getValue()));
return new AddShadowDataSourceEvent(databaseName, dataSourceName, event.getKey(), event.getValue());
}
if (Type.UPDATED == event.getType()) {
return Optional.of(new AlterShadowDataSourceEvent(databaseName, dataSourceName, event.getKey(), event.getValue()));
return new AlterShadowDataSourceEvent(databaseName, dataSourceName, event.getKey(), event.getValue());
}
return Optional.of(new DeleteShadowDataSourceEvent(databaseName, dataSourceName));
return new DeleteShadowDataSourceEvent(databaseName, dataSourceName);
}

private Optional<GovernanceEvent> createShadowTableConfigEvent(final String databaseName, final String tableName, final DataChangedEvent event) {
private GovernanceEvent createShadowTableConfigEvent(final String databaseName, final String tableName, final DataChangedEvent event) {
if (Type.ADDED == event.getType()) {
return Optional.of(new AddShadowTableEvent(databaseName, tableName, event.getKey(), event.getValue()));
return new AddShadowTableEvent(databaseName, tableName, event.getKey(), event.getValue());
}
if (Type.UPDATED == event.getType()) {
return Optional.of(new AlterShadowTableEvent(databaseName, tableName, event.getKey(), event.getValue()));
return new AlterShadowTableEvent(databaseName, tableName, event.getKey(), event.getValue());
}
return Optional.of(new DeleteShadowTableEvent(databaseName, tableName));
return new DeleteShadowTableEvent(databaseName, tableName);
}

private Optional<GovernanceEvent> createShadowAlgorithmEvent(final String databaseName, final String algorithmName, final DataChangedEvent event) {
private GovernanceEvent createShadowAlgorithmEvent(final String databaseName, final String algorithmName, final DataChangedEvent event) {
if (Type.ADDED == event.getType() || Type.UPDATED == event.getType()) {
return Optional.of(new AlterShadowAlgorithmEvent(databaseName, algorithmName, event.getKey(), event.getValue()));
return new AlterShadowAlgorithmEvent(databaseName, algorithmName, event.getKey(), event.getValue());
}
return Optional.of(new DeleteShadowAlgorithmEvent(databaseName, algorithmName));
return new DeleteShadowAlgorithmEvent(databaseName, algorithmName);
}

private Optional<GovernanceEvent> createDefaultShadowAlgorithmNameEvent(final String databaseName, final DataChangedEvent event) {
private GovernanceEvent createDefaultShadowAlgorithmNameEvent(final String databaseName, final DataChangedEvent event) {
if (Type.ADDED == event.getType() || Type.UPDATED == event.getType()) {
return Optional.of(new AlterDefaultShadowAlgorithmNameEvent(databaseName, event.getKey(), event.getValue()));
return new AlterDefaultShadowAlgorithmNameEvent(databaseName, event.getKey(), event.getValue());
}
return Optional.of(new DeleteDefaultShadowAlgorithmNameEvent(databaseName));
return new DeleteDefaultShadowAlgorithmNameEvent(databaseName);
}
}
Loading

0 comments on commit 7fd45e3

Please sign in to comment.