Example #1
0
        public MongoCollection <T> GetCollection <T>(string collectionName, IMongoCollectionOptions options) where T : class
        {
            if (!_database.GetCollectionNames().Contains(collectionName))
            {
                _database.CreateCollection(collectionName, options);
            }

            return(_database.GetCollection <T>(collectionName));
        }
Example #2
0
        /// <summary>
        /// Construct a sink posting to the specified database.
        /// </summary>
        /// <param name="databaseUrl">The URL of a CouchDB database.</param>
        /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
        /// <param name="period">The time to wait between checking for event batches.</param>
        /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        /// <param name="collectionName">Name of the MongoDb collection to use for the log. Default is "log".</param>
        /// <param name="collectionCreationOptions">Collection Creation Options for the log collection creation.</param>
        public MongoDBSink(string databaseUrl, int batchPostingLimit, TimeSpan period, IFormatProvider formatProvider, string collectionName, IMongoCollectionOptions collectionCreationOptions)
            : base(batchPostingLimit, period)
        {
            if (databaseUrl == null) throw new ArgumentNullException("databaseUrl");

            _collectionName = collectionName;
            _collectionCreationOptions = collectionCreationOptions;
            _formatProvider = formatProvider;
            _mongoUrl = new MongoUrl(databaseUrl);
        }
Example #3
0
        /// <summary>
        /// Creates a collection. MongoDB creates collections automatically when they are first used, so
        /// you only need to call this method if you want to provide non-default options.
        /// </summary>
        /// <param name="collectionName">The name of the collection.</param>
        /// <param name="options">Options for creating this collection (usually a CollectionOptionsDocument or constructed using the CollectionOptions builder).</param>
        /// <returns>A CommandResult.</returns>
        public virtual CommandResult CreateCollection(string collectionName, IMongoCollectionOptions options)
        {
            var command = new CommandDocument("create", collectionName);

            if (options != null)
            {
                command.Merge(options.ToBsonDocument());
            }
            return(RunCommand(command));
        }
