use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.
the class UnifiedCrudHelper method executeListIndexes.
OperationResult executeListIndexes(final BsonDocument operation) {
MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
BsonDocument arguments = operation.getDocument("arguments");
ClientSession session = getSession(arguments);
ListIndexesIterable<BsonDocument> iterable = session == null ? collection.listIndexes(BsonDocument.class) : collection.listIndexes(session, BsonDocument.class);
for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
switch(cur.getKey()) {
case "session":
break;
case "batchSize":
iterable.batchSize(cur.getValue().asNumber().intValue());
break;
default:
throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
}
}
return resultOf(() -> new BsonArray(iterable.into(new ArrayList<>())));
}
use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.
the class UnifiedCrudHelper method executeUpdateMany.
OperationResult executeUpdateMany(final BsonDocument operation) {
MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
BsonDocument arguments = operation.getDocument("arguments");
BsonDocument filter = arguments.getDocument("filter");
BsonValue update = arguments.get("update");
ClientSession session = getSession(arguments);
UpdateOptions options = getUpdateOptions(arguments);
return resultOf(() -> {
if (session == null) {
return update.isArray() ? toExpected(collection.updateMany(filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options)) : toExpected(collection.updateMany(filter, update.asDocument(), options));
} else {
return update.isArray() ? toExpected(collection.updateMany(session, filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options)) : toExpected(collection.updateMany(session, filter, update.asDocument(), options));
}
});
}
use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.
the class FailPoint method createClient.
private static MongoClient createClient(final BsonDocument operation, final Entities entities) {
BsonDocument arguments = operation.getDocument("arguments", new BsonDocument());
ClientSession clientSession = entities.getSession(arguments.getString("session").getValue());
if (clientSession.getPinnedServerAddress() == null) {
throw new UnsupportedOperationException("Can't target a failpoint to a server where the session is not pinned");
}
return MongoClients.create(getMongoClientSettingsBuilder().applyToClusterSettings(builder -> builder.hosts(singletonList(clientSession.getPinnedServerAddress()))).build());
}
use of com.mongodb.client.ClientSession in project spring-data-mongodb by spring-projects.
the class MongoDatabaseUtils method doGetMongoDatabase.
private static MongoDatabase doGetMongoDatabase(@Nullable String dbName, MongoDatabaseFactory factory, SessionSynchronization sessionSynchronization) {
Assert.notNull(factory, "Factory must not be null!");
if (sessionSynchronization == SessionSynchronization.NEVER || !TransactionSynchronizationManager.isSynchronizationActive()) {
return StringUtils.hasText(dbName) ? factory.getMongoDatabase(dbName) : factory.getMongoDatabase();
}
ClientSession session = doGetSession(factory, sessionSynchronization);
if (session == null) {
return StringUtils.hasText(dbName) ? factory.getMongoDatabase(dbName) : factory.getMongoDatabase();
}
MongoDatabaseFactory factoryToUse = factory.withSession(session);
return StringUtils.hasText(dbName) ? factoryToUse.getMongoDatabase(dbName) : factoryToUse.getMongoDatabase();
}
use of com.mongodb.client.ClientSession in project spring-data-mongodb by spring-projects.
the class SessionBoundMongoTemplateUnitTests method setUp.
@Before
public void setUp() {
when(client.getDatabase(anyString())).thenReturn(database);
when(codecRegistry.get(any(Class.class))).thenReturn(new BsonValueCodec());
when(database.getCodecRegistry()).thenReturn(codecRegistry);
when(database.getCollection(anyString(), any())).thenReturn(collection);
when(database.listCollectionNames(any(ClientSession.class))).thenReturn(mongoIterable);
when(collection.find(any(ClientSession.class), any(), any())).thenReturn(findIterable);
when(collection.aggregate(any(ClientSession.class), anyList(), any())).thenReturn(aggregateIterable);
when(collection.distinct(any(ClientSession.class), any(), any(), any())).thenReturn(distinctIterable);
when(collection.mapReduce(any(ClientSession.class), any(), any(), any())).thenReturn(mapReduceIterable);
when(findIterable.iterator()).thenReturn(cursor);
when(aggregateIterable.collation(any())).thenReturn(aggregateIterable);
when(aggregateIterable.allowDiskUse(anyBoolean())).thenReturn(aggregateIterable);
when(aggregateIterable.batchSize(anyInt())).thenReturn(aggregateIterable);
when(aggregateIterable.map(any())).thenReturn(aggregateIterable);
when(aggregateIterable.into(any())).thenReturn(Collections.emptyList());
when(mongoIterable.iterator()).thenReturn(cursor);
when(distinctIterable.map(any())).thenReturn(distinctIterable);
when(distinctIterable.into(any())).thenReturn(Collections.emptyList());
when(mapReduceIterable.sort(any())).thenReturn(mapReduceIterable);
when(mapReduceIterable.filter(any())).thenReturn(mapReduceIterable);
when(mapReduceIterable.map(any())).thenReturn(mapReduceIterable);
when(mapReduceIterable.iterator()).thenReturn(cursor);
when(cursor.hasNext()).thenReturn(false);
when(findIterable.projection(any())).thenReturn(findIterable);
factory = new SimpleMongoClientDatabaseFactory(client, "foo");
this.mappingContext = new MongoMappingContext();
this.converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), mappingContext);
this.template = new SessionBoundMongoTemplate(clientSession, new MongoTemplate(factory, converter));
}
Aggregations