You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2020/04/27 06:04:34 UTC

[GitHub] [shardingsphere] tristaZero commented on a change in pull request #5327: create RANGE sharding algorithm

tristaZero commented on a change in pull request #5327:
URL: https://github.com/apache/shardingsphere/pull/5327#discussion_r415533641



##########
File path: sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/strategy/algorithm/sharding/RangeShardingAlgorithm.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.core.strategy.algorithm.sharding;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Range;
+import com.google.common.primitives.Longs;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
+import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;
+import org.apache.shardingsphere.api.sharding.standard.StandardShardingAlgorithm;
+
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+/**
+ * Range sharding algorithm.
+ * <p>
+ * Range sharding algorithm is similar to the rule of partition table.
+ * User can specify the range by setting the `range.partition.split.value` parameter.
+ * The `range.partition.split.value` parameter is an ordered list of numbers, separated by commas.
+ * </p>
+ * <p>
+ * For example: If the `range.partition.split.value` parameter is set to `1,5,10`,
+ * the parameter will split all values into four intervals——(-∞, 1), [1,5), [5,10), [10, +∞),
+ * which corresponding to partition_0, partition_1, partition_2, partition_3.
+ * The sharding values will be divided into different partition by its value.
+ * </p>
+ */
+public final class RangeShardingAlgorithm implements StandardShardingAlgorithm<Long> {
+
+    private static final String RANGE_PARTITION_SPLIT_VALUE = "range.partition.split.value";
+
+    private Properties properties = new Properties();
+
+    @Override
+    public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Long> shardingValue) {
+        Preconditions.checkNotNull(properties.get(RANGE_PARTITION_SPLIT_VALUE), "Range sharding algorithm range partition split value cannot be null.");
+        Map<Integer, Range<Long>> partitionRangeMap = getPartitionRangeMap();
+        for (String each : availableTargetNames) {
+            if (each.endsWith(getPartition(partitionRangeMap, shardingValue.getValue()))) {
+                return each;
+            }
+        }
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public Collection<String> doSharding(final Collection<String> availableTargetNames, final RangeShardingValue<Long> shardingValue) {
+        Preconditions.checkNotNull(properties.get(RANGE_PARTITION_SPLIT_VALUE), "Range sharding algorithm range partition split value cannot be null.");
+        Map<Integer, Range<Long>> partitionRangeMap = getPartitionRangeMap();

Review comment:
       @SanmuWangZJU Thanks. :)




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org