public override void CreateCollection(IClientSessionHandle session, string name, CreateCollectionOptions options, CancellationToken cancellationToken) { Ensure.IsNotNull(session, nameof(session)); Ensure.IsNotNullOrEmpty(name, nameof(name)); if (options == null) { CreateCollectionHelper <BsonDocument>(session, name, null, cancellationToken); return; } if (options.GetType() == typeof(CreateCollectionOptions)) { var genericOptions = CreateCollectionOptions <BsonDocument> .CoercedFrom(options); CreateCollectionHelper <BsonDocument>(session, name, genericOptions, cancellationToken); return; } var genericMethodDefinition = typeof(MongoDatabaseImpl).GetTypeInfo().GetMethod("CreateCollectionHelper", BindingFlags.NonPublic | BindingFlags.Instance); var documentType = options.GetType().GetTypeInfo().GetGenericArguments()[0]; var methodInfo = genericMethodDefinition.MakeGenericMethod(documentType); methodInfo.Invoke(this, new object[] { session, name, options, cancellationToken }); }
public void CreateCollection_should_execute_the_CreateCollectionOperation_when_options_is_null( [Values(false, true)] bool async) { var storageEngine = new BsonDocument("awesome", true); CreateCollectionOptions options = null; if (async) { _subject.CreateCollectionAsync("bar", options, CancellationToken.None).GetAwaiter().GetResult(); } else { _subject.CreateCollection("bar", options, CancellationToken.None); } var call = _operationExecutor.GetWriteCall <BsonDocument>(); call.Operation.Should().BeOfType <CreateCollectionOperation>(); var op = (CreateCollectionOperation)call.Operation; op.CollectionNamespace.Should().Be(new CollectionNamespace(new DatabaseNamespace("foo"), "bar")); op.AutoIndexId.Should().NotHaveValue(); op.Capped.Should().NotHaveValue(); op.IndexOptionDefaults.Should().BeNull(); op.MaxDocuments.Should().NotHaveValue(); op.MaxSize.Should().NotHaveValue(); op.StorageEngine.Should().BeNull(); op.UsePowerOf2Sizes.Should().NotHaveValue(); op.ValidationAction.Should().BeNull(); op.ValidationLevel.Should().BeNull(); op.Validator.Should().BeNull(); }
public async Task CreateCollectionAsync_should_execute_the_CreateCollectionOperation() { var storageEngine = new BsonDocument("awesome", true); var options = new CreateCollectionOptions { AutoIndexId = false, Capped = true, MaxDocuments = 10, MaxSize = 11, StorageEngine = storageEngine, UsePowerOf2Sizes = false, ValidationAction = DocumentValidationAction.Warn, ValidationLevel = DocumentValidationLevel.Off, Validator = new BsonDocument("x", 1) }; await _subject.CreateCollectionAsync("bar", options, CancellationToken.None); var call = _operationExecutor.GetWriteCall<BsonDocument>(); call.Operation.Should().BeOfType<CreateCollectionOperation>(); var op = (CreateCollectionOperation)call.Operation; op.CollectionNamespace.Should().Be(new CollectionNamespace(new DatabaseNamespace("foo"), "bar")); op.AutoIndexId.Should().Be(options.AutoIndexId); op.Capped.Should().Be(options.Capped); op.MaxDocuments.Should().Be(options.MaxDocuments); op.MaxSize.Should().Be(options.MaxSize); op.StorageEngine.Should().Be(storageEngine); op.UsePowerOf2Sizes.Should().Be(options.UsePowerOf2Sizes); op.ValidationAction.Should().Be(options.ValidationAction); op.ValidationLevel.Should().Be(options.ValidationLevel); var serializerRegistry = options.SerializerRegistry ?? BsonSerializer.SerializerRegistry; var documentSerializer = options.DocumentSerializer ?? serializerRegistry.GetSerializer<BsonDocument>(); var renderedValidator = options.Validator.Render(documentSerializer, serializerRegistry); op.Validator.Should().Be(renderedValidator); }
public async Task CreateCollectionAsync_should_execute_the_CreateCollectionOperation() { var storageEngine = new BsonDocument("awesome", true); var options = new CreateCollectionOptions { AutoIndexId = false, Capped = true, MaxDocuments = 10, MaxSize = 11, StorageEngine = storageEngine, UsePowerOf2Sizes = false }; await _subject.CreateCollectionAsync("bar", options, CancellationToken.None); var call = _operationExecutor.GetWriteCall<BsonDocument>(); call.Operation.Should().BeOfType<CreateCollectionOperation>(); var op = (CreateCollectionOperation)call.Operation; op.CollectionNamespace.Should().Be(new CollectionNamespace(new DatabaseNamespace("foo"), "bar")); op.AutoIndexId.Should().Be(options.AutoIndexId); op.Capped.Should().Be(options.Capped); op.MaxDocuments.Should().Be(options.MaxDocuments); op.MaxSize.Should().Be(options.MaxSize); op.StorageEngine.Should().Be(storageEngine); op.UsePowerOf2Sizes.Should().Be(options.UsePowerOf2Sizes); }
public async Task CreateCollectionAsync_should_execute_the_CreateCollectionOperation() { var storageOptions = new BsonDocument("awesome", true); var options = new CreateCollectionOptions { AutoIndexId = false, Capped = true, MaxDocuments = 10, MaxSize = 11, StorageOptions = storageOptions, UsePowerOf2Sizes = false }; await _subject.CreateCollectionAsync("bar", options, CancellationToken.None); var call = _operationExecutor.GetWriteCall <BsonDocument>(); call.Operation.Should().BeOfType <CreateCollectionOperation>(); var op = (CreateCollectionOperation)call.Operation; op.CollectionNamespace.Should().Be(new CollectionNamespace(new DatabaseNamespace("foo"), "bar")); op.AutoIndexId.Should().Be(options.AutoIndexId); op.Capped.Should().Be(options.Capped); op.MaxDocuments.Should().Be(options.MaxDocuments); op.MaxSize.Should().Be(options.MaxSize); op.StorageOptions.Should().Be(storageOptions); op.UsePowerOf2Sizes.Should().Be(options.UsePowerOf2Sizes); }
// private methods private void CreateCollectionHelper <TDocument>(string name, CreateCollectionOptions <TDocument> options, CancellationToken cancellationToken) { options = options ?? new CreateCollectionOptions <TDocument>(); var operation = CreateCreateCollectionOperation(name, options); ExecuteWriteOperation(operation, cancellationToken); }
private Task CreateCollectionHelperAsync <TDocument>(string name, CreateCollectionOptions <TDocument> options, CancellationToken cancellationToken) { options = options ?? new CreateCollectionOptions <TDocument>(); var operation = CreateCreateCollectionOperation(name, options); return(ExecuteWriteOperationAsync(operation, cancellationToken)); }
public void CreateCollection_should_execute_the_CreateCollectionOperation_when_options_is_generic( [Values(false, true)] bool async) { var storageEngine = new BsonDocument("awesome", true); var options = new CreateCollectionOptions <BsonDocument> { AutoIndexId = false, Capped = true, Collation = new Collation("en_US"), IndexOptionDefaults = new IndexOptionDefaults { StorageEngine = new BsonDocument("x", 1) }, MaxDocuments = 10, MaxSize = 11, NoPadding = true, StorageEngine = storageEngine, UsePowerOf2Sizes = true, ValidationAction = DocumentValidationAction.Warn, ValidationLevel = DocumentValidationLevel.Off, Validator = new BsonDocument("x", 1) }; var writeConcern = new WriteConcern(1); var subject = _subject.WithWriteConcern(writeConcern); if (async) { subject.CreateCollectionAsync("bar", options, CancellationToken.None).GetAwaiter().GetResult(); } else { subject.CreateCollection("bar", options, CancellationToken.None); } var call = _operationExecutor.GetWriteCall <BsonDocument>(); call.Operation.Should().BeOfType <CreateCollectionOperation>(); var op = (CreateCollectionOperation)call.Operation; op.CollectionNamespace.Should().Be(new CollectionNamespace(new DatabaseNamespace("foo"), "bar")); op.AutoIndexId.Should().Be(options.AutoIndexId); op.Capped.Should().Be(options.Capped); op.Collation.Should().BeSameAs(options.Collation); op.IndexOptionDefaults.ToBsonDocument().Should().Be(options.IndexOptionDefaults.ToBsonDocument()); op.MaxDocuments.Should().Be(options.MaxDocuments); op.MaxSize.Should().Be(options.MaxSize); op.NoPadding.Should().Be(options.NoPadding); op.StorageEngine.Should().Be(storageEngine); op.UsePowerOf2Sizes.Should().Be(options.UsePowerOf2Sizes); op.ValidationAction.Should().Be(options.ValidationAction); op.ValidationLevel.Should().Be(options.ValidationLevel); var serializerRegistry = options.SerializerRegistry ?? BsonSerializer.SerializerRegistry; var documentSerializer = options.DocumentSerializer ?? serializerRegistry.GetSerializer <BsonDocument>(); var renderedValidator = options.Validator.Render(documentSerializer, serializerRegistry); op.Validator.Should().Be(renderedValidator); op.WriteConcern.Should().BeSameAs(writeConcern); }
/// <summary> /// Construct a sink posting to a specified database. /// </summary> /// <param name="database">The 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(IMongoDatabase database, int batchPostingLimit, TimeSpan period, IFormatProvider formatProvider, string collectionName, CreateCollectionOptions collectionCreationOptions) : base(batchPostingLimit, period) { if (database == null) throw new ArgumentNullException("database"); _mongoDatabase = database; _collectionName = collectionName; _collectionCreationOptions = collectionCreationOptions; _formatProvider = formatProvider; }
public void CreateCollection_should_execute_the_CreateCollectionOperation_when_options_is_generic( [Values(false, true)] bool async) { var storageEngine = new BsonDocument("awesome", true); var options = new CreateCollectionOptions<BsonDocument> { AutoIndexId = false, Capped = true, Collation = new Collation("en_US"), IndexOptionDefaults = new IndexOptionDefaults { StorageEngine = new BsonDocument("x", 1) }, MaxDocuments = 10, MaxSize = 11, StorageEngine = storageEngine, UsePowerOf2Sizes = false, ValidationAction = DocumentValidationAction.Warn, ValidationLevel = DocumentValidationLevel.Off, Validator = new BsonDocument("x", 1) }; var writeConcern = new WriteConcern(1); var subject = _subject.WithWriteConcern(writeConcern); if (async) { subject.CreateCollectionAsync("bar", options, CancellationToken.None).GetAwaiter().GetResult(); } else { subject.CreateCollection("bar", options, CancellationToken.None); } var call = _operationExecutor.GetWriteCall<BsonDocument>(); call.Operation.Should().BeOfType<CreateCollectionOperation>(); var op = (CreateCollectionOperation)call.Operation; op.CollectionNamespace.Should().Be(new CollectionNamespace(new DatabaseNamespace("foo"), "bar")); op.AutoIndexId.Should().Be(options.AutoIndexId); op.Capped.Should().Be(options.Capped); op.Collation.Should().BeSameAs(options.Collation); op.IndexOptionDefaults.ToBsonDocument().Should().Be(options.IndexOptionDefaults.ToBsonDocument()); op.MaxDocuments.Should().Be(options.MaxDocuments); op.MaxSize.Should().Be(options.MaxSize); op.StorageEngine.Should().Be(storageEngine); op.UsePowerOf2Sizes.Should().Be(options.UsePowerOf2Sizes); op.ValidationAction.Should().Be(options.ValidationAction); op.ValidationLevel.Should().Be(options.ValidationLevel); var serializerRegistry = options.SerializerRegistry ?? BsonSerializer.SerializerRegistry; var documentSerializer = options.DocumentSerializer ?? serializerRegistry.GetSerializer<BsonDocument>(); var renderedValidator = options.Validator.Render(documentSerializer, serializerRegistry); op.Validator.Should().Be(renderedValidator); op.WriteConcern.Should().BeSameAs(writeConcern); }
// methods public override Task CreateCollectionAsync(string name, CreateCollectionOptions options, CancellationToken cancellationToken) { Ensure.IsNotNullOrEmpty(name, nameof(name)); options = options ?? new CreateCollectionOptions(); var messageEncoderSettings = GetMessageEncoderSettings(); var operation = new CreateCollectionOperation(new CollectionNamespace(_databaseNamespace, name), messageEncoderSettings) { AutoIndexId = options.AutoIndexId, Capped = options.Capped, MaxDocuments = options.MaxDocuments, MaxSize = options.MaxSize, StorageEngine = options.StorageEngine, UsePowerOf2Sizes = options.UsePowerOf2Sizes }; return(ExecuteWriteOperationAsync(operation, cancellationToken)); }
// methods public Task CreateCollectionAsync(string name, CreateCollectionOptions options, CancellationToken cancellationToken) { Ensure.IsNotNullOrEmpty(name, "name"); options = options ?? new CreateCollectionOptions(); var messageEncoderSettings = GetMessageEncoderSettings(); var operation = new CreateCollectionOperation(new CollectionNamespace(_databaseNamespace, name), messageEncoderSettings) { AutoIndexId = options.AutoIndexId, Capped = options.Capped, MaxDocuments = options.MaxDocuments, MaxSize = options.MaxSize, StorageOptions = BsonDocumentHelper.ToBsonDocument(_settings.SerializerRegistry, options.StorageOptions), UsePowerOf2Sizes = options.UsePowerOf2Sizes }; return(ExecuteWriteOperation(operation, cancellationToken)); }
public void CreateCollection_should_execute_the_CreateCollectionOperation_when_options_is_not_generic( [Values(false, true)] bool async) { var storageEngine = new BsonDocument("awesome", true); var options = new CreateCollectionOptions { AutoIndexId = false, Capped = true, IndexOptionDefaults = new IndexOptionDefaults { StorageEngine = new BsonDocument("x", 1) }, MaxDocuments = 10, MaxSize = 11, StorageEngine = storageEngine, UsePowerOf2Sizes = false, ValidationAction = DocumentValidationAction.Warn, ValidationLevel = DocumentValidationLevel.Off }; if (async) { _subject.CreateCollectionAsync("bar", options, CancellationToken.None).GetAwaiter().GetResult(); } else { _subject.CreateCollection("bar", options, CancellationToken.None); } var call = _operationExecutor.GetWriteCall <BsonDocument>(); call.Operation.Should().BeOfType <CreateCollectionOperation>(); var op = (CreateCollectionOperation)call.Operation; op.CollectionNamespace.Should().Be(new CollectionNamespace(new DatabaseNamespace("foo"), "bar")); op.AutoIndexId.Should().Be(options.AutoIndexId); op.Capped.Should().Be(options.Capped); op.IndexOptionDefaults.ToBsonDocument().Should().Be(options.IndexOptionDefaults.ToBsonDocument()); op.MaxDocuments.Should().Be(options.MaxDocuments); op.MaxSize.Should().Be(options.MaxSize); op.StorageEngine.Should().Be(storageEngine); op.UsePowerOf2Sizes.Should().Be(options.UsePowerOf2Sizes); op.ValidationAction.Should().Be(options.ValidationAction); op.ValidationLevel.Should().Be(options.ValidationLevel); op.Validator.Should().BeNull(); }
// methods public override Task CreateCollectionAsync(string name, CreateCollectionOptions options, CancellationToken cancellationToken) { if (options == null) { return(CreateCollectionHelperAsync <BsonDocument>(name, null, cancellationToken)); } if (options.GetType() == typeof(CreateCollectionOptions)) { var genericOptions = CreateCollectionOptions <BsonDocument> .CoercedFrom(options); return(CreateCollectionHelperAsync <BsonDocument>(name, genericOptions, cancellationToken)); } var genericMethodDefinition = typeof(MongoDatabaseImpl).GetMethod("CreateCollectionHelperAsync", BindingFlags.NonPublic | BindingFlags.Instance); var documentType = options.GetType().GetGenericArguments()[0]; var methodInfo = genericMethodDefinition.MakeGenericMethod(documentType); return((Task)methodInfo.Invoke(this, new object[] { name, options, cancellationToken })); }
public async Task CreateCollectionAsync_should_execute_the_CreateCollectionOperation() { var storageEngine = new BsonDocument("awesome", true); var options = new CreateCollectionOptions <BsonDocument> { AutoIndexId = false, Capped = true, IndexOptionDefaults = new IndexOptionDefaults { StorageEngine = new BsonDocument("x", 1) }, MaxDocuments = 10, MaxSize = 11, StorageEngine = storageEngine, UsePowerOf2Sizes = false, ValidationAction = DocumentValidationAction.Warn, ValidationLevel = DocumentValidationLevel.Off, Validator = new BsonDocument("x", 1) }; await _subject.CreateCollectionAsync("bar", options, CancellationToken.None); var call = _operationExecutor.GetWriteCall <BsonDocument>(); call.Operation.Should().BeOfType <CreateCollectionOperation>(); var op = (CreateCollectionOperation)call.Operation; op.CollectionNamespace.Should().Be(new CollectionNamespace(new DatabaseNamespace("foo"), "bar")); op.AutoIndexId.Should().Be(options.AutoIndexId); op.Capped.Should().Be(options.Capped); op.IndexOptionDefaults.ToBsonDocument().Should().Be(options.IndexOptionDefaults.ToBsonDocument()); op.MaxDocuments.Should().Be(options.MaxDocuments); op.MaxSize.Should().Be(options.MaxSize); op.StorageEngine.Should().Be(storageEngine); op.UsePowerOf2Sizes.Should().Be(options.UsePowerOf2Sizes); op.ValidationAction.Should().Be(options.ValidationAction); op.ValidationLevel.Should().Be(options.ValidationLevel); var serializerRegistry = options.SerializerRegistry ?? BsonSerializer.SerializerRegistry; var documentSerializer = options.DocumentSerializer ?? serializerRegistry.GetSerializer <BsonDocument>(); var renderedValidator = options.Validator.Render(documentSerializer, serializerRegistry); op.Validator.Should().Be(renderedValidator); }
/// <summary> /// Construct a sink posting to a specified database. /// </summary> /// <param name="database">The 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(IMongoDatabase database, int batchPostingLimit, TimeSpan period, IFormatProvider formatProvider, string collectionName, CreateCollectionOptions collectionCreationOptions) : base(batchPostingLimit, period) { if (database == null) throw new ArgumentNullException("database"); _mongoDatabase = database; _collectionName = collectionName; _collectionCreationOptions = collectionCreationOptions; _formatProvider = formatProvider; // capped collections have to be created because GetCollection doesn't offer the option to create one implicitly // only create one if it doesn't already exist if (_collectionCreationOptions.Capped.GetValueOrDefault(false)) { if (_mongoDatabase.ListCollections(new ListCollectionsOptions { Filter = new BsonDocument {{"name", _collectionName}} }).ToList().Count == 0) { _mongoDatabase.CreateCollection(_collectionName, _collectionCreationOptions); } } }
/// <summary> /// Adds a sink that writes log events as documents to a capped collection in a MongoDb database. /// </summary> /// <param name="loggerConfiguration">The logger configuration.</param> /// <param name="database">The MongoDb database where the log collection will live.</param> /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param> /// <param name="cappedMaxSizeMb">Max total size in megabytes of the created capped collection. (Default: 50mb)</param> /// <param name="cappedMaxDocuments">Max number of documents of the created capped collection.</param> /// <param name="collectionName">Name of the collection. Default is "log".</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> /// <returns>Logger configuration, allowing configuration to continue.</returns> /// <exception cref="ArgumentNullException">A required parameter is null.</exception> public static LoggerConfiguration MongoDBCapped( this LoggerSinkConfiguration loggerConfiguration, IMongoDatabase database, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, long cappedMaxSizeMb = 50, long? cappedMaxDocuments = null, string collectionName = null, int batchPostingLimit = MongoDBSink.DefaultBatchPostingLimit, TimeSpan? period = null, IFormatProvider formatProvider = null) { if (loggerConfiguration == null) throw new ArgumentNullException("loggerConfiguration"); if (database == null) throw new ArgumentNullException("database"); var options = new CreateCollectionOptions() { Capped = true, MaxSize = cappedMaxSizeMb * 1024 * 1024 }; if (cappedMaxDocuments.HasValue) { options.MaxDocuments = cappedMaxDocuments.Value; } var defaultedPeriod = period ?? MongoDBSink.DefaultPeriod; return loggerConfiguration.Sink( new MongoDBSink( database, batchPostingLimit, defaultedPeriod, formatProvider, collectionName ?? MongoDBSink.DefaultCollectionName, options), restrictedToMinimumLevel); }
private CreateCollectionOperation CreateCreateCollectionOperation <TDocument>(string name, CreateCollectionOptions <TDocument> options) { var messageEncoderSettings = GetMessageEncoderSettings(); BsonDocument validator = null; if (options.Validator != null) { var serializerRegistry = options.SerializerRegistry ?? BsonSerializer.SerializerRegistry; var documentSerializer = options.DocumentSerializer ?? serializerRegistry.GetSerializer <TDocument>(); validator = options.Validator.Render(documentSerializer, serializerRegistry); } return(new CreateCollectionOperation(new CollectionNamespace(_databaseNamespace, name), messageEncoderSettings) { AutoIndexId = options.AutoIndexId, Capped = options.Capped, IndexOptionDefaults = options.IndexOptionDefaults?.ToBsonDocument(), MaxDocuments = options.MaxDocuments, MaxSize = options.MaxSize, StorageEngine = options.StorageEngine, UsePowerOf2Sizes = options.UsePowerOf2Sizes, ValidationAction = options.ValidationAction, ValidationLevel = options.ValidationLevel, Validator = validator }); }
private void Initialize() { lock (Sync) { var mongoUrl = MongoUrl.Create(_connectionString); var mongoClient = new MongoClient(mongoUrl); var database = mongoClient.GetDatabase(mongoUrl.DatabaseName); if (database.GetCollection<BsonDocument>(_collectionName) == null) { var options = new CreateCollectionOptions(); options.Capped = true; options.AutoIndexId = true; options.MaxSize = _maxSize; if (_maxDocuments != int.MaxValue) options.MaxDocuments = _maxDocuments; var task = database.CreateCollectionAsync(_collectionName, options); task.Wait(); } _collection = database.GetCollection<BsonDocument>(_collectionName); //_mongoInsertOptions = new MongoInsertOptions { CheckElementNames = false }; } }
private CreateCollectionOperation CreateCreateCollectionOperation <TDocument>(string name, CreateCollectionOptions <TDocument> options) { var messageEncoderSettings = GetMessageEncoderSettings(); BsonDocument validator = null; if (options.Validator != null) { var serializerRegistry = options.SerializerRegistry ?? BsonSerializer.SerializerRegistry; var documentSerializer = options.DocumentSerializer ?? serializerRegistry.GetSerializer <TDocument>(); validator = options.Validator.Render(documentSerializer, serializerRegistry, _linqProvider); } #pragma warning disable 618 return(new CreateCollectionOperation(new CollectionNamespace(_databaseNamespace, name), messageEncoderSettings) { AutoIndexId = options.AutoIndexId, Capped = options.Capped, Collation = options.Collation, ExpireAfter = options.ExpireAfter, IndexOptionDefaults = options.IndexOptionDefaults?.ToBsonDocument(), MaxDocuments = options.MaxDocuments, MaxSize = options.MaxSize, NoPadding = options.NoPadding, StorageEngine = options.StorageEngine, TimeSeriesOptions = options.TimeSeriesOptions, UsePowerOf2Sizes = options.UsePowerOf2Sizes, ValidationAction = options.ValidationAction, ValidationLevel = options.ValidationLevel, Validator = validator, WriteConcern = _settings.WriteConcern }); #pragma warning restore }
public override void CreateCollection(string name, CreateCollectionOptions options, CancellationToken cancellationToken) { UsingImplicitSession(session => CreateCollection(session, name, options, cancellationToken), cancellationToken); }
/// <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, CreateCollectionOptions collectionCreationOptions) : this(DatabaseFromMongoUrl(databaseUrl), batchPostingLimit, period, formatProvider, collectionName, collectionCreationOptions) { }
// public methods /// <inheritdoc /> public virtual void CreateCollection(string name, CreateCollectionOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); }
public void CreateCollection_should_execute_the_CreateCollectionOperation_when_options_is_not_generic( [Values(false, true)] bool async) { var storageEngine = new BsonDocument("awesome", true); var options = new CreateCollectionOptions { AutoIndexId = false, Capped = true, IndexOptionDefaults = new IndexOptionDefaults { StorageEngine = new BsonDocument("x", 1) }, MaxDocuments = 10, MaxSize = 11, StorageEngine = storageEngine, UsePowerOf2Sizes = false, ValidationAction = DocumentValidationAction.Warn, ValidationLevel = DocumentValidationLevel.Off }; if (async) { _subject.CreateCollectionAsync("bar", options, CancellationToken.None).GetAwaiter().GetResult(); } else { _subject.CreateCollection("bar", options, CancellationToken.None); } var call = _operationExecutor.GetWriteCall<BsonDocument>(); call.Operation.Should().BeOfType<CreateCollectionOperation>(); var op = (CreateCollectionOperation)call.Operation; op.CollectionNamespace.Should().Be(new CollectionNamespace(new DatabaseNamespace("foo"), "bar")); op.AutoIndexId.Should().Be(options.AutoIndexId); op.Capped.Should().Be(options.Capped); op.IndexOptionDefaults.ToBsonDocument().Should().Be(options.IndexOptionDefaults.ToBsonDocument()); op.MaxDocuments.Should().Be(options.MaxDocuments); op.MaxSize.Should().Be(options.MaxSize); op.StorageEngine.Should().Be(storageEngine); op.UsePowerOf2Sizes.Should().Be(options.UsePowerOf2Sizes); op.ValidationAction.Should().Be(options.ValidationAction); op.ValidationLevel.Should().Be(options.ValidationLevel); op.Validator.Should().BeNull(); }
private static async Task CreateCollectionAsync(IMongoDatabase mongoDatabase, string collectionName, long? cappedCollectionSize, long? maxNumberOfDocuments) { var createCollectionOptions = new CreateCollectionOptions() { Capped = true, MaxSize = cappedCollectionSize ?? DefaultCappedCollectionSize, MaxDocuments = maxNumberOfDocuments ?? DefaultCappedCollectionMaxDocuments }; await mongoDatabase.CreateCollectionAsync(collectionName ?? CollectionDefaulName, createCollectionOptions); }
public async Task CreateCollectionAsync(string databaseName, string collection, CreateCollectionOptions options) { var db = client.GetDatabase(databaseName); await db.CreateCollectionAsync(collection, options); }
private void Initialize() { lock (Sync) { var mongoUrl = MongoUrl.Create(_connectionString); var mongoClient = new MongoClient(mongoUrl); var database = mongoClient.GetDatabase(mongoUrl.DatabaseName); if (database.GetCollection<BsonDocument>(_collectionName) == null) { var options = new CreateCollectionOptions(); options.Capped = true; options.AutoIndexId = true; options.MaxSize = _maxSize; if (_maxDocuments != int.MaxValue) options.MaxDocuments = _maxDocuments; var task = database.CreateCollectionAsync(_collectionName, options); task.Wait(); } _collection = database.GetCollection<BsonDocument>(_collectionName); //create an index on time try { IndexKeysDefinition<BsonDocument> keys = "{ time: -1 }"; var taskIx = _collection.Indexes.CreateOneAsync( keys, new CreateIndexOptions { Name = "ix_time" } ); taskIx.Wait(); } catch { } //_mongoInsertOptions = new MongoInsertOptions { CheckElementNames = false }; } }
public async Task Stream_CreateCollectionWithOptions() { var tailer = new Tailer(m_client); IOutlet outlet = Substitute.For<IOutlet>(); var stream = new Stream(tailer, outlet); Oplog lastOplog = await tailer.GetMostRecentOplog(); var collectionOptions = new CreateCollectionOptions { Capped = true, MaxSize = 10 }; var collectionOptionsDocument = new BsonDocument(new List<BsonElement> { new BsonElement("capped", true), new BsonElement("size", 10) }); var databaseName = "_Test_MongoRiver"; var collectionName = "_Test_MongoRiver"; IMongoDatabase database = m_client.GetDatabase(databaseName); await database.CreateCollectionAsync(collectionName, collectionOptions); await m_client.DropDatabaseAsync(databaseName); await RunStream(stream, lastOplog); outlet.Received(2).UpdateOptime(Arg.Any<BsonTimestamp>()); Received.InOrder(() => { outlet.CreateCollection(databaseName, collectionName, collectionOptionsDocument); outlet.DeleteDatabase(databaseName); }); }
/// <inheritdoc /> public abstract Task CreateCollectionAsync(string name, CreateCollectionOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
/// <inheritdoc /> public virtual Task CreateCollectionAsync(IClientSessionHandle session, string name, CreateCollectionOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); }
private void CreateCollection(IMongoDatabase db, string collectionName) { var cob = new CreateCollectionOptions(); SetCappedCollectionOptions(cob); db.CreateCollectionAsync(collectionName, cob).GetAwaiter().GetResult(); }
private CreateCollectionOperation CreateCreateCollectionOperation(string name, CreateCollectionOptions options) { options = options ?? new CreateCollectionOptions(); var messageEncoderSettings = GetMessageEncoderSettings(); #pragma warning disable 618 return(new CreateCollectionOperation(new CollectionNamespace(_databaseNamespace, name), messageEncoderSettings) { AutoIndexId = options.AutoIndexId, Collation = options.Collation, Capped = options.Capped, MaxDocuments = options.MaxDocuments, MaxSize = options.MaxSize, NoPadding = options.NoPadding, StorageEngine = options.StorageEngine, UsePowerOf2Sizes = options.UsePowerOf2Sizes, WriteConcern = _settings.WriteConcern }); #pragma warning restore }
private void SetCappedCollectionOptions(CreateCollectionOptions options) { var unitResolver = new UnitResolver(); var newCollectionMaxSize = unitResolver.Resolve(NewCollectionMaxSize); var newCollectionMaxDocs = unitResolver.Resolve(NewCollectionMaxDocs); if (newCollectionMaxSize > 0) { options.Capped = true; options.MaxSize = newCollectionMaxSize; if (newCollectionMaxDocs > 0) { options.MaxDocuments = newCollectionMaxDocs; } } }
public override Task CreateCollectionAsync(string name, CreateCollectionOptions options, CancellationToken cancellationToken) { return(UsingImplicitSessionAsync(session => CreateCollectionAsync(session, name, options, cancellationToken), cancellationToken)); }
private CreateCollectionOperation CreateCreateCollectionOperation(string name, CreateCollectionOptions options) { options = options ?? new CreateCollectionOptions(); var messageEncoderSettings = GetMessageEncoderSettings(); return(new CreateCollectionOperation(new CollectionNamespace(_databaseNamespace, name), messageEncoderSettings) { AutoIndexId = options.AutoIndexId, Capped = options.Capped, MaxDocuments = options.MaxDocuments, MaxSize = options.MaxSize, StorageEngine = options.StorageEngine, UsePowerOf2Sizes = options.UsePowerOf2Sizes }); }
public void CreateCollection_should_execute_a_CreateCollectionOperation_when_options_is_generic( [Values(false, true)] bool usingSession, [Values(false, true)] bool async) { var writeConcern = new WriteConcern(1); var subject = _subject.WithWriteConcern(writeConcern); var session = CreateSession(usingSession); var name = "bar"; var storageEngine = new BsonDocument("awesome", true); var validatorDocument = BsonDocument.Parse("{ x : 1 }"); var validatorDefinition = (FilterDefinition <BsonDocument>)validatorDocument; #pragma warning disable 618 var options = new CreateCollectionOptions <BsonDocument> { AutoIndexId = false, Capped = true, Collation = new Collation("en_US"), IndexOptionDefaults = new IndexOptionDefaults { StorageEngine = new BsonDocument("x", 1) }, MaxDocuments = 10, MaxSize = 11, NoPadding = true, StorageEngine = storageEngine, UsePowerOf2Sizes = true, ValidationAction = DocumentValidationAction.Warn, ValidationLevel = DocumentValidationLevel.Off, Validator = validatorDefinition }; #pragma warning restore var cancellationToken = new CancellationTokenSource().Token; if (usingSession) { if (async) { subject.CreateCollectionAsync(session, name, options, cancellationToken).GetAwaiter().GetResult(); } else { subject.CreateCollection(session, name, options, cancellationToken); } } else { if (async) { subject.CreateCollectionAsync(name, options, cancellationToken).GetAwaiter().GetResult(); } else { subject.CreateCollection(name, options, cancellationToken); } } var call = _operationExecutor.GetWriteCall <BsonDocument>(); VerifySessionAndCancellationToken(call, session, cancellationToken); var op = call.Operation.Should().BeOfType <CreateCollectionOperation>().Subject; op.CollectionNamespace.Should().Be(new CollectionNamespace(_subject.DatabaseNamespace, name)); #pragma warning disable 618 op.AutoIndexId.Should().Be(options.AutoIndexId); #pragma warning restore op.Capped.Should().Be(options.Capped); op.Collation.Should().BeSameAs(options.Collation); op.IndexOptionDefaults.ToBsonDocument().Should().Be(options.IndexOptionDefaults.ToBsonDocument()); op.MaxDocuments.Should().Be(options.MaxDocuments); op.MaxSize.Should().Be(options.MaxSize); op.NoPadding.Should().Be(options.NoPadding); op.StorageEngine.Should().Be(storageEngine); op.UsePowerOf2Sizes.Should().Be(options.UsePowerOf2Sizes); op.ValidationAction.Should().Be(options.ValidationAction); op.ValidationLevel.Should().Be(options.ValidationLevel); op.Validator.Should().Be(validatorDocument); op.WriteConcern.Should().BeSameAs(writeConcern); }
private void Initialize() { lock (Sync) { var mongoUrl = MongoUrl.Create(_connectionString); var mongoClient = new MongoClient(mongoUrl); //var server = mongoClient.GetServer(); var database = mongoClient.GetDatabase(mongoUrl.DatabaseName); //var filter = new Filter(new BsonDocument("name", _collectionName)); var findThisOne = new ListCollectionsOptions(); findThisOne.Filter = Builders<BsonDocument>.Filter.Eq("name", _collectionName); var cursor = database.ListCollectionsAsync(findThisOne).Result; var list = cursor.ToListAsync().GetAwaiter().GetResult(); var allCollections = list.Select(c => c["name"].AsString).OrderBy(n => n).ToList(); if (!allCollections.Contains(_collectionName)) { var options = new CreateCollectionOptions(); options.Capped = true; options.AutoIndexId = true; options.MaxSize = _maxSize; database.CreateCollectionAsync(_collectionName, options).GetAwaiter().GetResult(); } _collection = database.GetCollection<ErrorModel>(_collectionName); //_mongoInsertOptions = new MongoInsertOptions { CheckElementNames = false }; } }