Esempio n. 1
0
        public UnifiedTargetedFailPointOperation Build(BsonDocument arguments)
        {
            BsonDocument         failPointCommand = null;
            IClientSessionHandle session          = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "failPoint":
                    failPointCommand = argument.Value.AsBsonDocument;
                    break;

                case "session":
                    var sessionId = argument.Value.AsString;
                    session = _entityMap.GetSession(sessionId);
                    break;

                default:
                    throw new FormatException($"Invalid TargetedFailPointOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedTargetedFailPointOperation(session, failPointCommand));
        }
Esempio n. 2
0
        public UnifiedCreateIndexOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            BsonDocument         keys    = null;
            CreateIndexOptions   options = null;
            IClientSessionHandle session = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "keys":
                    keys = argument.Value.AsBsonDocument;
                    break;

                case "name":
                    options      = options ?? new CreateIndexOptions();
                    options.Name = argument.Value.AsString;
                    break;

                case "session":
                    var sessionId = argument.Value.AsString;
                    session = _entityMap.GetSession(sessionId);
                    break;

                default:
                    throw new FormatException($"Invalid CreateIndexOperation argument name: '{argument.Name}'.");
                }
            }

            var createIndexModel = new CreateIndexModel <BsonDocument>(keys, options);

            return(new UnifiedCreateIndexOperation(session, collection, createIndexModel));
        }
        public UnifiedUpdateManyOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            FilterDefinition <BsonDocument> filter  = null;
            UpdateOptions                   options = null;
            IClientSessionHandle            session = null;
            UpdateDefinition <BsonDocument> update  = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "filter":
                    filter = argument.Value.AsBsonDocument;
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                case "update":
                    update = argument.Value.AsBsonDocument;
                    break;

                default:
                    throw new FormatException($"Invalid UpdateManyOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedUpdateManyOperation(session, collection, filter, update, options));
        }
Esempio n. 4
0
        public UnifiedFindOneAndUpdateOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            FilterDefinition <BsonDocument>        filter  = null;
            FindOneAndUpdateOptions <BsonDocument> options = null;
            UpdateDefinition <BsonDocument>        update  = null;
            IClientSessionHandle session = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "filter":
                    filter = new BsonDocumentFilterDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                case "hint":
                    options ??= new FindOneAndUpdateOptions <BsonDocument>();
                    options.Hint = argument.Value;
                    break;

                case "returnDocument":
                    options ??= new FindOneAndUpdateOptions <BsonDocument>();
                    options.ReturnDocument = (ReturnDocument)Enum.Parse(typeof(ReturnDocument), argument.Value.AsString);
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                case "sort":
                    options ??= new FindOneAndUpdateOptions <BsonDocument>();
                    options.Sort = new BsonDocumentSortDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                case "update":
                    switch (argument.Value)
                    {
                    case BsonDocument:
                        update = argument.Value.AsBsonDocument;
                        break;

                    case BsonArray:
                        update = PipelineDefinition <BsonDocument, BsonDocument> .Create(argument.Value.AsBsonArray.Cast <BsonDocument>());

                        break;

                    default:
                        throw new FormatException($"Invalid FindOneAndUpdateOperation update argument: '{argument.Value}'.");
                    }
                    break;

                default:
                    throw new FormatException($"Invalid FindOneAndUpdateOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedFindOneAndUpdateOperation(collection, filter, update, session, options));
        }
Esempio n. 5
0
        public UnifiedUpdateManyOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            FilterDefinition <BsonDocument> filter  = null;
            UpdateOptions                   options = null;
            IClientSessionHandle            session = null;
            UpdateDefinition <BsonDocument> update  = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "comment":
                    options ??= new UpdateOptions();
                    options.Comment = argument.Value;
                    break;

                case "filter":
                    filter = argument.Value.AsBsonDocument;
                    break;

                case "hint":
                    options ??= new UpdateOptions();
                    options.Hint = argument.Value;
                    break;

                case "let":
                    options ??= new UpdateOptions();
                    options.Let = argument.Value.AsBsonDocument;
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                case "update":
                    switch (argument.Value)
                    {
                    case BsonDocument:
                        update = argument.Value.AsBsonDocument;
                        break;

                    case BsonArray:
                        update = PipelineDefinition <BsonDocument, BsonDocument> .Create(argument.Value.AsBsonArray.Cast <BsonDocument>());

                        break;

                    default:
                        throw new FormatException($"Invalid BulkWrite Update model update argument: '{argument.Value}'.");
                    }
                    break;

                default:
                    throw new FormatException($"Invalid UpdateManyOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedUpdateManyOperation(session, collection, filter, update, options));
        }
        public UnifiedInsertManyOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            List <BsonDocument>  documents = null;
            InsertManyOptions    options   = null;
            IClientSessionHandle session   = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "documents":
                    documents = argument.Value.AsBsonArray.Cast <BsonDocument>().ToList();
                    break;

                case "ordered":
                    options           = options ?? new InsertManyOptions();
                    options.IsOrdered = argument.Value.AsBoolean;
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                default:
                    throw new FormatException($"Invalid InsertManyOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedInsertManyOperation(session, collection, documents, options));
        }
