This is an automated email from the ASF dual-hosted git repository.
miaoliyao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shardingsphere-on-cloud.git
The following commit(s) were added to refs/heads/main by this push:
new cf2edac feat: using wasm to implementing custom sharding algorithm
(#227)
cf2edac is described below
commit cf2edac85b258a51041f21043a1845a9b3268d62
Author: pierce <[email protected]>
AuthorDate: Fri Feb 24 17:30:55 2023 +0800
feat: using wasm to implementing custom sharding algorithm (#227)
* Update README.md
* Update build.yaml
* Update build.yaml
* feat(wasm): initial wasm sharding
Signed-off-by: xuanyuan300 <[email protected]>
* chore(doc): remove space line
Signed-off-by: xuanyuan300 <[email protected]>
* fix(license): fix license check
Signed-off-by: xuanyuan300 <[email protected]>
* fix(wasm): remove lib jar
Signed-off-by: xuanyuan300 <[email protected]>
---------
Signed-off-by: xuanyuan300 <[email protected]>
---
.licenserc.yaml | 1 +
wasm/README.md | 1 +
wasm/pom.xml | 76 ++++++++++
.../main/java/org/apache/shardingsphere/demo.java | 53 +++++++
.../infra/algorithm/ShardingSphereAlgorithm.java | 36 +++++
.../config/props/ConfigurationProperties.java | 33 ++++
.../infra/datanode/DataNodeInfo.java | 35 +++++
.../infra/util/props/ConfigurationPropertyKey.java | 144 ++++++++++++++++++
.../infra/util/props/TypedProperties.java | 63 ++++++++
.../infra/util/props/TypedPropertyKey.java | 45 ++++++
.../infra/util/props/TypedPropertyValue.java | 65 ++++++++
.../infra/util/spi/lifecycle/SPIPostProcessor.java | 33 ++++
.../infra/util/spi/type/typed/TypedSPI.java | 45 ++++++
.../sharding/ModShardingAlgorithm.java | 166 +++++++++++++++++++++
.../sharding/WasmShardingAlgorithm.java | 130 ++++++++++++++++
.../sharding/api/sharding/ShardingValue.java | 24 +++
.../sharding/standard/PreciseShardingValue.java | 41 +++++
.../api/sharding/standard/RangeShardingValue.java | 42 ++++++
.../standard/StandardShardingAlgorithm.java | 48 ++++++
.../value/ListShardingConditionValue.java | 53 +++++++
.../value/RangeShardingConditionValue.java | 47 ++++++
.../condition/value/ShardingConditionValue.java | 47 ++++++
.../sharding/route/strategy/ShardingStrategy.java | 56 +++++++
.../type/standard/StandardShardingStrategy.java | 86 +++++++++++
.../sharding/spi/ShardingAlgorithm.java | 26 ++++
wasm/wasm-sharding/Cargo.lock | 7 +
wasm/wasm-sharding/Cargo.toml | 9 ++
wasm/wasm-sharding/src/lib.rs | 45 ++++++
28 files changed, 1457 insertions(+)
diff --git a/.licenserc.yaml b/.licenserc.yaml
index 11b5492..446fc80 100644
--- a/.licenserc.yaml
+++ b/.licenserc.yaml
@@ -41,5 +41,6 @@ header:
- 'cloudformation/multi-az/*.service'
- 'cloudformation/multi-az/*.json'
- '**/*.json'
+ - '**/Cargo.lock'
comment: on-failure
diff --git a/wasm/README.md b/wasm/README.md
new file mode 100644
index 0000000..d7b97fe
--- /dev/null
+++ b/wasm/README.md
@@ -0,0 +1 @@
+This is an experimental project using WebAssembly for sharding in Apache
ShardingSphere
\ No newline at end of file
diff --git a/wasm/pom.xml b/wasm/pom.xml
new file mode 100644
index 0000000..392ed4d
--- /dev/null
+++ b/wasm/pom.xml
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.shardingsphere</groupId>
+ <artifactId>wasm-sharding</artifactId>
+ <version>1.0-SNAPSHOT</version>
+
+ <properties>
+ <maven.compiler.source>8</maven.compiler.source>
+ <maven.compiler.target>8</maven.compiler.target>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.projectlombok</groupId>
+ <artifactId>lombok</artifactId>
+ <version>1.18.20</version>
+ </dependency>
+
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ <version>30.0-jre</version>
+ <exclusions>
+ <exclusion>
+ <groupId>com.google.guava</groupId>
+ <artifactId>listenablefuture</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>com.google.errorprone</groupId>
+ <artifactId>error_prone_annotations</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>com.google.j2objc</groupId>
+ <artifactId>j2objc-annotations</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <!--<dependency>
+ <groupId>io.github.kawamuray.wasmtime</groupId>
+ <artifactId>wasmtime-java</artifactId>
+ <version>0.14.0</version>
+ </dependency>-->
+ <dependency>
+ <groupId>io.github.kawamuray.wasmtime</groupId>
+ <artifactId>wasmtime-java</artifactId>
+ <version>0.14.0</version>
+ <scope>system</scope>
+
<systemPath>${project.basedir}/lib/wasmtime-java-0.14.0.jar</systemPath>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>1.7.30</version>
+ </dependency>
+ </dependencies>
+</project>
diff --git a/wasm/src/main/java/org/apache/shardingsphere/demo.java
b/wasm/src/main/java/org/apache/shardingsphere/demo.java
new file mode 100644
index 0000000..98cde47
--- /dev/null
+++ b/wasm/src/main/java/org/apache/shardingsphere/demo.java
@@ -0,0 +1,53 @@
+/*
+ * 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;
+
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.datanode.DataNodeInfo;
+import org.apache.shardingsphere.sharding.WasmShardingAlgorithm;
+import
org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
+import
org.apache.shardingsphere.sharding.route.engine.condition.value.ListShardingConditionValue;
+import
org.apache.shardingsphere.sharding.route.engine.condition.value.ShardingConditionValue;
+import org.apache.shardingsphere.sharding.route.strategy.ShardingStrategy;
+import
org.apache.shardingsphere.sharding.route.strategy.type.standard.StandardShardingStrategy;
+
+import java.util.*;
+
+public class demo {
+ public static void main(String[] args) {
+ String shardingColumn = "id";
+ StandardShardingAlgorithm<?> shardingAlgorithm = new
WasmShardingAlgorithm();
+ Properties props = new Properties();
+ props.setProperty("sharding-count", "3");
+ shardingAlgorithm.init(props);
+ ShardingStrategy strategy = new
StandardShardingStrategy(shardingColumn, shardingAlgorithm);
+
+ Collection<String> targetNames = new ArrayList<>();
+ targetNames.add("t_order_0");
+ targetNames.add("t_order_1");
+ targetNames.add("t_order_2");
+
+ DataNodeInfo dataNodeInfo = new DataNodeInfo("t_order_", 1, '0');
+
+ Collection<ShardingConditionValue> conditionValues =
Collections.singletonList(
+ new ListShardingConditionValue<>("id", "t_order",
Collections.singletonList(10)));
+
+ Collection<String> result = strategy.doSharding(targetNames,
conditionValues, dataNodeInfo, new ConfigurationProperties(props));
+ System.out.println(result);
+ }
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/infra/algorithm/ShardingSphereAlgorithm.java
b/wasm/src/main/java/org/apache/shardingsphere/infra/algorithm/ShardingSphereAlgorithm.java
new file mode 100644
index 0000000..6408c2f
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/infra/algorithm/ShardingSphereAlgorithm.java
@@ -0,0 +1,36 @@
+/*
+ * 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.infra.algorithm;
+
+import org.apache.shardingsphere.infra.util.spi.lifecycle.SPIPostProcessor;
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPI;
+
+import java.util.Properties;
+
+/**
+ * ShardingSphere algorithm.
+ */
+public interface ShardingSphereAlgorithm extends TypedSPI, SPIPostProcessor {
+
+ /**
+ * Get properties.
+ *
+ * @return properties
+ */
+ Properties getProps();
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/infra/config/props/ConfigurationProperties.java
b/wasm/src/main/java/org/apache/shardingsphere/infra/config/props/ConfigurationProperties.java
new file mode 100644
index 0000000..43716fb
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/infra/config/props/ConfigurationProperties.java
@@ -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.infra.config.props;
+
+import org.apache.shardingsphere.infra.util.props.ConfigurationPropertyKey;
+import org.apache.shardingsphere.infra.util.props.TypedProperties;
+
+import java.util.Properties;
+
+/**
+ * Typed properties of configuration.
+ */
+public final class ConfigurationProperties extends
TypedProperties<ConfigurationPropertyKey> {
+
+ public ConfigurationProperties(final Properties props) {
+ super(ConfigurationPropertyKey.class, props);
+ }
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodeInfo.java
b/wasm/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodeInfo.java
new file mode 100644
index 0000000..70b495e
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodeInfo.java
@@ -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.infra.datanode;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+/**
+ * Data node info.
+ */
+@RequiredArgsConstructor
+@Getter
+public final class DataNodeInfo {
+
+ private final String prefix;
+
+ private final int suffixMinLength;
+
+ private final char paddingChar;
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/infra/util/props/ConfigurationPropertyKey.java
b/wasm/src/main/java/org/apache/shardingsphere/infra/util/props/ConfigurationPropertyKey.java
new file mode 100644
index 0000000..cc9b292
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/infra/util/props/ConfigurationPropertyKey.java
@@ -0,0 +1,144 @@
+/*
+ * 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.infra.util.props;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.stream.Collectors;
+
+/**
+ * Typed property key of configuration.
+ */
+@RequiredArgsConstructor
+@Getter
+public enum ConfigurationPropertyKey implements TypedPropertyKey {
+
+ /**
+ * Whether show SQL in log.
+ */
+ SQL_SHOW("sql-show", String.valueOf(Boolean.FALSE), boolean.class, false),
+
+ /**
+ * Whether show SQL details in simple style.
+ */
+ SQL_SIMPLE("sql-simple", String.valueOf(Boolean.FALSE), boolean.class,
false),
+
+ /**
+ * The max thread size of worker group to execute SQL.
+ */
+ KERNEL_EXECUTOR_SIZE("kernel-executor-size", String.valueOf(0), int.class,
true),
+
+ /**
+ * Max opened connection size for each query.
+ */
+ MAX_CONNECTIONS_SIZE_PER_QUERY("max-connections-size-per-query",
String.valueOf(1), int.class, false),
+
+ /**
+ * Whether validate table meta data consistency when application startup
or updated.
+ */
+ CHECK_TABLE_META_DATA_ENABLED("check-table-metadata-enabled",
String.valueOf(Boolean.FALSE), boolean.class, false),
+
+ /**
+ * SQL federation type.
+ */
+ SQL_FEDERATION_TYPE("sql-federation-type", "NONE", String.class, false),
+
+ /**
+ * Frontend database protocol type for ShardingSphere-Proxy.
+ */
+
PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE("proxy-frontend-database-protocol-type",
"", String.class, false),
+
+ /**
+ * Flush threshold for every records from databases for
ShardingSphere-Proxy.
+ */
+ PROXY_FRONTEND_FLUSH_THRESHOLD("proxy-frontend-flush-threshold",
String.valueOf(128), int.class, false),
+
+ /**
+ * Whether enable hint for ShardingSphere-Proxy.
+ */
+ PROXY_HINT_ENABLED("proxy-hint-enabled", String.valueOf(Boolean.FALSE),
boolean.class, false),
+
+ /**
+ * Proxy backend query fetch size. A larger value may increase the memory
usage of ShardingSphere Proxy.
+ * The default value is -1, which means set the minimum value for
different JDBC drivers.
+ */
+ PROXY_BACKEND_QUERY_FETCH_SIZE("proxy-backend-query-fetch-size",
String.valueOf(-1), int.class, false),
+
+ /**
+ * Proxy frontend executor size. The default value is 0, which means let
Netty decide.
+ */
+ PROXY_FRONTEND_EXECUTOR_SIZE("proxy-frontend-executor-size",
String.valueOf(0), int.class, true),
+
+// /**
+// * Available options of proxy backend executor suitable: OLAP(default),
OLTP. The OLTP option may reduce time cost of writing packets to client, but it
may increase the latency of SQL execution
+// * and block other clients if client connections are more than {@link
ConfigurationPropertyKey#PROXY_FRONTEND_EXECUTOR_SIZE}, especially executing
slow SQL.
+// */
+// PROXY_BACKEND_EXECUTOR_SUITABLE("proxy-backend-executor-suitable",
BackendExecutorType.OLAP.name(), BackendExecutorType.class, false),
+
+ /**
+ * Less than or equal to 0 means no limitation.
+ */
+ PROXY_FRONTEND_MAX_CONNECTIONS("proxy-frontend-max-connections", "0",
int.class, false),
+
+ /**
+ * Proxy MySQL default version.
+ */
+ PROXY_MYSQL_DEFAULT_VERSION("proxy-mysql-default-version", "5.7.22",
String.class, false),
+
+ /**
+ * Proxy default start port.
+ */
+ PROXY_DEFAULT_PORT("proxy-default-port", "3307", int.class, true),
+
+ /**
+ * Proxy Netty backlog size.
+ */
+ PROXY_NETTY_BACKLOG("proxy-netty-backlog", "1024", int.class, false),
+
+ /**
+ * Proxy instance type.
+ */
+ PROXY_INSTANCE_TYPE("proxy-instance-type", "Proxy", String.class, true),
+
+ /**
+ * Proxy wasm sharding
+ */
+ Proxy_WASM_SHARDING("wasm-sharding-enabled",
String.valueOf(Boolean.FALSE), boolean.class, false);
+
+
+
+ private final String key;
+
+ private final String defaultValue;
+
+ private final Class<?> type;
+
+ private final boolean rebootRequired;
+
+ /**
+ * Get property key names.
+ *
+ * @return collection of key names
+ */
+ public static Collection<String> getKeyNames() {
+ return
Arrays.stream(values()).map(ConfigurationPropertyKey::name).collect(Collectors.toList());
+ }
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/infra/util/props/TypedProperties.java
b/wasm/src/main/java/org/apache/shardingsphere/infra/util/props/TypedProperties.java
new file mode 100644
index 0000000..340d55d
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/infra/util/props/TypedProperties.java
@@ -0,0 +1,63 @@
+/*
+ * 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.infra.util.props;
+
+import lombok.Getter;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Typed properties with a specified enum.
+ */
+public abstract class TypedProperties<E extends Enum<?> & TypedPropertyKey> {
+
+ @Getter
+ private final Properties props;
+
+ private final Map<E, TypedPropertyValue> cache;
+
+ protected TypedProperties(final Class<E> keyClass, final Properties props)
{
+ this.props = null == props ? new Properties() : props;
+ cache = preload(keyClass);
+ }
+
+ private Map<E, TypedPropertyValue> preload(final Class<E> keyClass) {
+ E[] enumConstants = keyClass.getEnumConstants();
+ Map<E, TypedPropertyValue> result = new
HashMap<>(enumConstants.length, 1);
+ for (E each : enumConstants) {
+ TypedPropertyValue value = null;
+ value = new TypedPropertyValue(each,
props.getOrDefault(each.getKey(), each.getDefaultValue()).toString());
+ result.put(each, value);
+ }
+ return result;
+ }
+
+ /**
+ * Get property value.
+ *
+ * @param key property key
+ * @param <T> class type of return value
+ * @return property value
+ */
+ @SuppressWarnings("unchecked")
+ public final <T> T getValue(final E key) {
+ return (T) cache.get(key).getValue();
+ }
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/infra/util/props/TypedPropertyKey.java
b/wasm/src/main/java/org/apache/shardingsphere/infra/util/props/TypedPropertyKey.java
new file mode 100644
index 0000000..846f235
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/infra/util/props/TypedPropertyKey.java
@@ -0,0 +1,45 @@
+/*
+ * 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.infra.util.props;
+
+/**
+ * Typed property key.
+ */
+public interface TypedPropertyKey {
+
+ /**
+ * Get property key.
+ *
+ * @return property key
+ */
+ String getKey();
+
+ /**
+ * Get default property value.
+ *
+ * @return default property value
+ */
+ String getDefaultValue();
+
+ /**
+ * Get property type.
+ *
+ * @return property type
+ */
+ Class<?> getType();
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/infra/util/props/TypedPropertyValue.java
b/wasm/src/main/java/org/apache/shardingsphere/infra/util/props/TypedPropertyValue.java
new file mode 100644
index 0000000..dd3829f
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/infra/util/props/TypedPropertyValue.java
@@ -0,0 +1,65 @@
+/*
+ * 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.infra.util.props;
+
+import lombok.Getter;
+
+/**
+ * Typed property value.
+ */
+@Getter
+public final class TypedPropertyValue {
+
+ private final Object value;
+
+ public TypedPropertyValue(final TypedPropertyKey key, final String value) {
+ this.value = createTypedValue(key, value);
+ }
+
+ private Object createTypedValue(final TypedPropertyKey key, final String
value) {
+ if (boolean.class == key.getType() || Boolean.class == key.getType()) {
+ return Boolean.valueOf(value);
+ }
+ if (int.class == key.getType() || Integer.class == key.getType()) {
+ try {
+ return Integer.valueOf(value);
+ } catch (final NumberFormatException ex) {
+ throw ex;
+ }
+ }
+ if (long.class == key.getType() || Long.class == key.getType()) {
+ try {
+ return Long.valueOf(value);
+ } catch (final NumberFormatException ex) {
+ throw ex;
+ }
+ }
+ if (Enum.class.isAssignableFrom(key.getType())) {
+ return getEnumValue(key, value);
+ }
+ return value;
+ }
+
+ private Enum<?> getEnumValue(final TypedPropertyKey key, final String
value) {
+ try {
+ return (Enum<?>) key.getType().getMethod("valueOf",
String.class).invoke(null, value);
+ } catch (final ReflectiveOperationException | IllegalArgumentException
ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/infra/util/spi/lifecycle/SPIPostProcessor.java
b/wasm/src/main/java/org/apache/shardingsphere/infra/util/spi/lifecycle/SPIPostProcessor.java
new file mode 100644
index 0000000..b340c44
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/infra/util/spi/lifecycle/SPIPostProcessor.java
@@ -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.infra.util.spi.lifecycle;
+
+import java.util.Properties;
+
+/**
+ * SPI post processor.
+ */
+public interface SPIPostProcessor {
+
+ /**
+ * Initialize SPI.
+ *
+ * @param props properties to be initialized
+ */
+ void init(Properties props);
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPI.java
b/wasm/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPI.java
new file mode 100644
index 0000000..1c58ad2
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPI.java
@@ -0,0 +1,45 @@
+/*
+ * 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.infra.util.spi.type.typed;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * Typed SPI.
+ */
+public interface TypedSPI {
+
+ /**
+ * Get type.
+ *
+ * @return type
+ */
+ default String getType() {
+ return "";
+ }
+
+ /**
+ * Get type aliases.
+ *
+ * @return type aliases
+ */
+ default Collection<String> getTypeAliases() {
+ return Collections.emptyList();
+ }
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/sharding/ModShardingAlgorithm.java
b/wasm/src/main/java/org/apache/shardingsphere/sharding/ModShardingAlgorithm.java
new file mode 100644
index 0000000..7b7ee32
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/sharding/ModShardingAlgorithm.java
@@ -0,0 +1,166 @@
+/*
+ * 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.sharding;
+
+import com.google.common.base.Strings;
+import lombok.Getter;
+//import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
+//import
org.apache.shardingsphere.sharding.algorithm.sharding.ShardingAutoTableAlgorithmUtil;
+//import
org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm;
+import org.apache.shardingsphere.infra.datanode.DataNodeInfo;
+import
org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
+import
org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
+import
org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
+//import
org.apache.shardingsphere.sharding.exception.algorithm.sharding.ShardingAlgorithmInitializationException;
+//import
org.apache.shardingsphere.sharding.exception.data.ShardingValueOffsetException;
+
+import java.math.BigInteger;
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.Optional;
+import java.util.Properties;
+
+/**
+ * Modulo sharding algorithm.
+ */
+public final class ModShardingAlgorithm implements
StandardShardingAlgorithm<Comparable<?>> {
+
+ private static final String SHARDING_COUNT_KEY = "sharding-count";
+
+ private static final String START_OFFSET_INDEX_KEY = "start-offset";
+
+ private static final String STOP_OFFSET_INDEX_KEY = "stop-offset";
+
+ private static final String ZERO_PADDING_KEY = "zero-padding";
+
+ @Getter
+ private Properties props;
+
+ private int shardingCount;
+
+ private int startOffset;
+
+ private int stopOffset;
+
+ private boolean zeroPadding;
+
+ private int maxPaddingSize;
+
+ @Override
+ public void init(final Properties props) {
+ this.props = props;
+ shardingCount = getShardingCount(props);
+ startOffset = getStartOffset(props);
+ stopOffset = getStopOffset(props);
+ zeroPadding = isZeroPadding(props);
+ maxPaddingSize = calculateMaxPaddingSize();
+ }
+
+ private int getShardingCount(final Properties props) {
+
//ShardingSpherePreconditions.checkState(props.containsKey(SHARDING_COUNT_KEY),
() -> new ShardingAlgorithmInitializationException(getType(), "Sharding count
can not be null."));
+ return Integer.parseInt(props.getProperty(SHARDING_COUNT_KEY));
+ }
+
+ private int getStartOffset(final Properties props) {
+ int result =
Integer.parseInt(String.valueOf(props.getProperty(START_OFFSET_INDEX_KEY,
"0")));
+ //ShardingSpherePreconditions.checkState(result >= 0, () -> new
ShardingAlgorithmInitializationException(getType(), "Start offset can not be
less than 0."));
+ return result;
+ }
+
+ private int getStopOffset(final Properties props) {
+ int result =
Integer.parseInt(String.valueOf(props.getProperty(STOP_OFFSET_INDEX_KEY, "0")));
+ //ShardingSpherePreconditions.checkState(result >= 0, () -> new
ShardingAlgorithmInitializationException(getType(), "Stop offset can not be
less than 0."));
+ return result;
+ }
+
+ private boolean isZeroPadding(final Properties props) {
+ return
Boolean.parseBoolean(String.valueOf(props.getProperty(ZERO_PADDING_KEY,
Boolean.FALSE.toString())));
+ }
+
+ private int calculateMaxPaddingSize() {
+ int result = 0;
+ int calculatingShardingCount = shardingCount - 1;
+ while (0 != calculatingShardingCount) {
+ result++;
+ calculatingShardingCount = calculatingShardingCount / 10;
+ }
+ return Math.max(result, 1);
+ }
+
+ @Override
+ public String doSharding(final Collection<String> availableTargetNames,
final PreciseShardingValue<Comparable<?>> shardingValue) {
+ String shardingResultSuffix =
getShardingResultSuffix(cutShardingValue(shardingValue.getValue()).mod(new
BigInteger(String.valueOf(shardingCount))).toString());
+ return findMatchedTargetName(availableTargetNames,
shardingResultSuffix, shardingValue.getDataNodeInfo()).orElse(null);
+ }
+
+ @Override
+ public Collection<String> doSharding(final Collection<String>
availableTargetNames, final RangeShardingValue<Comparable<?>> shardingValue) {
+ return containsAllTargets(shardingValue) ? availableTargetNames :
getAvailableTargetNames(availableTargetNames, shardingValue);
+ }
+
+ private boolean containsAllTargets(final RangeShardingValue<Comparable<?>>
shardingValue) {
+ return !shardingValue.getValueRange().hasUpperBound() ||
shardingValue.getValueRange().hasLowerBound()
+ &&
getBigInteger(shardingValue.getValueRange().upperEndpoint()).subtract(getBigInteger(shardingValue.getValueRange().lowerEndpoint())).intValue()
>= shardingCount - 1;
+ }
+
+ private Collection<String> getAvailableTargetNames(final
Collection<String> availableTargetNames, final
RangeShardingValue<Comparable<?>> shardingValue) {
+ Collection<String> result = new
LinkedHashSet<>(availableTargetNames.size());
+ BigInteger lower = new
BigInteger(shardingValue.getValueRange().lowerEndpoint().toString());
+ BigInteger upper = new
BigInteger(shardingValue.getValueRange().upperEndpoint().toString());
+ BigInteger shardingCountBigInter = new
BigInteger(String.valueOf(shardingCount));
+ for (BigInteger i = lower; i.compareTo(upper) <= 0; i = i.add(new
BigInteger("1"))) {
+ String shardingResultSuffix =
getShardingResultSuffix(String.valueOf(i.mod(shardingCountBigInter)));
+ findMatchedTargetName(availableTargetNames, shardingResultSuffix,
shardingValue.getDataNodeInfo()).ifPresent(result::add);
+ }
+ return result;
+ }
+
+ private String getShardingResultSuffix(final String shardingResultSuffix) {
+ return zeroPadding ? fillZero(shardingResultSuffix) :
shardingResultSuffix;
+ }
+
+ private String fillZero(final String value) {
+ return String.format("%0" + maxPaddingSize + "d",
Integer.parseInt(value));
+ }
+
+ private BigInteger cutShardingValue(final Comparable<?> shardingValue) {
+
//ShardingSpherePreconditions.checkState(shardingValue.toString().length() -
stopOffset > startOffset, () -> new ShardingValueOffsetException(shardingValue,
startOffset, stopOffset));
+ return 0 == startOffset && 0 == stopOffset ?
getBigInteger(shardingValue) : new
BigInteger(shardingValue.toString().substring(startOffset,
shardingValue.toString().length() - stopOffset));
+ }
+
+ private BigInteger getBigInteger(final Comparable<?> value) {
+ return value instanceof Number ? BigInteger.valueOf(((Number)
value).longValue()) : new BigInteger(value.toString());
+ }
+
+ //@Override
+ //public int getAutoTablesAmount() {
+ // return shardingCount;
+ //}
+
+ @Override
+ public String getType() {
+ return "MOD";
+ }
+
+ public static Optional<String> findMatchedTargetName(final
Collection<String> availableTargetNames, final String suffix, final
DataNodeInfo dataNodeInfo) {
+ String targetName = dataNodeInfo.getPrefix() +
Strings.padStart(suffix, dataNodeInfo.getSuffixMinLength(),
dataNodeInfo.getPaddingChar());
+ return availableTargetNames.contains(targetName) ?
Optional.of(targetName) : Optional.empty();
+ }
+}
+
+
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/sharding/WasmShardingAlgorithm.java
b/wasm/src/main/java/org/apache/shardingsphere/sharding/WasmShardingAlgorithm.java
new file mode 100644
index 0000000..0493df7
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/sharding/WasmShardingAlgorithm.java
@@ -0,0 +1,130 @@
+/*
+ * 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.sharding;
+
+import com.google.common.base.Strings;
+import io.github.kawamuray.wasmtime.*;
+import io.github.kawamuray.wasmtime.wasi.WasiCtx;
+import io.github.kawamuray.wasmtime.wasi.WasiCtxBuilder;
+import static io.github.kawamuray.wasmtime.WasmValType.I32;
+import static io.github.kawamuray.wasmtime.WasmValType.I64;
+import io.github.kawamuray.wasmtime.Store;
+import lombok.Getter;
+import org.apache.shardingsphere.infra.datanode.DataNodeInfo;
+import
org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
+import
org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
+import
org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
+
+import java.math.BigInteger;
+import java.nio.ByteBuffer;
+import java.util.*;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * Modulo sharding algorithm.
+ */
+public final class WasmShardingAlgorithm implements
StandardShardingAlgorithm<Comparable<?>> {
+
+ @Getter
+ private Properties props;
+
+ private static final String WASM_PATH =
"./wasm-sharding/target/wasm32-wasi/debug/wasm_sharding.wasm";
+
+ @Override
+ public void init(final Properties props) {
+ this.props = props;
+ }
+
+ @Override
+ public String doSharding(final Collection<String> availableTargetNames,
final PreciseShardingValue<Comparable<?>> shardingValue) {
+ return wasmDoSharding(availableTargetNames, shardingValue);
+ }
+
+ @Override
+ public Collection<String> doSharding(final Collection<String>
availableTargetNames, final RangeShardingValue<Comparable<?>> shardingValue) {
+ return Collections.EMPTY_LIST;
+ }
+
+ private String wasmDoSharding(final Collection<String>
availableTargetNames, final PreciseShardingValue<Comparable<?>> shardingValue) {
+ String availableTargetNamesArgs = String.join(",",
availableTargetNames);
+ String conditionValuesArgs = String.format("%s,%s,%s",
shardingValue.getColumnName(), shardingValue.getLogicTableName(),
shardingValue.getValue());
+ AtomicReference<Memory> memRef = new AtomicReference<>();
+ final Integer[] offset = {1};
+ try (WasiCtx wasi = new
WasiCtxBuilder().inheritStdout().inheritStderr().build();
+ Store<Void> store = Store.withoutData(wasi);
+ Linker linker = new Linker(store.engine());
+ Func pollWordFn = WasmFunctions.wrap(store, I64, I32, I32, (addr,
len) -> {
+ ByteBuffer buf = memRef.get().buffer(store);
+ buf.put(addr.intValue(), (byte)
availableTargetNamesArgs.length());
+
+ for (int j = 0; j < availableTargetNamesArgs.length(); j++) {
+ buf.put(addr.intValue() + j + offset[0], (byte)
availableTargetNamesArgs.charAt(j));
+ }
+
+ offset[0] += availableTargetNamesArgs.length();
+
+ buf.put(addr.intValue() + offset[0], (byte)
conditionValuesArgs.length());
+ offset[0] += 1;
+ for (int j = 0; j < conditionValuesArgs.length(); j++) {
+ buf.put(addr.intValue() + j + offset[0], (byte)
conditionValuesArgs.charAt(j));
+ }
+
+ offset[0] += conditionValuesArgs.length();
+ return Math.min(offset[0], len);
+ });
+ Module module = Module.fromFile(store.engine(), WASM_PATH)) {
+
+ WasiCtx.addToLinker(linker);
+ linker.define("sharding", "poll_table",
Extern.fromFunc(pollWordFn));
+ linker.module(store, "", module);
+
+
+ try (Memory mem = linker.get(store, "", "memory").get().memory();
+ Func doWorkFn = linker.get(store, "",
"do_work").get().func()) {
+ WasmFunctions.Function0<Long> doWork =
WasmFunctions.func(store, doWorkFn, I64);
+
+ memRef.set(mem);
+
+ ByteBuffer buf = mem.buffer(store);
+
+ int ptr = doWork.call().intValue();
+ buf.position(ptr + offset[0]);
+ StringBuilder result = new StringBuilder(10);
+
+ while (true) {
+ Byte byt = buf.get();
+ char a = (char) byt.byteValue();
+ if (byt.intValue() == 0) {
+ break;
+ }
+
+ result.append(a);
+ }
+
+ return result.toString();
+ }
+ }
+ }
+
+ @Override
+ public String getType() {
+ return "WASM";
+ }
+}
+
+
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/sharding/api/sharding/ShardingValue.java
b/wasm/src/main/java/org/apache/shardingsphere/sharding/api/sharding/ShardingValue.java
new file mode 100644
index 0000000..257092e
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/sharding/api/sharding/ShardingValue.java
@@ -0,0 +1,24 @@
+/*
+ * 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.sharding.api.sharding;
+
+/**
+ * Sharding value.
+ */
+public interface ShardingValue {
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/sharding/api/sharding/standard/PreciseShardingValue.java
b/wasm/src/main/java/org/apache/shardingsphere/sharding/api/sharding/standard/PreciseShardingValue.java
new file mode 100644
index 0000000..582f15d
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/sharding/api/sharding/standard/PreciseShardingValue.java
@@ -0,0 +1,41 @@
+/*
+ * 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.sharding.api.sharding.standard;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.ToString;
+import org.apache.shardingsphere.infra.datanode.DataNodeInfo;
+import org.apache.shardingsphere.sharding.api.sharding.ShardingValue;
+
+/**
+ * Sharding value for precise.
+ */
+@RequiredArgsConstructor
+@Getter
+@ToString
+public final class PreciseShardingValue<T extends Comparable<?>> implements
ShardingValue {
+
+ private final String logicTableName;
+
+ private final String columnName;
+
+ private final DataNodeInfo dataNodeInfo;
+
+ private final T value;
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/sharding/api/sharding/standard/RangeShardingValue.java
b/wasm/src/main/java/org/apache/shardingsphere/sharding/api/sharding/standard/RangeShardingValue.java
new file mode 100644
index 0000000..f59b1f4
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/sharding/api/sharding/standard/RangeShardingValue.java
@@ -0,0 +1,42 @@
+/*
+ * 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.sharding.api.sharding.standard;
+
+import com.google.common.collect.Range;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.ToString;
+import org.apache.shardingsphere.infra.datanode.DataNodeInfo;
+import org.apache.shardingsphere.sharding.api.sharding.ShardingValue;
+
+/**
+ * Sharding value for range.
+ */
+@RequiredArgsConstructor
+@Getter
+@ToString
+public final class RangeShardingValue<T extends Comparable<?>> implements
ShardingValue {
+
+ private final String logicTableName;
+
+ private final String columnName;
+
+ private final DataNodeInfo dataNodeInfo;
+
+ private final Range<T> valueRange;
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/sharding/api/sharding/standard/StandardShardingAlgorithm.java
b/wasm/src/main/java/org/apache/shardingsphere/sharding/api/sharding/standard/StandardShardingAlgorithm.java
new file mode 100644
index 0000000..75fdd5b
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/sharding/api/sharding/standard/StandardShardingAlgorithm.java
@@ -0,0 +1,48 @@
+/*
+ * 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.sharding.api.sharding.standard;
+
+import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
+
+import java.util.Collection;
+
+/**
+ * Standard sharding algorithm.
+ *
+ * @param <T> class type of sharding value
+ */
+public interface StandardShardingAlgorithm<T extends Comparable<?>> extends
ShardingAlgorithm {
+
+ /**
+ * Sharding.
+ *
+ * @param availableTargetNames available data sources or table names
+ * @param shardingValue sharding value
+ * @return sharding result for data source or table name
+ */
+ String doSharding(Collection<String> availableTargetNames,
PreciseShardingValue<T> shardingValue);
+
+ /**
+ * Sharding.
+ *
+ * @param availableTargetNames available data sources or table names
+ * @param shardingValue sharding value
+ * @return sharding results for data sources or table names
+ */
+ Collection<String> doSharding(Collection<String> availableTargetNames,
RangeShardingValue<T> shardingValue);
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/ListShardingConditionValue.java
b/wasm/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/ListShardingConditionValue.java
new file mode 100644
index 0000000..583d1f0
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/ListShardingConditionValue.java
@@ -0,0 +1,53 @@
+/*
+ * 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.sharding.route.engine.condition.value;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Sharding condition value for list values.
+ */
+@RequiredArgsConstructor
+@Getter
+public final class ListShardingConditionValue<T extends Comparable<?>>
implements ShardingConditionValue {
+
+ private final String columnName;
+
+ private final String tableName;
+
+ private final Collection<T> values;
+
+ private final List<Integer> parameterMarkerIndexes;
+
+ public ListShardingConditionValue(final String columnName, final String
tableName, final Collection<T> values) {
+ this(columnName, tableName, values, Collections.emptyList());
+ }
+
+ @Override
+ public String toString() {
+ String condition = 1 == values.size() ? " = " + new
ArrayList<>(values).get(0) : " in (" +
values.stream().map(Object::toString).collect(Collectors.joining(",")) + ")";
+ return tableName + "." + columnName + condition;
+ }
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/RangeShardingConditionValue.java
b/wasm/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/RangeShardingConditionValue.java
new file mode 100644
index 0000000..65c4b26
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/RangeShardingConditionValue.java
@@ -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.sharding.route.engine.condition.value;
+
+import com.google.common.collect.Range;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.ToString;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Sharding condition value for range.
+ */
+@RequiredArgsConstructor
+@Getter
+@ToString
+public final class RangeShardingConditionValue<T extends Comparable<?>>
implements ShardingConditionValue {
+
+ private final String columnName;
+
+ private final String tableName;
+
+ private final Range<T> valueRange;
+
+ private final List<Integer> parameterMarkerIndexes;
+
+ public RangeShardingConditionValue(final String columnName, final String
tableName, final Range<T> valueRange) {
+ this(columnName, tableName, valueRange, Collections.emptyList());
+ }
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/ShardingConditionValue.java
b/wasm/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/ShardingConditionValue.java
new file mode 100644
index 0000000..15cbe9a
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/ShardingConditionValue.java
@@ -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.sharding.route.engine.condition.value;
+
+import java.util.List;
+
+/**
+ * Sharding condition value.
+ */
+public interface ShardingConditionValue {
+
+ /**
+ * Get column name.
+ *
+ * @return column name
+ */
+ String getColumnName();
+
+ /**
+ * Get table name.
+ *
+ * @return table name
+ */
+ String getTableName();
+
+ /**
+ * Get parameter marker indexes.
+ *
+ * @return parameter marker indexes
+ */
+ List<Integer> getParameterMarkerIndexes();
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/sharding/route/strategy/ShardingStrategy.java
b/wasm/src/main/java/org/apache/shardingsphere/sharding/route/strategy/ShardingStrategy.java
new file mode 100644
index 0000000..8940770
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/sharding/route/strategy/ShardingStrategy.java
@@ -0,0 +1,56 @@
+/*
+ * 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.sharding.route.strategy;
+
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.datanode.DataNodeInfo;
+import
org.apache.shardingsphere.sharding.route.engine.condition.value.ShardingConditionValue;
+import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
+
+import java.util.Collection;
+
+/**
+ * Sharding strategy.
+ */
+public interface ShardingStrategy {
+
+ /**
+ * Get sharding columns.
+ *
+ * @return sharding columns
+ */
+ Collection<String> getShardingColumns();
+
+ /**
+ * Get sharding algorithm.
+ *
+ * @return sharding algorithm
+ */
+ ShardingAlgorithm getShardingAlgorithm();
+
+ /**
+ * Sharding.
+ *
+ * @param availableTargetNames available data source or table names
+ * @param shardingConditionValues sharding condition values
+ * @param dataNodeInfo data node info
+ * @param props configuration properties
+ * @return sharding results for data source or table names
+ */
+ Collection<String> doSharding(Collection<String> availableTargetNames,
Collection<ShardingConditionValue> shardingConditionValues, DataNodeInfo
dataNodeInfo, ConfigurationProperties props);
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/sharding/route/strategy/type/standard/StandardShardingStrategy.java
b/wasm/src/main/java/org/apache/shardingsphere/sharding/route/strategy/type/standard/StandardShardingStrategy.java
new file mode 100644
index 0000000..afb4bd3
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/sharding/route/strategy/type/standard/StandardShardingStrategy.java
@@ -0,0 +1,86 @@
+/*
+ * 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.sharding.route.strategy.type.standard;
+
+import lombok.Getter;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.datanode.DataNodeInfo;
+import
org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
+import
org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
+import
org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
+import
org.apache.shardingsphere.sharding.route.engine.condition.value.ListShardingConditionValue;
+import
org.apache.shardingsphere.sharding.route.engine.condition.value.RangeShardingConditionValue;
+import
org.apache.shardingsphere.sharding.route.engine.condition.value.ShardingConditionValue;
+import org.apache.shardingsphere.sharding.route.strategy.ShardingStrategy;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.TreeSet;
+
+/**
+ * Standard sharding strategy.
+ */
+@Getter
+public final class StandardShardingStrategy implements ShardingStrategy {
+
+ private final Collection<String> shardingColumns;
+
+ private final StandardShardingAlgorithm<?> shardingAlgorithm;
+
+ public StandardShardingStrategy(final String shardingColumn, final
StandardShardingAlgorithm<?> shardingAlgorithm) {
+ Collection<String> shardingColumns = new
TreeSet<>(String.CASE_INSENSITIVE_ORDER);
+ shardingColumns.add(shardingColumn);
+ this.shardingColumns =
Collections.unmodifiableCollection(shardingColumns);
+ this.shardingAlgorithm = shardingAlgorithm;
+ }
+
+ @SuppressWarnings("rawtypes")
+ @Override
+ public Collection<String> doSharding(final Collection<String>
availableTargetNames, final Collection<ShardingConditionValue>
shardingConditionValues,
+ final DataNodeInfo dataNodeInfo,
final ConfigurationProperties props) {
+ ShardingConditionValue shardingConditionValue =
shardingConditionValues.iterator().next();
+ Collection<String> shardingResult = shardingConditionValue instanceof
ListShardingConditionValue
+ ? doSharding(availableTargetNames,
(ListShardingConditionValue) shardingConditionValue, dataNodeInfo)
+ : doSharding(availableTargetNames,
(RangeShardingConditionValue) shardingConditionValue, dataNodeInfo);
+ Collection<String> result = new
TreeSet<>(String.CASE_INSENSITIVE_ORDER);
+ result.addAll(shardingResult);
+ return result;
+ }
+
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ private Collection<String> doSharding(final Collection<String>
availableTargetNames, final ListShardingConditionValue<?> shardingValue, final
DataNodeInfo dataNodeInfo) {
+ Collection<String> result = new LinkedList<>();
+ for (Comparable<?> each : shardingValue.getValues()) {
+ String target = shardingAlgorithm.doSharding(availableTargetNames,
+ new PreciseShardingValue(shardingValue.getTableName(),
shardingValue.getColumnName(), dataNodeInfo, each));
+ if (null != target && availableTargetNames.contains(target)) {
+ result.add(target);
+ } else if (null != target &&
!availableTargetNames.contains(target)) {
+ throw new RuntimeException();
+ }
+ }
+ return result;
+ }
+
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ private Collection<String> doSharding(final Collection<String>
availableTargetNames, final RangeShardingConditionValue<?> shardingValue, final
DataNodeInfo dataNodeInfo) {
+ return shardingAlgorithm.doSharding(availableTargetNames,
+ new RangeShardingValue(shardingValue.getTableName(),
shardingValue.getColumnName(), dataNodeInfo, shardingValue.getValueRange()));
+ }
+}
diff --git
a/wasm/src/main/java/org/apache/shardingsphere/sharding/spi/ShardingAlgorithm.java
b/wasm/src/main/java/org/apache/shardingsphere/sharding/spi/ShardingAlgorithm.java
new file mode 100644
index 0000000..3d92cda
--- /dev/null
+++
b/wasm/src/main/java/org/apache/shardingsphere/sharding/spi/ShardingAlgorithm.java
@@ -0,0 +1,26 @@
+/*
+ * 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.sharding.spi;
+
+import org.apache.shardingsphere.infra.algorithm.ShardingSphereAlgorithm;
+
+/**
+ * Sharding algorithm.
+ */
+public interface ShardingAlgorithm extends ShardingSphereAlgorithm {
+}
diff --git a/wasm/wasm-sharding/Cargo.lock b/wasm/wasm-sharding/Cargo.lock
new file mode 100644
index 0000000..d42aff3
--- /dev/null
+++ b/wasm/wasm-sharding/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "wasm-sharding"
+version = "0.1.0"
diff --git a/wasm/wasm-sharding/Cargo.toml b/wasm/wasm-sharding/Cargo.toml
new file mode 100644
index 0000000..d9e4bb6
--- /dev/null
+++ b/wasm/wasm-sharding/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "wasm-sharding"
+version = "0.1.0"
+edition = "2021"
+
+[lib]
+crate-type = ['cdylib']
+
+[dependencies]
\ No newline at end of file
diff --git a/wasm/wasm-sharding/src/lib.rs b/wasm/wasm-sharding/src/lib.rs
new file mode 100644
index 0000000..ba2149f
--- /dev/null
+++ b/wasm/wasm-sharding/src/lib.rs
@@ -0,0 +1,45 @@
+//
+// 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.
+//
+
+#[link(wasm_import_module = "sharding")]
+extern "C" {
+ fn poll_table(addr: i64, len: i32) -> i32;
+}
+
+// The value of sharding_count must be consistent with the value of the
AvaliableTargetNames
+const SHARDING_COUNT: u8 = 3;
+
+#[no_mangle]
+pub unsafe extern "C" fn do_work() -> i64 {
+ let mut buf = [0u8; 1024];
+ let buf_ptr = buf.as_mut_ptr() as i64;
+ let len = poll_table(buf_ptr, buf.len() as i32);
+ let parts = std::slice::from_raw_parts(buf_ptr as *const u8, len as usize);
+ let target_names_length = parts[0] as usize;
+ //let raw_str = String::from_utf8_lossy(&parts[1..1+ target_names_length]);
+ //let target_names = raw_str.split(',').collect::<Vec<&str>>();
+
+ let condition_length = parts[1+ target_names_length] as usize;
+ let raw_str = String::from_utf8_lossy(&parts[2 + target_names_length .. 2
+ target_names_length + condition_length]);
+ let condition_values = raw_str.split(',').collect::<Vec<&str>>();
+ let column_value = condition_values[2].parse::<u8>().unwrap();
+ let sharding = column_value % SHARDING_COUNT;
+ let mut table_name = format!("{}_{}", condition_values[1], sharding);
+ //buf_slice.append(table_name.as_mut_vec());
+ std::ptr::copy_nonoverlapping(table_name.as_mut_ptr() as *const _,
buf.as_mut_ptr().add(len as usize), table_name.len());
+ buf_ptr
+}