Search in sources :

Example 6 with ClientSession

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

the class UnifiedCrudHelper method executeListDatabases.

OperationResult executeListDatabases(final BsonDocument operation) {
    MongoClient client = entities.getClient(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments", new BsonDocument());
    ClientSession session = getSession(arguments);
    ListDatabasesIterable<BsonDocument> iterable = session == null ? client.listDatabases(BsonDocument.class) : client.listDatabases(session, BsonDocument.class);
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        // noinspection SwitchStatementWithTooFewBranches
        switch(cur.getKey()) {
            case "session":
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return resultOf(() -> new BsonArray(iterable.into(new ArrayList<>())));
}
Also used : MongoClient(com.mongodb.client.MongoClient) 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 7 with ClientSession

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

the class UnifiedCrudHelper method executeDistinct.

OperationResult executeDistinct(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    ClientSession session = getSession(arguments);
    BsonString fieldName = arguments.getString("fieldName");
    DistinctIterable<BsonValue> iterable = session == null ? collection.distinct(fieldName.getValue(), BsonValue.class) : collection.distinct(session, fieldName.getValue(), BsonValue.class);
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        switch(cur.getKey()) {
            case "fieldName":
            case "session":
                break;
            case "filter":
                iterable.filter(cur.getValue().asDocument());
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return resultOf(() -> new BsonArray(iterable.into(new ArrayList<>())));
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) ClientSession(com.mongodb.client.ClientSession) BsonArray(org.bson.BsonArray) BsonString(org.bson.BsonString) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 8 with ClientSession

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

the class UnifiedTest method executeAssertSessionTransactionState.

private OperationResult executeAssertSessionTransactionState(final BsonDocument operation) {
    BsonDocument arguments = operation.getDocument("arguments");
    ClientSession session = entities.getSession(arguments.getString("session").getValue());
    String state = arguments.getString("state").getValue();
    // noinspection SwitchStatementWithTooFewBranches
    switch(state) {
        case "starting":
            assertTrue(session.hasActiveTransaction());
            break;
        default:
            throw new UnsupportedOperationException("Unsupported transaction state: " + state);
    }
    return OperationResult.NONE;
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) BsonString(org.bson.BsonString)

Example 9 with ClientSession

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

the class UnifiedTest method executeAssertSessionPinniness.

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

Example 10 with ClientSession

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

the class UnifiedCrudHelper method executeWithTransaction.

OperationResult executeWithTransaction(final BsonDocument operation, final OperationAsserter operationAsserter) {
    ClientSession session = entities.getSession(operation.getString("object").getValue());
    BsonArray callback = operation.getDocument("arguments").getArray("callback");
    TransactionOptions.Builder optionsBuilder = TransactionOptions.builder();
    for (Map.Entry<String, BsonValue> entry : operation.getDocument("arguments").entrySet()) {
        switch(entry.getKey()) {
            case "callback":
                break;
            case "readConcern":
                optionsBuilder.readConcern(asReadConcern(entry.getValue().asDocument()));
                break;
            case "writeConcern":
                optionsBuilder.writeConcern(asWriteConcern(entry.getValue().asDocument()));
                break;
            default:
                throw new UnsupportedOperationException("Unsupported transaction option: " + entry.getKey());
        }
    }
    return resultOf(() -> {
        session.withTransaction(() -> {
            for (int i = 0; i < callback.size(); i++) {
                BsonValue cur = callback.get(i);
                operationAsserter.assertOperation(cur.asDocument(), i);
            }
            // noinspection ConstantConditions
            return null;
        }, optionsBuilder.build());
        return null;
    });
}
Also used : TransactionOptions(com.mongodb.TransactionOptions) ClientSession(com.mongodb.client.ClientSession) BsonArray(org.bson.BsonArray) BsonString(org.bson.BsonString) Map(java.util.Map) 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