一、前言
关于taobao的tddl-rule(5.0.0)源码包中定义目标数据库com.taobao.tddl.rule.model.TargetDB类、及其属性字段映射Field类,实现将目标数据库及其所有对应字段存储到内存中,用于ORM等相关应用场景,详情源码说明部分。
二、源码说明
1. TargetDB目标书库类
package com.taobao.tddl.rule.model;@b@@b@import com.taobao.tddl.common.utils.TddlToStringStyle;@b@import java.util.Collection;@b@import java.util.HashMap;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@import java.util.Set;@b@import org.apache.commons.lang.builder.ToStringBuilder;@b@@b@public class TargetDB@b@{@b@ private String dbIndex;@b@ private Map<String, Field> tableNames;@b@@b@ public Set<String> getTableNames()@b@ {@b@ if (this.tableNames == null)@b@ return null;@b@@b@ return this.tableNames.keySet();@b@ }@b@@b@ public void setTableNames(Map<String, Field> tableNames) {@b@ this.tableNames = tableNames;@b@ }@b@@b@ public Map<String, Field> getTableNameMap() {@b@ return this.tableNames;@b@ }@b@@b@ public void addOneTable(String table) {@b@ if (this.tableNames == null)@b@ this.tableNames = new HashMap();@b@@b@ this.tableNames.put(table, Field.EMPTY_FIELD);@b@ }@b@@b@ public void addOneTable(String table, Field field) {@b@ if (this.tableNames == null)@b@ this.tableNames = new HashMap();@b@@b@ this.tableNames.put(table, field);@b@ }@b@@b@ public void addOneTableWithSameTable(String table, Field field) {@b@ Field inField;@b@ if (this.tableNames == null) {@b@ this.tableNames = new HashMap();@b@ this.tableNames.put(table, field);@b@ } else {@b@ inField = (Field)this.tableNames.get(table);@b@ if (inField == null) {@b@ this.tableNames.put(table, field);@b@ }@b@ else if (field.getSourceKeys() != null)@b@ for (Map.Entry entry : field.getSourceKeys().entrySet())@b@ {@b@ ((Set)inField.getSourceKeys().get(entry.getKey())).addAll((Collection)entry.getValue());@b@ }@b@ }@b@ }@b@@b@ public String getDbIndex()@b@ {@b@ return this.dbIndex;@b@ }@b@@b@ public void setDbIndex(String dbIndex) {@b@ this.dbIndex = dbIndex;@b@ }@b@@b@ public String toString()@b@ {@b@ return ToStringBuilder.reflectionToString(this, TddlToStringStyle.DEFAULT_STYLE);@b@ }@b@}
2. 数据库字段Field类
package com.taobao.tddl.rule.model;@b@@b@import com.taobao.tddl.common.utils.TddlToStringStyle;@b@import java.util.HashMap;@b@import java.util.Iterator;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@import java.util.Set;@b@import org.apache.commons.lang.builder.ToStringBuilder;@b@@b@public class Field@b@{@b@ public static final Field EMPTY_FIELD = new Field(0);@b@ private Map<String, Set<Object>> sourceKeys;@b@@b@ public Field(int capacity)@b@ {@b@ this.sourceKeys = new HashMap(capacity);@b@ }@b@@b@ public boolean equals(Object obj, Map<String, String> alias)@b@ {@b@ Set targetValueSet;@b@ Iterator i$;@b@ if (!(obj instanceof Field))@b@ return false;@b@@b@ Map target = ((Field)obj).sourceKeys;@b@ for (Map.Entry entry : this.sourceKeys.entrySet()) {@b@ String srcKey = (String)entry.getKey();@b@ if (alias.containsKey(srcKey))@b@ srcKey = (String)alias.get(srcKey);@b@@b@ targetValueSet = (Set)target.get(srcKey);@b@ Set sourceValueSet = (Set)entry.getValue();@b@ for (i$ = sourceValueSet.iterator(); i$.hasNext(); ) { Object srcValue = i$.next();@b@ boolean eq = false;@b@ for (Iterator i$ = targetValueSet.iterator(); i$.hasNext(); ) { Object tarValue = i$.next();@b@ if (tarValue.equals(srcValue))@b@ eq = true;@b@ }@b@@b@ if (!(eq))@b@ return false;@b@ }@b@ }@b@@b@ return true;@b@ }@b@@b@ public Map<String, Set<Object>> getSourceKeys() {@b@ return this.sourceKeys;@b@ }@b@@b@ public void setSourceKeys(Map<String, Set<Object>> sourceKeys) {@b@ this.sourceKeys = sourceKeys;@b@ }@b@@b@ public String toString()@b@ {@b@ return ToStringBuilder.reflectionToString(this, TddlToStringStyle.DEFAULT_STYLE);@b@ }@b@}