Search in sources :

Example 26 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project newrelic-java-agent by newrelic.

the class QuickTour method run.

/**
 * Exercises MongoDB reactive APIs.
 *
 * @param mongoClient MongoClient instance
 */
public static void run(MongoClient mongoClient) {
    // get handle to "mydb" database
    MongoDatabase database = mongoClient.getDatabase("mydb");
    // get a handle to the "test" collection
    final MongoCollection<Document> collection = database.getCollection("test");
    // drop all the data in it
    ObservableSubscriber<Void> successSubscriber = new OperationSubscriber<>();
    collection.drop().subscribe(successSubscriber);
    successSubscriber.await();
    // make a document and insert it
    Document doc = new Document("name", "MongoDB").append("type", "database").append("count", 1).append("info", new Document("x", 203).append("y", 102));
    ObservableSubscriber<InsertOneResult> insertOneSubscriber = new OperationSubscriber<>();
    collection.insertOne(doc).subscribe(insertOneSubscriber);
    insertOneSubscriber.await();
    // get it (since it's the only one in there since we dropped the rest earlier on)
    ObservableSubscriber<Document> documentSubscriber = new PrintDocumentSubscriber();
    collection.find().first().subscribe(documentSubscriber);
    documentSubscriber.await();
    // now, lets add lots of little documents to the collection so we can explore queries and cursors
    List<Document> documents = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        documents.add(new Document("i", i));
    }
    ObservableSubscriber<InsertManyResult> insertManySubscriber = new OperationSubscriber<>();
    collection.insertMany(documents).subscribe(insertManySubscriber);
    insertManySubscriber.await();
    // count documents
    collection.countDocuments().subscribe(new PrintSubscriber<>("total # of documents after inserting 100 small ones (should be 101): %s"));
    // find first
    documentSubscriber = new PrintDocumentSubscriber();
    collection.find().first().subscribe(documentSubscriber);
    documentSubscriber.await();
    // lets get all the documents in the collection and print them out
    documentSubscriber = new PrintDocumentSubscriber();
    collection.find().subscribe(documentSubscriber);
    documentSubscriber.await();
    // Query Filters
    // now use a query to get 1 document out
    documentSubscriber = new PrintDocumentSubscriber();
    collection.find(eq("i", 71)).first().subscribe(documentSubscriber);
    documentSubscriber.await();
    // now use a range query to get a larger subset
    documentSubscriber = new PrintDocumentSubscriber();
    collection.find(gt("i", 50)).subscribe(documentSubscriber);
    successSubscriber.await();
    // range query with multiple constraints
    documentSubscriber = new PrintDocumentSubscriber();
    collection.find(and(gt("i", 50), lte("i", 100))).subscribe(documentSubscriber);
    successSubscriber.await();
    // Sorting
    documentSubscriber = new PrintDocumentSubscriber();
    collection.find(exists("i")).sort(descending("i")).first().subscribe(documentSubscriber);
    documentSubscriber.await();
    // Projection
    documentSubscriber = new PrintDocumentSubscriber();
    collection.find().projection(excludeId()).first().subscribe(documentSubscriber);
    documentSubscriber.await();
    // Aggregation
    documentSubscriber = new PrintDocumentSubscriber();
    collection.aggregate(asList(match(gt("i", 0)), project(Document.parse("{ITimes10: {$multiply: ['$i', 10]}}")))).subscribe(documentSubscriber);
    documentSubscriber.await();
    documentSubscriber = new PrintDocumentSubscriber();
    collection.aggregate(singletonList(group(null, sum("total", "$i")))).first().subscribe(documentSubscriber);
    documentSubscriber.await();
    // Update One
    ObservableSubscriber<UpdateResult> updateSubscriber = new OperationSubscriber<>();
    collection.updateOne(eq("i", 10), set("i", 110)).subscribe(updateSubscriber);
    updateSubscriber.await();
    // Update Many
    updateSubscriber = new OperationSubscriber<>();
    collection.updateMany(lt("i", 100), inc("i", 100)).subscribe(updateSubscriber);
    updateSubscriber.await();
    // 1. Ordered bulk operation - order is guaranteed
    collection.bulkWrite(Arrays.asList(new InsertOneModel<>(new Document("_id", 4)), new InsertOneModel<>(new Document("_id", 5)), new InsertOneModel<>(new Document("_id", 6)), new UpdateOneModel<>(new Document("_id", 1), new Document("$set", new Document("x", 2))), new DeleteOneModel<>(new Document("_id", 2)), new ReplaceOneModel<>(new Document("_id", 3), new Document("_id", 3).append("x", 4)))).subscribe(new OperationSubscriber<>());
    // 2. Unordered bulk operation - no guarantee of order of operation
    collection.bulkWrite(Arrays.asList(new InsertOneModel<>(new Document("_id", 4)), new InsertOneModel<>(new Document("_id", 5)), new InsertOneModel<>(new Document("_id", 6)), new UpdateOneModel<>(new Document("_id", 1), new Document("$set", new Document("x", 2))), new DeleteOneModel<>(new Document("_id", 2)), new ReplaceOneModel<>(new Document("_id", 3), new Document("_id", 3).append("x", 4))), new BulkWriteOptions().ordered(false)).subscribe(new OperationSubscriber<>());
    // Delete One
    ObservableSubscriber<DeleteResult> deleteSubscriber = new OperationSubscriber<>();
    collection.deleteOne(eq("i", 110)).subscribe(deleteSubscriber);
    deleteSubscriber.await();
    // Delete Many
    deleteSubscriber = new OperationSubscriber<>();
    collection.deleteMany(gte("i", 100)).subscribe(deleteSubscriber);
    deleteSubscriber.await();
    // Create Index
    OperationSubscriber<String> createIndexSubscriber = new PrintSubscriber<>("Create Index Result: %s");
    collection.createIndex(new Document("i", 1)).subscribe(createIndexSubscriber);
    createIndexSubscriber.await();
    // Clean up
    successSubscriber = new OperationSubscriber<>();
    collection.drop().subscribe(successSubscriber);
    successSubscriber.await();
    // release resources
    mongoClient.close();
}
Also used : PrintDocumentSubscriber(com.nr.agent.instrumentation.mongodb.SubscriberHelpers.PrintDocumentSubscriber) ArrayList(java.util.ArrayList) Document(org.bson.Document) InsertOneResult(com.mongodb.client.result.InsertOneResult) UpdateResult(com.mongodb.client.result.UpdateResult) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) OperationSubscriber(com.nr.agent.instrumentation.mongodb.SubscriberHelpers.OperationSubscriber) InsertManyResult(com.mongodb.client.result.InsertManyResult) PrintSubscriber(com.nr.agent.instrumentation.mongodb.SubscriberHelpers.PrintSubscriber) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) DeleteResult(com.mongodb.client.result.DeleteResult)

Example 27 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project gravitee-access-management by gravitee-io.

the class EmbeddedClient method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    final IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION).build();
    IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, logger).processOutput(ProcessOutput.getDefaultInstanceSilent()).build();
    MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
    MongodExecutable mongodExecutable = runtime.prepare(mongodConfig);
    mongod = mongodExecutable.start();
    // cluster configuration
    ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Collections.singletonList(new ServerAddress(mongodConfig.net().getServerAddress().getHostName(), mongodConfig.net().getPort()))).build();
    // codec configuration
    CodecRegistry pojoCodecRegistry = fromRegistries(MongoClients.getDefaultCodecRegistry(), fromProviders(PojoCodecProvider.builder().automatic(true).build()));
    MongoClientSettings settings = MongoClientSettings.builder().applyToClusterSettings(clusterBuilder -> clusterBuilder.applySettings(clusterSettings)).codecRegistry(pojoCodecRegistry).writeConcern(WriteConcern.ACKNOWLEDGED).build();
    mongoClient = MongoClients.create(settings);
    mongoDatabase = mongoClient.getDatabase(databaseName);
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) LoggerFactory(org.slf4j.LoggerFactory) MongoClients(com.mongodb.reactivestreams.client.MongoClients) MongodExecutable(de.flapdoodle.embed.mongo.MongodExecutable) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) InitializingBean(org.springframework.beans.factory.InitializingBean) MongoClient(com.mongodb.reactivestreams.client.MongoClient) ProcessOutput(de.flapdoodle.embed.process.config.io.ProcessOutput) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Version(de.flapdoodle.embed.mongo.distribution.Version) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) CodecRegistries.fromRegistries(org.bson.codecs.configuration.CodecRegistries.fromRegistries) ServerAddress(com.mongodb.ServerAddress) RuntimeConfigBuilder(de.flapdoodle.embed.mongo.config.RuntimeConfigBuilder) Logger(org.slf4j.Logger) PojoCodecProvider(org.bson.codecs.pojo.PojoCodecProvider) CodecRegistries.fromProviders(org.bson.codecs.configuration.CodecRegistries.fromProviders) Command(de.flapdoodle.embed.mongo.Command) MongodProcess(de.flapdoodle.embed.mongo.MongodProcess) MongodStarter(de.flapdoodle.embed.mongo.MongodStarter) DisposableBean(org.springframework.beans.factory.DisposableBean) IRuntimeConfig(de.flapdoodle.embed.process.config.IRuntimeConfig) MongoClientSettings(com.mongodb.MongoClientSettings) WriteConcern(com.mongodb.WriteConcern) Collections(java.util.Collections) MongodExecutable(de.flapdoodle.embed.mongo.MongodExecutable) ClusterSettings(com.mongodb.connection.ClusterSettings) ServerAddress(com.mongodb.ServerAddress) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) MongodStarter(de.flapdoodle.embed.mongo.MongodStarter) MongoClientSettings(com.mongodb.MongoClientSettings) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) IRuntimeConfig(de.flapdoodle.embed.process.config.IRuntimeConfig) RuntimeConfigBuilder(de.flapdoodle.embed.mongo.config.RuntimeConfigBuilder)

Example 28 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project mongock by mongock.

the class LegacyServiceTest method mockDatabase.

@SuppressWarnings("unchecked")
private MongoDatabase mockDatabase() {
    FindPublisherMock<Document> spy = Mockito.spy(new FindPublisherMock<>(Arrays.asList(getDocument(0), getDocument(1), getDocument(2))));
    MongoCollection<Document> collection = (MongoCollection<Document>) mock(MongoCollection.class);
    when(collection.find()).thenReturn(spy);
    when(collection.find(any(Bson.class))).thenReturn(spy);
    MongoDatabase mongoDatabase = mock(MongoDatabase.class);
    when(mongoDatabase.getCollection(Mockito.anyString())).thenReturn(collection);
    return mongoDatabase;
}
Also used : MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) Document(org.bson.Document) Bson(org.bson.conversions.Bson) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase)

Example 29 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project mongock by mongock.

the class LegacyService method executeMigration.

public void executeMigration(@NonLockGuarded(NonLockGuardedType.NONE) @Named("legacy-migration") LegacyMigration legacyMigration, MongoDatabase mongoDatabase, ChangeEntryService changeEntryService) {
    int changesMigrated = 0;
    Integer changesCountExpectation = legacyMigration.getChangesCountExpectation();
    if (changesCountExpectation == null) {
        logger.warn("[legacy-migration] - There is no changes count expectation!");
    }
    try {
        validateLegacyMigration(legacyMigration);
        List<ChangeEntry> changesToMigrate = getOriginalMigrationAsChangeEntryList(mongoDatabase.getCollection(legacyMigration.getOrigin()), legacyMigration);
        Set<String> allMigratedChanges = changeEntryService.getEntriesLog().stream().map(c -> String.format("%s-%s", c.getChangeId(), c.getAuthor())).collect(Collectors.toSet());
        Set<String> executedChanges = changeEntryService.getExecuted().stream().map(c -> String.format("%s-%s", c.getChangeId(), c.getAuthor())).collect(Collectors.toSet());
        /**
         * For each change from the origin:
         * - if it's in the target and in executed state, it's fine. Nothing is done
         * - If it's in target but not in executed state, another changeEntry is inserted with the same (id,author), state= origin.state and date = NOW
         * - If it's not in target, another changeEntry is inserted with the same (id,author), state= origin.state and date = origin.date
         *
         * Explanation:
         * - if a change is already in target in a non executed state, it is probably in a corrupted state. So the origin change is migrated with date=now,
         * so it's the one it will be prioritised over the older ones
         */
        for (ChangeEntry originalChange : changesToMigrate) {
            boolean hasBeenPreviouslyMigrated = allMigratedChanges.contains(String.format("%s-%s", originalChange.getChangeId(), originalChange.getAuthor()));
            boolean migratedAndExecutedState = executedChanges.contains(String.format("%s-%s", originalChange.getChangeId(), originalChange.getAuthor()));
            if (migratedAndExecutedState) {
                logAlreadyTracked(originalChange);
            } else {
                final ChangeEntry changeToInsert;
                if (hasBeenPreviouslyMigrated) {
                    changeToInsert = new ChangeEntry(originalChange.getExecutionId(), originalChange.getChangeId(), originalChange.getAuthor(), new Date(), originalChange.getState(), originalChange.getType(), originalChange.getChangeLogClass(), originalChange.getChangeSetMethod(), originalChange.getExecutionMillis(), originalChange.getExecutionHostname(), originalChange.getMetadata(), originalChange.getErrorTrace().orElse(""));
                } else {
                    changeToInsert = originalChange;
                }
                logTracking(changeToInsert);
                changeEntryService.saveOrUpdate(changeToInsert);
                logSuccessfullyTracked(changeToInsert);
            }
            changesMigrated++;
        }
        if (changesCountExpectation != null && changesCountExpectation != changesMigrated) {
            throw new MongockException(String.format("[legacy-migration] - Expectation [%d] changes migrated. Actual [%d] migrated", changesCountExpectation, changesMigrated));
        }
    } catch (MongockException ex) {
        processException(legacyMigration.isFailFast(), ex);
    } catch (Exception ex) {
        processException(legacyMigration.isFailFast(), new MongockException(ex));
    }
}
Also used : Document(org.bson.Document) ChangeEntry(io.mongock.driver.api.entry.ChangeEntry) ChangeEntryService(io.mongock.driver.api.entry.ChangeEntryService) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) LocalDateTime(java.time.LocalDateTime) MongoCollectionSync(io.mongock.driver.mongodb.reactive.util.MongoCollectionSync) HashMap(java.util.HashMap) Random(java.util.Random) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) ArrayList(java.util.ArrayList) LegacyMigration(io.mongock.api.config.LegacyMigration) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Map(java.util.Map) Named(javax.inject.Named) LegacyMigrationMappingFields(io.mongock.api.config.LegacyMigrationMappingFields) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ChangeState(io.mongock.driver.api.entry.ChangeState) Set(java.util.Set) Collectors(java.util.stream.Collectors) MongockException(io.mongock.api.exception.MongockException) List(java.util.List) NonLockGuardedType(io.changock.migration.api.annotations.NonLockGuardedType) ChangeType(io.mongock.driver.api.entry.ChangeType) NonLockGuarded(io.changock.migration.api.annotations.NonLockGuarded) MongockException(io.mongock.api.exception.MongockException) ChangeEntry(io.mongock.driver.api.entry.ChangeEntry) Date(java.util.Date) MongockException(io.mongock.api.exception.MongockException)

Example 30 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project mongock by mongock.

the class MongoReactiveDriverGeneric method specificInitialization.

@Override
public void specificInitialization() {
    dependencies.add(new ChangeSetDependency(MongoDatabase.class, mongoDatabase, true));
    dependencies.add(new ChangeSetDependency(ChangeEntryService.class, getChangeEntryService(), false));
    txOptions = TransactionOptions.builder().writeConcern(getWriteConcern()).readConcern(getReadConcern()).readPreference(getReadPreference()).build();
}
Also used : ChangeSetDependency(io.mongock.driver.api.driver.ChangeSetDependency) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) ChangeEntryService(io.mongock.driver.api.entry.ChangeEntryService)

Aggregations

MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)38 Document (org.bson.Document)15 MongoClient (com.mongodb.reactivestreams.client.MongoClient)9 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)8 DeleteResult (com.mongodb.client.result.DeleteResult)6 List (java.util.List)6 Map (java.util.Map)6 HashMap (java.util.HashMap)5 BsonDocument (org.bson.BsonDocument)5 MongoClientSettings (com.mongodb.MongoClientSettings)4 MongoException (com.mongodb.MongoException)4 InsertOneResult (com.mongodb.client.result.InsertOneResult)4 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)4 Publisher (org.reactivestreams.Publisher)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 WriteConcern (com.mongodb.WriteConcern)3 DeleteOneModel (com.mongodb.client.model.DeleteOneModel)3 FindPublisher (com.mongodb.reactivestreams.client.FindPublisher)3 MongoClients (com.mongodb.reactivestreams.client.MongoClients)3