Search in sources :

Example 31 with ClientSession

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<>())));
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) BsonArray(org.bson.BsonArray) BsonString(org.bson.BsonString) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 32 with ClientSession

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));
        }
    });
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) BsonValue(org.bson.BsonValue)

Example 33 with ClientSession

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());
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession)

Example 34 with ClientSession

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();
}
Also used : ClientSession(com.mongodb.client.ClientSession)

Example 35 with ClientSession

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));
}
Also used : BsonValueCodec(org.bson.codecs.BsonValueCodec) ClientSession(com.mongodb.client.ClientSession) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) Before(org.junit.Before)

Aggregations

ClientSession (com.mongodb.client.ClientSession)52 BsonDocument (org.bson.BsonDocument)19 BsonValue (org.bson.BsonValue)17 BsonString (org.bson.BsonString)16 Map (java.util.Map)15 Document (org.bson.Document)12 Test (org.junit.jupiter.api.Test)10 BsonArray (org.bson.BsonArray)6 SessionBoundMongoTemplate (org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate)4 TransactionOptions (com.mongodb.TransactionOptions)3 MongoClient (com.mongodb.client.MongoClient)3 MongoDatabase (com.mongodb.client.MongoDatabase)3 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)3 Test (org.junit.Test)3 Point (org.springframework.data.geo.Point)3 MongoVersion (org.springframework.data.mongodb.test.util.MongoVersion)3 DeleteOptions (com.mongodb.client.model.DeleteOptions)2 FindOneAndDeleteOptions (com.mongodb.client.model.FindOneAndDeleteOptions)2 UpdateOptions (com.mongodb.client.model.UpdateOptions)2 NonNull (com.mongodb.lang.NonNull)2