public MongoDocumentStorageProvider(MongoConnection connection, string documentType)
        {
            DocumentType = documentType;

            _collection    = new Lazy <IMongoCollection <DynamicWrapper> >(() => connection.GetDatabase().GetCollection <DynamicWrapper>(documentType));
            _filterBuilder = new MongoDocumentFilterBuilder <DynamicWrapper>();
        }
예제 #2
0
        /// <summary>
        /// Создает хранилище данных для заданного типа документа.
        /// </summary>
        /// <param name="documentMetadata">Метаданные документа.</param>
        public async Task CreateStorageAsync(DocumentMetadata documentMetadata)
        {
            var collectionName = documentMetadata.Type;

            var database = _connection.GetDatabase();

            // Создание коллекции документов, если она еще не создана
            if (!await CollectionExistsAsync(database, collectionName))
            {
                await database.CreateCollectionAsync(collectionName);
            }

            var collection = database.GetCollection <BsonDocument>(collectionName);

            // Получение актуального списка индексов коллекции документов
            var actualIndexes = await GetCollectionIndexesAsync(collection);

            // Получение требуемого списка индексов коллекции документов
            var neededIndexes = SystemIndexes.Union((documentMetadata.Indexes ?? new DocumentIndex[] { })).ToArray();

            // Удаление неактуальных индексов

            var dropIndexes = actualIndexes.Except(neededIndexes).Distinct();

            foreach (var dropIndex in dropIndexes)
            {
                await collection.Indexes.DropOneAsync(dropIndex.Name);
            }

            // Создание недостающих индексов

            var createIndexes = neededIndexes.Except(actualIndexes).Distinct();

            foreach (var createIndex in createIndexes)
            {
                if (createIndex.Key != null && createIndex.Key.Count > 0)
                {
                    var keysDefinitionList = new List <IndexKeysDefinition <BsonDocument> >(createIndex.Key.Count);

                    foreach (var property in createIndex.Key)
                    {
                        switch (property.Value)
                        {
                        case DocumentIndexKeyType.Asc:
                            keysDefinitionList.Add(Builders <BsonDocument> .IndexKeys.Ascending(property.Key));
                            break;

                        case DocumentIndexKeyType.Desc:
                            keysDefinitionList.Add(Builders <BsonDocument> .IndexKeys.Descending(property.Key));
                            break;

                        case DocumentIndexKeyType.Text:
                            keysDefinitionList.Add(Builders <BsonDocument> .IndexKeys.Text(property.Key));
                            break;
                        }
                    }

                    var keysDefinition = (keysDefinitionList.Count == 1) ? keysDefinitionList[0] : Builders <BsonDocument> .IndexKeys.Combine(keysDefinitionList);

                    CreateIndexOptions indexOptions = null;

                    if (createIndex.Unique || !string.IsNullOrEmpty(createIndex.Name))
                    {
                        indexOptions = new CreateIndexOptions
                        {
                            Unique = createIndex.Unique,
                            Name   = createIndex.Name
                        };
                    }

                    if (indexOptions != null)
                    {
                        await collection.Indexes.CreateOneAsync(keysDefinition, indexOptions);
                    }
                    else
                    {
                        await collection.Indexes.CreateOneAsync(keysDefinition);
                    }
                }
            }
        }