Skip to content

Commit

Permalink
Revert "Remove RuleItemChangedSubscriber and RuleItemEvent (#34099)"
Browse files Browse the repository at this point in the history
This reverts commit 0b8df98.
  • Loading branch information
menghaoranss committed Dec 19, 2024
1 parent 0b8df98 commit 49effb3
Show file tree
Hide file tree
Showing 19 changed files with 709 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.mode.event.dispatch.rule;

import org.apache.shardingsphere.mode.event.dispatch.DispatchEvent;

/**
* Rule item changed event.
*/
public interface RuleItemChangedEvent extends DispatchEvent {

/**
* Get rule item type.
*
* @return rule item type
*/
String getType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.mode.event.dispatch.rule.alter;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* Alter named rule item event.
*/
@RequiredArgsConstructor
@Getter
public final class AlterNamedRuleItemEvent implements AlterRuleItemEvent {

private final String databaseName;

private final String itemName;

private final String activeVersionKey;

private final String activeVersion;

private final String type;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.mode.event.dispatch.rule.alter;

import org.apache.shardingsphere.mode.event.dispatch.rule.RuleItemChangedEvent;

/**
* Alter rule item event.
*/
public interface AlterRuleItemEvent extends RuleItemChangedEvent {

/**
* Get database name.
*
* @return database name
*/
String getDatabaseName();

/**
* Get active version key.
*
* @return active version key
*/
String getActiveVersionKey();

/**
* Get active version.
*
* @return active version
*/
String getActiveVersion();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.mode.event.dispatch.rule.alter;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* Alter unique rule item event.
*/
@RequiredArgsConstructor
@Getter
public final class AlterUniqueRuleItemEvent implements AlterRuleItemEvent {

private final String databaseName;

private final String activeVersionKey;

private final String activeVersion;

private final String type;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.mode.event.dispatch.rule.drop;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* Drop named rule item event.
*/
@RequiredArgsConstructor
@Getter
public final class DropNamedRuleItemEvent implements DropRuleItemEvent {

private final String databaseName;

private final String itemName;

private final String type;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.mode.event.dispatch.rule.drop;

import org.apache.shardingsphere.mode.event.dispatch.rule.RuleItemChangedEvent;

/**
* Drop rule item event.
*/
public interface DropRuleItemEvent extends RuleItemChangedEvent {

/**
* Get database name.
*
* @return database name
*/
String getDatabaseName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.mode.event.dispatch.rule.drop;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* Drop unique rule item event.
*/
@RequiredArgsConstructor
@Getter
public final class DropUniqueRuleItemEvent implements DropRuleItemEvent {

private final String databaseName;

private final String type;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.mode.event.dispatch.builder;

import com.google.common.base.Strings;
import org.apache.shardingsphere.mode.event.dispatch.DispatchEvent;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.mode.event.DataChangedEvent;
import org.apache.shardingsphere.mode.event.DataChangedEvent.Type;
import org.apache.shardingsphere.mode.event.dispatch.rule.RuleItemChangedEvent;
import org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterNamedRuleItemEvent;
import org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterUniqueRuleItemEvent;
import org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropNamedRuleItemEvent;
import org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropUniqueRuleItemEvent;
import org.apache.shardingsphere.mode.path.rule.RuleNodePath;
import org.apache.shardingsphere.mode.path.rule.item.NamedRuleItemNodePath;
import org.apache.shardingsphere.mode.path.rule.item.UniqueRuleItemNodePath;
import org.apache.shardingsphere.mode.spi.RuleNodePathProvider;

import java.util.Map.Entry;
import java.util.Optional;

/**
* Rule configuration changed event builder.
*/
public final class RuleConfigurationChangedEventBuilder {

/**
* Build rule changed event.
*
* @param databaseName database name
* @param event data changed event
* @return built event
*/
public Optional<DispatchEvent> build(final String databaseName, final DataChangedEvent event) {
for (RuleNodePathProvider each : ShardingSphereServiceLoader.getServiceInstances(RuleNodePathProvider.class)) {
Optional<DispatchEvent> result = build(each.getRuleNodePath(), databaseName, event);
if (result.isPresent()) {
return result;
}
}
return Optional.empty();
}

private Optional<DispatchEvent> build(final RuleNodePath ruleNodePath, final String databaseName, final DataChangedEvent event) {
if (!ruleNodePath.getRoot().isValidatedPath(event.getKey()) || Type.DELETED != event.getType() && Strings.isNullOrEmpty(event.getValue())) {
return Optional.empty();
}
for (Entry<String, NamedRuleItemNodePath> entry : ruleNodePath.getNamedItems().entrySet()) {
Optional<String> itemName;
if (Type.ADDED == event.getType() || Type.UPDATED == event.getType()) {
itemName = entry.getValue().getNameByActiveVersion(event.getKey());
} else {
itemName = entry.getValue().getNameByItemPath(event.getKey());
}
if (itemName.isPresent()) {
return Optional.of(create(databaseName, itemName.get(), event, ruleNodePath.getRoot().getRuleType() + "." + entry.getKey()));
}
}
for (Entry<String, UniqueRuleItemNodePath> entry : ruleNodePath.getUniqueItems().entrySet()) {
if (entry.getValue().isActiveVersionPath(event.getKey())) {
return Optional.of(create(databaseName, event, ruleNodePath.getRoot().getRuleType() + "." + entry.getKey()));
}
}
return Optional.empty();
}

private RuleItemChangedEvent create(final String databaseName, final String itemName, final DataChangedEvent event, final String type) {
return Type.ADDED == event.getType() || Type.UPDATED == event.getType()
? new AlterNamedRuleItemEvent(databaseName, itemName, event.getKey(), event.getValue(), type)
: new DropNamedRuleItemEvent(databaseName, itemName, type);
}

private RuleItemChangedEvent create(final String databaseName, final DataChangedEvent event, final String type) {
return Type.ADDED == event.getType() || Type.UPDATED == event.getType()
? new AlterUniqueRuleItemEvent(databaseName, event.getKey(), event.getValue(), type)
: new DropUniqueRuleItemEvent(databaseName, type);
}
}
Loading

0 comments on commit 49effb3

Please sign in to comment.