You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/05/05 15:37:39 UTC

[shardingsphere] branch master updated: Add sharding-nanoid module (#17357)

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

zhangliang 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 c7f7ef856b2 Add sharding-nanoid module (#17357)
c7f7ef856b2 is described below

commit c7f7ef856b2c56c07a74582b4b31516c0b33e230
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Thu May 5 23:37:32 2022 +0800

    Add sharding-nanoid module (#17357)
    
    * Add sharding-nanoid module
---
 docs/document/content/dev-manual/sharding.cn.md    |  9 ++++----
 docs/document/content/dev-manual/sharding.en.md    |  3 ++-
 pom.xml                                            |  6 ------
 .../shardingsphere-sharding-core/pom.xml           | 10 ++++-----
 .../shardingsphere-sharding-plugin/pom.xml         |  1 +
 .../keygen/CosIdKeyGenerateAlgorithmTest.java      | 16 ++++++++------
 .../CosIdSnowflakeKeyGenerateAlgorithmTest.java    | 19 ++++++++--------
 .../{ => shardingsphere-sharding-nanoid}/pom.xml   | 25 ++++++++++++++++------
 .../keygen/NanoIdKeyGenerateAlgorithm.java         |  2 +-
 ...hardingsphere.sharding.spi.KeyGenerateAlgorithm | 18 ++++++++++++++++
 .../keygen/NanoIdKeyGenerateAlgorithmTest.java     | 10 +++++----
 11 files changed, 76 insertions(+), 43 deletions(-)

diff --git a/docs/document/content/dev-manual/sharding.cn.md b/docs/document/content/dev-manual/sharding.cn.md
index b3d23becb78..465e9f46850 100644
--- a/docs/document/content/dev-manual/sharding.cn.md
+++ b/docs/document/content/dev-manual/sharding.cn.md
@@ -33,12 +33,13 @@ chapter = true
 | ----------------------------- | ------------------------- |
 | KeyGenerateAlgorithm          | 分布式主键生成算法           |
 
-| *已知实现类*                            | *详细说明*                  |
-|------------------------------------|-------------------------|
+| *已知实现类*                         | *详细说明*                         |
+|----------------------------------- |---------------------------------- |
 | SnowflakeKeyGenerateAlgorithm      | 基于雪花算法的分布式主键生成算法        |
-| UUIDKeyGenerateAlgorithm           | 基于 UUID 的分布式主键生成算法      |
-| CosIdKeyGenerateAlgorithm          | 基于 CosId 的分布式主键生成算法     |
+| UUIDKeyGenerateAlgorithm           | 基于 UUID 的分布式主键生成算法        |
+| CosIdKeyGenerateAlgorithm          | 基于 CosId 的分布式主键生成算法       |
 | CosIdSnowflakeKeyGenerateAlgorithm | 基于 CosId 的雪花算法分布式主键生成算法 |
+| NanoIdKeyGenerateAlgorithm         | 基于 NanoId 的分布式主键生成算法      |
 
 ## DatetimeService
 
diff --git a/docs/document/content/dev-manual/sharding.en.md b/docs/document/content/dev-manual/sharding.en.md
index 504ecea285c..5815af196d4 100644
--- a/docs/document/content/dev-manual/sharding.en.md
+++ b/docs/document/content/dev-manual/sharding.en.md
@@ -34,11 +34,12 @@ chapter = true
 | KeyGenerateAlgorithm          | Key generate algorithm           |
 
 | *Implementation Class*             | *Description*                                      |
-|------------------------------------|----------------------------------------------------|
+|----------------------------------- |--------------------------------------------------- |
 | SnowflakeKeyGenerateAlgorithm      | Snowflake key generate algorithm                   |
 | UUIDKeyGenerateAlgorithm           | UUID key generate algorithm                        |
 | CosIdKeyGenerateAlgorithm          | CosId key generate algorithm                       |
 | CosIdSnowflakeKeyGenerateAlgorithm | Snowflake key generate algorithm provided by CosId |
+| NanoIdKeyGenerateAlgorithm         | NanoId key generate algorithm                      |
 
 ## DatetimeService
 
diff --git a/pom.xml b/pom.xml
index 1a476267fc2..bb6f176e522 100644
--- a/pom.xml
+++ b/pom.xml
@@ -104,7 +104,6 @@
         <opentelemetry.version>1.3.0</opentelemetry.version>
         <opentelemetry-autoconfigure.version>1.3.0-alpha</opentelemetry-autoconfigure.version>
         
-        <jnanoid.version>2.0.0</jnanoid.version>
         <bcprov-jdk15on.version>1.70</bcprov-jdk15on.version>
         
         <slf4j.version>1.7.7</slf4j.version>
@@ -577,11 +576,6 @@
                 <scope>test</scope>
             </dependency>
             
-            <dependency>
-                <groupId>com.aventrix.jnanoid</groupId>
-                <artifactId>jnanoid</artifactId>
-                <version>${jnanoid.version}</version>
-            </dependency>
             <dependency>
                 <groupId>org.bouncycastle</groupId>
                 <artifactId>bcprov-jdk15on</artifactId>
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/pom.xml b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/pom.xml
index 1d0f8354c22..c30e164d27f 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/pom.xml
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/pom.xml
@@ -84,6 +84,11 @@
             <artifactId>shardingsphere-sharding-cosid</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-sharding-nanoid</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
@@ -127,10 +132,5 @@
             <version>${project.version}</version>
             <scope>test</scope>
         </dependency>
-        
-        <dependency>
-            <groupId>com.aventrix.jnanoid</groupId>
-            <artifactId>jnanoid</artifactId>
-        </dependency>
     </dependencies>
 </project>
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/pom.xml b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/pom.xml
index a7c58719680..8de01ab74ea 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/pom.xml
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/pom.xml
@@ -30,5 +30,6 @@
     
     <modules>
         <module>shardingsphere-sharding-cosid</module>
+        <module>shardingsphere-sharding-nanoid</module>
     </modules>
 </project>
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdKeyGenerateAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdKeyGenerateAlgorithmTest.java
index 15a2324fca8..1e33bd87f56 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdKeyGenerateAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdKeyGenerateAlgorithmTest.java
@@ -23,11 +23,13 @@ import me.ahoo.cosid.segment.DefaultSegmentId;
 import me.ahoo.cosid.segment.IdSegmentDistributor;
 import me.ahoo.cosid.util.MockIdGenerator;
 import org.apache.shardingsphere.sharding.cosid.algorithm.CosIdAlgorithmConstants;
-import org.hamcrest.CoreMatchers;
 import org.junit.Test;
 
 import java.util.Properties;
 
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.startsWith;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
@@ -42,8 +44,8 @@ public final class CosIdKeyGenerateAlgorithmTest {
         props.setProperty(CosIdAlgorithmConstants.ID_NAME_KEY, idName);
         CosIdKeyGenerateAlgorithm keyGenerateAlgorithm = new CosIdKeyGenerateAlgorithm();
         keyGenerateAlgorithm.init(props);
-        assertThat(keyGenerateAlgorithm.generateKey(), CoreMatchers.is(1L));
-        assertThat(keyGenerateAlgorithm.generateKey(), CoreMatchers.is(2L));
+        assertThat(keyGenerateAlgorithm.generateKey(), is(1L));
+        assertThat(keyGenerateAlgorithm.generateKey(), is(2L));
     }
     
     @Test
@@ -52,8 +54,8 @@ public final class CosIdKeyGenerateAlgorithmTest {
         DefaultIdGeneratorProvider.INSTANCE.setShare(defaultSegmentId);
         CosIdKeyGenerateAlgorithm keyGenerateAlgorithm = new CosIdKeyGenerateAlgorithm();
         keyGenerateAlgorithm.init(new Properties());
-        assertThat(keyGenerateAlgorithm.generateKey(), CoreMatchers.is(1L));
-        assertThat(keyGenerateAlgorithm.generateKey(), CoreMatchers.is(2L));
+        assertThat(keyGenerateAlgorithm.generateKey(), is(1L));
+        assertThat(keyGenerateAlgorithm.generateKey(), is(2L));
     }
     
     @Test(expected = NotFoundIdGeneratorException.class)
@@ -74,8 +76,8 @@ public final class CosIdKeyGenerateAlgorithmTest {
         CosIdKeyGenerateAlgorithm keyGenerateAlgorithm = new CosIdKeyGenerateAlgorithm();
         keyGenerateAlgorithm.init(props);
         Comparable<?> actual = keyGenerateAlgorithm.generateKey();
-        assertThat(actual, CoreMatchers.instanceOf(String.class));
-        assertThat(actual.toString(), CoreMatchers.startsWith("test_"));
+        assertThat(actual, instanceOf(String.class));
+        assertThat(actual.toString(), startsWith("test_"));
         assertTrue(actual.toString().length() <= 16);
     }
 }
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java
index 92b48eae924..0b9d32d92da 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java
@@ -28,12 +28,13 @@ import org.apache.shardingsphere.infra.instance.InstanceContext;
 import org.apache.shardingsphere.infra.instance.definition.InstanceDefinition;
 import org.apache.shardingsphere.infra.lock.LockContext;
 import org.apache.shardingsphere.sharding.cosid.algorithm.keygen.fixture.WorkerIdGeneratorFixture;
-import org.hamcrest.CoreMatchers;
 import org.junit.Test;
 import org.mockito.Mockito;
 
 import java.util.Properties;
 
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
 
 public final class CosIdSnowflakeKeyGenerateAlgorithmTest {
@@ -56,11 +57,11 @@ public final class CosIdSnowflakeKeyGenerateAlgorithmTest {
         long secondActualKey = (Long) cosIdSnowflakeKeyGenerateAlgorithm.generateKey();
         SnowflakeIdState firstActualState = snowflakeIdStateParser.parse(firstActualKey);
         SnowflakeIdState secondActualState = snowflakeIdStateParser.parse(secondActualKey);
-        assertThat(firstActualState.getMachineId(), CoreMatchers.is(FIXTURE_WORKER_ID));
-        assertThat(firstActualState.getSequence(), CoreMatchers.is(0L));
-        assertThat(secondActualState.getMachineId(), CoreMatchers.is(FIXTURE_WORKER_ID));
+        assertThat(firstActualState.getMachineId(), is(FIXTURE_WORKER_ID));
+        assertThat(firstActualState.getSequence(), is(0L));
+        assertThat(secondActualState.getMachineId(), is(FIXTURE_WORKER_ID));
         long expectedSecondSequence = secondActualState.getTimestamp().isAfter(firstActualState.getTimestamp()) ? 0L : 1L;
-        assertThat(secondActualState.getSequence(), CoreMatchers.is(expectedSecondSequence));
+        assertThat(secondActualState.getSequence(), is(expectedSecondSequence));
     }
     
     @Test
@@ -73,13 +74,13 @@ public final class CosIdSnowflakeKeyGenerateAlgorithmTest {
         cosIdSnowflakeKeyGenerateAlgorithm.init(props);
         cosIdSnowflakeKeyGenerateAlgorithm.setProps(props);
         Comparable<?> actualKey = cosIdSnowflakeKeyGenerateAlgorithm.generateKey();
-        assertThat(actualKey, CoreMatchers.instanceOf(String.class));
+        assertThat(actualKey, instanceOf(String.class));
         String actualStringKey = (String) actualKey;
-        assertThat(actualStringKey.length(), CoreMatchers.is(Radix62IdConverter.MAX_CHAR_SIZE));
+        assertThat(actualStringKey.length(), is(Radix62IdConverter.MAX_CHAR_SIZE));
         long actualLongKey = Radix62IdConverter.PAD_START.asLong(actualStringKey);
         SnowflakeIdState actualState = snowflakeIdStateParser.parse(actualLongKey);
-        assertThat(actualState.getMachineId(), CoreMatchers.is(FIXTURE_WORKER_ID));
-        assertThat(actualState.getSequence(), CoreMatchers.is(0L));
+        assertThat(actualState.getMachineId(), is(FIXTURE_WORKER_ID));
+        assertThat(actualState.getSequence(), is(0L));
     }
     
     @Test(expected = NullPointerException.class)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/pom.xml b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-nanoid/pom.xml
similarity index 65%
copy from shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/pom.xml
copy to shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-nanoid/pom.xml
index a7c58719680..9ff7a23e02d 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/pom.xml
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-nanoid/pom.xml
@@ -21,14 +21,27 @@
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <groupId>org.apache.shardingsphere</groupId>
-        <artifactId>shardingsphere-sharding</artifactId>
+        <artifactId>shardingsphere-sharding-plugin</artifactId>
         <version>5.1.2-SNAPSHOT</version>
     </parent>
-    <artifactId>shardingsphere-sharding-plugin</artifactId>
-    <packaging>pom</packaging>
+    <artifactId>shardingsphere-sharding-nanoid</artifactId>
     <name>${project.artifactId}</name>
     
-    <modules>
-        <module>shardingsphere-sharding-cosid</module>
-    </modules>
+    <properties>
+        <jnanoid.version>2.0.0</jnanoid.version>
+    </properties>
+    
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-sharding-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        
+        <dependency>
+            <groupId>com.aventrix.jnanoid</groupId>
+            <artifactId>jnanoid</artifactId>
+            <version>${jnanoid.version}</version>
+        </dependency>
+    </dependencies>
 </project>
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/keygen/NanoIdKeyGenerateAlgorithm.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-nanoid/src/main/java/org/apache/shardingsphere/sharding/nanoid/algorithm/keygen/NanoIdKeyGenerateAlgorithm.java
similarity index 95%
rename from shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/keygen/NanoIdKeyGenerateAlgorithm.java
rename to shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-nanoid/src/main/java/org/apache/shardingsphere/sharding/nanoid/algorithm/keygen/NanoIdKeyGenerateAlgorithm.java
index 249b1cb5f19..fd22fe1595c 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/keygen/NanoIdKeyGenerateAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-nanoid/src/main/java/org/apache/shardingsphere/sharding/nanoid/algorithm/keygen/NanoIdKeyGenerateAlgorithm.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.sharding.algorithm.keygen;
+package org.apache.shardingsphere.sharding.nanoid.algorithm.keygen;
 
 import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
 import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-nanoid/src/main/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-nanoid/src/main/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm
new file mode 100644
index 00000000000..759153ddffd
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-nanoid/src/main/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm
@@ -0,0 +1,18 @@
+#
+# 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.sharding.nanoid.algorithm.keygen.NanoIdKeyGenerateAlgorithm
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/NanoIdKeyGenerateAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-nanoid/src/test/java/org/apache/shardingsphere/sharding/nanoid/algorithm/keygen/NanoIdKeyGenerateAlgorithmTest.java
similarity index 69%
rename from shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/NanoIdKeyGenerateAlgorithmTest.java
rename to shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-nanoid/src/test/java/org/apache/shardingsphere/sharding/nanoid/algorithm/keygen/NanoIdKeyGenerateAlgorithmTest.java
index 60f23668032..6eed32f2b65 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/NanoIdKeyGenerateAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-nanoid/src/test/java/org/apache/shardingsphere/sharding/nanoid/algorithm/keygen/NanoIdKeyGenerateAlgorithmTest.java
@@ -15,19 +15,21 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.sharding.algorithm.keygen;
+package org.apache.shardingsphere.sharding.nanoid.algorithm.keygen;
 
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.sharding.factory.KeyGenerateAlgorithmFactory;
 import org.junit.Test;
 
+import java.util.Properties;
+
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
 
 public final class NanoIdKeyGenerateAlgorithmTest {
     
-    private final NanoIdKeyGenerateAlgorithm nanoIdKeyGenerateAlgorithm = new NanoIdKeyGenerateAlgorithm();
-    
     @Test
     public void assertGenerateKey() {
-        assertThat(((String) nanoIdKeyGenerateAlgorithm.generateKey()).length(), is(21));
+        assertThat(((String) KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("NANOID", new Properties())).generateKey()).length(), is(21));
     }
 }