Ejemplo n.º 1
0
        /// <summary>
        /// Creates all of the required MongoDB collections that this logset requires.
        /// </summary>
        private void CreateMongoDbCollections()
        {
            var collections = new Dictionary <string, HashSet <string> >();

            IEnumerable <IParser> parsers;

            if (logsharkRequest.RunContext.LogsetType == LogsetType.Desktop)
            {
                parsers = ParserFactory.GetDesktopParsers();
            }
            else
            {
                parsers = ParserFactory.GetServerParsers();
            }

            // 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);
                }
            }
        }