use of com.alibaba.cobar.config.model.rule.RuleAlgorithm in project cobar by alibaba.
the class ServerRouter method ruleCalculate.
/**
* @return dataNodeIndex -> [partitionKeysValueTuple+]
*/
private static Map<Integer, List<Object[]>> ruleCalculate(TableConfig matchedTable, RuleConfig rule, Map<String, List<Object>> columnValues) {
Map<Integer, List<Object[]>> map = new HashMap<Integer, List<Object[]>>(1, 1);
RuleAlgorithm algorithm = rule.getRuleAlgorithm();
List<String> cols = rule.getColumns();
Map<String, Object> parameter = new HashMap<String, Object>(cols.size(), 1);
ArrayList<Iterator<Object>> colsValIter = new ArrayList<Iterator<Object>>(columnValues.size());
for (String rc : cols) {
List<Object> list = columnValues.get(rc);
if (list == null) {
String msg = "route err: rule column " + rc + " dosn't exist in extract: " + columnValues;
throw new IllegalArgumentException(msg);
}
colsValIter.add(list.iterator());
}
try {
for (Iterator<Object> mainIter = colsValIter.get(0); mainIter.hasNext(); ) {
Object[] tuple = new Object[cols.size()];
for (int i = 0, len = cols.size(); i < len; ++i) {
Object value = colsValIter.get(i).next();
tuple[i] = value;
parameter.put(cols.get(i), value);
}
Integer[] dataNodeIndexes = calcDataNodeIndexesByFunction(algorithm, parameter);
for (int i = 0; i < dataNodeIndexes.length; ++i) {
Integer dataNodeIndex = dataNodeIndexes[i];
List<Object[]> list = map.get(dataNodeIndex);
if (list == null) {
list = new LinkedList<Object[]>();
map.put(dataNodeIndex, list);
}
list.add(tuple);
}
}
} catch (NoSuchElementException e) {
String msg = "route err: different rule columns should have same value number: " + columnValues;
throw new IllegalArgumentException(msg, e);
}
return map;
}
use of com.alibaba.cobar.config.model.rule.RuleAlgorithm in project cobar by alibaba.
the class RouteRuleInitializer method initRouteRule.
public static void initRouteRule(SchemaLoader loader) throws SQLSyntaxErrorException {
Map<String, RuleAlgorithm> functions = loader.getFunctions();
MySQLFunctionManager functionManager = new MySQLFunctionManager(true);
buildFuncManager(functionManager, functions);
for (RuleConfig conf : loader.listRuleConfig()) {
String algorithmString = conf.getAlgorithm();
MySQLLexer lexer = new MySQLLexer(algorithmString);
MySQLExprParser parser = new MySQLExprParser(lexer, functionManager, false, MySQLParser.DEFAULT_CHARSET);
Expression expression = parser.expression();
if (lexer.token() != MySQLToken.EOF) {
throw new ConfigException("route algorithm not end with EOF: " + algorithmString);
}
RuleAlgorithm algorithm;
if (expression instanceof RuleAlgorithm) {
algorithm = (RuleAlgorithm) expression;
} else {
algorithm = new ExpressionAdapter(expression);
}
conf.setRuleAlgorithm(algorithm);
}
}
use of com.alibaba.cobar.config.model.rule.RuleAlgorithm in project cobar by alibaba.
the class XMLRuleLoader method loadFunctions.
private void loadFunctions(Element root) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
NodeList list = root.getElementsByTagName("function");
for (int i = 0, n = list.getLength(); i < n; ++i) {
Node node = list.item(i);
if (node instanceof Element) {
Element e = (Element) node;
String name = e.getAttribute("name");
if (functions.containsKey(name)) {
throw new ConfigException("rule function " + name + " duplicated!");
}
String clazz = e.getAttribute("class");
RuleAlgorithm function = createFunction(name, clazz);
ParameterMapping.mapping(function, ConfigUtil.loadElements(e));
functions.put(name, function);
}
}
}
Aggregations