use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.
the class UnifiedCrudHelper method executeCreateCollection.
public OperationResult executeCreateCollection(final BsonDocument operation) {
MongoDatabase database = entities.getDatabase(operation.getString("object").getValue());
BsonDocument arguments = operation.getDocument("arguments");
String collectionName = arguments.getString("collection").getValue();
ClientSession session = getSession(arguments);
CreateCollectionOptions options = new CreateCollectionOptions();
for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
switch(cur.getKey()) {
case "collection":
case "session":
break;
case "expireAfterSeconds":
options.expireAfter(cur.getValue().asNumber().longValue(), TimeUnit.SECONDS);
break;
case "timeseries":
options.timeSeriesOptions(createTimeSeriesOptions(cur.getValue().asDocument()));
break;
default:
throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
}
}
return resultOf(() -> {
if (session == null) {
database.createCollection(collectionName, options);
} else {
database.createCollection(session, collectionName, options);
}
return null;
});
}
use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.
the class UnifiedCrudHelper method createFindIterable.
@NotNull
private FindIterable<BsonDocument> createFindIterable(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");
FindIterable<BsonDocument> iterable = session == null ? collection.find(filter) : collection.find(session, filter);
for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
switch(cur.getKey()) {
case "session":
case "filter":
break;
case "sort":
iterable.sort(cur.getValue().asDocument());
break;
case "batchSize":
iterable.batchSize(cur.getValue().asInt32().intValue());
break;
case "limit":
iterable.limit(cur.getValue().asInt32().intValue());
break;
case "allowDiskUse":
iterable.allowDiskUse(cur.getValue().asBoolean().getValue());
break;
default:
throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
}
}
return iterable;
}
use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.
the class UnifiedCrudHelper method executeAggregate.
OperationResult executeAggregate(final BsonDocument operation) {
String entityName = operation.getString("object").getValue();
BsonDocument arguments = operation.getDocument("arguments");
ClientSession session = getSession(arguments);
List<BsonDocument> pipeline = arguments.getArray("pipeline").stream().map(BsonValue::asDocument).collect(toList());
AggregateIterable<BsonDocument> iterable;
if (entities.hasDatabase(entityName)) {
iterable = session == null ? entities.getDatabase(entityName).aggregate(requireNonNull(pipeline), BsonDocument.class) : entities.getDatabase(entityName).aggregate(session, requireNonNull(pipeline), BsonDocument.class);
} else if (entities.hasCollection(entityName)) {
iterable = session == null ? entities.getCollection(entityName).aggregate(requireNonNull(pipeline)) : entities.getCollection(entityName).aggregate(session, requireNonNull(pipeline));
} else {
throw new UnsupportedOperationException("Unsupported entity type with name: " + entityName);
}
for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
switch(cur.getKey()) {
case "pipeline":
case "session":
break;
case "batchSize":
iterable.batchSize(cur.getValue().asNumber().intValue());
break;
case "allowDiskUse":
iterable.allowDiskUse(cur.getValue().asBoolean().getValue());
break;
case "let":
iterable.let(cur.getValue().asDocument());
break;
default:
throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
}
}
String lastStageName = pipeline.isEmpty() ? null : pipeline.get(pipeline.size() - 1).getFirstKey();
boolean useToCollection = Objects.equals(lastStageName, "$out") || Objects.equals(lastStageName, "$merge");
return resultOf(() -> {
if (!pipeline.isEmpty() && useToCollection) {
iterable.toCollection();
return null;
} else {
return new BsonArray(iterable.into(new ArrayList<>()));
}
});
}
use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.
the class UnifiedCrudHelper method executeCountDocuments.
public OperationResult executeCountDocuments(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);
for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
switch(cur.getKey()) {
case "filter":
case "session":
break;
default:
throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
}
}
return resultOf(() -> {
if (session == null) {
return new BsonInt64(collection.countDocuments(filter));
} else {
return new BsonInt64(collection.countDocuments(session, filter));
}
});
}
use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.
the class UnifiedCrudHelper method executeInsertOne.
OperationResult executeInsertOne(final BsonDocument operation) {
MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
BsonDocument arguments = operation.getDocument("arguments");
ClientSession session = getSession(arguments);
BsonDocument document = arguments.getDocument("document").asDocument();
InsertOneOptions options = new InsertOneOptions();
for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
switch(cur.getKey()) {
case "session":
case "document":
break;
default:
throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
}
}
return resultOf(() -> toExpected(session == null ? collection.insertOne(document, options) : collection.insertOne(session, document, options)));
}
Aggregations