use of io.mycat.RangeVariable in project Mycat2 by MyCATApache.
the class YYYYWEEKFunctionTest method testYYYYWEEK.
/**
* https://help.aliyun.com/document_detail/71334.html
*/
@Test
public void testYYYYWEEK() {
ShardingTableConfig mainSharding = new ShardingTableConfig();
mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" + " `id` bigint NOT NULL AUTO_INCREMENT,\n" + " `user_id` varchar(100) DEFAULT NULL,\n" + " `traveldate` date DEFAULT NULL,\n" + " `fee` decimal(10,0) DEFAULT NULL,\n" + " `days` int DEFAULT NULL,\n" + " `blob` longblob,\n" + " PRIMARY KEY (`id`),\n" + " KEY `id` (`id`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8" + " dbpartition by YYYYWEEK(traveldate) tbpartition by YYYYWEEK(traveldate) tbpartitions 3 dbpartitions 8;");
mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"8\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${index}\",\n" + "\t\t\t\t\t\"tableNum\":\"14\",\n" + "\t\t\t\t\t\"tableMethod\":\"YYYYWEEK(traveldate)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"YYYYWEEK(traveldate)\"\n" + "\t\t\t\t}", Map.class)).build());
MetadataManager metadataManager = getMetadataManager(mainSharding);
ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
List<Partition> calculate = shardingFuntion.calculate(Collections.singletonMap("traveldate", (new RangeVariable("traveldate", RangeVariableType.EQUAL, LocalDate.of(2020, 6, 5)))));
String s = calculate.toString();
Assert.assertTrue(s.contains("[{targetName='c0', schemaName='db1_1', tableName='sharding_15', index=15, dbIndex=1, tableIndex=1}]"));
Assert.assertEquals(112, shardingFuntion.calculate(Collections.emptyMap()).size());
System.out.println();
}
use of io.mycat.RangeVariable in project Mycat2 by MyCATApache.
the class MergeSubTablesFunction method calculate.
@Override
public List<Partition> calculate(Map<String, RangeVariable> values) {
ArrayList<Partition> res = new ArrayList<>();
for (RangeVariable rangeVariable : values.values()) {
// 匹配字段名
if (getColumnName().equalsIgnoreCase(rangeVariable.getColumnName())) {
// /////////////////////////////////////////////////////////////
String begin = Objects.toString(rangeVariable.getBegin());
String end = Objects.toString(rangeVariable.getEnd());
switch(rangeVariable.getOperator()) {
case EQUAL:
{
Partition partition = this.calculate(begin);
if (partition != null) {
CollectionUtil.setOpAdd(res, partition);
} else {
return getTable().dataNodes();
}
break;
}
case RANGE:
{
List<Partition> partitions = this.calculateRange(begin, end);
if (partitions == null || partitions.size() == 0) {
return getTable().dataNodes();
}
CollectionUtil.setOpAdd(res, partitions);
break;
}
default:
continue;
}
}
}
return res.isEmpty() ? getTable().dataNodes() : res;
}
use of io.mycat.RangeVariable in project Mycat2 by MyCATApache.
the class YYYYMMFunctionTest method testYYYYMM.
/**
* https://help.aliyun.com/document_detail/71334.html
*/
@Test
public void testYYYYMM() {
ShardingTableConfig mainSharding = new ShardingTableConfig();
mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" + " `id` bigint NOT NULL AUTO_INCREMENT,\n" + " `user_id` varchar(100) DEFAULT NULL,\n" + " `traveldate` date DEFAULT NULL,\n" + " `fee` decimal(10,0) DEFAULT NULL,\n" + " `days` int DEFAULT NULL,\n" + " `blob` longblob,\n" + " PRIMARY KEY (`id`),\n" + " KEY `id` (`id`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8" + " dbpartition by YYYYMM(traveldate) tbpartition by YYYYMM(traveldate) tbpartitions 3 dbpartitions 8;");
mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"8\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${index}\",\n" + "\t\t\t\t\t\"tableNum\":\"3\",\n" + "\t\t\t\t\t\"tableMethod\":\"YYYYMM(traveldate)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"YYYYMM(traveldate)\"\n" + "\t\t\t\t}", Map.class)).build());
MetadataManager metadataManager = getMetadataManager(mainSharding);
ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
List<Partition> calculate = shardingFuntion.calculate(Collections.singletonMap("traveldate", (new RangeVariable("traveldate", RangeVariableType.EQUAL, LocalDate.of(2020, 6, 5)))));
String s = calculate.toString();
Assert.assertTrue(s.contains("[{targetName='c0', schemaName='db1_2', tableName='sharding_6', index=6, dbIndex=2, tableIndex=0}]"));
Assert.assertEquals(24, shardingFuntion.calculate(Collections.emptyMap()).size());
System.out.println();
}
use of io.mycat.RangeVariable in project Mycat2 by MyCATApache.
the class YYYYMMFunctionTest method testYYYYMMHash.
@Test
public void testYYYYMMHash() {
ShardingTableConfig mainSharding = new ShardingTableConfig();
mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" + " `id` bigint NOT NULL AUTO_INCREMENT,\n" + " `user_id` varchar(100) DEFAULT NULL,\n" + " `traveldate` date DEFAULT NULL,\n" + " `fee` decimal(10,0) DEFAULT NULL,\n" + " `days` int DEFAULT NULL,\n" + " `blob` longblob,\n" + " PRIMARY KEY (`id`),\n" + " KEY `id` (`id`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8" + " dbpartition by mod_hash(id) tbpartition by YYYYMM(traveldate) tbpartitions 3 dbpartitions 24;");
mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"24\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${tableIndex}\",\n" + "\t\t\t\t\t\"tableNum\":\"3\",\n" + "\t\t\t\t\t\"tableMethod\":\"HASH(id)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"YYYYMM(traveldate)\"\n" + "\t\t\t\t}", Map.class)).build());
MetadataManager metadataManager = getMetadataManager(mainSharding);
ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
List<Partition> calculate = shardingFuntion.calculate(Collections.singletonMap("traveldate", (new RangeVariable("traveldate", RangeVariableType.EQUAL, LocalDate.of(2020, 6, 5)))));
String s = calculate.toString();
Assert.assertTrue(s.contains("[{targetName='c0', schemaName='db1_6', tableName='sharding_0', index=18, dbIndex=6, tableIndex=0}, {targetName='c0', schemaName='db1_6', tableName='sharding_1', index=19, dbIndex=6, tableIndex=1}, {targetName='c0', schemaName='db1_6', tableName='sharding_2', index=20, dbIndex=6, tableIndex=2}]"));
Assert.assertEquals(72, shardingFuntion.calculate(Collections.emptyMap()).size());
System.out.println();
}
use of io.mycat.RangeVariable in project Mycat2 by MyCATApache.
the class Mycat1xSingleValueRuleFunction method calculate.
@Override
public List<Partition> calculate(Map<String, RangeVariable> values) {
ArrayList<Partition> res = new ArrayList<>();
for (RangeVariable rangeVariable : values.values()) {
// 匹配字段名
if (getColumnName().equalsIgnoreCase(rangeVariable.getColumnName())) {
// /////////////////////////////////////////////////////////////
String begin = Objects.toString(rangeVariable.getBegin());
String end = Objects.toString(rangeVariable.getEnd());
switch(rangeVariable.getOperator()) {
case EQUAL:
{
Partition partition = this.calculate(begin);
if (partition != null) {
CollectionUtil.setOpAdd(res, partition);
} else {
return getTable().dataNodes();
}
break;
}
case RANGE:
{
List<Partition> partitions = this.calculateRange(begin, end);
if (partitions == null || partitions.size() == 0) {
return getTable().dataNodes();
}
CollectionUtil.setOpAdd(res, partitions);
break;
}
default:
continue;
}
}
}
return res.isEmpty() ? getTable().dataNodes() : res;
}
Aggregations