use of io.mycat.config.ShardingTableConfig in project Mycat2 by MyCATApache.
the class YYYYDDFunctionTest method testYYYYDDHashId.
@Test
public void testYYYYDDHashId() {
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 YYYYDD(traveldate) tbpartition by mod_hash(id) tbpartitions 8 dbpartitions 366;");
mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"366\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${tableIndex}\",\n" + "\t\t\t\t\t\"tableNum\":\"2\",\n" + "\t\t\t\t\t\"tableMethod\":\"hash(id)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"YYYYDD(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_157', tableName='sharding_0', index=314, dbIndex=157, tableIndex=0}, {targetName='c0', schemaName='db1_157', tableName='sharding_1', index=315, dbIndex=157, tableIndex=1}]"));
Assert.assertEquals(732, shardingFuntion.calculate(Collections.emptyMap()).size());
System.out.println();
}
use of io.mycat.config.ShardingTableConfig in project Mycat2 by MyCATApache.
the class HashFunctionTest method testHashTableId.
@Test
public void testHashTableId() {
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" + " tbpartition by mod_hash(id) tbpartitions 2;");
mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${tableIndex}\",\n" + "\t\t\t\t\t\"tableNum\":\"2\",\n" + "\t\t\t\t\t\"tableMethod\":\"hash(id)\",\n" + "\t\t\t\t\t\"storeNum\":1,\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("id", (new RangeVariable("id", RangeVariableType.EQUAL, 15))));
Assert.assertEquals(1, calculate.size());
Assert.assertEquals(2, shardingFuntion.calculate(Collections.emptyMap()).size());
System.out.println();
}
use of io.mycat.config.ShardingTableConfig in project Mycat2 by MyCATApache.
the class HashFunctionTest method testHashDbId.
@Test
public void testHashDbId() {
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);");
mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"2\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${tableIndex}\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"hash(id)\"\n" + "\t\t\t\t}", Map.class)).build());
MetadataManager metadataManager = getMetadataManager(mainSharding);
ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
String s1 = shardingFuntion.calculate(Collections.emptyMap()).toString();
Assert.assertEquals("[{targetName='c0', schemaName='db1_0', tableName='sharding_0', index=0, dbIndex=0, tableIndex=0}, {targetName='c0', schemaName='db1_1', tableName='sharding_0', index=1, dbIndex=1, tableIndex=0}]", s1);
System.out.println();
}
use of io.mycat.config.ShardingTableConfig in project Mycat2 by MyCATApache.
the class ShardingTableConfigVO method save.
public void save() {
try {
String schemaName = this.schemaName.getText();
String tableName = this.tableName.getText();
Objects.requireNonNull(schemaName, "schemaName must not be null");
Objects.requireNonNull(tableName, "tableName must not be null");
ShardingTableConfig shardingTableConfig = getShardingTableConfig();
controller.save(schemaName, tableName, validate(shardingTableConfig));
controller.flashSchemas();
} catch (Exception e) {
popAlter(e);
}
}
Aggregations