コード例 #1
0
ファイル: OverlayStorage.cs プロジェクト: shigasumi/NService
 private Query SetQueryResult(Query q)
 {
     return new Query { Condition = SetQueryResult(q.Condition) }.CopyStatistics(q);
 }
コード例 #2
0
ファイル: OverlayStorage.cs プロジェクト: shigasumi/NService
        private Query MergeQueryResults(IEnumerable<Query> executedQueries, Query originalQuery)
        {
            if (executedQueries.Any()) {
                // 1. flatten all trees into lists
                var flattenedExecuteQueries = executedQueries.Select(q => GetLeafQueryNodes(q)).ToList();

                // 2. merge nodes
                var firstResult = flattenedExecuteQueries.First();
                // copy first result as the base of the merged result
                var mergedResult = firstResult.ToList();
                for (var i = 0; i < firstResult.Count; ++i) {
                    // means that some other overlay node result has the result data
                    if (firstResult[i] is QueryConditionNodeResult) {
                        if (!(firstResult[i] as QueryConditionNodeResult).IsExecutedByResponsibleNode) {
                            // iterate other nodes
                            foreach (var otherR in flattenedExecuteQueries.Skip(1)) {
                                // the result must be on the same position in the flattened query since all queries are just copies of original query
                                if (otherR[i] is QueryConditionNodeResult) {
                                    if ((otherR[i] as QueryConditionNodeResult).IsExecutedByResponsibleNode) {
                                        mergedResult[i] = (otherR[i] as QueryConditionNodeResult).Copy();
                                    }
                                }
                            }
                        }
                    }
                }
                // if we have some nodes that are not results (happens only when responsible node is not reached) -> make ther zero result nodes
                for (var i = 0; i < mergedResult.Count; ++i) {
                    if (!(mergedResult[i] is QueryConditionNodeResult)) {
                        mergedResult[i] = new QueryConditionNodeResult(mergedResult[i]) { ResultData = null, IsExecutedByResponsibleNode = true };
                    }
                }

                // 3. restore the original query
                return MergeResultsIntoQuery(mergedResult, originalQuery);
            }
            return null;
        }
コード例 #3
0
ファイル: OverlayStorage.cs プロジェクト: shigasumi/NService
 private Query MergeResultsIntoQuery(IList<QueryNode> mergedLeafNodes, Query originalQuery)
 {
     return new Query { Condition = MergeResultsIntoQueryNode(mergedLeafNodes.Cast<QueryConditionNodeResult>().ToList(), originalQuery.Condition) };
 }
コード例 #4
0
ファイル: OverlayStorage.cs プロジェクト: shigasumi/NService
        private IList<OverlayData> BuildResultSet(Query q)
        {
            if (q == null) {
                return new OverlayData[0];
            }

            // merge result sets according to AND/OR groups
            return BuildResultSet(q.Condition);
        }
コード例 #5
0
ファイル: OverlayStorage.cs プロジェクト: shigasumi/NService
 private IList<QueryNode> GetLeafQueryNodes(Query q)
 {
     return GetLeafQueryNodes(q.Condition);
 }
コード例 #6
0
ファイル: OverlayStorage.cs プロジェクト: shigasumi/NService
 private IList<IOverlayNode> AssignResponsibleNodes(Query q)
 {
     var result = AssignResponsibleNodes(q.Condition.CopyStatistics(q));
     q.ResponsibleNodesAssigned = true;
     q = q.CopyStatistics(q.Condition);
     return result;
 }
コード例 #7
0
ファイル: OverlayStorage.cs プロジェクト: shigasumi/NService
        public GetServicesResult GetServices(Query q, bool returnExtendedInfo)
        {
            if (OverlayParams.StatisticsEnabled) {
                q.StatsCollector.InitStat(String.Format("Query: {0}", q));
            }

            try {
                var overlayDataResult = Get(q).ServiceData;
                var services = ServiceDescription.FromOverlayData(overlayDataResult);

                IList<ServiceDescription> result = null;

                if (returnExtendedInfo) {
                    var extendedServices = new List<ServiceDescription>();
                    foreach (var s in services) {
                        var chnl = CommunicationMgr.GetChannel(s.OwnerNode);
                        var srv = chnl.Service.GetServiceLocal(s.Name);
                        if (srv != null) {
                            extendedServices.Add(srv);
                        }
                    }
                    result = extendedServices.ToArray();
                } else {
                    result = services;
                }

                if (OverlayParams.StatisticsEnabled) {
                    OverlayStateSrv.StoreStatistics("Query", q.StatsCollector);
                }

                var filteredResult = new List<ServiceDescription>();
                if (ReturnOnlySuccessfullyStoredServices) {
                    foreach (var r in result) {
                        var chnl = CommunicationMgr.GetChannel(r.OwnerNode);
                        var srvLocal = chnl.Service.GetServiceLocal(r.Name);
                        if (srvLocal.StorageState == ServiceStorageState.Success) {
                            filteredResult.Add(r);
                        }
                    }
                }

                return new GetServicesResult { ServicesData = filteredResult.ToArray(), IsSuccessful = true };
            } catch (GetServiceException) {
                return new GetServicesResult { IsSuccessful = false };
            }
        }
コード例 #8
0
ファイル: OverlayStorage.cs プロジェクト: shigasumi/NService
 public GetServiceResult Get(Query q)
 {
     try {
         var qResult = Execute(q);
         var result = BuildResultSet(qResult).ToArray();
         return new GetServiceResult { ServiceData = result, IsSuccessful = true };
     } catch (GetServiceException) {
         return new GetServiceResult { IsSuccessful = false };
     }
 }