Example #4
0
        /// <summary>
        /// Construct a sink posting to the specified database.
        /// </summary>
        /// <param name="databaseUrl">The URL of a CouchDB database.</param>
        /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
        /// <param name="period">The time to wait between checking for event batches.</param>
        /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        /// <param name="collectionName">Name of the MongoDb collection to use for the log. Default is "log".</param>
        /// <param name="collectionCreationOptions">Collection Creation Options for the log collection creation.</param>
        public MongoDBSink(string databaseUrl, int batchPostingLimit, TimeSpan period, IFormatProvider formatProvider, string collectionName, IMongoCollectionOptions collectionCreationOptions)
            : base(batchPostingLimit, period)
        {
            if (databaseUrl == null)
            {
                throw new ArgumentNullException("databaseUrl");
            }

            _collectionName            = collectionName;
            _collectionCreationOptions = collectionCreationOptions;
            _formatProvider            = formatProvider;
            _mongoUrl = new MongoUrl(databaseUrl);
        }
        private CommandResult CreateCollection(IClientSessionHandle session, string collectionName, IMongoCollectionOptions options)
        {
            if (collectionName == null)
            {
                throw new ArgumentNullException("collectionName");
            }

            var                      collectionNamespace    = new CollectionNamespace(_namespace, collectionName);
            var                      messageEncoderSettings = GetMessageEncoderSettings();
            bool?                    autoIndexId            = null;
            bool?                    capped              = null;
            Collation                collation           = null;
            BsonDocument             indexOptionDefaults = null;
            int?                     maxDocuments        = null;
            long?                    maxSize             = null;
            bool?                    noPadding           = null;
            BsonDocument             storageEngine       = null;
            bool?                    usePowerOf2Sizes    = null;
            DocumentValidationAction?validationAction    = null;
            DocumentValidationLevel? validationLevel     = null;
            BsonDocument             validator           = null;

            if (options != null)
            {
                var optionsDocument = options.ToBsonDocument();

                BsonValue value;
                if (optionsDocument.TryGetValue("autoIndexId", out value))
                {
                    autoIndexId = value.ToBoolean();
                }
                if (optionsDocument.TryGetValue("capped", out value))
                {
                    capped = value.ToBoolean();
                }
                if (optionsDocument.TryGetValue("collation", out value))
                {
                    collation = Collation.FromBsonDocument(value.AsBsonDocument);
                }
                if (optionsDocument.TryGetValue("indexOptionDefaults", out value))
                {
                    indexOptionDefaults = value.AsBsonDocument;
                }
                if (optionsDocument.TryGetValue("max", out value))
                {
                    maxDocuments = value.ToInt32();
                }
                if (optionsDocument.TryGetValue("flags", out value))
                {
                    noPadding = ((CollectionUserFlags)value.ToInt32() & CollectionUserFlags.NoPadding) != 0;
                }
                if (optionsDocument.TryGetValue("size", out value))
                {
                    maxSize = value.ToInt64();
                }
                if (optionsDocument.TryGetValue("storageEngine", out value))
                {
                    storageEngine = value.AsBsonDocument;
                }
                if (optionsDocument.TryGetValue("flags", out value))
                {
                    usePowerOf2Sizes = ((CollectionUserFlags)value.ToInt32() & CollectionUserFlags.UsePowerOf2Sizes) != 0;
                }
                if (optionsDocument.TryGetValue("validationAction", out value))
                {
                    validationAction = (DocumentValidationAction)Enum.Parse(typeof(DocumentValidationAction), value.AsString, ignoreCase: true);
                }
                if (optionsDocument.TryGetValue("validationLevel", out value))
                {
                    validationLevel = (DocumentValidationLevel)Enum.Parse(typeof(DocumentValidationLevel), value.AsString, ignoreCase: true);
                }
                if (optionsDocument.TryGetValue("validator", out value))
                {
                    validator = value.AsBsonDocument;
                }
            }

            var operation = new CreateCollectionOperation(collectionNamespace, messageEncoderSettings)
            {
                AutoIndexId         = autoIndexId,
                Capped              = capped,
                Collation           = collation,
                IndexOptionDefaults = indexOptionDefaults,
                MaxDocuments        = maxDocuments,
                MaxSize             = maxSize,
                NoPadding           = noPadding,
                StorageEngine       = storageEngine,
                UsePowerOf2Sizes    = usePowerOf2Sizes,
                ValidationAction    = validationAction,
                ValidationLevel     = validationLevel,
                Validator           = validator,
                WriteConcern        = _settings.WriteConcern
            };

            var response = ExecuteWriteOperation(session, operation);

            return(new CommandResult(response));
        }
 /// <summary>
 /// Creates a collection. MongoDB creates collections automatically when they are first used, so
 /// you only need to call this method if you want to provide non-default options.
 /// </summary>
 /// <param name="collectionName">The name of the collection.</param>
 /// <param name="options">Options for creating this collection (usually a CollectionOptionsDocument or constructed using the CollectionOptions builder).</param>
 /// <returns>A CommandResult.</returns>
 public virtual CommandResult CreateCollection(string collectionName, IMongoCollectionOptions options)
 {
     return(UsingImplicitSession(session => CreateCollection(session, collectionName, options)));
 }
        /// <summary>
        /// Creates a collection. MongoDB creates collections automatically when they are first used, so
        /// you only need to call this method if you want to provide non-default options.
        /// </summary>
        /// <param name="collectionName">The name of the collection.</param>
        /// <param name="options">Options for creating this collection (usually a CollectionOptionsDocument or constructed using the CollectionOptions builder).</param>
        /// <returns>A CommandResult.</returns>
        public virtual CommandResult CreateCollection(string collectionName, IMongoCollectionOptions options)
        {
            if (collectionName == null)
            {
                throw new ArgumentNullException("collectionName");
            }

            var          collectionNamespace    = new CollectionNamespace(_namespace, collectionName);
            var          messageEncoderSettings = GetMessageEncoderSettings();
            bool?        autoIndexId            = null;
            bool?        capped           = null;
            int?         maxDocuments     = null;
            int?         maxSize          = null;
            BsonDocument storageEngine    = null;
            bool?        usePowerOf2Sizes = null;

            if (options != null)
            {
                var optionsDocument = options.ToBsonDocument();

                BsonValue value;
                if (optionsDocument.TryGetValue("autoIndexId", out value))
                {
                    autoIndexId = value.ToBoolean();
                }
                if (optionsDocument.TryGetValue("capped", out value))
                {
                    capped = value.ToBoolean();
                }
                if (optionsDocument.TryGetValue("max", out value))
                {
                    maxDocuments = value.ToInt32();
                }
                if (optionsDocument.TryGetValue("size", out value))
                {
                    maxSize = value.ToInt32();
                }
                if (optionsDocument.TryGetValue("storageEngine", out value))
                {
                    storageEngine = value.AsBsonDocument;
                }
                if (optionsDocument.TryGetValue("flags", out value))
                {
                    usePowerOf2Sizes = value.ToInt32() == 1;
                }
            }

            var operation = new CreateCollectionOperation(collectionNamespace, messageEncoderSettings)
            {
                AutoIndexId      = autoIndexId,
                Capped           = capped,
                MaxDocuments     = maxDocuments,
                MaxSize          = maxSize,
                StorageEngine    = storageEngine,
                UsePowerOf2Sizes = usePowerOf2Sizes
            };

            var response = ExecuteWriteOperation(operation);

            return(new CommandResult(response));
        }
 public CommandResult CreateCollection(string collectionName, IMongoCollectionOptions options)
 {
     return(_mongoDatabase.CreateCollection(collectionName, options));
 }
