protected static int getExpressionCount(Expression expr) { PrefixInfo info = (PrefixInfo) expr.getInfo(); return info == null ? 0 : info.count; }
protected Document walkAndOr(Expression expr, List<? extends Operand> values) { if (values.size() == 1) { return (Document) walkOperand(values.get(0)); } boolean and = expr.operator == Operator.AND; String op = and ? QueryOperators.AND : QueryOperators.OR; // PrefixInfo was computed by the QueryOptimizer for common AND predicates PrefixInfo info = (PrefixInfo) expr.getInfo(); if (info == null || info.count < 2 || !and) { List<Object> list = walkOperandList(values); return new Document(op, list); } // we have a common prefix for all underlying references, extract it into an $elemMatch node // info.prefix is the DBS common prefix, ex: foo/bar/*1; ecm:acp/*1/acl/*1 // compute MongoDB prefix: foo.bar.; ecm:acp.acl. String prefix = SLASH_WILDCARD_SLASH.matcher(info.prefix).replaceAll("."); // remove current prefix and trailing . for actual field match String fieldBase = stripElemMatchPrefix(prefix.substring(0, prefix.length() - 1)); String previousElemMatchPrefix = elemMatchPrefix; elemMatchPrefix = prefix; List<Object> list = walkOperandList(values); elemMatchPrefix = previousElemMatchPrefix; return new Document(fieldBase, new Document(QueryOperators.ELEM_MATCH, new Document(op, list))); }
protected void processExpression(Expression node, List<? extends Operand> operands) { PrefixInfo commonInfo = null; for (Operand operand : operands) { // find longest prefix for the operand PrefixInfo info; if (operand instanceof Reference) { Reference reference = (Reference) operand; info = (PrefixInfo) reference.getInfo(); } else if (operand instanceof Expression) { Expression expression = (Expression) operand; info = (PrefixInfo) expression.getInfo(); } else { info = null; } if (info != null) { if (commonInfo == null) { commonInfo = info; } else if (commonInfo.prefix.equals(info.prefix)) { commonInfo = new PrefixInfo(commonInfo.prefix, commonInfo.count + info.count); } else { commonInfo = PrefixInfo.EMPTY; } } } node.setInfo(commonInfo); } }