Ejemplo n.º 1
0
        private static void TrySetCollectionName(MergeProposal mergeProposal, MergeSuggestions mergeSuggestion)
        {
            if (mergeProposal.ProposedForMerge[0].Collection != null)
            {
                mergeSuggestion.Collection = mergeProposal.ProposedForMerge[0].Collection;
            }

            else if (mergeProposal.ProposedForMerge[0].FromExpression is SimpleNameSyntax name)
            {
                mergeSuggestion.Collection = name.Identifier.ValueText;
            }

            else if (mergeProposal.ProposedForMerge[0].FromExpression is MemberAccessExpressionSyntax member)
            {
                var identifier = ExtractIdentifierFromExpression(member);
                if (identifier == "docs")
                {
                    mergeSuggestion.Collection = ExtractValueFromExpression(member);
                }
            }
        }
Ejemplo n.º 2
0
        private IndexMergeResults CreateMergeIndexDefinition(List <MergeProposal> indexDataForMerge)
        {
            var indexMergeResults = new IndexMergeResults();

            foreach (var mergeProposal in indexDataForMerge.Where(m => m.ProposedForMerge.Count == 0 && m.MergedData != null))
            {
                indexMergeResults.Unmergables.Add(mergeProposal.MergedData.IndexName, mergeProposal.MergedData.Comment);
            }

            foreach (var mergeProposal in indexDataForMerge)
            {
                if (mergeProposal.ProposedForMerge.Count == 0)
                {
                    continue;
                }

                var mergeSuggestion      = new MergeSuggestions();
                var selectExpressionDict = new Dictionary <string, ExpressionSyntax>();

                MergeSelectExpressionsAndFields(mergeProposal, selectExpressionDict, mergeSuggestion);
                TrySetCollectionName(mergeProposal, mergeSuggestion);

                var map = mergeProposal.ProposedForMerge[0].BuildExpression(selectExpressionDict);
                mergeSuggestion.MergedIndex.Maps.Add(map);
                RemoveMatchingIndexes(mergeProposal, selectExpressionDict, mergeSuggestion, indexMergeResults);

                if (mergeProposal.ProposedForMerge.Count == 1 && mergeProposal.ProposedForMerge[0].IsSuitedForMerge == false)
                {
                    const string comment = "Can't find any other index to merge this with";
                    indexMergeResults.Unmergables.Add(mergeProposal.ProposedForMerge[0].IndexName, comment);
                }
            }

            indexMergeResults = ExcludePartialResults(indexMergeResults);
            return(indexMergeResults);
        }
Ejemplo n.º 3
0
        private static void MergeSelectExpressionsAndFields(MergeProposal mergeProposal, Dictionary <string, ExpressionSyntax> selectExpressionDict, MergeSuggestions mergeSuggestion)
        {
            foreach (var curProposedData in mergeProposal.ProposedForMerge)
            {
                foreach (var curExpr in curProposedData.SelectExpressions)
                {
                    var expression     = curExpr.Value as MemberAccessExpressionSyntax;
                    var identifierName = ExtractIdentifierFromExpression(expression);

                    if (identifierName != null && identifierName == curProposedData.FromIdentifier)
                    {
                        if (expression.Expression is MemberAccessExpressionSyntax)
                        {
                            var valueStr    = ExtractValueFromExpression(curExpr.Value);
                            var valueExp    = SyntaxFactory.ParseExpression(valueStr).NormalizeWhitespace();
                            var innerName   = ExtractIdentifierFromExpression(valueExp as MemberAccessExpressionSyntax);
                            var innerMember = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
                                                                                   SyntaxFactory.IdentifierName("doc"), SyntaxFactory.IdentifierName(innerName));
                            expression = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, innerMember, expression.Name);
                        }

                        else if (expression.Expression is SimpleNameSyntax)
                        {
                            expression = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("doc"), expression.Name);
                        }
                    }

                    selectExpressionDict[curExpr.Key] = expression ?? curExpr.Value;
                }

                mergeSuggestion.CanMerge.Add(curProposedData.IndexName);
                DataDictionaryMerge(mergeSuggestion.MergedIndex.Fields, curProposedData.Index.Fields);
                mergeSuggestion.MergedIndex.Fields = mergeSuggestion.MergedIndex.Fields;
            }
        }
Ejemplo n.º 4
0
        private static void RemoveMatchingIndexes(MergeProposal mergeProposal, Dictionary <string, ExpressionSyntax> selectExpressionDict, MergeSuggestions mergeSuggestion,
                                                  IndexMergeResults indexMergeResults)
        {
            if (mergeProposal.ProposedForMerge.Count > 1)
            {
                var matchingExistingIndexes = mergeProposal.ProposedForMerge.Where(x =>
                                                                                   AreSelectClausesTheSame(x, selectExpressionDict) &&
                                                                                   (x.Index.Compare(mergeSuggestion.MergedIndex) == IndexDefinitionCompareDifferences.None ||
                                                                                    x.Index.Compare(mergeSuggestion.MergedIndex) == IndexDefinitionCompareDifferences.Maps))
                                              .OrderBy(x => x.IndexName.StartsWith("Auto/", StringComparison.CurrentCultureIgnoreCase))
                                              .ToList();

                if (matchingExistingIndexes.Count > 0)
                {
                    var surpassingIndex = matchingExistingIndexes.First();
                    mergeSuggestion.SurpassingIndex = surpassingIndex.IndexName;

                    mergeSuggestion.MergedIndex = null;
                    mergeSuggestion.CanMerge.Clear();
                    mergeSuggestion.CanDelete = mergeProposal.ProposedForMerge.Except(new[]
                    {
                        surpassingIndex
                    }).Select(x => x.IndexName).ToList();
                }

                indexMergeResults.Suggestions.Add(mergeSuggestion);
            }
        }