private QueryNode SetQueryResult(QueryNode qn) { if (qn is QueryConditionNode) { var resultData = LocalStorage.Get(new Query { Condition = qn }); if (resultData != null) { resultData = resultData.Select(r => new OverlayData(r) { ResponsibleNode = Node }).ToArray(); } else { resultData = new OverlayData[0]; } return new QueryConditionNodeResult(qn) { ResultData = resultData, IsExecutedByResponsibleNode = (Node.Equals((qn as QueryConditionNode).ResponsibleNode)) }; } else if (qn is QueryGroupNode) { var qng = qn as QueryGroupNode; QueryGroupNode result = new QueryGroupNode(qng.Group); foreach (var innerQn in qng.Nodes) { result.Nodes.Add(SetQueryResult(innerQn)); } return result; } else { throw new NotImplementedException(); } }
private IList<QueryNode> GetLeafQueryNodes(QueryNode qn) { var result = new List<QueryNode>(); if (qn is QueryConditionNode || qn is QueryConditionNodeResult) { result.Add(qn); return result; } else if (qn is QueryGroupNode) { foreach (var innerQn in (qn as QueryGroupNode).Nodes) { result.AddRange(GetLeafQueryNodes(innerQn)); } return result; } else { throw new NotImplementedException(); } }
private QueryNode MergeResultsIntoQueryNode(IList<QueryConditionNodeResult> mergedLeafNodes, QueryNode originalQueryNode) { if (originalQueryNode is QueryGroupNode) { var qng = originalQueryNode as QueryGroupNode; for (var i = 0; i < qng.Nodes.Count; ++i) { if (qng.Nodes[i] is QueryGroupNode) { qng.Nodes[i] = MergeResultsIntoQueryNode(mergedLeafNodes, qng.Nodes[i]); } else { var qnr = qng.Nodes[i] as QueryConditionNode; qng.Nodes[i] = mergedLeafNodes.Single(mn => mn.Id == qnr.Id); } } return qng; } else { return mergedLeafNodes.Single(mn => mn.Id == (originalQueryNode as QueryConditionNode).Id); } }
private IList<OverlayData> BuildResultSet(QueryNode qn) { if (qn is QueryConditionNodeResult) { return (qn as QueryConditionNodeResult).ResultData.ToList(); } else if (qn is QueryGroupNode) { var qng = qn as QueryGroupNode; var result = new List<OverlayData>(); if (qng.Group == GroupType.And) { bool isFirst = true; foreach (var innerQ in qng.Nodes) { if (isFirst) { isFirst = false; result = BuildResultSet(innerQ).ToList(); } else { result = result.Intersect(BuildResultSet(innerQ), new OverlayData.ServiceNameComparer()).ToList(); } } } else { foreach (var innerQ in qng.Nodes) { result = result.Union(BuildResultSet(innerQ)).ToList(); } } return result; } else { throw new NotImplementedException(); } }
private IList<IOverlayNode> AssignResponsibleNodes(QueryNode qn) { var responsibleNodes = new List<IOverlayNode>(); if (qn is QueryConditionNode) { var qnc = qn as QueryConditionNode; if (qnc.LValue.Name.StartsWith("__")) { // we cannot process non-attribute based l-values - just ignore them? return null; } else { var queryNodeKey = OverlayHashKeyPrv.GetHashKey(OverlayHelper.GetHashArgument(qnc.LValue.Name)); var arg = new GetOverlayNodeByKeyArg(queryNodeKey); var responsibleNode = MainOverlaySrv.GetNodeByKey(arg.CopyStatistics(qn)); qn.CopyStatistics(arg); responsibleNodes.Add(responsibleNode); qnc.ResponsibleNode = responsibleNode; qnc.Id = Guid.NewGuid().ToString(); return responsibleNodes; } } else if (qn is QueryGroupNode) { var qng = qn as QueryGroupNode; foreach (var innerQn in qng.Nodes) { responsibleNodes.AddRange(AssignResponsibleNodes(innerQn.CopyStatistics(qn))); } qn.AggregateStatistics(qng.Nodes.ToArray()); return responsibleNodes; } else { throw new NotImplementedException(); } }
public QueryConditionNodeResult(QueryNode qn) : base() { OriginalNode = (QueryConditionNode)qn; }
private static IMongoQuery MapFromQueryNode(QueryNode q, string attributeFieldName, string valueFieldName, string serviceNameFieldName) { if (q is QueryConditionNode) { var qc = q as QueryConditionNode; IMongoQuery valueQuery = null; var lValue = qc.LValue.Name; var conditionLFieldName = (lValue == "__service_name" ? serviceNameFieldName : valueFieldName); switch (qc.Condition) { case Conditions.Equal : valueQuery = MongoQueryBuilder.EQ(conditionLFieldName, GetBsonValue(qc.RValue)); break; case Conditions.Not | Conditions.Equal : valueQuery = MongoQueryBuilder.NE(conditionLFieldName, GetBsonValue(qc.RValue)); break; case Conditions.GreaterThan : valueQuery = MongoQueryBuilder.GT(conditionLFieldName, GetBsonValue(qc.RValue)); break; case Conditions.GreaterThan | Conditions.Equal : valueQuery = MongoQueryBuilder.GTE(conditionLFieldName, GetBsonValue(qc.RValue)); break; case Conditions.LessThan : valueQuery = MongoQueryBuilder.LT(conditionLFieldName, GetBsonValue(qc.RValue)); break; case Conditions.LessThan | Conditions.Equal : valueQuery = MongoQueryBuilder.LTE(conditionLFieldName, GetBsonValue(qc.RValue)); break; case Conditions.Like : valueQuery = MongoQueryBuilder.Matches(conditionLFieldName, LikeExprToRegexExpr(qc.RValue)); break; } if (lValue != "__service_name") { if (valueQuery != null) { return MongoQueryBuilder.And(MongoQueryBuilder.EQ(attributeFieldName, lValue), valueQuery); } //else if (rValue == null) { // return all values for the attribute // return MongoQueryBuilder.EQ(attributeFieldName, lValue); //} } } else if (q is QueryGroupNode) { var nodes = (q as QueryGroupNode).Nodes; var mongoNodes = nodes.Select(n => MapFromQueryNode(q, attributeFieldName, valueFieldName, serviceNameFieldName)).ToArray(); if ((q as QueryGroupNode).Group == GroupType.And) { return MongoQueryBuilder.And(mongoNodes); } else { return MongoQueryBuilder.Or(mongoNodes); } } throw new NotImplementedException(); }
protected QueryGroupNode ComposeGroupNode(QueryNode node1, QueryNode node2, GroupType groupType) { QueryGroupNode group1 = node1 as QueryGroupNode, group2 = node2 as QueryGroupNode; if (group1 != null && group1.Group != groupType) group1 = null; if (group2 != null && group2.Group != groupType) group2 = null; // don't corrupt named groups //if (group1 != null && group1.Name != null || group2 != null && group2.Name != null) // group1 = group2 = null; if (group1 == null) { if (group2 == null) { QueryGroupNode group = new QueryGroupNode(groupType); group.Nodes.Add(node1); group.Nodes.Add(node2); return group; } else { group2.Nodes.Insert(0, node1); return group2; } } else { if (group2 == null) group1.Nodes.Add(node2); else foreach (QueryNode qn in group2.Nodes) group1.Nodes.Add(qn); return group1; } }