Ejemplo n.º 1
0
        public void index_pretty_printer_ignores_whitespaces()
        {
            var firstFormat  = IndexPrettyPrinter.TryFormat("from order in docs.Orders select new { order.Company, Count = 1, Total = order.Lines.Sum(l=>(l.Quantity * l.PricePerUnit) *  ( 1 - l.Discount)) }");
            var secondFormat = IndexPrettyPrinter.TryFormat("from order  \t   in docs.Orders       select new { order.Company, Count = 1, Total = order.Lines.Sum(l=>(l.Quantity * l.PricePerUnit) *  ( 1 - l.Discount)) }");

            Assert.Equal(firstFormat, secondFormat);
        }
Ejemplo n.º 2
0
        private List <IndexData> ParseIndexesAndGetReadyToMerge()
        {
            var parser  = new CSharpParser();
            var indexes = new List <IndexData>();

            foreach (var kvp in indexDefinitions)
            {
                var index     = kvp.Value;
                var indexData = new IndexData(index)
                {
                    IndexId     = index.IndexId,
                    IndexName   = index.Name,
                    OriginalMap = index.Map,
                };

                indexes.Add(indexData);

                if (index.IsMapReduce || index.Maps.Count > 1)
                {
                    indexData.IsMapReduceOrMultiMap = true;
                    continue;
                }

                indexData.OriginalMap = IndexPrettyPrinter.TryFormat(indexData.OriginalMap);

                Expression map     = parser.ParseExpression(indexData.OriginalMap);
                var        visitor = new IndexVisitor(indexData);
                map.AcceptVisitor(visitor);
            }
            return(indexes);
        }
        /// <summary>
        /// Creates the index definition.
        /// </summary>
        /// <returns></returns>
        public override IndexDefinition CreateIndexDefinition()
        {
            if (Conventions == null)
            {
                Conventions = new DocumentConvention();
            }

            var indexDefinition = new IndexDefinitionBuilder <object, TReduceResult>()
            {
                Indexes                    = Indexes,
                SortOptions                = IndexSortOptions,
                SortOptionsStrings         = IndexSortOptionsStrings,
                Analyzers                  = Analyzers,
                Reduce                     = Reduce,
                Stores                     = Stores,
                TermVectors                = TermVectors,
                SpatialIndexes             = SpatialIndexes,
                SuggestionsOptions         = IndexSuggestions,
                AnalyzersStrings           = AnalyzersStrings,
                IndexesStrings             = IndexesStrings,
                StoresStrings              = StoresStrings,
                TermVectorsStrings         = TermVectorsStrings,
                SpatialIndexesStrings      = SpatialIndexesStrings,
                DisableInMemoryIndexing    = DisableInMemoryIndexing,
                MaxIndexOutputsPerDocument = MaxIndexOutputsPerDocument
            }.ToIndexDefinition(Conventions, validateMap: false);

            foreach (var map in maps.Select(generateMap => generateMap()))
            {
                string formattedMap = map;
#if !DNXCORE50
                if (Conventions.PrettifyGeneratedLinqExpressions)
                {
                    formattedMap = IndexPrettyPrinter.TryFormat(formattedMap);
                }
#endif
                indexDefinition.Maps.Add(formattedMap);
            }
            return(indexDefinition);
        }
