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.Block;
import com.mongodb.WriteConcern;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.async.client.*;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import io.vertx.core.*;
import io.vertx.core.Future;
import io.vertx.core.json.JsonArray;
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.*;
import io.vertx.ext.mongo.MongoClient;
import io.vertx.ext.mongo.impl.codec.json.JsonObjectCodec;
import io.vertx.ext.mongo.impl.config.MongoClientOptionsParser;
import org.bson.BsonDocument;
import org.bson.BsonDocumentReader;
import org.bson.BsonValue;
import org.bson.codecs.DecoderContext;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import java.util.*;
import java.util.function.Function;
import static java.util.Objects.requireNonNull;
/**
* The implementation of the {@link io.vertx.ext.mongo.MongoClient}. This implementation is based on the async driver
* provided by Mongo.
*
* @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;
protected boolean useObjectId;
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();
this.useObjectId = config.getBoolean("useObjectId", false);
}
@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 -> useObjectId ? document.getJsonObject(ID_FIELD).getString(JsonObjectCodec.OID_FIELD) : document.getString(ID_FIELD)));
} else {
JsonObject filter = new JsonObject();
JsonObject encodedDocument = encodeKeyWhenUseObjectId(document);
filter.put(ID_FIELD, encodedDocument.getValue(ID_FIELD));
com.mongodb.client.model.UpdateOptions updateOptions = new com.mongodb.client.model.UpdateOptions()
.upsert(true);
coll.replaceOne(wrap(filter), encodedDocument, updateOptions, 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);
JsonObject encodedDocument = encodeKeyWhenUseObjectId(document);
MongoCollection coll = getCollection(collection, writeOption);
coll.insertOne(encodedDocument, convertCallback(resultHandler, wr -> {
if (id) {
return null;
} else {
JsonObject decodedDocument = decodeKeyWhenUseObjectId(encodedDocument);
return decodedDocument.getString(ID_FIELD);
}
}));
return this;
}
@Deprecated @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 updateCollection(String collection, JsonObject query, JsonObject update, Handler> resultHandler) {
updateCollectionWithOptions(collection, query, update, DEFAULT_UPDATE_OPTIONS, resultHandler);
return this;
}
@Deprecated @Override
public io.vertx.ext.mongo.MongoClient updateWithOptions(String collection, JsonObject query, JsonObject update, UpdateOptions options, Handler> resultHandler) {
updateCollectionWithOptions(collection, query, update, options, toVoidAsyncResult(resultHandler));
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient updateCollectionWithOptions(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), toMongoClientUpdateResult(resultHandler));
} else {
coll.updateOne(bquery, bupdate, mongoUpdateOptions(options), toMongoClientUpdateResult(resultHandler));
}
return this;
}
@Deprecated @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 MongoClient replaceDocuments(String collection, JsonObject query, JsonObject replace, Handler> resultHandler) {
replaceDocumentsWithOptions(collection, query, replace, DEFAULT_UPDATE_OPTIONS, resultHandler);
return this;
}
@Deprecated @Override
public io.vertx.ext.mongo.MongoClient replaceWithOptions(String collection, JsonObject query, JsonObject replace, UpdateOptions options, Handler> resultHandler) {
return replaceDocumentsWithOptions(collection, query, replace, options, toVoidAsyncResult(resultHandler));
}
@Override
public MongoClient replaceDocumentsWithOptions(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");
boolean id = query.containsKey(ID_FIELD);
query = encodeKeyWhenUseObjectId(query); //TODO: Need to write test for this and delete
MongoCollection coll = getCollection(collection, options.getWriteOption());
Bson bquery = wrap(query);
coll.replaceOne(bquery, replace, mongoUpdateOptions(options), toMongoClientUpdateResult(resultHandler));
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 findBatch(String collection, JsonObject query, Handler> resultHandler) {
findBatchWithOptions(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 findBatchWithOptions(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);
Block documentBlock = document -> wrapCallback(resultHandler).onResult(document, null);
SingleResultCallback callbackWhenFinished = (result, throwable) -> {
if (throwable != null) {
resultHandler.handle(Future.failedFuture(throwable));
}
};
view.forEach(documentBlock, callbackWhenFinished);
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");
JsonObject encodedQuery = encodeKeyWhenUseObjectId(query);
Bson bquery = wrap(encodedQuery);
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;
}
@Deprecated @Override
public io.vertx.ext.mongo.MongoClient remove(String collection, JsonObject query, Handler> resultHandler) {
removeWithOptions(collection, query, null, resultHandler);
return this;
}
@Override
public MongoClient removeDocuments(String collection, JsonObject query, Handler> resultHandler) {
removeDocumentsWithOptions(collection, query, null, resultHandler);
return this;
}
@Deprecated @Override
public io.vertx.ext.mongo.MongoClient removeWithOptions(String collection, JsonObject query, WriteOption writeOption, Handler> resultHandler) {
removeDocumentsWithOptions(collection, query, writeOption, toVoidAsyncResult(resultHandler));
return this;
}
@Override
public MongoClient removeDocumentsWithOptions(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, toMongoClientDeleteResult(resultHandler));
return this;
}
@Deprecated @Override
public io.vertx.ext.mongo.MongoClient removeOne(String collection, JsonObject query, Handler> resultHandler) {
removeOneWithOptions(collection, query, null, resultHandler);
return this;
}
@Override
public MongoClient removeDocument(String collection, JsonObject query, Handler> resultHandler) {
removeDocumentWithOptions(collection, query, null, resultHandler);
return this;
}
@Deprecated @Override
public io.vertx.ext.mongo.MongoClient removeOneWithOptions(String collection, JsonObject query, WriteOption writeOption, Handler> resultHandler) {
removeDocumentWithOptions(collection, query, writeOption, toVoidAsyncResult(resultHandler));
return this;
}
@Override
public MongoClient removeDocumentWithOptions(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, toMongoClientDeleteResult(resultHandler));
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<>();
Context context = vertx.getOrCreateContext();
holder.db.listCollectionNames().into(names, (res, error) -> {
context.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;
}
@Override
public io.vertx.ext.mongo.MongoClient distinct(String collection, String fieldName, String resultClassname, Handler> resultHandler) {
DistinctIterable distinctValues = findDistinctValues(collection, fieldName, resultClassname, resultHandler);
if (distinctValues != null) {
List results = new ArrayList();
try {
Context context = vertx.getOrCreateContext();
distinctValues.into(results, (result, throwable) -> {
context.runOnContext(v -> {
if (throwable != null) {
resultHandler.handle(Future.failedFuture(throwable));
} else {
resultHandler.handle(Future.succeededFuture(new JsonArray((List) result)));
}
});
});
} catch (Exception unhandledEx) {
resultHandler.handle(Future.failedFuture(unhandledEx));
}
}
return this;
}
@Override
public io.vertx.ext.mongo.MongoClient distinctBatch(String collection, String fieldName, String resultClassname, Handler> resultHandler) {
DistinctIterable distinctValues = findDistinctValues(collection, fieldName, resultClassname, resultHandler);
if (distinctValues != null) {
Context context = vertx.getOrCreateContext();
Block valueBlock = value -> {
context.runOnContext(v -> {
Map mapValue = new HashMap();
mapValue.put(fieldName, value);
resultHandler.handle(Future.succeededFuture(new JsonObject(mapValue)));
});
};
SingleResultCallback callbackWhenFinished = (result, throwable) -> {
if (throwable != null) {
resultHandler.handle(Future.failedFuture(throwable));
}
};
try {
distinctValues.forEach(valueBlock, callbackWhenFinished);
} catch (Exception unhandledEx) {
resultHandler.handle(Future.failedFuture(unhandledEx));
}
}
return this;
}
private DistinctIterable findDistinctValues(String collection, String fieldName, String resultClassname, Handler resultHandler) {
requireNonNull(collection, "collection cannot be null");
requireNonNull(fieldName, "fieldName cannot be null");
requireNonNull(resultHandler, "resultHandler cannot be null");
final Class resultClass;
try {
resultClass = Class.forName(resultClassname);
} catch (ClassNotFoundException e) {
resultHandler.handle(Future.failedFuture(e));
return null;
}
MongoCollection mongoCollection = getCollection(collection);
return mongoCollection.distinct(fieldName, resultClass);
}
private JsonObject encodeKeyWhenUseObjectId(JsonObject json) {
if (useObjectId && json.containsKey(ID_FIELD) && json.getValue(ID_FIELD) instanceof String) {
String idString = json.getString(ID_FIELD);
if (ObjectId.isValid(idString)) {
json.put(ID_FIELD, new JsonObject().put(JsonObjectCodec.OID_FIELD, idString));
}
}
return json;
}
private JsonObject decodeKeyWhenUseObjectId(JsonObject json) {
if (useObjectId && json.containsKey(ID_FIELD)) {
Object idValue = json.getValue(ID_FIELD);
if (idValue instanceof JsonObject && ((JsonObject) idValue).containsKey(JsonObjectCodec.OID_FIELD)) {
json.put(ID_FIELD, json.getJsonObject(ID_FIELD).getString(JsonObjectCodec.OID_FIELD));
}
}
return json;
}
private SingleResultCallback convertCallback(Handler> resultHandler, Function converter) {
Context context = vertx.getOrCreateContext();
return (result, error) -> {
context.runOnContext(v -> {
if (error != null) {
resultHandler.handle(Future.failedFuture(error));
} else {
resultHandler.handle(Future.succeededFuture(converter.apply(result)));
}
});
};
}
private Handler> toVoidAsyncResult(Handler> resultHandler) {
return result -> {
if(result.succeeded()) {
resultHandler.handle(Future.succeededFuture(null));
} else {
resultHandler.handle(Future.failedFuture(result.cause()));
}
};
}
private SingleResultCallback toMongoClientUpdateResult(Handler> resultHandler) {
return convertCallback(resultHandler, result -> {
if (result.wasAcknowledged()) {
return convertToMongoClientUpdateResult(result.getMatchedCount(), result.getUpsertedId(), result.getModifiedCount());
} else {
return null;
}
});
}
private SingleResultCallback toMongoClientDeleteResult(Handler> resultHandler) {
return convertCallback(resultHandler, result -> {
if (result.wasAcknowledged()) {
return new MongoClientDeleteResult(result.getDeletedCount());
} else {
return null;
}
});
}
private SingleResultCallback wrapCallback(Handler> resultHandler) {
Context context = vertx.getOrCreateContext();
return (result, error) -> {
context.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 MongoClientUpdateResult convertToMongoClientUpdateResult(long docMatched, BsonValue upsertId, long docModified) {
JsonObject jsonUpsertId;
if (upsertId != null) {
JsonObjectCodec jsonObjectCodec = new JsonObjectCodec(new JsonObject());
BsonDocument upsertIdDocument = new BsonDocument();
upsertIdDocument.append(MongoClientUpdateResult.ID_FIELD, upsertId);
BsonDocumentReader bsonDocumentReader = new BsonDocumentReader(upsertIdDocument);
jsonUpsertId = jsonObjectCodec.decode(bsonDocumentReader, DecoderContext.builder().build());
} else {
jsonUpsertId = null;
}
return new MongoClientUpdateResult(docMatched, jsonUpsertId, docModified);
}
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());
db = mongo.getDatabase(parser.database());
}
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