Esempio n. 7
0
        public UnifiedInsertOneOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            BsonDocument         document = null;
            InsertOneOptions     options  = null;
            IClientSessionHandle session  = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "bypassDocumentValidation":
                    options = options ?? new InsertOneOptions();
                    options.BypassDocumentValidation = argument.Value.AsBoolean;
                    break;

                case "document":
                    document = argument.Value.AsBsonDocument;
                    break;

                case "session":
                    var sessionId = argument.Value.AsString;
                    session = _entityMap.GetSession(sessionId);
                    break;

                default:
                    throw new FormatException($"Invalid InsertOneOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedInsertOneOperation(session, collection, document, options));
        }
Esempio n. 8
0
        public UnifiedBulkWriteOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            BulkWriteOptions options = null;
            List <WriteModel <BsonDocument> > requests = null;
            IClientSessionHandle session = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "ordered":
                    options           = options ?? new BulkWriteOptions();
                    options.IsOrdered = argument.Value.AsBoolean;
                    break;

                case "requests":
                    requests = ParseWriteModels(argument.Value.AsBsonArray.Cast <BsonDocument>());
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                default:
                    throw new FormatException($"Invalid BulkWriteOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedBulkWriteOperation(session, collection, requests, options));
        }
Esempio n. 9
0
        public UnifiedCreateFindCursorOperation Build(string targetDatabaseId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetDatabaseId);

            IClientSessionHandle session = null;
            BsonDocument         filter  = null;
            var findOptions = new FindOptions <BsonDocument>();

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "filter":
                    filter = argument.Value.AsBsonDocument;
                    break;

                case "batchSize":
                    findOptions.BatchSize = argument.Value.AsInt32;
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.ToString());
                    break;

                default:
                    throw new FormatException($"Invalid {nameof(UnifiedCreateFindCursorOperation)} argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedCreateFindCursorOperation(session, collection, filter, findOptions));
        }
