Skip to content

Commit

Permalink
rule修改为lazy init
Browse files Browse the repository at this point in the history
  • Loading branch information
agapple committed Jan 2, 2014
1 parent c76237a commit 2dc9f68
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions tddl-qatest/src/test/resources/client/mysql_rule.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<bean id="vtabroot" class="com.taobao.tddl.interact.rule.VirtualTableRoot"
init-method="init">
<property name="dbType" value="MYSQL" />
<property name="lazyInit" value="true" />
<property name="tableRules">
<map>
<!-- ===========================================mysql============================================= -->
Expand Down
45 changes: 33 additions & 12 deletions tddl-rule/src/main/java/com/taobao/tddl/rule/VirtualTableRoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.taobao.tddl.common.model.DBType;
import com.taobao.tddl.common.model.lifecycle.AbstractLifecycle;
import com.taobao.tddl.common.model.lifecycle.Lifecycle;
import com.taobao.tddl.rule.exceptions.TddlRuleException;
import com.taobao.tddl.rule.utils.RuleUtils;

import com.taobao.tddl.common.utils.logger.Logger;
Expand All @@ -29,21 +30,13 @@ public class VirtualTableRoot extends AbstractLifecycle implements Lifecycle {
protected String defaultDbIndex;
protected boolean needIdInGroup = false;
protected boolean completeDistinct = false;
protected boolean lazyInit = false;

public void init() throws TddlException {
for (Map.Entry<String, TableRule> entry : virtualTableMap.entrySet()) {
logger.warn("virtual table start to init :" + entry.getKey());
TableRule vtab = entry.getValue();
if (vtab.getDbType() == null) {
// 如果虚拟表中dbType为null,那指定全局dbType
vtab.setDbType(this.getDbTypeEnumObj());
if (!lazyInit) {
initTableRule(entry.getKey(), entry.getValue());
}

if (vtab.getVirtualTbName() == null) {
vtab.setVirtualTbName(entry.getKey());
}
vtab.init();
logger.warn("virtual table inited :" + entry.getKey());
}
}

Expand All @@ -55,7 +48,16 @@ public void init() throws TddlException {
*/
public TableRule getVirtualTable(String virtualTableName) {
RuleUtils.notNull(virtualTableName, "virtual table name is null");
return virtualTableMap.get(virtualTableName.toLowerCase());
TableRule tablRule = virtualTableMap.get(virtualTableName.toLowerCase());
if (lazyInit && !tablRule.isInited()) {
try {
initTableRule(virtualTableName.toLowerCase(), tablRule);
} catch (TddlException e) {
throw new TddlRuleException(e);
}
}

return tablRule;
}

public void setTableRules(Map<String, TableRule> virtualTableMap) {
Expand All @@ -74,6 +76,20 @@ public void setDbIndexMap(Map<String, String> dbIndexMap) {
this.dbIndexMap = lowerKeysDbIndexMap;
}

private void initTableRule(String tableNameKey, TableRule tableRule) throws TddlException {
logger.warn("virtual table start to init :" + tableNameKey);
if (tableRule.getDbType() == null) {
// 如果虚拟表中dbType为null,那指定全局dbType
tableRule.setDbType(this.getDbTypeEnumObj());
}

if (tableRule.getVirtualTbName() == null) {
tableRule.setVirtualTbName(tableNameKey);
}
tableRule.init();
logger.warn("virtual table inited :" + tableNameKey);
}

public Map<String, TableRule> getVirtualTableMap() {
return virtualTableMap;
}
Expand Down Expand Up @@ -125,4 +141,9 @@ public boolean isCompleteDistinct() {
public void setCompleteDistinct(boolean completeDistinct) {
this.completeDistinct = completeDistinct;
}

public void setLazyInit(boolean lazyInit) {
this.lazyInit = lazyInit;
}

}

0 comments on commit 2dc9f68

Please sign in to comment.