Example #9
0
        /// <summary>
        /// Creates a collection. MongoDB creates collections automatically when they are first used, so
        /// you only need to call this method if you want to provide non-default options.
        /// </summary>
        /// <param name="collectionName">The name of the collection.</param>
        /// <param name="options">Options for creating this collection (usually a CollectionOptionsDocument or constructed using the CollectionOptions builder).</param>
        /// <returns>A CommandResult.</returns>
        public virtual CommandResult CreateCollection(string collectionName, IMongoCollectionOptions options)
        {
            if (collectionName == null)
            {
                throw new ArgumentNullException(nameof(collectionName));
            }

            var                      collectionNamespace    = new CollectionNamespace(_namespace, collectionName);
            var                      messageEncoderSettings = GetMessageEncoderSettings();
            bool?                    autoIndexId            = null;
            bool?                    capped = null;
            BsonDocument             indexOptionDefaults = null;
            int?                     maxDocuments        = null;
            long?                    maxSize             = null;
            BsonDocument             storageEngine       = null;
            bool?                    usePowerOf2Sizes    = null;
            DocumentValidationAction?validationAction    = null;
            DocumentValidationLevel? validationLevel     = null;
            BsonDocument             validator           = null;

            if (options != null)
            {
                var optionsDocument = options.ToBsonDocument();

                BsonValue value;
                if (optionsDocument.TryGetValue("autoIndexId", out value))
                {
                    autoIndexId = value.ToBoolean();
                }
                if (optionsDocument.TryGetValue("capped", out value))
                {
                    capped = value.ToBoolean();
                }
                if (optionsDocument.TryGetValue("indexOptionDefaults", out value))
                {
                    indexOptionDefaults = value.AsBsonDocument;
                }
                if (optionsDocument.TryGetValue("max", out value))
                {
                    maxDocuments = value.ToInt32();
                }
                if (optionsDocument.TryGetValue("size", out value))
                {
                    maxSize = value.ToInt64();
                }
                if (optionsDocument.TryGetValue("storageEngine", out value))
                {
                    storageEngine = value.AsBsonDocument;
                }
                if (optionsDocument.TryGetValue("flags", out value))
                {
                    usePowerOf2Sizes = value.ToInt32() == 1;
                }
                if (optionsDocument.TryGetValue("validationAction", out value))
                {
                    validationAction = (DocumentValidationAction)Enum.Parse(typeof(DocumentValidationAction), value.AsString, ignoreCase: true);
                }
                if (optionsDocument.TryGetValue("validationLevel", out value))
                {
                    validationLevel = (DocumentValidationLevel)Enum.Parse(typeof(DocumentValidationLevel), value.AsString, ignoreCase: true);
                }
                if (optionsDocument.TryGetValue("validator", out value))
                {
                    validator = value.AsBsonDocument;
                }
            }

            var operation = new CreateCollectionOperation(collectionNamespace, messageEncoderSettings)
            {
                AutoIndexId         = autoIndexId,
                Capped              = capped,
                IndexOptionDefaults = indexOptionDefaults,
                MaxDocuments        = maxDocuments,
                MaxSize             = maxSize,
                StorageEngine       = storageEngine,
                UsePowerOf2Sizes    = usePowerOf2Sizes,
                ValidationAction    = validationAction,
                ValidationLevel     = validationLevel,
                Validator           = validator
            };

            var response = ExecuteWriteOperation(operation);

            return(new CommandResult(response));
        }
Example #10
0
 /// <summary>
 /// Construct a sink posting to the specified database.
 /// </summary>
 /// <param name="databaseUrl">The URL of a MongoDB database.</param>
 /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
 /// <param name="period">The time to wait between checking for event batches.</param>
 /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
 /// <param name="collectionName">Name of the MongoDb collection to use for the log. Default is "log".</param>
 /// <param name="collectionCreationOptions">Collection Creation Options for the log collection creation.</param>
 public MongoDBSink(string databaseUrl, int batchPostingLimit, TimeSpan period, IFormatProvider formatProvider, string collectionName, IMongoCollectionOptions collectionCreationOptions)
     : this(DatabaseFromMongoUrl(databaseUrl), batchPostingLimit, period, formatProvider, collectionName, collectionCreationOptions)
 {
 }