Esempio n. 10
0
        public UnifiedRunCommandOperation Build(string targetDatabaseId, BsonDocument arguments)
        {
            var database = _entityMap.GetDatabase(targetDatabaseId);

            string               commandName = null;
            BsonDocument         command     = null;
            IClientSessionHandle session     = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "commandName":
                    commandName = argument.Value.AsString;
                    break;

                case "command":
                    command = argument.Value.AsBsonDocument;
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                default:
                    throw new FormatException($"Invalid RunCommandOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedRunCommandOperation(database, commandName, command, session));
        }
Esempio n. 11
0
        public UnifiedDistinctOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            string fieldName = null;
            FilterDefinition <BsonDocument> filter  = null;
            IClientSessionHandle            session = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "fieldName":
                    fieldName = argument.Value.AsString;
                    break;

                case "filter":
                    filter = argument.Value.AsBsonDocument;
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                default:
                    throw new FormatException($"Invalid DistinctOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedDistinctOperation(collection, fieldName, filter, session));
        }
Esempio n. 12
0
        public UnifiedListIndexesOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            var listIndexesOptions       = new ListIndexesOptions();
            IClientSessionHandle session = null;

            if (arguments != null)
            {
                foreach (var argument in arguments)
                {
                    switch (argument.Name)
                    {
                    case "batchSize":
                        listIndexesOptions.BatchSize = argument.Value.ToInt32();
                        break;

                    case "session":
                        session = _entityMap.GetSession(argument.Value.AsString);
                        break;

                    default:
                        throw new FormatException($"Invalid {nameof(UnifiedListIndexesOperation)} argument name: '{argument.Name}'.");
                    }
                }
            }

            return(new UnifiedListIndexesOperation(collection, listIndexesOptions, session));
        }
        public UnifiedCountDocumentsOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            FilterDefinition <BsonDocument> filter = null;
            CountOptions         options           = null;
            IClientSessionHandle session           = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "comment":
                    options ??= new CountOptions();
                    options.Comment = argument.Value;
                    break;

                case "filter":
                    filter = new BsonDocumentFilterDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                default:
                    throw new FormatException($"Invalid CountDocumentsOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedCountDocumentsOperation(collection, filter, options, session));
        }
        public UnifiedCreateCollectionOperation Build(string targetDatabaseId, BsonDocument arguments)
        {
            var database = _entityMap.GetDatabase(targetDatabaseId);

            string collectionName        = null;
            IClientSessionHandle session = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "collection":
                    collectionName = argument.Value.AsString;
                    break;

                case "session":
                    var sessionId = argument.Value.AsString;
                    session = _entityMap.GetSession(sessionId);
                    break;

                default:
                    throw new FormatException($"Invalid CreateCollectionOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedCreateCollectionOperation(session, database, collectionName));
        }
        public UnifiedListCollectionsOperation Build(string targetDatabaseId, BsonDocument arguments)
        {
            var database = _entityMap.GetDatabase(targetDatabaseId);

            var listCollectionsOptions   = new ListCollectionsOptions();
            IClientSessionHandle session = null;

            if (arguments != null)
            {
                foreach (var argument in arguments)
                {
                    switch (argument.Name)
                    {
                    case "filter":
                        listCollectionsOptions.Filter = argument.Value.AsBsonDocument;
                        break;

                    case "batchSize":
                        listCollectionsOptions.BatchSize = argument.Value.ToInt32();
                        break;

                    case "session":
                        session = _entityMap.GetSession(argument.Value.AsString);
                        break;

                    default:
                        throw new FormatException($"Invalid AssertIndexNotExistsOperation argument name: '{argument.Name}'.");
                    }
                }
            }

            return(new UnifiedListCollectionsOperation(database, listCollectionsOptions, session));
        }
        private IUnifiedEntityTestOperation Build(IMongoDatabase database, IMongoCollection <BsonDocument> collection, BsonDocument arguments)
        {
            AggregateOptions     options  = null;
            List <BsonDocument>  pipeline = null;
            IClientSessionHandle session  = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "allowDiskUse":
                    options ??= new AggregateOptions();
                    options.AllowDiskUse = argument.Value.AsBoolean;
                    break;

                case "batchSize":
                    options ??= new AggregateOptions();
                    options.BatchSize = argument.Value.ToInt32();
                    break;

                case "comment":
                    options ??= new AggregateOptions();
                    options.Comment = argument.Value;
                    break;

                case "let":
                    options ??= new AggregateOptions();
                    options.Let = argument.Value.AsBsonDocument;
                    break;

                case "pipeline":
                    pipeline = argument.Value.AsBsonArray.Cast <BsonDocument>().ToList();
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                default:
                    throw new FormatException($"Invalid AggregateOperation argument name: '{argument.Name}'.");
                }
            }

            var lastStageName = pipeline.LastOrDefault()?.GetElement(0).Name;

            if (lastStageName == "$out" || lastStageName == "$merge")
            {
                return(collection == null ?
                       new UnifiedDatabaseAggregateToCollectionOperation(session, database, pipeline, options) :
                       new UnifiedCollectionAggregateToCollectionOperation(session, collection, pipeline, options));
            }
            else
            {
                return(collection == null ?
                       new UnifiedDatabaseAggregateOperation(session, database, pipeline, options) :
                       new UnifiedCollectionAggregateOperation(session, collection, pipeline, options));
            }
        }
Esempio n. 17
0
        public UnifiedFindOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            FilterDefinition <BsonDocument> filter  = null;
            FindOptions <BsonDocument>      options = null;
            IClientSessionHandle            session = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "allowDiskUse":
                    options ??= new FindOptions <BsonDocument>();
                    options.AllowDiskUse = argument.Value.AsBoolean;
                    break;

                case "batchSize":
                    options ??= new FindOptions <BsonDocument>();
                    options.BatchSize = argument.Value.AsInt32;
                    break;

                case "comment":
                    options ??= new FindOptions <BsonDocument>();
                    options.Comment = argument.Value;
                    break;

                case "filter":
                    filter = new BsonDocumentFilterDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                case "let":
                    options ??= new FindOptions <BsonDocument>();
                    options.Let = argument.Value.AsBsonDocument;
                    break;

                case "limit":
                    options ??= new FindOptions <BsonDocument>();
                    options.Limit = argument.Value.AsInt32;
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                case "sort":
                    options ??= new FindOptions <BsonDocument>();
                    options.Sort = new BsonDocumentSortDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                default:
                    throw new FormatException($"Invalid FindOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedFindOperation(collection, filter, session, options));
        }
        public UnifiedAbortTransactionOperation Build(string targetSessionId, BsonDocument arguments)
        {
            var session = _entityMap.GetSession(targetSessionId);

            if (arguments != null)
            {
                throw new FormatException("AbortTransactionOperation is not expected to contain arguments.");
            }

            return(new UnifiedAbortTransactionOperation(session));
        }
