public static QueryExpression combine(NaryLogicalOperator op, List<QueryExpression> list) { List<QueryExpression> l = list.stream().filter(q -> q != null).collect(Collectors.toList()); if (l.size() == 0) { return null; } else if (l.size() == 1) { return l.get(0); } else { return new NaryLogicalExpression(op, l); } }
public static QueryExpression combine(NaryLogicalOperator op, List<QueryExpression> list) { List<QueryExpression> l = list.stream().filter(q -> q != null).collect(Collectors.toList()); if (l.size() == 0) { return null; } else if (l.size() == 1) { return l.get(0); } else { return new NaryLogicalExpression(op, l); } }
public void setQueries(List<Conjunct> conjuncts) { this.conjuncts = conjuncts; List<QueryExpression> l = new ArrayList<>(conjuncts.size()); for (Conjunct c : conjuncts) { l.add(c.getClause()); } if (l.isEmpty()) { query = null; } else if (l.size() == 1) { query = l.get(0); } else { query = new NaryLogicalExpression(NaryLogicalOperator._and, l); } }
public void setQueries(List<Conjunct> conjuncts) { this.conjuncts = conjuncts; List<QueryExpression> l = new ArrayList<>(conjuncts.size()); for (Conjunct c : conjuncts) { l.add(c.getClause()); } if (l.isEmpty()) { query = null; } else if (l.size() == 1) { query = l.get(0); } else { query = new NaryLogicalExpression(NaryLogicalOperator._and, l); } }
/** * Appends objectType:X to the query */ public static QueryExpression appendObjectType(QueryExpression q,String entityName) { QueryExpression ot=new ValueComparisonExpression(DocTranslator.OBJECT_TYPE,BinaryComparisonOperator._eq,new Value(entityName)); if(q==null) { return ot; } if(q instanceof NaryLogicalExpression && ((NaryLogicalExpression)q).getOp()==NaryLogicalOperator._and) { List<QueryExpression> l=new ArrayList<>(((NaryLogicalExpression)q).getQueries()); l.add(ot); return new NaryLogicalExpression(NaryLogicalOperator._and,l); } else { return new NaryLogicalExpression(NaryLogicalOperator._and,q,ot); } }
/** * Parses an n-ary logical expression from the given json object */ public static NaryLogicalExpression fromJson(ObjectNode node) { if (node.size() != 1) { throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString()); } String fieldName = node.fieldNames().next(); NaryLogicalOperator op = NaryLogicalOperator.fromString(fieldName); if (op == null) { throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString()); } JsonNode x = node.get(fieldName); if (x instanceof ArrayNode) { ArrayList<QueryExpression> list = new ArrayList<>(((ArrayNode) x).size()); for (Iterator<JsonNode> itr = ((ArrayNode) x).elements(); itr.hasNext();) { list.add(QueryExpression.fromJson(itr.next())); } return new NaryLogicalExpression(op, list); } else { throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString()); } } }
/** * Parses an n-ary logical expression from the given json object */ public static NaryLogicalExpression fromJson(ObjectNode node) { if (node.size() != 1) { throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString()); } String fieldName = node.fieldNames().next(); NaryLogicalOperator op = NaryLogicalOperator.fromString(fieldName); if (op == null) { throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString()); } JsonNode x = node.get(fieldName); if (x instanceof ArrayNode) { ArrayList<QueryExpression> list = new ArrayList<>(((ArrayNode) x).size()); for (Iterator<JsonNode> itr = ((ArrayNode) x).elements(); itr.hasNext();) { list.add(QueryExpression.fromJson(itr.next())); } return new NaryLogicalExpression(op, list); } else { throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString()); } } }
@Override public QueryExpression rewrite(QueryExpression q) { UnaryLogicalExpression le=dyncast(UnaryLogicalExpression.class,q); if(le!=null&&le.getOp()==UnaryLogicalOperator._not) { NaryLogicalExpression oreq=dyncast(NaryLogicalExpression.class,le.getQuery()); if(oreq!=null&&oreq.getOp()==NaryLogicalOperator._or) { List<QueryExpression> newList=new ArrayList<>(oreq.getQueries().size()); for(QueryExpression x:oreq.getQueries()) newList.add(new UnaryLogicalExpression(UnaryLogicalOperator._not,x)); return new NaryLogicalExpression(NaryLogicalOperator._and,newList); } } return q; } }
@Override public QueryExpression rewrite(QueryExpression q) { UnaryLogicalExpression le = dyncast(UnaryLogicalExpression.class, q); if (le != null && le.getOp() == UnaryLogicalOperator._not) { NaryLogicalExpression oreq = dyncast(NaryLogicalExpression.class, le.getQuery()); if (oreq != null && oreq.getOp() == NaryLogicalOperator._or) { List<QueryExpression> newList = new ArrayList<>(oreq.getQueries().size()); for (QueryExpression x : oreq.getQueries()) { newList.add(new UnaryLogicalExpression(UnaryLogicalOperator._not, x)); } return new NaryLogicalExpression(NaryLogicalOperator._and, newList); } } return q; } }
@Override public QueryExpression rewrite(QueryExpression q) { UnaryLogicalExpression le = dyncast(UnaryLogicalExpression.class, q); if (le != null && le.getOp() == UnaryLogicalOperator._not) { NaryLogicalExpression oreq = dyncast(NaryLogicalExpression.class, le.getQuery()); if (oreq != null && oreq.getOp() == NaryLogicalOperator._or) { List<QueryExpression> newList = new ArrayList<>(oreq.getQueries().size()); for (QueryExpression x : oreq.getQueries()) { newList.add(new UnaryLogicalExpression(UnaryLogicalOperator._not, x)); } return new NaryLogicalExpression(NaryLogicalOperator._and, newList); } } return q; } }
runExpression=queryClauses.get(0); } else if(queryClauses.size()>0) { runExpression=new NaryLogicalExpression(NaryLogicalOperator._and,queryClauses); } else { runExpression=null;
newq=new NaryLogicalExpression( ((NaryLogicalExpression)q).getOp(),cowr.getCopiedList()); } else if (q instanceof ArrayMatchExpression) { LOGGER.debug("q is an array match expression, rewriting nested query");
ret=newList.get(0); } else { ret=new NaryLogicalExpression(q.getOp(), newList);
@Override public QueryExpression rewrite(QueryExpression q) { NaryLogicalExpression le=dyncast(NaryLogicalExpression.class,q); if(le!=null) { if(le.getOp()==NaryLogicalOperator._and) { CopyOnWriteIterator<QueryExpression> itr=new CopyOnWriteIterator<>(le.getQueries()); while(itr.hasNext()) { QueryExpression x=itr.next(); NaryLogicalExpression nested=dyncast(NaryLogicalExpression.class,x); if(nested!=null&&nested.getOp()==NaryLogicalOperator._and) { // Remove this element, and add all its queries to the parent query itr.remove(); itr.getCopiedList().addAll(nested.getQueries()); } } if(itr.isCopied()) return new NaryLogicalExpression(NaryLogicalOperator._and,itr.getCopiedList()); } } return q; } }
protected QueryExpression itrValueComparisonExpression(ValueComparisonExpression q, Path context) { Binder binding = getBoundValue(q.getRvalue()); if (binding != null) { if(binding.getValue() instanceof List) { // If field = [v1,v2,v3], then rewrite the query as: // field in [v1,v2,v3] // otherwise if query is field op [v1,v2,v3], then: // $or:[ // { field op v1}, // { field op v2}, // { field op v3 } ] if(q.getOp()==BinaryComparisonOperator._eq) { return new NaryValueRelationalExpression(q.getField(),NaryRelationalOperator._in,(List<Value>)binding.getValue()); } else { List<QueryExpression> resultList=new ArrayList<>(); for(Value v:(List<Value>)binding.getValue()) { resultList.add(new ValueComparisonExpression(q.getField(),q.getOp(),v)); } return new NaryLogicalExpression(NaryLogicalOperator._or,resultList); } } else { return new ValueComparisonExpression(q.getField(), q.getOp(), (Value) binding.getValue()); } } else { return q; } }
@Override public QueryExpression rewrite(QueryExpression q) { NaryLogicalExpression le = dyncast(NaryLogicalExpression.class, q); if (le != null) { if (le.getOp() == NaryLogicalOperator._and) { CopyOnWriteIterator<QueryExpression> itr = new CopyOnWriteIterator<>(le.getQueries()); while (itr.hasNext()) { QueryExpression x = itr.next(); NaryLogicalExpression nested = dyncast(NaryLogicalExpression.class, x); if (nested != null && nested.getOp() == NaryLogicalOperator._and) { // Remove this element, and add all its queries to the parent query itr.remove(); itr.getCopiedList().addAll(nested.getQueries()); } } if (itr.isCopied()) { return new NaryLogicalExpression(NaryLogicalOperator._and, itr.getCopiedList()); } } } return q; } }
/** * Default behavior is to recursively iterate the nested quereies. If nested * processing returns objects different from the original nested queries, * this method creates a new n-ary logical expression using the new query * expressions and returns that. */ protected QueryExpression itrNaryLogicalExpression(NaryLogicalExpression q, Path context) { CopyOnWriteIterator<QueryExpression> itr = new CopyOnWriteIterator<>(q.getQueries()); while (itr.hasNext()) { QueryExpression nestedq = itr.next(); QueryExpression newq = iterate(nestedq, context); if (newq != nestedq) { itr.set(newq); } } if (itr.isCopied()) { return new NaryLogicalExpression(q.getOp(), itr.getCopiedList()); } else { return q; } }
protected QueryExpression itrValueComparisonExpression(ValueComparisonExpression q, Path context) { Binder binding = getBoundValue(q.getRvalue()); if (binding != null) { if(binding.getValue() instanceof List) { // If field = [v1,v2,v3], then rewrite the query as: // field in [v1,v2,v3] // otherwise if query is field op [v1,v2,v3], then: // $or:[ // { field op v1}, // { field op v2}, // { field op v3 } ] if(q.getOp()==BinaryComparisonOperator._eq) { return new NaryValueRelationalExpression(q.getField(),NaryRelationalOperator._in,(List<Value>)binding.getValue()); } else { List<QueryExpression> resultList=new ArrayList<>(); for(Value v:(List<Value>)binding.getValue()) { resultList.add(new ValueComparisonExpression(q.getField(),q.getOp(),v)); } return new NaryLogicalExpression(NaryLogicalOperator._or,resultList); } } else { return new ValueComparisonExpression(q.getField(), q.getOp(), (Value) binding.getValue()); } } else { return q; } }
/** * Default behavior is to recursively iterate the nested quereies. If nested * processing returns objects different from the original nested queries, * this method creates a new n-ary logical expression using the new query * expressions and returns that. */ protected QueryExpression itrNaryLogicalExpression(NaryLogicalExpression q, Path context) { CopyOnWriteIterator<QueryExpression> itr = new CopyOnWriteIterator<>(q.getQueries()); while (itr.hasNext()) { QueryExpression nestedq = itr.next(); QueryExpression newq = iterate(nestedq, context); if (newq != nestedq) { itr.set(newq); } } if (itr.isCopied()) { return new NaryLogicalExpression(q.getOp(), itr.getCopiedList()); } else { return q; } }
@Override public QueryExpression rewrite(QueryExpression q) { NaryLogicalExpression le = dyncast(NaryLogicalExpression.class, q); if (le != null) { if (le.getOp() == NaryLogicalOperator._and) { CopyOnWriteIterator<QueryExpression> itr = new CopyOnWriteIterator<>(le.getQueries()); while (itr.hasNext()) { QueryExpression x = itr.next(); NaryLogicalExpression nested = dyncast(NaryLogicalExpression.class, x); if (nested != null && nested.getOp() == NaryLogicalOperator._and) { // Remove this element, and add all its queries to the parent query itr.remove(); itr.getCopiedList().addAll(nested.getQueries()); } } if (itr.isCopied()) { return new NaryLogicalExpression(NaryLogicalOperator._and, itr.getCopiedList()); } } } return q; } }