Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename ShadowCondition #33534

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@ In extreme circumstances, Spotless formatted code cannot pass Checkstyle checkin
The underlying cause is a conflict between the checking mechanism and formatting configurations set by both. For example, Spotless formats a newline with a 16-space indent, while Checkstyle checks for a 12-space newline.

```java
private static Collection<PreciseHintShadowValue<Comparable<?>>> createNoteShadowValues(final ShadowDetermineCondition shadowDetermineCondition) {
private static Collection<PreciseHintShadowValue<Comparable<?>>> createNoteShadowValues(final ShadowDetermineCondition shadowCondition) {
// format that can pass Checkstyle
return shadowDetermineCondition.getSqlComments().stream().<PreciseHintShadowValue<Comparable<?>>>map(
return shadowCondition.getSqlComments().stream().<PreciseHintShadowValue<Comparable<?>>>map(
each -> new PreciseHintShadowValue<>(tableName, shadowOperationType, each)).collect(Collectors.toList());
// After being formatted by Spotless
return shadowDetermineCondition.getSqlComments().stream().<PreciseHintShadowValue<Comparable<?>>>map(
return shadowCondition.getSqlComments().stream().<PreciseHintShadowValue<Comparable<?>>>map(
each -> new PreciseHintShadowValue<>(tableName, shadowOperationType, each)).collect(Collectors.toList());
}
```
Expand Down
6 changes: 3 additions & 3 deletions docs/blog/content/material/spotless.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ user@machine repo % mvn spotless:check
根本原因在于两者设定的检查配置和格式化配置冲突。举个例子,Spotless 格式化后换行缩进了 16 个空格,而 Checkstyle 的换行检查是 12 个空格。

