use of io.mycat.config.model.rule.RuleConfig in project Mycat-Server by MyCATApache.
the class RouterUtil method ruleCalculate.
/**
* @return dataNodeIndex -> [partitionKeysValueTuple+]
*/
public static Set<String> ruleCalculate(TableConfig tc, Set<ColumnRoutePair> colRoutePairSet, Map<String, Integer> dataNodeSlotMap) {
Set<String> routeNodeSet = new LinkedHashSet<String>();
String col = tc.getRule().getColumn();
RuleConfig rule = tc.getRule();
AbstractPartitionAlgorithm algorithm = rule.getRuleAlgorithm();
for (ColumnRoutePair colPair : colRoutePairSet) {
if (colPair.colValue != null) {
Integer nodeIndx = algorithm.calculate(colPair.colValue);
if (nodeIndx == null) {
throw new IllegalArgumentException("can't find datanode for sharding column:" + col + " val:" + colPair.colValue);
} else {
String dataNode = tc.getDataNodes().get(nodeIndx);
routeNodeSet.add(dataNode);
if (algorithm instanceof SlotFunction) {
dataNodeSlotMap.put(dataNode, ((SlotFunction) algorithm).slotValue());
}
colPair.setNodeId(nodeIndx);
}
} else if (colPair.rangeValue != null) {
Integer[] nodeRange = algorithm.calculateRange(String.valueOf(colPair.rangeValue.beginValue), String.valueOf(colPair.rangeValue.endValue));
if (nodeRange != null) {
/**
* 不能确认 colPair的 nodeid是否会有其它影响
*/
if (nodeRange.length == 0) {
routeNodeSet.addAll(tc.getDataNodes());
} else {
ArrayList<String> dataNodes = tc.getDataNodes();
String dataNode = null;
for (Integer nodeId : nodeRange) {
dataNode = dataNodes.get(nodeId);
if (algorithm instanceof SlotFunction) {
dataNodeSlotMap.put(dataNode, ((SlotFunction) algorithm).slotValue());
}
routeNodeSet.add(dataNode);
}
}
}
}
}
return routeNodeSet;
}
use of io.mycat.config.model.rule.RuleConfig in project Mycat-Server by MyCATApache.
the class RuleFunctionSuitTableTest method testPartitionByDate.
@Test
public void testPartitionByDate() {
PartitionByDate partition = new PartitionByDate();
partition.setDateFormat("yyyy-MM-dd");
partition.setsBeginDate("2014-01-01");
partition.setsEndDate("2014-01-31");
partition.setsPartionDay("10");
// partition = 4
partition.init();
Assert.assertEquals(4, partition.getPartitionNum());
RuleConfig rule = new RuleConfig("col_date", "partition-date");
rule.setRuleAlgorithm(partition);
TableConfig tableConf = new TableConfig("test", "id", true, false, -1, "dn1,dn2,dn3", null, rule, true, null, false, null, null, null);
int suit1 = partition.suitableFor(tableConf);
Assert.assertEquals(-1, suit1);
tableConf.getDataNodes().clear();
tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2", "dn3", "dn4"));
int suit2 = partition.suitableFor(tableConf);
Assert.assertEquals(0, suit2);
tableConf.getDataNodes().clear();
tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2", "dn3", "dn4", "dn5"));
int suit3 = partition.suitableFor(tableConf);
Assert.assertEquals(1, suit3);
PartitionByDate partition1 = new PartitionByDate();
partition.setDateFormat("yyyy-MM-dd");
partition.setsBeginDate("2014-01-01");
partition.setsPartionDay("10");
// partition no limit
partition.init();
int suit4 = partition1.suitableFor(tableConf);
Assert.assertEquals(0, suit4);
}
use of io.mycat.config.model.rule.RuleConfig in project Mycat-Server by MyCATApache.
the class RuleFunctionSuitTableTest method testPartitionByHashMod.
@Test
public void testPartitionByHashMod() {
PartitionByHashMod partition = new PartitionByHashMod();
// partition = 3;
partition.setCount(3);
Assert.assertEquals(3, partition.getPartitionNum());
RuleConfig rule = new RuleConfig("id", "partition-hash-mod");
rule.setRuleAlgorithm(partition);
TableConfig tableConf = new TableConfig("test", "id", true, false, -1, "dn1,dn2,dn3", null, rule, true, null, false, null, null, null);
int suit1 = partition.suitableFor(tableConf);
Assert.assertEquals(0, suit1);
tableConf.getDataNodes().clear();
tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2", "dn3", "dn4"));
int suit2 = partition.suitableFor(tableConf);
Assert.assertEquals(1, suit2);
tableConf.getDataNodes().clear();
tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2"));
int suit3 = partition.suitableFor(tableConf);
Assert.assertEquals(-1, suit3);
}
Aggregations