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