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