Esempio n. 19
0
        public IUnifiedEntityTestOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            AggregateOptions options = null;
            PipelineDefinition <BsonDocument, BsonDocument> pipeline = null;
            IClientSessionHandle session = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "batchSize":
                    options ??= new AggregateOptions();
                    options.BatchSize = argument.Value.ToInt32();
                    break;

                case "let":
                    options ??= new AggregateOptions();
                    options.Let = argument.Value.AsBsonDocument;
                    break;

                case "pipeline":
                    var stages = argument.Value.AsBsonArray.Cast <BsonDocument>();
                    pipeline = new BsonDocumentStagePipelineDefinition <BsonDocument, BsonDocument>(stages);
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                default:
                    throw new FormatException($"Invalid AggregateOperation argument name: '{argument.Name}'.");
                }
            }

            var lastStageName = pipeline.Stages.LastOrDefault()?.OperatorName;

            if (lastStageName == "$out" || lastStageName == "$merge")
            {
                return(new UnifiedAggregateToCollectionOperation(collection, pipeline, options));
            }
            else
            {
                return(new UnifiedAggregateOnCollectionOperation(collection, pipeline, options, session));
            }
        }
Esempio n. 20
0
        public UnifiedCreateCollectionOperation Build(string targetDatabaseId, BsonDocument arguments)
        {
            var database = _entityMap.GetDatabase(targetDatabaseId);

            string collectionName = null;
            CreateCollectionOptions createCollectionOptions = null;
            IClientSessionHandle    session = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "collection":
                    collectionName = argument.Value.AsString;
                    break;

                case "expireAfterSeconds":
                    createCollectionOptions ??= new CreateCollectionOptions();
                    createCollectionOptions.ExpireAfter = TimeSpan.FromSeconds(argument.Value.ToInt64());
                    break;

                case "session":
                    var sessionId = argument.Value.AsString;
                    session = _entityMap.GetSession(sessionId);
                    break;

                case "timeseries":
                    var timeseries = argument.Value.AsBsonDocument;
                    var timeField  = timeseries["timeField"].AsString;
                    var metaField  = timeseries.TryGetValue("metaField", out var metaFieldValue) ? metaFieldValue.AsString : null;
                    TimeSeriesGranularity?granularity = null;
                    if (timeseries.TryGetValue("granularity", out var granularityValue))
                    {
                        granularity = (TimeSeriesGranularity)Enum.Parse(typeof(TimeSeriesGranularity), granularityValue.AsString, true);
                    }
                    createCollectionOptions ??= new CreateCollectionOptions();
                    createCollectionOptions.TimeSeriesOptions = new TimeSeriesOptions(timeField, metaField, granularity);
                    break;

                default:
                    throw new FormatException($"Invalid CreateCollectionOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedCreateCollectionOperation(session, database, collectionName, createCollectionOptions));
        }
Esempio n. 21
0
        public UnifiedAssertSessionUnpinnedOperation Build(BsonDocument arguments)
        {
            IClientSessionHandle session = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                default:
                    throw new FormatException($"Invalid AssertSessionUnpinnedOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedAssertSessionUnpinnedOperation(session));
        }
Esempio n. 22
0
        public UnifiedListDatabasesOperation Build(string targetClientId, BsonDocument arguments)
        {
            var client = _entityMap.GetClient(targetClientId);
            IClientSessionHandle session = null;

            if (arguments != null)
            {
                foreach (var argument in arguments)
                {
                    switch (argument.Name)
                    {
                    case "session":
                        session = _entityMap.GetSession(argument.Value.AsString);
                        break;

                    default:
                        throw new FormatException($"Invalid ListDatabasesOperation argument name: '{argument.Name}'.");
                    }
                }
            }

            return(new UnifiedListDatabasesOperation(client, session));
        }
Esempio n. 23
0
        public UnifiedWithTransactionOperation Build(string targetSessionId, BsonDocument arguments)
        {
            var session = _entityMap.GetSession(targetSessionId);

            BsonArray          operations = null;
            TransactionOptions options    = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "callback":
                    operations = argument.Value.AsBsonArray;
                    break;

                case "readConcern":
                    options = options ?? new TransactionOptions();
                    options = options.With(readConcern: ReadConcern.FromBsonDocument(argument.Value.AsBsonDocument));
                    break;

                case "readPreference":
                    options = options ?? new TransactionOptions();
                    options = options.With(readPreference: ReadPreference.FromBsonDocument(argument.Value.AsBsonDocument));
                    break;

                case "writeConcern":
                    options = options ?? new TransactionOptions();
                    options = options.With(writeConcern: WriteConcern.FromBsonDocument(argument.Value.AsBsonDocument));
                    break;

                default:
                    throw new FormatException($"Invalid WithTransactionOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedWithTransactionOperation(session, operations, options));
        }
Esempio n. 24
0
        public UnifiedAssertSessionTransactionStateOperation Build(BsonDocument arguments)
        {
            CoreTransactionState?coreTransactionState = null;
            IClientSessionHandle session = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                case "state":
                    coreTransactionState = (CoreTransactionState)Enum.Parse(typeof(CoreTransactionState), argument.Value.AsString, ignoreCase: true);
                    break;

                default:
                    throw new FormatException($"Invalid AssertSessionTransactionStateOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedAssertSessionTransactionStateOperation(session, coreTransactionState.Value));
        }
Esempio n. 25
0
        public IUnifiedEntityTestOperation Build(string targetDatabaseId, BsonDocument arguments)
        {
            var database = _entityMap.GetDatabase(targetDatabaseId);

            string name   = null;
            string viewOn = null;
            PipelineDefinition <BsonDocument, BsonDocument> pipeline = null;
            IClientSessionHandle session           = null;
            TimeSpan?            expireAfter       = null;
            TimeSeriesOptions    timeSeriesOptions = null;
            ClusteredIndexOptions <BsonDocument> clusteredIndex = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "clusteredIndex":
                    var clusteredIndexSpecification = argument.Value.AsBsonDocument;
                    clusteredIndex = new ClusteredIndexOptions <BsonDocument>
                    {
                        Key    = clusteredIndexSpecification["key"].AsBsonDocument,
                        Unique = clusteredIndexSpecification["unique"].AsBoolean,
                        Name   = clusteredIndexSpecification["name"].AsString
                    };
                    break;

                case "collection":
                    name = argument.Value.AsString;
                    break;

                case "expireAfterSeconds":
                    expireAfter = TimeSpan.FromSeconds(argument.Value.ToInt64());
                    break;

                case "pipeline":
                    pipeline = new EmptyPipelineDefinition <BsonDocument>();
                    foreach (var stage in argument.Value.AsBsonArray)
                    {
                        pipeline = pipeline.AppendStage <BsonDocument, BsonDocument, BsonDocument>(stage.AsBsonDocument);
                    }
                    break;

                case "session":
                    var sessionId = argument.Value.AsString;
                    session = _entityMap.GetSession(sessionId);
                    break;

                case "timeseries":
                    var timeseries = argument.Value.AsBsonDocument;
                    var timeField  = timeseries["timeField"].AsString;
                    var metaField  = timeseries.TryGetValue("metaField", out var metaFieldValue) ? metaFieldValue.AsString : null;
                    TimeSeriesGranularity?granularity = null;
                    if (timeseries.TryGetValue("granularity", out var granularityValue))
                    {
                        granularity = (TimeSeriesGranularity)Enum.Parse(typeof(TimeSeriesGranularity), granularityValue.AsString, true);
                    }
                    timeSeriesOptions = new TimeSeriesOptions(timeField, metaField, granularity);
                    break;

                case "viewOn":
                    viewOn = argument.Value.AsString;
                    break;

                default:
                    throw new FormatException($"Invalid CreateCollectionOperation argument name: '{argument.Name}'.");
                }
            }

            if (viewOn == null && pipeline == null)
            {
                var options = new CreateCollectionOptions <BsonDocument> {
                    ExpireAfter = expireAfter, TimeSeriesOptions = timeSeriesOptions, ClusteredIndex = clusteredIndex
                };
                return(new UnifiedCreateCollectionOperation(session, database, name, options));
            }
            if (viewOn != null && expireAfter == null && timeSeriesOptions == null && clusteredIndex == null)
            {
                var options = new CreateViewOptions <BsonDocument>();
                return(new UnifiedCreateViewOperation(session, database, name, viewOn, pipeline, options));
            }

            var invalidArguments = string.Join(",", arguments.Elements.Select(x => x.Name));

            throw new InvalidOperationException($"Invalid combination of CreateCollectionOperation arguments: {invalidArguments}");
        }