PartitionTable extends PartitionTableMetaData { public List<TargetTableEntity> execute(CalculateUnit calculateUnit) { Set<ConditionUnit> conditionUnits = calculateUnit.getTablesAndConditions().get(getLogicTableName()); if (conditionUnits == null || conditionUnits.size() == 0){ return null; } List<TargetTableEntity> targetList = new ArrayList<TargetTableEntity>(1); for (PartitionColumn column : columns){ ConditionUnit matchingConditon = findColumnFromConditions(conditionUnits, column.getName()); if (matchingConditon != null){ if (matchingConditon.getOperator() == ConditionUnitOperator.EQUAL){ executeRule(targetList, rule.getFunction(), column, matchingConditon.getValues().get(0), conditionUnits); }else if (matchingConditon.getOperator() == ConditionUnitOperator.IN){ for (Object obj : matchingConditon.getValues()){ executeRule(targetList, rule.getFunction(), column, obj, conditionUnits); } } } if (targetList.size() != 0){ break; } } return targetList; }  List<TargetTableEntity> execute(CalculateUnit calculateUnit); List<TargetTableEntity> getAllTableNames(); @Override String toString();  }
@Test public void testExecute() throws Exception { PartitionTable table = new TableBuilder().appenTable("trade_order", "trade_order_{0}", "value % 4") .appendNodeMapping("p0:0,1,2,3") .appendColumn("user_id", null).toTable(); CalculateUnit unit = new CalculateUnit(); unit.addCondition(ConditionUnit.buildConditionUnit("trade_order", "user_id", new String[]{"4"}, ConditionUnitOperator.EQUAL)); List<TargetTableEntity> target = table.execute(unit); System.out.println(target); Assert.assertEquals("p0", target.get(0).getTargetDB()); Assert.assertEquals("trade_order_0", target.get(0).getTargetTable()); }
@Test public void testExecute_0() throws Exception { PartitionTable table = new TableBuilder() .appenTable("trade_order", "trade_order_{0}", "value % 4") .appendNodeMapping("p0:0,1") .appendNodeMapping("p1:2,3") .appendColumn("user_id", null) .toTable(); CalculateUnit unit = new CalculateUnit(); unit.addCondition(ConditionUnit.buildConditionUnit("trade_order", "user_id", new String[]{"1","2"}, ConditionUnitOperator.IN)); List<TargetTableEntity> target = table.execute(unit); System.out.println(target); Assert.assertEquals("p0", target.get(0).getTargetDB()); Assert.assertEquals("trade_order_1", target.get(0).getTargetTable()); Assert.assertEquals("p1", target.get(1).getTargetDB()); Assert.assertEquals("trade_order_2", target.get(1).getTargetTable()); }
@Test public void testExecute_1() throws Exception { PartitionTable table = new TableBuilder() .appenTable("trade_order", "trade_order_{0}", "user_id % 4") .appendNodeMapping("p0:0,1,2,3") .appendColumn("user_id", null) .toTable(); CalculateUnit unit = new CalculateUnit(); unit.addCondition(ConditionUnit.buildConditionUnit("trade_order", "user_id", new String[]{"1"}, ConditionUnitOperator.EQUAL)); unit.addCondition(ConditionUnit.buildConditionUnit("trade_order", "user_id", new String[]{"2"}, ConditionUnitOperator.EQUAL)); try{ List<TargetTableEntity> target = table.execute(unit); }catch (BayMaxException e){ System.out.print(e); Assert.assertEquals("有多个相同列的Condition : user_id:[2]", e.getMessage()); } }
@Test public void testExecute_2() throws Exception { PartitionTable table = new TableBuilder() .appenTable("trade_order", "trade_order_{0}", "value % 4") .appendNodeMapping("p0:0,1,2,3") .appendColumn("id", null) .appendColumn("user_id", null) .toTable(); CalculateUnit unit = new CalculateUnit(); unit.addCondition(ConditionUnit.buildConditionUnit("trade_order", "id", new String[]{"2"}, ConditionUnitOperator.EQUAL)); unit.addCondition(ConditionUnit.buildConditionUnit("trade_order", "user_id", new String[]{"1"}, ConditionUnitOperator.EQUAL)); try{ List<TargetTableEntity> target = table.execute(unit); System.out.println(target); }catch (BayMaxException e){ Assert.assertEquals("有多个相同列的Condition : user_id:[2]", e.getMessage()); } }