You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by so...@apache.org on 2022/07/08 09:12:22 UTC

[shardingsphere] branch master updated: #18932: fix bug in spi-based-sharding-spring-boot-jpa-example (#18947)

This is an automated email from the ASF dual-hosted git repository.

soulasuna pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 1754e771666 #18932: fix bug in spi-based-sharding-spring-boot-jpa-example (#18947)
1754e771666 is described below

commit 1754e771666567978348881edb4622809ea69421
Author: 本宫在,尔等都是妃 <11...@qq.com>
AuthorDate: Fri Jul 8 17:12:08 2022 +0800

    #18932: fix bug in spi-based-sharding-spring-boot-jpa-example (#18947)
    
    * #18932: fix bug in spi-based-sharding-spring-boot-jpa-example
    
    * #18932: fix bug in spi-based-sharding-spring-boot-jpa-example
    
    * #18932: fix bug in spi-based-sharding-spring-boot-jpa-example
    
    Co-authored-by: qubo <“doveylovey@126.com”>
---
 ...DatasourceStandardShardingAlgorithmFixture.java | 59 ++++++++++++++++++++++
 ...BasedOrderStandardShardingAlgorithmFixture.java | 59 ++++++++++++++++++++++
 ...e.shardingsphere.sharding.spi.ShardingAlgorithm | 19 +++++++
 3 files changed, 137 insertions(+)

diff --git a/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/spi-based-sharding-algorithm-example/spi-based-sharding-spring-boot-jpa-example/src/main/java/org/apache/shardingsphere/example/sharding/spring/boot/jpa/fixture/SPIBasedDatasourceStandardShardingAlgorithmFixture.java b/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/spi-based-sharding-algorithm-example/ [...]
new file mode 100644
index 00000000000..2b4360bb52e
--- /dev/null
+++ b/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/spi-based-sharding-algorithm-example/spi-based-sharding-spring-boot-jpa-example/src/main/java/org/apache/shardingsphere/example/sharding/spring/boot/jpa/fixture/SPIBasedDatasourceStandardShardingAlgorithmFixture.java
@@ -0,0 +1,59 @@
+/*
+ * 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.example.sharding.spring.boot.jpa.fixture;
+
+import lombok.Getter;
+import org.apache.shardingsphere.sharding.api.sharding.standard.*;
+
+import java.util.Collection;
+import java.util.Properties;
+
+@Getter
+public final class SPIBasedDatasourceStandardShardingAlgorithmFixture implements StandardShardingAlgorithm<Integer> {
+
+    private Properties props;
+
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+    }
+
+    @Override
+    public String doSharding(final Collection<String> dataSourceNames, final PreciseShardingValue<Integer> shardingValue) {
+        for (String each : dataSourceNames) {
+            if (each.endsWith(shardingSuffix(shardingValue.getValue()))) {
+                return each;
+            }
+        }
+        return null;
+    }
+
+    private String shardingSuffix(final Integer shardingValue) {
+        return "-" + (shardingValue % 2);
+    }
+
+    @Override
+    public Collection<String> doSharding(final Collection<String> availableTargetNames, final RangeShardingValue<Integer> shardingValue) {
+        return availableTargetNames;
+    }
+
+    @Override
+    public String getType() {
+        return "DATASOURCE_SPI_BASED";
+    }
+}
diff --git a/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/spi-based-sharding-algorithm-example/spi-based-sharding-spring-boot-jpa-example/src/main/java/org/apache/shardingsphere/example/sharding/spring/boot/jpa/fixture/SPIBasedOrderStandardShardingAlgorithmFixture.java b/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/spi-based-sharding-algorithm-example/spi-b [...]
new file mode 100644
index 00000000000..e0f984e3f2f
--- /dev/null
+++ b/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/spi-based-sharding-algorithm-example/spi-based-sharding-spring-boot-jpa-example/src/main/java/org/apache/shardingsphere/example/sharding/spring/boot/jpa/fixture/SPIBasedOrderStandardShardingAlgorithmFixture.java
@@ -0,0 +1,59 @@
+/*
+ * 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.example.sharding.spring.boot.jpa.fixture;
+
+import lombok.Getter;
+import org.apache.shardingsphere.sharding.api.sharding.standard.*;
+
+import java.util.Collection;
+import java.util.Properties;
+
+@Getter
+public final class SPIBasedOrderStandardShardingAlgorithmFixture implements StandardShardingAlgorithm<Long> {
+
+    private Properties props;
+
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+    }
+
+    @Override
+    public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Long> shardingValue) {
+        for (String each : availableTargetNames) {
+            if (each.endsWith(shardingSuffix(shardingValue.getValue()))) {
+                return each;
+            }
+        }
+        return null;
+    }
+
+    private String shardingSuffix(final Long shardingValue) {
+        return "_" + (shardingValue % 2);
+    }
+
+    @Override
+    public Collection<String> doSharding(final Collection<String> availableTargetNames, final RangeShardingValue<Long> shardingValue) {
+        return availableTargetNames;
+    }
+
+    @Override
+    public String getType() {
+        return "T_ORDER_SPI_BASED";
+    }
+}
diff --git a/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/spi-based-sharding-algorithm-example/spi-based-sharding-spring-boot-jpa-example/src/main/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.ShardingAlgorithm b/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/spi-based-sharding-algorithm-example/spi-based-sharding-spring-boot-jpa-example/ [...]
new file mode 100644
index 00000000000..696160233cd
--- /dev/null
+++ b/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/spi-based-sharding-algorithm-example/spi-based-sharding-spring-boot-jpa-example/src/main/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.ShardingAlgorithm
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.example.sharding.spring.boot.jpa.fixture.SPIBasedDatasourceStandardShardingAlgorithmFixture
+org.apache.shardingsphere.example.sharding.spring.boot.jpa.fixture.SPIBasedOrderStandardShardingAlgorithmFixture