コード例 #9
0
ファイル: OverlayStorage.cs プロジェクト: shigasumi/NService
        public Query Execute(Query q)
        {
            if (!q.ResponsibleNodesAssigned) {
                var responsibleNodes = AssignResponsibleNodes(q);
                var resultQList = new ConcurrentBag<Query>();

                Parallel.ForEach(responsibleNodes, delegate (IOverlayNode node) {
                    var channel = CommunicationMgr.GetChannel(node);
                    resultQList.Add(channel.Service.Execute(q));
                });

                if (OverlayParams.StatisticsEnabled) {
                    // add hops for query execution itself
                    q.AggregateStatistics(resultQList.ToArray());
                    q.StatsCollector.IncreaseHopsCount(responsibleNodes.Distinct().Count());
                }
                return MergeQueryResults(resultQList, q);
            } else {
                q = SetQueryResult(q);
                return q;
            }
        }
コード例 #10
0
        protected virtual IQueryValue ParseInternal(string input, int startIdx, out int endIdx)
        {
            LexemType lexemType = GetLexemType(input, startIdx, out endIdx);
            string lexem = GetLexem(input, startIdx, endIdx);

            if (lexemType==LexemType.Constant)
                return new QConst(lexem, TypeCode.String);

            if (lexemType==LexemType.QuotedConstant) {
                // remove first and last chars
                string constant = lexem.Substring(1, lexem.Length-2);
                // replace "" with "
                constant = constant.Replace( "\"\"", "\"" );
                // typed?
                int newEndIdx;
                if ( GetLexemType(input, endIdx, out newEndIdx)==LexemType.Delimiter &&
                     GetLexem(input, endIdx, newEndIdx)==":" ) {
                    int typeEndIdx;
                    if (GetLexemType(input, newEndIdx, out typeEndIdx)==LexemType.Name) {
                        string typeCodeString = GetLexem(input, newEndIdx, typeEndIdx);
                        endIdx = typeEndIdx;
                        // read [] at the end if specified
                        if (GetLexemType(input, endIdx, out newEndIdx)==LexemType.Delimiter &&
                            GetLexem(input, endIdx, newEndIdx)=="[")
                            if (GetLexemType(input, newEndIdx, out typeEndIdx)==LexemType.Delimiter &&
                                GetLexem(input, newEndIdx, typeEndIdx)=="]") {
                                endIdx = typeEndIdx;
                                typeCodeString += "[]";
                            }

                        return ParseTypedConstant(typeCodeString, constant);
                    }
                }

                return new QConst(constant, TypeCode.String);
            }

            if (lexemType==LexemType.Name) {
                int nextEndIdx;

                // query
                string sourceName = lexem;
                QueryNode rootCondition = null;
                string[] fields = null;
                string[] sort = null;

                LexemType nextLexemType = GetLexemType(input, endIdx, out nextEndIdx);
                string nextLexem = GetLexem(input, endIdx, nextEndIdx);
                if (nextLexemType==LexemType.Delimiter && nextLexem=="(") {
                    // compose conditions
                    rootCondition = ParseConditionGroup(input, nextEndIdx, out endIdx);
                    // read ')'
                    nextLexemType = GetLexemType(input, endIdx, out nextEndIdx);
                    if (nextLexemType!=LexemType.Delimiter || GetLexem(input, endIdx,nextEndIdx)!=")")
                        throw new RelExParseException(
                            String.Format("Invalid syntax (position: {0}, expression: {1})", endIdx, input ) );

                    // read next lexem
                    nextLexemType = GetLexemType(input, nextEndIdx, out endIdx);
                    nextLexem = GetLexem(input, nextEndIdx, endIdx);
                    nextEndIdx = endIdx;
                }

                if (nextLexemType==LexemType.Delimiter && nextLexem=="[") {
                    nextLexemType = GetLexemType(input, nextEndIdx, out endIdx);
                    nextLexem = GetLexem(input, nextEndIdx, endIdx, nextLexemType);
                    nextEndIdx = endIdx;

                    StringBuilder fieldsBuilder = new StringBuilder();
                    StringBuilder sortBuilder = new StringBuilder();
                    fieldsBuilder.Append(nextLexem);
                    bool sortPart = false;
                    do {
                        LexemType prevLexemType = nextLexemType;
                        nextLexemType = GetLexemType(input, endIdx, out nextEndIdx);
                        nextLexem = GetLexem(input, endIdx, nextEndIdx, nextLexemType);
                        //Console.Write(nextLexemType.ToString() + ":'" + nextLexem + "' ");
                        endIdx = nextEndIdx;
                        if (nextLexemType==LexemType.Delimiter && nextLexem=="]")
                                break;
                        if (nextLexemType==LexemType.Stop)
                                break;
                        // handle sort separator
                        if (nextLexemType == LexemType.Delimiter && nextLexem == ";") {
                                sortPart = true;
                        } else {
                            if (sortPart) {
                                if (prevLexemType != LexemType.Delimiter)
                                    sortBuilder.Append(' '); // default delimiter for asc/desc
                                sortBuilder.Append(nextLexem);
                            } else
                                fieldsBuilder.Append(nextLexem);
                        }
                    } while (true);
                    string fieldsStr = fieldsBuilder.ToString();
                    if (fieldsStr!="*")
                            fields = fieldsStr.Split(',');
                    if (sortBuilder.Length > 0)
                            sort = sortBuilder.ToString().Split(',');
                } else {
                    // if brackets [] not specified near the name, threat it as field name
                    if (AllowDumpConstants && lexem.ToLower()!=nullField)
                        return new QConst(lexem, TypeCode.String);
                    else
                        return new QField(lexem);
                }
                endIdx = nextEndIdx;

                Query q = new Query { Condition = rootCondition };
                return q;
            }

            throw new RelExParseException(
                String.Format("Invalid syntax (position: {0}, expression: {1})", endIdx, input ) );
        }