From f0381e9fd710235c3ea95446cc955f82ea0286db Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sat, 14 Dec 2024 17:22:08 +0800 Subject: [PATCH] Refactor DatabaseMetaDataChangedListener --- .../type/DatabaseMetaDataChangedListener.java | 37 ++++++++----------- .../type/GlobalMetaDataChangedListener.java | 6 +-- ...andaloneMetaDataManagerPersistService.java | 4 +- 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/DatabaseMetaDataChangedListener.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/DatabaseMetaDataChangedListener.java index f17735beb28c7..2e33d83316161 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/DatabaseMetaDataChangedListener.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/DatabaseMetaDataChangedListener.java @@ -17,7 +17,6 @@ package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type; -import com.google.common.base.Preconditions; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; @@ -52,7 +51,7 @@ public final class DatabaseMetaDataChangedListener implements DataChangedEventLi private final EventBusContext eventBusContext; - private final RuleConfigurationEventBuilder builder = new RuleConfigurationEventBuilder(); + private final RuleConfigurationEventBuilder eventBuilder = new RuleConfigurationEventBuilder(); @Override public void onChange(final DataChangedEvent event) { @@ -70,16 +69,16 @@ private Optional createDispatchEvent(final DataChangedEvent event return createSchemaChangedEvent(databaseName.get(), schemaName.get(), event); } schemaName = DatabaseMetaDataNode.getSchemaNameByTableNode(key); - if (schemaName.isPresent() && tableMetaDataChanged(event.getKey())) { + if (schemaName.isPresent() && isTableMetaDataChanged(event.getKey())) { return createTableChangedEvent(databaseName.get(), schemaName.get(), event); } - if (schemaName.isPresent() && viewMetaDataChanged(event.getKey())) { + if (schemaName.isPresent() && isViewMetaDataChanged(event.getKey())) { return createViewChangedEvent(databaseName.get(), schemaName.get(), event); } if (DataSourceMetaDataNode.isDataSourcesNode(key)) { - return createDataSourceEvent(databaseName.get(), event); + return createDataSourceChangedEvent(databaseName.get(), event); } - return builder.build(databaseName.get(), event); + return eventBuilder.build(databaseName.get(), event); } private Optional createSchemaChangedEvent(final String databaseName, final String schemaName, final DataChangedEvent event) { @@ -94,43 +93,39 @@ private Optional createSchemaChangedEvent(final String databaseNa } } - private boolean tableMetaDataChanged(final String key) { + private boolean isTableMetaDataChanged(final String key) { return TableMetaDataNode.isTableActiveVersionNode(key) || TableMetaDataNode.isTableNode(key); } private Optional createTableChangedEvent(final String databaseName, final String schemaName, final DataChangedEvent event) { if ((Type.ADDED == event.getType() || Type.UPDATED == event.getType()) && TableMetaDataNode.isTableActiveVersionNode(event.getKey())) { - Optional tableName = TableMetaDataNode.getTableNameByActiveVersionNode(event.getKey()); - Preconditions.checkState(tableName.isPresent(), "Not found table name."); - return Optional.of(new CreateOrAlterTableEvent(databaseName, schemaName, tableName.get(), event.getKey(), event.getValue())); + String tableName = TableMetaDataNode.getTableNameByActiveVersionNode(event.getKey()).orElseThrow(() -> new IllegalStateException("Table name not found.")); + return Optional.of(new CreateOrAlterTableEvent(databaseName, schemaName, tableName, event.getKey(), event.getValue())); } if (Type.DELETED == event.getType() && TableMetaDataNode.isTableNode(event.getKey())) { - Optional tableName = TableMetaDataNode.getTableName(event.getKey()); - Preconditions.checkState(tableName.isPresent(), "Not found table name."); - return Optional.of(new DropTableEvent(databaseName, schemaName, tableName.get())); + String tableName = TableMetaDataNode.getTableName(event.getKey()).orElseThrow(() -> new IllegalStateException("Table name not found.")); + return Optional.of(new DropTableEvent(databaseName, schemaName, tableName)); } return Optional.empty(); } - private boolean viewMetaDataChanged(final String key) { + private boolean isViewMetaDataChanged(final String key) { return ViewMetaDataNode.isViewActiveVersionNode(key) || ViewMetaDataNode.isViewNode(key); } private Optional createViewChangedEvent(final String databaseName, final String schemaName, final DataChangedEvent event) { if ((Type.ADDED == event.getType() || Type.UPDATED == event.getType()) && ViewMetaDataNode.isViewActiveVersionNode(event.getKey())) { - Optional viewName = ViewMetaDataNode.getViewNameByActiveVersionNode(event.getKey()); - Preconditions.checkState(viewName.isPresent(), "Not found view name."); - return Optional.of(new CreateOrAlterViewEvent(databaseName, schemaName, viewName.get(), event.getKey(), event.getValue())); + String viewName = ViewMetaDataNode.getViewNameByActiveVersionNode(event.getKey()).orElseThrow(() -> new IllegalStateException("View name not found.")); + return Optional.of(new CreateOrAlterViewEvent(databaseName, schemaName, viewName, event.getKey(), event.getValue())); } if (Type.DELETED == event.getType() && ViewMetaDataNode.isViewNode(event.getKey())) { - Optional viewName = ViewMetaDataNode.getViewName(event.getKey()); - Preconditions.checkState(viewName.isPresent(), "Not found view name."); - return Optional.of(new DropViewEvent(databaseName, schemaName, viewName.get(), event.getKey(), event.getValue())); + String viewName = ViewMetaDataNode.getViewName(event.getKey()).orElseThrow(() -> new IllegalStateException("View name not found.")); + return Optional.of(new DropViewEvent(databaseName, schemaName, viewName, event.getKey(), event.getValue())); } return Optional.empty(); } - private Optional createDataSourceEvent(final String databaseName, final DataChangedEvent event) { + private Optional createDataSourceChangedEvent(final String databaseName, final DataChangedEvent event) { if (DataSourceMetaDataNode.isDataSourceUnitActiveVersionNode(event.getKey()) || DataSourceMetaDataNode.isDataSourceUnitNode(event.getKey())) { return createStorageUnitChangedEvent(databaseName, event); } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListener.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListener.java index cea2df2bbe04b..cbdc96d6a0db7 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListener.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListener.java @@ -31,12 +31,12 @@ public final class GlobalMetaDataChangedListener implements DataChangedEventList private final EventBusContext eventBusContext; - private final DispatchEventBuilder builder; + private final DispatchEventBuilder eventBuilder; @Override public void onChange(final DataChangedEvent event) { - if (builder.getSubscribedTypes().contains(event.getType())) { - builder.build(event).ifPresent(eventBusContext::post); + if (eventBuilder.getSubscribedTypes().contains(event.getType())) { + eventBuilder.build(event).ifPresent(eventBusContext::post); } } } diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistService.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistService.java index 264648a1035cf..5e5482666b850 100644 --- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistService.java +++ b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistService.java @@ -64,7 +64,7 @@ */ public final class StandaloneMetaDataManagerPersistService implements MetaDataManagerPersistService { - private final RuleConfigurationEventBuilder ruleConfigurationEventBuilder = new RuleConfigurationEventBuilder(); + private final RuleConfigurationEventBuilder ruleConfigEventBuilder = new RuleConfigurationEventBuilder(); private final MetaDataPersistService metaDataPersistService; @@ -302,7 +302,7 @@ public void alterRuleConfiguration(final String databaseName, final RuleConfigur } private Optional buildAlterRuleItemEvent(final String databaseName, final MetaDataVersion metaDataVersion, final Type type) { - return ruleConfigurationEventBuilder.build(databaseName, new DataChangedEvent(metaDataVersion.getActiveVersionNodePath(), metaDataVersion.getNextActiveVersion(), type)); + return ruleConfigEventBuilder.build(databaseName, new DataChangedEvent(metaDataVersion.getActiveVersionNodePath(), metaDataVersion.getNextActiveVersion(), type)); } @Override