```java
private static Collection<PreciseHintShadowValue<Comparable<?>>> createNoteShadowValues(final ShadowDetermineCondition shadowDetermineCondition) {
private static Collection<PreciseHintShadowValue<Comparable<?>>> createNoteShadowValues(final ShadowDetermineCondition shadowCondition) {
// Checkstyle 可以通过的格式
return shadowDetermineCondition.getSqlComments().stream().<PreciseHintShadowValue<Comparable<?>>>map(
return shadowCondition.getSqlComments().stream().<PreciseHintShadowValue<Comparable<?>>>map(
each -> new PreciseHintShadowValue<>(tableName, shadowOperationType, each)).collect(Collectors.toList());
// Spotless 格式化后
return shadowDetermineCondition.getSqlComments().stream().<PreciseHintShadowValue<Comparable<?>>>map(
return shadowCondition.getSqlComments().stream().<PreciseHintShadowValue<Comparable<?>>>map(
each -> new PreciseHintShadowValue<>(tableName, shadowOperationType, each)).collect(Collectors.toList());
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;

/**
* Shadow determine condition.
* Shadow condition.
*/
@RequiredArgsConstructor
@Getter
public final class ShadowDetermineCondition {
public final class ShadowCondition {

private final String tableName;

private final ShadowOperationType shadowOperationType;
private final ShadowOperationType operationType;

private final ShadowColumnCondition shadowColumnCondition;
private final ShadowColumnCondition columnCondition;

public ShadowDetermineCondition(final String tableName, final ShadowOperationType shadowOperationType) {
this(tableName, shadowOperationType, null);
public ShadowCondition(final String tableName, final ShadowOperationType operationType) {
this(tableName, operationType, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.shadow.condition.ShadowCondition;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;
import org.apache.shardingsphere.shadow.spi.column.ColumnShadowAlgorithm;
import org.apache.shardingsphere.shadow.spi.column.PreciseColumnShadowValue;
import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
import org.apache.shardingsphere.shadow.condition.ShadowDetermineCondition;

import java.util.Collection;
import java.util.LinkedList;
Expand All @@ -41,10 +41,10 @@ public final class ColumnShadowAlgorithmDeterminer {
* @param shadowCondition shadow determine condition
* @return is shadow or not
*/
public static boolean isShadow(final ColumnShadowAlgorithm<Comparable<?>> shadowAlgorithm, final ShadowDetermineCondition shadowCondition) {
ShadowColumnCondition shadowColumnCondition = shadowCondition.getShadowColumnCondition();
public static boolean isShadow(final ColumnShadowAlgorithm<Comparable<?>> shadowAlgorithm, final ShadowCondition shadowCondition) {
ShadowColumnCondition shadowColumnCondition = shadowCondition.getColumnCondition();
String tableName = shadowCondition.getTableName();
ShadowOperationType operationType = shadowCondition.getShadowOperationType();
ShadowOperationType operationType = shadowCondition.getOperationType();
for (PreciseColumnShadowValue<Comparable<?>> each : createColumnShadowValues(shadowColumnCondition.getColumn(), shadowColumnCondition.getValues(), tableName, operationType)) {
if (!tableName.equals(shadowColumnCondition.getOwner()) || !shadowAlgorithm.isShadow(each)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.shadow.condition.ShadowDetermineCondition;
import org.apache.shardingsphere.shadow.condition.ShadowCondition;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;
import org.apache.shardingsphere.shadow.spi.hint.HintShadowAlgorithm;
Expand All @@ -40,14 +40,14 @@ public final class HintShadowAlgorithmDeterminer {
* @param useShadow use shadow or not
* @return is shadow or not
*/
public static boolean isShadow(final HintShadowAlgorithm<Comparable<?>> shadowAlgorithm, final ShadowDetermineCondition shadowCondition, final ShadowRule shadowRule, final boolean useShadow) {
public static boolean isShadow(final HintShadowAlgorithm<Comparable<?>> shadowAlgorithm, final ShadowCondition shadowCondition, final ShadowRule shadowRule, final boolean useShadow) {
PreciseHintShadowValue<Comparable<?>> shadowValue = createHintShadowValues(shadowCondition, useShadow);
return shadowAlgorithm.isShadow(shadowRule.getAllShadowTableNames(), shadowValue);
}

private static PreciseHintShadowValue<Comparable<?>> createHintShadowValues(final ShadowDetermineCondition shadowDetermineCondition, final boolean useShadow) {
ShadowOperationType shadowOperationType = shadowDetermineCondition.getShadowOperationType();
String tableName = shadowDetermineCondition.getTableName();
private static PreciseHintShadowValue<Comparable<?>> createHintShadowValues(final ShadowCondition shadowCondition, final boolean useShadow) {
ShadowOperationType shadowOperationType = shadowCondition.getOperationType();
String tableName = shadowCondition.getTableName();
return new PreciseHintShadowValue<>(tableName, shadowOperationType, useShadow);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.shardingsphere.infra.binder.context.type.TableAvailable;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
import org.apache.shardingsphere.shadow.condition.ShadowDetermineCondition;
import org.apache.shardingsphere.shadow.condition.ShadowCondition;
import org.apache.shardingsphere.shadow.route.determiner.ColumnShadowAlgorithmDeterminer;
import org.apache.shardingsphere.shadow.route.determiner.HintShadowAlgorithmDeterminer;
import org.apache.shardingsphere.shadow.route.finder.ShadowDataSourceMappingsFinder;
Expand Down Expand Up @@ -83,24 +83,24 @@ public Map<String, String> find(final ShadowRule rule) {
private boolean isMatchDefaultAlgorithm(final ShadowRule rule) {
Optional<ShadowAlgorithm> defaultAlgorithm = rule.getDefaultShadowAlgorithm();
if (defaultAlgorithm.isPresent() && defaultAlgorithm.get() instanceof HintShadowAlgorithm<?>) {
ShadowDetermineCondition determineCondition = new ShadowDetermineCondition("", ShadowOperationType.HINT_MATCH);
return HintShadowAlgorithmDeterminer.isShadow((HintShadowAlgorithm<Comparable<?>>) defaultAlgorithm.get(), determineCondition, rule, isShadow);
ShadowCondition shadowCondition = new ShadowCondition("", ShadowOperationType.HINT_MATCH);
return HintShadowAlgorithmDeterminer.isShadow((HintShadowAlgorithm<Comparable<?>>) defaultAlgorithm.get(), shadowCondition, rule, isShadow);
}
return false;
}

private Map<String, String> findBySQLHints(final ShadowRule rule, final Collection<String> relatedShadowTables) {
Map<String, String> result = new LinkedHashMap<>();
for (String each : relatedShadowTables) {
if (isContainsShadowInSQLHints(rule, each, new ShadowDetermineCondition(each, operationType))) {
if (isContainsShadowInSQLHints(rule, each, new ShadowCondition(each, operationType))) {
result.putAll(rule.getShadowDataSourceMappings(each));
return result;
}
}
return result;
}

private boolean isContainsShadowInSQLHints(final ShadowRule rule, final String tableName, final ShadowDetermineCondition shadowCondition) {
private boolean isContainsShadowInSQLHints(final ShadowRule rule, final String tableName, final ShadowCondition shadowCondition) {
for (HintShadowAlgorithm<Comparable<?>> each : rule.getHintShadowAlgorithms(tableName)) {
if (HintShadowAlgorithmDeterminer.isShadow(each, shadowCondition, rule, isShadow)) {
return true;
Expand Down Expand Up @@ -143,7 +143,7 @@ private boolean isMatchAnyColumnShadowAlgorithms(final ShadowRule rule, final St

private boolean isMatchColumnShadowAlgorithm(final String shadowTable, final Collection<ColumnShadowAlgorithm<Comparable<?>>> algorithms, final ShadowColumnCondition condition) {
for (ColumnShadowAlgorithm<Comparable<?>> each : algorithms) {
if (ColumnShadowAlgorithmDeterminer.isShadow(each, new ShadowDetermineCondition(shadowTable, operationType, condition))) {
if (ColumnShadowAlgorithmDeterminer.isShadow(each, new ShadowCondition(shadowTable, operationType, condition))) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.shadow.condition.ShadowDetermineCondition;
import org.apache.shardingsphere.shadow.condition.ShadowCondition;
import org.apache.shardingsphere.shadow.route.determiner.HintShadowAlgorithmDeterminer;
import org.apache.shardingsphere.shadow.route.finder.ShadowDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
Expand All @@ -41,13 +41,13 @@ public Map<String, String> find(final ShadowRule rule) {
if (!hintValueContext.isShadow()) {
return Collections.emptyMap();
}
if (isMatchAnyHintShadowAlgorithms(rule, new ShadowDetermineCondition("", ShadowOperationType.HINT_MATCH))) {
if (isMatchAnyHintShadowAlgorithms(rule, new ShadowCondition("", ShadowOperationType.HINT_MATCH))) {
return rule.getAllShadowDataSourceMappings();
}
return Collections.emptyMap();
}

private boolean isMatchAnyHintShadowAlgorithms(final ShadowRule rule, final ShadowDetermineCondition shadowCondition) {
private boolean isMatchAnyHintShadowAlgorithms(final ShadowRule rule, final ShadowCondition shadowCondition) {
return rule.getAllHintShadowAlgorithms().stream().anyMatch(each -> HintShadowAlgorithmDeterminer.isShadow(each, shadowCondition, rule, hintValueContext.isShadow()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
import org.apache.shardingsphere.shadow.condition.ShadowDetermineCondition;
import org.apache.shardingsphere.shadow.condition.ShadowCondition;
import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;
import org.apache.shardingsphere.shadow.spi.column.ColumnShadowAlgorithm;
Expand All @@ -38,11 +38,10 @@ class ColumnShadowAlgorithmDeterminerTest {
@Test
void assertIsShadow() {
Properties props = PropertiesBuilder.build(new Property("column", "user_id"), new Property("operation", "insert"), new Property("regex", "[1]"));
assertTrue(ColumnShadowAlgorithmDeterminer.isShadow(
(ColumnShadowAlgorithm) TypedSPILoader.getService(ShadowAlgorithm.class, "REGEX_MATCH", props), createShadowDetermineCondition()));
assertTrue(ColumnShadowAlgorithmDeterminer.isShadow((ColumnShadowAlgorithm) TypedSPILoader.getService(ShadowAlgorithm.class, "REGEX_MATCH", props), createShadowCondition()));
}

private ShadowDetermineCondition createShadowDetermineCondition() {
return new ShadowDetermineCondition("t_order", ShadowOperationType.INSERT, new ShadowColumnCondition("t_order", "user_id", Collections.singleton(1)));
private ShadowCondition createShadowCondition() {
return new ShadowCondition("t_order", ShadowOperationType.INSERT, new ShadowColumnCondition("t_order", "user_id", Collections.singleton(1)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.shadow.condition.ShadowDetermineCondition;
import org.apache.shardingsphere.shadow.condition.ShadowCondition;
import org.apache.shardingsphere.shadow.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.config.datasource.ShadowDataSourceConfiguration;
import org.apache.shardingsphere.shadow.config.table.ShadowTableConfiguration;
Expand All @@ -44,7 +44,7 @@ class HintShadowAlgorithmDeterminerTest {
void assertIsShadow() {
HintShadowAlgorithm hintShadowAlgorithm = (HintShadowAlgorithm) TypedSPILoader.getService(ShadowAlgorithm.class, "SQL_HINT", new Properties());
HintValueContext hintValueContext = createHintValueContext();
assertTrue(HintShadowAlgorithmDeterminer.isShadow(hintShadowAlgorithm, createShadowDetermineCondition(), new ShadowRule(createShadowRuleConfiguration()), hintValueContext.isShadow()));
assertTrue(HintShadowAlgorithmDeterminer.isShadow(hintShadowAlgorithm, createShadowCondition(), new ShadowRule(createShadowRuleConfiguration()), hintValueContext.isShadow()));
}

private HintValueContext createHintValueContext() {
Expand All @@ -68,7 +68,7 @@ private Collection<ShadowDataSourceConfiguration> createDataSources() {
return result;
}

private ShadowDetermineCondition createShadowDetermineCondition() {
return new ShadowDetermineCondition("t_order", ShadowOperationType.INSERT);
private ShadowCondition createShadowCondition() {
return new ShadowCondition("t_order", ShadowOperationType.INSERT);
}
}