io.vertx.ext.mongo.impl.MongoClientImpl Maven / Gradle / Ivy
/*
* Copyright 2014 Red Hat, Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.vertx.ext.mongo.impl;
import com.mongodb.WriteConcern;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.async.client.FindIterable;
import com.mongodb.async.client.MongoClients;
import com.mongodb.async.client.MongoCollection;
import com.mongodb.async.client.MongoDatabase;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.core.shareddata.LocalMap;
import io.vertx.core.shareddata.Shareable;
import io.vertx.ext.mongo.FindOptions;
import io.vertx.ext.mongo.UpdateOptions;
import io.vertx.ext.mongo.WriteOption;
import io.vertx.ext.mongo.impl.config.MongoClientOptionsParser;
import org.bson.conversions.Bson;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import static java.util.Objects.requireNonNull;
/**
* @author Tim Fox
*/
public class MongoClientImpl implements io.vertx.ext.mongo.MongoClient {
private static final Logger log = LoggerFactory.getLogger(MongoClientImpl.class);
private static final UpdateOptions DEFAULT_UPDATE_OPTIONS = new UpdateOptions();
private static final FindOptions DEFAULT_FIND_OPTIONS = new FindOptions();
private static final String ID_FIELD = "_id";
private static final String DS_LOCAL_MAP_NAME = "__vertx.MongoClient.datasources";
private final Vertx vertx;
protected com.mongodb.async.client.MongoClient mongo;
protected final MongoHolder holder;
public MongoClientImpl(Vertx vertx, JsonObject config, String dataSourceName) {
Objects.requireNonNull(vertx);
Objects.requireNonNull(config);
Objects.requireNonNull(dataSourceName);
this.vertx = vertx;
this.holder = lookupHolder(dataSourceName, config);
this.mongo = holder.mongo();
}
@Override
public void close() {
holder.close();
}
@Override
public io.vertx.ext.mongo.MongoClient save(String collection, JsonObject document, Handler> resultHandler) {
saveWithOptions(collection, document, null, resultHandler);
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient saveWithOptions(String collection, JsonObject document, WriteOption writeOption, Handler> resultHandler) {
requireNonNull(collection, "collection cannot be null");
requireNonNull(document, "document cannot be null");
requireNonNull(resultHandler, "resultHandler cannot be null");
MongoCollection coll = getCollection(collection, writeOption);
Object id = document.getValue(ID_FIELD);
if (id == null) {
coll.insertOne(document, convertCallback(resultHandler, wr -> document.getString(ID_FIELD)));
} else {
coll.replaceOne(wrap(new JsonObject().put(ID_FIELD, document.getValue(ID_FIELD))), document, convertCallback(resultHandler, result -> null));
}
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient insert(String collection, JsonObject document, Handler> resultHandler) {
insertWithOptions(collection, document, null, resultHandler);
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient insertWithOptions(String collection, JsonObject document, WriteOption writeOption, Handler> resultHandler) {
requireNonNull(collection, "collection cannot be null");
requireNonNull(document, "document cannot be null");
requireNonNull(resultHandler, "resultHandler cannot be null");
boolean id = document.containsKey(ID_FIELD);
MongoCollection coll = getCollection(collection, writeOption);
coll.insertOne(document, convertCallback(resultHandler, wr -> {
if (id) {
return null;
} else {
return document.getString(ID_FIELD);
}
}));
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient update(String collection, JsonObject query, JsonObject update, Handler> resultHandler) {
updateWithOptions(collection, query, update, DEFAULT_UPDATE_OPTIONS, resultHandler);
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient updateWithOptions(String collection, JsonObject query, JsonObject update, UpdateOptions options, Handler> resultHandler) {
requireNonNull(collection, "collection cannot be null");
requireNonNull(query, "query cannot be null");
requireNonNull(update, "update cannot be null");
requireNonNull(options, "options cannot be null");
requireNonNull(resultHandler, "resultHandler cannot be null");
MongoCollection coll = getCollection(collection, options.getWriteOption());
Bson bquery = wrap(query);
Bson bupdate = wrap(update);
if (options.isMulti()) {
coll.updateMany(bquery, bupdate, mongoUpdateOptions(options), convertCallback(resultHandler, result -> null));
} else {
coll.updateOne(bquery, bupdate, mongoUpdateOptions(options), convertCallback(resultHandler, result -> null));
}
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient replace(String collection, JsonObject query, JsonObject replace, Handler> resultHandler) {
replaceWithOptions(collection, query, replace, DEFAULT_UPDATE_OPTIONS, resultHandler);
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient replaceWithOptions(String collection, JsonObject query, JsonObject replace, UpdateOptions options, Handler> resultHandler) {
requireNonNull(collection, "collection cannot be null");
requireNonNull(query, "query cannot be null");
requireNonNull(replace, "update cannot be null");
requireNonNull(options, "options cannot be null");
requireNonNull(resultHandler, "resultHandler cannot be null");
MongoCollection coll = getCollection(collection, options.getWriteOption());
Bson bquery = wrap(query);
coll.replaceOne(bquery, replace, mongoUpdateOptions(options), convertCallback(resultHandler, result -> null));
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient find(String collection, JsonObject query, Handler>> resultHandler) {
findWithOptions(collection, query, DEFAULT_FIND_OPTIONS, resultHandler);
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient findWithOptions(String collection, JsonObject query, FindOptions options, Handler>> resultHandler) {
requireNonNull(collection, "collection cannot be null");
requireNonNull(query, "query cannot be null");
requireNonNull(resultHandler, "resultHandler cannot be null");
FindIterable view = doFind(collection, query, options);
List results = new ArrayList<>();
view.into(results, wrapCallback(resultHandler));
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient findOne(String collection, JsonObject query, JsonObject fields, Handler> resultHandler) {
requireNonNull(collection, "collection cannot be null");
requireNonNull(query, "query cannot be null");
requireNonNull(resultHandler, "resultHandler cannot be null");
Bson bquery = wrap(query);
Bson bfields = wrap(fields);
getCollection(collection).find(bquery).projection(bfields).first(wrapCallback(resultHandler));
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient count(String collection, JsonObject query, Handler> resultHandler) {
requireNonNull(collection, "collection cannot be null");
requireNonNull(query, "query cannot be null");
requireNonNull(resultHandler, "resultHandler cannot be null");
Bson bquery = wrap(query);
MongoCollection coll = getCollection(collection);
coll.count(bquery, wrapCallback(resultHandler));
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient remove(String collection, JsonObject query, Handler> resultHandler) {
removeWithOptions(collection, query, null, resultHandler);
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient removeWithOptions(String collection, JsonObject query, WriteOption writeOption, Handler> resultHandler) {
requireNonNull(collection, "collection cannot be null");
requireNonNull(query, "query cannot be null");
requireNonNull(resultHandler, "resultHandler cannot be null");
MongoCollection coll = getCollection(collection, writeOption);
Bson bquery = wrap(query);
coll.deleteMany(bquery, convertCallback(resultHandler, result -> null));
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient removeOne(String collection, JsonObject query, Handler> resultHandler) {
removeOneWithOptions(collection, query, null, resultHandler);
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient removeOneWithOptions(String collection, JsonObject query, WriteOption writeOption, Handler> resultHandler) {
requireNonNull(collection, "collection cannot be null");
requireNonNull(query, "query cannot be null");
requireNonNull(resultHandler, "resultHandler cannot be null");
MongoCollection coll = getCollection(collection, writeOption);
Bson bquery = wrap(query);
coll.deleteOne(bquery, convertCallback(resultHandler, result -> null));
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient createCollection(String collection, Handler> resultHandler) {
requireNonNull(collection, "collection cannot be null");
requireNonNull(resultHandler, "resultHandler cannot be null");
holder.db.createCollection(collection, wrapCallback(resultHandler));
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient getCollections(Handler>> resultHandler) {
requireNonNull(resultHandler, "resultHandler cannot be null");
List names = new ArrayList<>();
holder.db.listCollectionNames().into(names, (res, error) -> {
vertx.runOnContext(v -> {
if (error != null) {
resultHandler.handle(Future.failedFuture(error));
} else {
resultHandler.handle(Future.succeededFuture(names));
}
});
});
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient dropCollection(String collection, Handler> resultHandler) {
requireNonNull(collection, "collection cannot be null");
requireNonNull(resultHandler, "resultHandler cannot be null");
MongoCollection coll = getCollection(collection);
coll.drop(wrapCallback(resultHandler));
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient runCommand(String commandName, JsonObject command, Handler> resultHandler) {
requireNonNull(commandName, "commandName cannot be null");
requireNonNull(command, "command cannot be null");
requireNonNull(resultHandler, "resultHandler cannot be null");
// The command name must be the first entry in the bson, so to ensure this we must recreate and add the command
// name as first (JsonObject is internally ordered)
JsonObject json = new JsonObject();
Object commandVal = command.getValue(commandName);
if (commandVal == null) {
throw new IllegalArgumentException("commandBody does not contain key for " + commandName);
}
json.put(commandName, commandVal);
command.forEach(entry -> {
if (!entry.getKey().equals(commandName)) {
json.put(entry.getKey(), entry.getValue());
}
});
holder.db.runCommand(wrap(json), JsonObject.class, wrapCallback(resultHandler));
return this;
}
private SingleResultCallback convertCallback(Handler> resultHandler, Function converter) {
return (result, error) -> {
vertx.runOnContext(v -> {
if (error != null) {
resultHandler.handle(Future.failedFuture(error));
} else {
resultHandler.handle(Future.succeededFuture(converter.apply(result)));
}
});
};
}
private SingleResultCallback wrapCallback(Handler> resultHandler) {
return (result, error) -> {
vertx.runOnContext(v -> {
if (error != null) {
resultHandler.handle(Future.failedFuture(error));
} else {
resultHandler.handle(Future.succeededFuture(result));
}
});
};
}
private FindIterable doFind(String collection, JsonObject query, FindOptions options) {
return doFind(collection, null, query, options);
}
private FindIterable doFind(String collection, WriteOption writeOption, JsonObject query, FindOptions options) {
MongoCollection coll = getCollection(collection, writeOption);
Bson bquery = wrap(query);
FindIterable find = coll.find(bquery, JsonObject.class);
if (options.getLimit() != -1) {
find.limit(options.getLimit());
}
if (options.getSkip() > 0) {
find.skip(options.getSkip());
}
if (options.getSort() != null) {
find.sort(wrap(options.getSort()));
}
if (options.getFields() != null) {
find.projection(wrap(options.getFields()));
}
return find;
}
private MongoCollection getCollection(String name) {
return getCollection(name, null);
}
private MongoCollection getCollection(String name, WriteOption writeOption) {
MongoCollection coll = holder.db.getCollection(name, JsonObject.class);
if (coll != null && writeOption != null) {
coll = coll.withWriteConcern(WriteConcern.valueOf(writeOption.name()));
}
return coll;
}
private static com.mongodb.client.model.UpdateOptions mongoUpdateOptions(UpdateOptions options) {
return new com.mongodb.client.model.UpdateOptions().upsert(options.isUpsert());
}
private JsonObjectBsonAdapter wrap(JsonObject jsonObject) {
return jsonObject == null ? null : new JsonObjectBsonAdapter(jsonObject);
}
private void removeFromMap(LocalMap map, String dataSourceName) {
synchronized (vertx) {
map.remove(dataSourceName);
if (map.isEmpty()) {
map.close();
}
}
}
private MongoHolder lookupHolder(String datasourceName, JsonObject config) {
synchronized (vertx) {
LocalMap map = vertx.sharedData().getLocalMap(DS_LOCAL_MAP_NAME);
MongoHolder theHolder = map.get(datasourceName);
if (theHolder == null) {
theHolder = new MongoHolder(config, () -> removeFromMap(map, datasourceName));
map.put(datasourceName, theHolder);
} else {
theHolder.incRefCount();
}
return theHolder;
}
}
private static class MongoHolder implements Shareable{
com.mongodb.async.client.MongoClient mongo;
MongoDatabase db;
JsonObject config;
Runnable closeRunner;
int refCount = 1;
public MongoHolder(JsonObject config, Runnable closeRunner) {
this.config = config;
this.closeRunner = closeRunner;
}
synchronized com.mongodb.async.client.MongoClient mongo() {
if (mongo == null) {
MongoClientOptionsParser parser = new MongoClientOptionsParser(config);
mongo = MongoClients.create(parser.settings());
String dbName = config.getString("db_name", DEFAULT_DB_NAME);
db = mongo.getDatabase(dbName);
}
return mongo;
}
synchronized void incRefCount() {
refCount++;
}
synchronized void close() {
if (--refCount == 0) {
if (mongo != null) {
mongo.close();
}
if (closeRunner != null) {
closeRunner.run();
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy