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();
}
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);
}
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;
}
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));
}
}
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();
}
Aggregations