Exemple #1
0
        /// <summary>
        /// Creates the index definition.
        /// </summary>
        /// <returns></returns>
        public override IndexDefinition CreateIndexDefinition()
        {
            if (Conventions == null)
            {
                Conventions = new DocumentConventions();
            }

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

            foreach (var map in _maps.Select(generateMap => generateMap()))
            {
                string formattedMap = map;
                if (Conventions.PrettifyGeneratedLinqExpressions)
                {
                    formattedMap = IndexPrettyPrinter.TryFormat(formattedMap);
                }
                indexDefinition.Maps.Add(formattedMap);
            }
            return(indexDefinition);
        }
Exemple #2
0
        public bool Equals(string x, string y)
        {
            if (x == y)
            {
                return(true);
            }
            if (x == null || y == null)
            {
                return(false);
            }

            var xFormatted = IndexPrettyPrinter.TryFormat(x);
            var yFormatted = IndexPrettyPrinter.TryFormat(y);

            return(xFormatted.Equals(yFormatted));
        }
        /// <summary>
        /// Creates the index definition.
        /// </summary>
        /// <returns></returns>
        public override IndexDefinition CreateIndexDefinition()
        {
            if (Conventions == null)
            {
                Conventions = new DocumentConventions();
            }

            var indexDefinition = new IndexDefinitionBuilder <object, TReduceResult>(IndexName)
            {
                Indexes                  = Indexes,
                Analyzers                = Analyzers,
                Reduce                   = Reduce,
                Stores                   = Stores,
                TermVectors              = TermVectors,
                SpatialIndexes           = SpatialIndexes,
                SuggestionsOptions       = IndexSuggestions,
                AnalyzersStrings         = AnalyzersStrings,
                IndexesStrings           = IndexesStrings,
                StoresStrings            = StoresStrings,
                TermVectorsStrings       = TermVectorsStrings,
                SpatialIndexesStrings    = SpatialIndexesStrings,
                OutputReduceToCollection = OutputReduceToCollection,
                PatternForOutputReduceToCollectionReferences = PatternForOutputReduceToCollectionReferences,
                PatternReferencesCollectionName = PatternReferencesCollectionName,
                AdditionalSources = AdditionalSources,
                Configuration     = Configuration,
                LockMode          = LockMode,
                Priority          = Priority
            }.ToIndexDefinition(Conventions, validateMap: false);

            foreach (var map in _maps.Select(generateMap => generateMap()))
            {
                string formattedMap = map;
#pragma warning disable CS0618 // Type or member is obsolete
                if (Conventions.PrettifyGeneratedLinqExpressions)
#pragma warning restore CS0618 // Type or member is obsolete
                {
                    formattedMap = IndexPrettyPrinter.TryFormat(formattedMap);
                }
                indexDefinition.Maps.Add(formattedMap);
            }
            return(indexDefinition);
        }
        /// <summary>
        /// Toes the index definition.
        /// </summary>
        public IndexDefinition ToIndexDefinition(DocumentConventions conventions, 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." + conventions.GetCollectionName(typeof(TDocument));
                var    indexDefinition = new IndexDefinition
                {
                    Name     = _indexName,
                    Reduce   = IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <TDocument, TReduceResult>(Reduce, conventions, "results", translateIdentityProperty: false),
                    LockMode = LockMode,
                    Priority = Priority,
                    OutputReduceToCollection = OutputReduceToCollection
                };

                var indexes            = ConvertToStringDictionary(Indexes);
                var stores             = ConvertToStringDictionary(Stores);
                var analyzers          = ConvertToStringDictionary(Analyzers);
                var suggestionsOptions = ConvertToStringSet(SuggestionsOptions).ToDictionary(x => x, x => true);
                var termVectors        = ConvertToStringDictionary(TermVectors);
                var spatialOptions     = ConvertToStringDictionary(SpatialIndexes);

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

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

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

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

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

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



                ApplyValues(indexDefinition, indexes, (options, value) => options.Indexing               = value);
                ApplyValues(indexDefinition, stores, (options, value) => options.Storage                 = value);
                ApplyValues(indexDefinition, analyzers, (options, value) => options.Analyzer             = value);
                ApplyValues(indexDefinition, termVectors, (options, value) => options.TermVector         = value);
                ApplyValues(indexDefinition, spatialOptions, (options, value) => options.Spatial         = value);
                ApplyValues(indexDefinition, suggestionsOptions, (options, value) => options.Suggestions = value);

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

                    indexDefinition.Maps.Add(conventions.PrettifyGeneratedLinqExpressions ? IndexPrettyPrinter.TryFormat(map) : map);
                }

                indexDefinition.AdditionalSources = AdditionalSources;

                return(indexDefinition);
            }
            catch (Exception e)
            {
                throw new IndexCompilationException("Failed to create index " + _indexName, e);
            }
        }
Exemple #5
0
 public int GetHashCode(string obj)
 {
     return(IndexPrettyPrinter.TryFormat(obj).GetHashCode());
 }