Search in sources :

Example 1 with RuleAlgorithm

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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CobarHint(com.alibaba.cobar.route.hint.CobarHint) RuleAlgorithm(com.alibaba.cobar.config.model.rule.RuleAlgorithm) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) InExpressionList(com.alibaba.cobar.parser.ast.expression.misc.InExpressionList) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with RuleAlgorithm

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);
    }
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) RuleAlgorithm(com.alibaba.cobar.config.model.rule.RuleAlgorithm) MySQLExprParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLExprParser) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) MySQLFunctionManager(com.alibaba.cobar.parser.recognizer.mysql.MySQLFunctionManager) ConfigException(com.alibaba.cobar.config.util.ConfigException) RuleConfig(com.alibaba.cobar.config.model.rule.RuleConfig) ExpressionAdapter(com.alibaba.cobar.route.function.ExpressionAdapter)

Example 3 with RuleAlgorithm

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);
        }
    }
}
Also used : RuleAlgorithm(com.alibaba.cobar.config.model.rule.RuleAlgorithm) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ConfigException(com.alibaba.cobar.config.util.ConfigException)

Aggregations

RuleAlgorithm (com.alibaba.cobar.config.model.rule.RuleAlgorithm)3 ConfigException (com.alibaba.cobar.config.util.ConfigException)2 RuleConfig (com.alibaba.cobar.config.model.rule.RuleConfig)1 Expression (com.alibaba.cobar.parser.ast.expression.Expression)1 InExpressionList (com.alibaba.cobar.parser.ast.expression.misc.InExpressionList)1 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)1 MySQLFunctionManager (com.alibaba.cobar.parser.recognizer.mysql.MySQLFunctionManager)1 MySQLLexer (com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer)1 MySQLExprParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLExprParser)1 ExpressionAdapter (com.alibaba.cobar.route.function.ExpressionAdapter)1 CobarHint (com.alibaba.cobar.route.hint.CobarHint)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1