Search in sources :

Example 21 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class UnifiedTest method executeAssertSessionDirtiness.

private OperationResult executeAssertSessionDirtiness(final BsonDocument operation, final boolean expected) {
    ClientSession session = entities.getSession(operation.getDocument("arguments").getString("session").getValue());
    assertNotNull(session.getServerSession());
    assertEquals(expected, session.getServerSession().isMarkedDirty());
    return OperationResult.NONE;
}
Also used : ClientSession(com.mongodb.client.ClientSession)

Example 22 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeDeleteMany.

OperationResult executeDeleteMany(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    BsonDocument filter = arguments.getDocument("filter");
    ClientSession session = getSession(arguments);
    DeleteOptions options = getDeleteOptions(arguments);
    return resultOf(() -> {
        if (session == null) {
            return toExpected(collection.deleteMany(filter, options));
        } else {
            return toExpected(collection.deleteMany(session, filter, options));
        }
    });
}
Also used : DeleteOptions(com.mongodb.client.model.DeleteOptions) FindOneAndDeleteOptions(com.mongodb.client.model.FindOneAndDeleteOptions) BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession)

Example 23 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeUpdateOne.

OperationResult executeUpdateOne(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    ClientSession session = getSession(arguments);
    BsonDocument filter = arguments.getDocument("filter");
    BsonValue update = arguments.get("update");
    UpdateOptions options = getUpdateOptions(arguments);
    return resultOf(() -> {
        UpdateResult updateResult;
        if (session == null) {
            updateResult = update.isArray() ? collection.updateOne(filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options) : collection.updateOne(filter, update.asDocument(), options);
        } else {
            updateResult = update.isArray() ? collection.updateOne(session, filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options) : collection.updateOne(session, filter, update.asDocument(), options);
        }
        return toExpected(updateResult);
    });
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) UpdateResult(com.mongodb.client.result.UpdateResult) BsonValue(org.bson.BsonValue)

Example 24 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeListCollections.

OperationResult executeListCollections(final BsonDocument operation) {
    MongoDatabase database = entities.getDatabase(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    ClientSession session = getSession(arguments);
    ListCollectionsIterable<BsonDocument> iterable = session == null ? database.listCollections(BsonDocument.class) : database.listCollections(session, BsonDocument.class);
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        switch(cur.getKey()) {
            case "session":
                break;
            case "filter":
                iterable.filter(cur.getValue().asDocument());
                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) MongoDatabase(com.mongodb.client.MongoDatabase) BsonValue(org.bson.BsonValue)

Example 25 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class Entities method initSession.

private void initSession(final BsonDocument entity, final String id) {
    MongoClient client = clients.get(entity.getString("client").getValue());
    ClientSessionOptions.Builder optionsBuilder = ClientSessionOptions.builder();
    if (entity.containsKey("sessionOptions")) {
        for (Map.Entry<String, BsonValue> entry : entity.getDocument("sessionOptions").entrySet()) {
            switch(entry.getKey()) {
                case "defaultTransactionOptions":
                    optionsBuilder.defaultTransactionOptions(getTransactionOptions(entry.getValue().asDocument()));
                    break;
                case "snapshot":
                    optionsBuilder.snapshot(entry.getValue().asBoolean().getValue());
                    break;
                default:
                    throw new UnsupportedOperationException("Unsupported session option: " + entry.getKey());
            }
        }
    }
    ClientSession session = client.startSession(optionsBuilder.build());
    putEntity(id, session, sessions);
    putEntity(id + "-identifier", session.getServerSession().getIdentifier(), sessionIdentifiers);
}
Also used : MongoClient(com.mongodb.client.MongoClient) ClientSession(com.mongodb.client.ClientSession) ClusterFixture.getMultiMongosConnectionString(com.mongodb.ClusterFixture.getMultiMongosConnectionString) EventMatcher.getReasonString(com.mongodb.client.unified.EventMatcher.getReasonString) BsonString(org.bson.BsonString) ClientSessionOptions(com.mongodb.ClientSessionOptions) Map(java.util.Map) HashMap(java.util.HashMap) BsonValue(org.bson.BsonValue)

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