Ejemplo n.º 4
0
        public IndexDefinition CreateIndexDefinition()
        {
            var fromClause   = string.Empty;
            var realMappings = new HashSet <string>();

            if (!string.IsNullOrEmpty(ForEntityName))
            {
                fromClause = "from doc in docs." + ForEntityName;
            }
            else
            {
                fromClause = "from doc in docs";
            }

            bool containsNestedItems = false;

            foreach (var map in Items)
            {
                var  currentDoc        = "doc";
                var  currentExpression = new StringBuilder();
                var  mapFromClauses    = new List <String>();
                int  currentIndex      = 0;
                bool nestedCollection  = false;
                while (currentIndex < map.From.Length)
                {
                    char currentChar = map.From[currentIndex++];
                    switch (currentChar)
                    {
                    case ',':
                        containsNestedItems = true;

                        // doc.NewDoc.Items
                        String newDocumentSource = string.Format("{0}.{1}", currentDoc, currentExpression);

                        // docNewDocItemsItem
                        String newDoc = string.Format("{0}Item", newDocumentSource.Replace(".", ""));

                        // from docNewDocItemsItem in doc.NewDoc.Items
                        String docInclude = string.Format("from {0} in ((IEnumerable<dynamic>){1}).DefaultIfEmpty()", newDoc, newDocumentSource);
                        mapFromClauses.Add(docInclude);
                        nestedCollection = true;
                        // Start building the property again
                        currentExpression.Clear();

                        // And from this new doc
                        currentDoc = newDoc;

                        break;

                    default:
                        nestedCollection = false;
                        currentExpression.Append(currentChar);
                        break;
                    }
                }

                if (currentExpression.Length > 0 && currentExpression[0] != '[')
                {
                    currentExpression.Insert(0, '.');
                }

                var    indexedMember = currentExpression.ToString().Replace("_Range", "");
                string rightHandSide;

                if (indexedMember.Length == 0 && nestedCollection == false)
                {
                    rightHandSide = currentDoc;
                }
                else if (mapFromClauses.Count > 0)
                {
                    rightHandSide = String.Format("({0} select {1}{2}).ToArray()", String.Join("\n", mapFromClauses), currentDoc,
                                                  indexedMember);
                }
                else
                {
                    rightHandSide = String.Format("{0}{1}", currentDoc, indexedMember);
                }

                realMappings.Add(string.Format("{0} = {1}",
                                               map.To.Replace("_Range", ""),
                                               rightHandSide
                                               ));
            }

            string mapDefinition;

            if (realMappings.Count == 1 && containsNestedItems == false)
            {
                mapDefinition = string.Format("{0}\r\nselect new {{ {1} }}", fromClause, realMappings.First());
            }
            else
            {
                mapDefinition = string.Format("{0}\r\nselect new\r\n{{\r\n\t{1}\r\n}}", fromClause, string.Join(",\r\n\t", realMappings));
            }

            mapDefinition = IndexPrettyPrinter.TryFormat(mapDefinition);

            var index = new IndexDefinition
            {
                Map = mapDefinition,
                InternalFieldsMapping = new Dictionary <string, string>()
            };

            foreach (var item in Items)
            {
                index.InternalFieldsMapping[item.To] = item.From;
            }

            foreach (var descriptor in SortDescriptors)
            {
                index.SortOptions[ToFieldName(descriptor.Field)] = descriptor.FieldType;
            }

            foreach (var field in HighlightedFields.EmptyIfNull())
            {
                index.Stores[field]      = FieldStorage.Yes;
                index.Indexes[field]     = FieldIndexing.Analyzed;
                index.TermVectors[field] = FieldTermVector.WithPositionsAndOffsets;
            }
            return(index);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Toes the index definition.
        /// </summary>
        public IndexDefinition ToIndexDefinition(DocumentConvention convention, bool validateMap = true)
        {
            if (Map == null && validateMap)
            {
                throw new InvalidOperationException(
                          string.Format("Map is required to generate an index, you cannot create an index without a valid Map property (in index {0}).", indexName));
            }

            try
            {
                if (Reduce != null)
                {
                    IndexDefinitionHelper.ValidateReduce(Reduce);
                }

                string querySource     = (typeof(TDocument) == typeof(object) || ContainsWhereEntityIs()) ? "docs" : "docs." + convention.GetTypeTagName(typeof(TDocument));
                var    indexDefinition = new IndexDefinition
                {
                    Reduce                     = IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <TDocument, TReduceResult>(Reduce, convention, "results", translateIdentityProperty: false),
                    Indexes                    = ConvertToStringDictionary(Indexes),
                    Stores                     = ConvertToStringDictionary(Stores),
                    SortOptions                = ConvertToStringDictionary(SortOptions),
                    Analyzers                  = ConvertToStringDictionary(Analyzers),
                    Suggestions                = ConvertToStringDictionary(Suggestions),
                    TermVectors                = ConvertToStringDictionary(TermVectors),
                    SpatialIndexes             = ConvertToStringDictionary(SpatialIndexes),
                    DisableInMemoryIndexing    = DisableInMemoryIndexing,
                    MaxIndexOutputsPerDocument = MaxIndexOutputsPerDocument,
                    LockMode                   = LockMode,
                };

                if (convention.PrettifyGeneratedLinqExpressions)
                {
                    indexDefinition.Reduce = IndexPrettyPrinter.TryFormat(indexDefinition.Reduce);
                }

                foreach (var indexesString in IndexesStrings)
                {
                    if (indexDefinition.Indexes.ContainsKey(indexesString.Key))
                    {
                        throw new InvalidOperationException("There is a duplicate key in indexes: " + indexesString.Key);
                    }
                    indexDefinition.Indexes.Add(indexesString);
                }

                foreach (var storeString in StoresStrings)
                {
                    if (indexDefinition.Stores.ContainsKey(storeString.Key))
                    {
                        throw new InvalidOperationException("There is a duplicate key in stores: " + storeString.Key);
                    }
                    indexDefinition.Stores.Add(storeString);
                }

                foreach (var analyzerString in AnalyzersStrings)
                {
                    if (indexDefinition.Analyzers.ContainsKey(analyzerString.Key))
                    {
                        throw new InvalidOperationException("There is a duplicate key in analyzers: " + analyzerString.Key);
                    }
                    indexDefinition.Analyzers.Add(analyzerString);
                }

                foreach (var termVectorString in TermVectorsStrings)
                {
                    if (indexDefinition.TermVectors.ContainsKey(termVectorString.Key))
                    {
                        throw new InvalidOperationException("There is a duplicate key in term vectors: " + termVectorString.Key);
                    }
                    indexDefinition.TermVectors.Add(termVectorString);
                }

                foreach (var spatialString in SpatialIndexesStrings)
                {
                    if (indexDefinition.SpatialIndexes.ContainsKey(spatialString.Key))
                    {
                        throw new InvalidOperationException("There is a duplicate key in spatial indexes: " + spatialString.Key);
                    }
                    indexDefinition.SpatialIndexes.Add(spatialString);
                }

                foreach (var sortOption in SortOptionsStrings)
                {
                    if (indexDefinition.SortOptions.ContainsKey(sortOption.Key))
                    {
                        throw new InvalidOperationException("There is a duplicate key in sort options: " + sortOption.Key);
                    }
                    indexDefinition.SortOptions.Add(sortOption);
                }

                if (Map != null)
                {
                    indexDefinition.Map = IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <TDocument, TReduceResult>(
                        Map, convention, querySource, translateIdentityProperty: true);

                    if (convention.PrettifyGeneratedLinqExpressions)
                    {
                        indexDefinition.Map = IndexPrettyPrinter.TryFormat(indexDefinition.Map);
                    }
                }
                return(indexDefinition);
            }
            catch (Exception e)
            {
                throw new IndexCompilationException("Failed to create index " + indexName, e);
            }
        }