Example #1
0
        protected IDictionary <string, ISet <string> > BuildCollectionIndexMap(ISet <string> requestedCollections, IParserFactory parserFactory)
        {
            // Maps collection names to the set of defined indexed fields for that collection
            var collections = new Dictionary <string, ISet <string> >();

            // Stuff collection names & indexes into the dictionary, deduping in the process.
            foreach (var parser in parserFactory.GetAllParsers())
            {
                var collectionName = parser.CollectionSchema.CollectionName.ToLowerInvariant();

                if (!collections.ContainsKey(collectionName) && requestedCollections.Contains(collectionName))
                {
                    collections.Add(collectionName, new HashSet <string>());
                }

                // Add indexes.
                if (collections.ContainsKey(collectionName))
                {
                    foreach (var index in parser.CollectionSchema.Indexes)
                    {
                        collections[collectionName].Add(index);
                    }
                }
            }

            return(collections);
        }
Example #2
0
        /// <summary>
        /// Creates all of the required MongoDB collections that this logset requires.
        /// </summary>
        private void CreateMongoDbCollections()
        {
            var collections = new Dictionary <string, HashSet <string> >();

            ISet <IParser> parsers = parserFactory.GetAllParsers();

            // Stuff collection names & indexes into the dictionary, deduping in the process.
            foreach (var parser in parsers)
            {
                var            collectionName = parser.CollectionSchema.CollectionName.ToLowerInvariant();
                IList <string> indexes        = parser.CollectionSchema.Indexes;

                if (!collections.ContainsKey(collectionName))
                {
                    if (LogsetDependencyHelper.IsCollectionRequiredForRequest(collectionName, logsharkRequest))
                    {
                        collections.Add(collectionName, new HashSet <string>());
                    }
                }

                // Add indexes.
                if (collections.ContainsKey(collectionName))
                {
                    foreach (var index in indexes)
                    {
                        if (collections.ContainsKey(collectionName))
                        {
                            collections[collectionName].Add(index);
                        }
                    }
                }
            }

            // New up collections & indexes using the dictionary.
            foreach (var collection in collections)
            {
                var           collectionName = collection.Key;
                ISet <string> indexes        = collection.Value;

                var dbCollection = database.GetCollection <BsonDocument>(collectionName);
                logsharkRequest.RunContext.CollectionsGenerated.Add(collectionName);

                foreach (var index in indexes)
                {
                    var indexKeysBuilder            = new IndexKeysDefinitionBuilder <BsonDocument>();
                    CreateIndexOptions indexOptions = new CreateIndexOptions {
                        Sparse = false
                    };
                    dbCollection.Indexes.CreateOne(indexKeysBuilder.Ascending(index), indexOptions);
                }

                // If we are working against a sharded Mongo cluster, we need to explicitly shard each collection.
                MongoConnectionInfo mongoConnectionInfo = logsharkRequest.Configuration.MongoConnectionInfo;
                if (mongoConnectionInfo.ConnectionType == MongoConnectionType.ShardedCluster)
                {
                    MongoAdminUtil.EnableShardingOnCollectionIfNotEnabled(mongoConnectionInfo.GetClient(), logsharkRequest.RunContext.MongoDatabaseName, collectionName);
                }
            }
        }