/** * 根据输入的path地址获取含有所有dbrule和tableRule的Map. 只在初始化的时候调用一次. * * @param path 路径名系统内采用 getClass().getResourceAsStream(path);的方式获取对应的stream资源 * @return */ @SuppressWarnings("unchecked") public synchronized void initLogicTabMarticMap(String path, Map<String, LogicTabMatrix> map) { Document doc = getDocument(path); Element ele = doc.getRootElement(); String dbType = trim(ele.attributeValue("dbType")); Iterator dbTabItr = ele.elementIterator("table"); int i = 1; while (dbTabItr.hasNext()) { Element aVtab = (Element) dbTabItr.next(); LogicTabMatrix aVtabMatrix = new LogicTabMatrix(); // globalTableRule,如果有则储存起来 Element globeRule = aVtab.element("globalTableRule"); if (globeRule != null) { TabRule globTabRule = this.getTabRule(globeRule); aVtabMatrix.setGlobalTableRule(globTabRule); } String logicName = validAndTrim(aVtab.attributeValue("logicName"), "无法找到第" + i + "个虚拟表的logicName"); // martrix里面的TableName是返还给前端用于表名替换的,因此要保持原样 aVtabMatrix.setTableName(logicName); String needRowCopy = trim(aVtab.attributeValue("rowCopy")); if (needRowCopy != null && !needRowCopy.equals("")) { aVtabMatrix.setNeedRowCopy(Boolean.valueOf(needRowCopy)); } String reverseOutput = trim(aVtab.attributeValue("reverseOutput")); if (reverseOutput != null && !reverseOutput.equals("")) { aVtabMatrix.setAllowReverseOutput(Boolean.valueOf(reverseOutput)); } aVtabMatrix.setDBType(dbType); String tabFactor = trim(aVtab.elementText("tableFactor")); aVtabMatrix.setTableFactor(tabFactor); Element dbRules = (Element) aVtab.element("dbRules"); Iterator rulesItr = dbRules.elementIterator("dbRule"); Map<String, DBRule> ruleMap = getRuleList(rulesItr, aVtabMatrix); // 当一个Rule对象有expression的时候放在depositedRule和allRule // 如果没有expressionString串,则只放在allRule待选 Map<String, DBRule> depositedRules = getDepositedRule(ruleMap); aVtabMatrix.setAllRules(ruleMap); aVtabMatrix.setDepositedRules(depositedRules); List<DBRule> defaultRuleList = getDefaultRuleList(aVtab, aVtabMatrix); aVtabMatrix.setDefaultRules(defaultRuleList); map.put(logicName.toLowerCase(), aVtabMatrix); i++; } }
/** * 解析每一个rule * * @param rulesItr * @return */ @SuppressWarnings("unchecked") private Map<String, DBRule> getRuleList(Iterator rulesItr, LogicTabMatrix aVtabMatrix) { Map<String, DBRule> rules = new HashMap<String, DBRule>(); while (rulesItr.hasNext()) { Element ruleEle = (Element) rulesItr.next(); String id = validAndTrim(ruleEle.attributeValue("id"), "必须指定Rule的id"); DBRule rule = new DBRule(); String exp = trim(ruleEle.elementText("expression")); rule.setExpression(exp); String primaryKey = trim(ruleEle.elementText("primaryKey")); rule.setPrimaryKey(primaryKey); String primaryKeyExp = trim(ruleEle.elementText("primaryKeyExp")); rule.setPrimaryKeyExp(primaryKeyExp); String parameters = trim(ruleEle.elementText("parameters")); rule.setParameters(parameters); String readPoolStr = ruleEle.elementText("readPools"); String writePoolStr = ruleEle.elementText("writePools"); if (readPoolStr == null || writePoolStr == null || readPoolStr.trim().equals("") || writePoolStr.trim().equals("")) { throw new TDLRunTimeException( "readPool和writePool必须同时指名" + "readPool可以和writePool同名,但writePool不能为多个,也不能为虚拟池"); } String[] readPools = readPoolStr.trim().split(","); String[] writePools = writePoolStr.trim().split(","); rule.setReadPool(readPools); rule.setWritePool(writePools); Element subTableRuleElement = ruleEle.element("tableRule"); if (subTableRuleElement != null) { TabRule tabRule = getTabRule(subTableRuleElement); tabRule.setPrimaryKey(primaryKey); rule.setDBSubTabRule(tabRule); log.debug("id:" + id + "的DBRule有subTableRule,因此使用subTableRule"); } // add by shenxun 现在的逻辑是直接替换subRuleEle,以后不会再 // 使用globalRule这个项目了,它只在载入的时候临时起作用 else { TabRule tabRule = aVtabMatrix.getGlobalTableRule(); if (tabRule != null) { tabRule.setPrimaryKey(primaryKey); } rule.setDBSubTabRule(tabRule); log.debug("id:" + id + "的DBRule没有subTableRule,因此使用globalTableRule"); } rules.put(id, rule); } return rules; }
/** * 根据DefaultPool字段(允许用“,”分隔),获取DBRule中含有指定写库的DBRule * * @param aVtab * @param aVtabMatrix * @param tempSet * @return */ private List<DBRule> getDefaultRuleList(Element aVtab, LogicTabMatrix aVtabMatrix) { List<DBRule> defaultList = new ArrayList<DBRule>(); String defaultPools = trim(aVtab.attributeValue("defaultWritePool")); if (defaultPools != null) { String[] defaultPoolsStrArray = defaultPools.split(","); Map<String, DBRule> map = aVtabMatrix.getAllRules(); for (String str : defaultPoolsStrArray) { DBRule dbrule = map.get(str.trim()); if (dbrule != null) { defaultList.add(dbrule); } else { throw new IllegalArgumentException( "defaultRule中id为:" + str + " 的字段不能找到" + "一个对应的规则,请确认该id对应一个dbRule的id参数"); } } } return defaultList; }