public async Task CreateCollectionsIfNotExist(IMongoDatabase db, string locale)
        {
            foreach (CollectionName collectionName in (CollectionName[])Enum.GetValues(typeof(CollectionName)))
            {
                IAsyncCursor <BsonDocument> collections = await db.ListCollectionsAsync(new ListCollectionsOptions
                {
                    Filter = new BsonDocument("name", collectionName.ToCollectionName())
                })
                                                          .AnyContext();

                if (!await collections.AnyAsync().AnyContext())
                {
                    await db.CreateCollectionAsync(collectionName.ToCollectionName(),
                                                   new CreateCollectionOptions { Collation = new Collation(locale, numericOrdering: true) })
                    .AnyContext();
                }

                IIndexBuilder builder;
                switch (collectionName)
                {
                case CollectionName.Addresses:
                    builder = new AddressIndexBuilder(db);
                    break;

                case CollectionName.AddressTransactions:
                    builder = new AddressTransactionIndexBuilder(db);
                    break;

                case CollectionName.Blocks:
                    builder = new BlockIndexBuilder(db);
                    break;

                case CollectionName.Contracts:
                    builder = new ContractIndexBuilder(db);
                    break;

                case CollectionName.Transactions:
                    builder = new TransactionIndexBuilder(db);
                    break;

                case CollectionName.TransactionLogs:
                    builder = new TransactionLogIndexBuilder(db);
                    break;

                case CollectionName.Promotions:
                    builder = new PromotionIndexBuilder(db);
                    break;

                default:
                    continue;
                }

                builder.EnsureIndexes();
            }
        }
Beispiel #2
0
        public async Task CreateCollectionsIfNotExist(IMongoDatabase db, string locale)
        {
            foreach (var collectionName in (MongoDbCollectionName[])Enum.GetValues(typeof(MongoDbCollectionName)))
            {
                var collections = await db.ListCollectionsAsync(new ListCollectionsOptions
                                                                { Filter = new BsonDocument("name", collectionName.ToString()) });

                if (!await collections.AnyAsync())
                {
                    await db.CreateCollectionAsync(collectionName.ToString(),
                                                   new CreateCollectionOptions()
                    {
                        Collation = new Collation(locale, numericOrdering: true)
                    });
                }

                IIndexBuilder builder;
                switch (collectionName)
                {
                case MongoDbCollectionName.AddressTransactions:
                    builder = new AddressTransactionIndexBuilder(db);
                    break;

                case MongoDbCollectionName.BlockProgress:
                    builder = new BlockProgressIndexBuilder(db);
                    break;

                case MongoDbCollectionName.Blocks:
                    builder = new BlockIndexBuilder(db);
                    break;

                case MongoDbCollectionName.Contracts:
                    builder = new ContractIndexBuilder(db);
                    break;

                case MongoDbCollectionName.Transactions:
                    builder = new TransactionIndexBuilder(db);
                    break;

                case MongoDbCollectionName.TransactionLogs:
                    builder = new TransactionLogIndexBuilder(db);
                    break;

                case MongoDbCollectionName.TransactionVMStacks:
                    builder = new TransactionVmStackIndexBuilder(db);
                    break;

                default:
                    return;
                }

                builder.EnsureIndexes();
            }
        }