Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.mongodb.async.client.MongoCollectionImpl Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2014 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.async.client;
import com.mongodb.MongoBulkWriteException;
import com.mongodb.MongoException;
import com.mongodb.MongoNamespace;
import com.mongodb.MongoWriteConcernException;
import com.mongodb.MongoWriteException;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import com.mongodb.WriteError;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.bulk.DeleteRequest;
import com.mongodb.bulk.InsertRequest;
import com.mongodb.bulk.UpdateRequest;
import com.mongodb.bulk.WriteRequest;
import com.mongodb.client.model.AggregateOptions;
import com.mongodb.client.model.BulkWriteOptions;
import com.mongodb.client.model.CountOptions;
import com.mongodb.client.model.CreateIndexOptions;
import com.mongodb.client.model.DeleteManyModel;
import com.mongodb.client.model.DeleteOneModel;
import com.mongodb.client.model.DistinctOptions;
import com.mongodb.client.model.FindOneAndDeleteOptions;
import com.mongodb.client.model.FindOneAndReplaceOptions;
import com.mongodb.client.model.FindOneAndUpdateOptions;
import com.mongodb.client.model.FindOptions;
import com.mongodb.client.model.InsertManyOptions;
import com.mongodb.client.model.InsertOneModel;
import com.mongodb.client.model.MapReduceOptions;
import com.mongodb.client.model.RenameCollectionOptions;
import com.mongodb.client.model.ReplaceOneModel;
import com.mongodb.client.model.UpdateManyModel;
import com.mongodb.client.model.UpdateOneModel;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.WriteModel;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.mongodb.operation.AggregateOperation;
import com.mongodb.operation.AggregateToCollectionOperation;
import com.mongodb.operation.AsyncOperationExecutor;
import com.mongodb.operation.CountOperation;
import com.mongodb.operation.CreateIndexOperation;
import com.mongodb.operation.DistinctOperation;
import com.mongodb.operation.DropCollectionOperation;
import com.mongodb.operation.DropIndexOperation;
import com.mongodb.operation.FindAndDeleteOperation;
import com.mongodb.operation.FindAndReplaceOperation;
import com.mongodb.operation.FindAndUpdateOperation;
import com.mongodb.operation.MapReduceStatistics;
import com.mongodb.operation.MapReduceToCollectionOperation;
import com.mongodb.operation.MapReduceWithInlineResultsOperation;
import com.mongodb.operation.MixedBulkWriteOperation;
import com.mongodb.operation.RenameCollectionOperation;
import org.bson.BsonArray;
import org.bson.BsonDocument;
import org.bson.BsonDocumentReader;
import org.bson.BsonDocumentWrapper;
import org.bson.BsonJavaScript;
import org.bson.BsonString;
import org.bson.BsonValue;
import org.bson.Document;
import org.bson.codecs.Codec;
import org.bson.codecs.CollectibleCodec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.configuration.CodecRegistry;
import java.util.ArrayList;
import java.util.List;
import static com.mongodb.ReadPreference.primary;
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.async.ErrorHandlingResultCallback.errorHandlingCallback;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
class MongoCollectionImpl implements MongoCollection {
private final MongoNamespace namespace;
private final Class clazz;
private final ReadPreference readPreference;
private final CodecRegistry codecRegistry;
private final WriteConcern writeConcern;
private final AsyncOperationExecutor executor;
MongoCollectionImpl(final MongoNamespace namespace, final Class clazz, final CodecRegistry codecRegistry,
final ReadPreference readPreference, final WriteConcern writeConcern, final AsyncOperationExecutor executor) {
this.namespace = notNull("namespace", namespace);
this.clazz = notNull("clazz", clazz);
this.codecRegistry = notNull("codecRegistry", codecRegistry);
this.readPreference = notNull("readPreference", readPreference);
this.writeConcern = notNull("writeConcern", writeConcern);
this.executor = notNull("executor", executor);
}
@Override
public MongoNamespace getNamespace() {
return namespace;
}
@Override
public Class getDefaultClass() {
return clazz;
}
@Override
public CodecRegistry getCodecRegistry() {
return codecRegistry;
}
@Override
public ReadPreference getReadPreference() {
return readPreference;
}
@Override
public WriteConcern getWriteConcern() {
return writeConcern;
}
@Override
public MongoCollection withDefaultClass(final Class clazz) {
return new MongoCollectionImpl(namespace, clazz, codecRegistry, readPreference, writeConcern, executor);
}
@Override
public MongoCollection withCodecRegistry(final CodecRegistry codecRegistry) {
return new MongoCollectionImpl(namespace, clazz, codecRegistry, readPreference, writeConcern, executor);
}
@Override
public MongoCollection withReadPreference(final ReadPreference readPreference) {
return new MongoCollectionImpl(namespace, clazz, codecRegistry, readPreference, writeConcern, executor);
}
@Override
public MongoCollection withWriteConcern(final WriteConcern writeConcern) {
return new MongoCollectionImpl(namespace, clazz, codecRegistry, readPreference, writeConcern, executor);
}
@Override
public void count(final SingleResultCallback callback) {
count(new BsonDocument(), new CountOptions(), callback);
}
@Override
public void count(final Object filter, final SingleResultCallback callback) {
count(filter, new CountOptions(), callback);
}
@Override
public void count(final Object filter, final CountOptions options, final SingleResultCallback callback) {
CountOperation operation = new CountOperation(namespace)
.filter(asBson(filter))
.skip(options.getSkip())
.limit(options.getLimit())
.maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS);
if (options.getHint() != null) {
operation.hint(asBson(options.getHint()));
} else if (options.getHintString() != null) {
operation.hint(new BsonString(options.getHintString()));
}
executor.execute(operation, readPreference, callback);
}
@Override
public void distinct(final String fieldName, final Object filter, final SingleResultCallback> callback) {
distinct(fieldName, filter, new DistinctOptions(), callback);
}
@Override
public void distinct(final String fieldName, final Object filter, final DistinctOptions options,
final SingleResultCallback> callback) {
DistinctOperation operation = new DistinctOperation(namespace, fieldName)
.filter(asBson(filter))
.maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS);
executor.execute(operation, readPreference, errorHandlingCallback(new SingleResultCallback() {
@Override
public void onResult(final BsonArray result, final Throwable t) {
if (t != null) {
callback.onResult(null, MongoException.fromThrowable(t));
} else {
try {
List distinctList = new ArrayList();
for (BsonValue value : result) {
BsonDocument bsonDocument = new BsonDocument("value", value);
Document document = getCodec(Document.class).decode(new BsonDocumentReader(bsonDocument),
DecoderContext.builder().build());
distinctList.add(document.get("value"));
}
callback.onResult(distinctList, null);
} catch (Throwable tr) {
callback.onResult(null, new MongoException("Error when decoding distinct results", tr));
}
}
}
}));
}
@Override
public FindFluent find() {
return find(new BsonDocument(), clazz);
}
@Override
public FindFluent find(final Class clazz) {
return find(new BsonDocument(), clazz);
}
@Override
public FindFluent find(final Object filter) {
return find(filter, clazz);
}
@Override
public FindFluent find(final Object filter, final Class clazz) {
return new FindFluentImpl(namespace, clazz, codecRegistry, readPreference, executor, filter, new FindOptions());
}
@Override
public MongoIterable aggregate(final List> pipeline) {
return aggregate(pipeline, new AggregateOptions(), Document.class);
}
@Override
public MongoIterable aggregate(final List> pipeline, final Class clazz) {
return aggregate(pipeline, new AggregateOptions(), clazz);
}
@Override
public MongoIterable aggregate(final List> pipeline, final AggregateOptions options) {
return aggregate(pipeline, options, Document.class);
}
@Override
public MongoIterable aggregate(final List> pipeline, final AggregateOptions options, final Class clazz) {
List aggregateList = createBsonDocumentList(pipeline);
BsonValue outCollection = getAggregateOutCollection(aggregateList);
if (outCollection != null) {
AggregateToCollectionOperation operation = new AggregateToCollectionOperation(namespace, aggregateList)
.maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS)
.allowDiskUse(options.getAllowDiskUse());
MongoIterable delegated = new FindFluentImpl(new MongoNamespace(namespace.getDatabaseName(),
outCollection.asString().getValue()),
clazz, codecRegistry, primary(), executor, new BsonDocument(),
new FindOptions());
return new AwaitingWriteOperationIterable(operation, executor, delegated);
} else {
return new OperationIterable(new AggregateOperation(namespace, aggregateList, getCodec(clazz))
.maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS)
.allowDiskUse(options.getAllowDiskUse())
.batchSize(options.getBatchSize())
.useCursor(options.getUseCursor()),
readPreference,
executor);
}
}
@Override
public void aggregateToCollection(final List> pipeline, final SingleResultCallback callback) {
aggregateToCollection(pipeline, new AggregateOptions(), callback);
}
@Override
public void aggregateToCollection(final List> pipeline, final AggregateOptions options, final SingleResultCallback callback) {
List aggregateList = createBsonDocumentList(pipeline);
BsonValue outCollection = getAggregateOutCollection(aggregateList);
if (outCollection == null) {
throw new IllegalArgumentException("The last stage of the aggregation pipeline must be $out");
}
executor.execute(new AggregateToCollectionOperation(namespace, aggregateList)
.maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS)
.allowDiskUse(options.getAllowDiskUse()), callback);
}
private BsonValue getAggregateOutCollection(final List aggregateList) {
return aggregateList.size() == 0 ? null : aggregateList.get(aggregateList.size() - 1).get("$out");
}
@Override
public MongoIterable mapReduce(final String mapFunction, final String reduceFunction) {
return mapReduce(mapFunction, reduceFunction, new MapReduceOptions());
}
@Override
public MongoIterable mapReduce(final String mapFunction, final String reduceFunction, final MapReduceOptions options) {
return mapReduce(mapFunction, reduceFunction, options, Document.class);
}
@Override
public MongoIterable mapReduce(final String mapFunction, final String reduceFunction, final Class clazz) {
return mapReduce(mapFunction, reduceFunction, new MapReduceOptions(), clazz);
}
@Override
public MongoIterable mapReduce(final String mapFunction, final String reduceFunction, final MapReduceOptions options,
final Class clazz) {
if (options.isInline()) {
MapReduceWithInlineResultsOperation operation =
new MapReduceWithInlineResultsOperation(getNamespace(),
new BsonJavaScript(mapFunction),
new BsonJavaScript(reduceFunction),
getCodec(clazz))
.filter(asBson(options.getFilter()))
.limit(options.getLimit())
.maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS)
.jsMode(options.isJsMode())
.scope(asBson(options.getScope()))
.sort(asBson(options.getSort()))
.verbose(options.isVerbose());
if (options.getFinalizeFunction() != null) {
operation.finalizeFunction(new BsonJavaScript(options.getFinalizeFunction()));
}
return new OperationIterable(operation, readPreference, executor);
} else {
MapReduceToCollectionOperation operation = createMapReduceToCollectionOperation(mapFunction, reduceFunction, options);
String databaseName = options.getDatabaseName() != null ? options.getDatabaseName() : namespace.getDatabaseName();
MongoIterable delegated = new FindFluentImpl(new MongoNamespace(databaseName, options.getCollectionName()),
clazz, codecRegistry, primary(), executor,
new BsonDocument(), new FindOptions());
return new AwaitingWriteOperationIterable(operation, executor, delegated);
}
}
@Override
public void mapReduceToCollection(final String mapFunction, final String reduceFunction, final MapReduceOptions options,
final SingleResultCallback callback) {
if (options.isInline()) {
throw new IllegalArgumentException("The options must specify a non-inline result");
}
executor.execute(createMapReduceToCollectionOperation(mapFunction, reduceFunction, options),
new SingleResultCallback() {
@Override
public void onResult(final MapReduceStatistics result, final Throwable t) {
callback.onResult(null, t);
}
});
}
private MapReduceToCollectionOperation createMapReduceToCollectionOperation(final String mapFunction, final String reduceFunction,
final MapReduceOptions options) {
MapReduceToCollectionOperation operation =
new MapReduceToCollectionOperation(getNamespace(),
new BsonJavaScript(mapFunction),
new BsonJavaScript(reduceFunction),
options.getCollectionName())
.filter(asBson(options.getFilter()))
.limit(options.getLimit())
.maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS)
.jsMode(options.isJsMode())
.scope(asBson(options.getScope()))
.sort(asBson(options.getSort()))
.verbose(options.isVerbose())
.action(options.getAction().getValue())
.nonAtomic(options.isNonAtomic())
.sharded(options.isSharded())
.databaseName(options.getDatabaseName());
if (options.getFinalizeFunction() != null) {
operation.finalizeFunction(new BsonJavaScript(options.getFinalizeFunction()));
}
return operation;
}
@Override
public void bulkWrite(final List extends WriteModel extends T>> requests, final SingleResultCallback callback) {
bulkWrite(requests, new BulkWriteOptions(), callback);
}
@SuppressWarnings("unchecked")
@Override
public void bulkWrite(final List extends WriteModel extends T>> requests, final BulkWriteOptions options,
final SingleResultCallback callback) {
List writeRequests = new ArrayList(requests.size());
for (WriteModel extends T> writeModel : requests) {
WriteRequest writeRequest;
if (writeModel instanceof InsertOneModel) {
InsertOneModel insertOneModel = (InsertOneModel) writeModel;
if (getCodec() instanceof CollectibleCodec) {
((CollectibleCodec) getCodec()).generateIdIfAbsentFromDocument(insertOneModel.getDocument());
}
writeRequest = new InsertRequest(asBson(insertOneModel.getDocument()));
} else if (writeModel instanceof ReplaceOneModel) {
ReplaceOneModel replaceOneModel = (ReplaceOneModel) writeModel;
writeRequest = new UpdateRequest(asBson(replaceOneModel.getFilter()), asBson(replaceOneModel.getReplacement()),
WriteRequest.Type.REPLACE)
.upsert(replaceOneModel.getOptions().isUpsert());
} else if (writeModel instanceof UpdateOneModel) {
UpdateOneModel updateOneModel = (UpdateOneModel) writeModel;
writeRequest = new UpdateRequest(asBson(updateOneModel.getFilter()), asBson(updateOneModel.getUpdate()),
WriteRequest.Type.UPDATE)
.multi(false)
.upsert(updateOneModel.getOptions().isUpsert());
} else if (writeModel instanceof UpdateManyModel) {
UpdateManyModel updateManyModel = (UpdateManyModel) writeModel;
writeRequest = new UpdateRequest(asBson(updateManyModel.getFilter()), asBson(updateManyModel.getUpdate()),
WriteRequest.Type.UPDATE)
.multi(true)
.upsert(updateManyModel.getOptions().isUpsert());
} else if (writeModel instanceof DeleteOneModel) {
DeleteOneModel deleteOneModel = (DeleteOneModel) writeModel;
writeRequest = new DeleteRequest(asBson(deleteOneModel.getFilter())).multi(false);
} else if (writeModel instanceof DeleteManyModel) {
DeleteManyModel deleteManyModel = (DeleteManyModel) writeModel;
writeRequest = new DeleteRequest(asBson(deleteManyModel.getFilter())).multi(true);
} else {
throw new UnsupportedOperationException(format("WriteModel of type %s is not supported", writeModel.getClass()));
}
writeRequests.add(writeRequest);
}
executor.execute(new MixedBulkWriteOperation(namespace, writeRequests, options.isOrdered(), writeConcern), callback);
}
@Override
public void insertOne(final T document, final SingleResultCallback callback) {
if (getCodec() instanceof CollectibleCodec) {
((CollectibleCodec) getCodec()).generateIdIfAbsentFromDocument(document);
}
executeSingleWriteRequest(new InsertRequest(asBson(document)), new SingleResultCallback() {
@Override
public void onResult(final BulkWriteResult result, final Throwable t) {
callback.onResult(null, t);
}
});
}
@Override
public void insertMany(final List extends T> documents, final SingleResultCallback callback) {
insertMany(documents, new InsertManyOptions(), callback);
}
@Override
public void insertMany(final List extends T> documents, final InsertManyOptions options,
final SingleResultCallback callback) {
List requests = new ArrayList(documents.size());
for (T document : documents) {
if (getCodec() instanceof CollectibleCodec) {
((CollectibleCodec) getCodec()).generateIdIfAbsentFromDocument(document);
}
requests.add(new InsertRequest(asBson(document)));
}
executor.execute(new MixedBulkWriteOperation(namespace, requests, options.isOrdered(), writeConcern),
errorHandlingCallback(new SingleResultCallback() {
@Override
public void onResult(final BulkWriteResult result, final Throwable t) {
callback.onResult(null, t);
}
}));
}
@Override
public void deleteOne(final Object filter, final SingleResultCallback callback) {
delete(filter, false, callback);
}
@Override
public void deleteMany(final Object filter, final SingleResultCallback callback) {
delete(filter, true, callback);
}
@Override
public void replaceOne(final Object filter, final T replacement, final SingleResultCallback callback) {
replaceOne(filter, replacement, new UpdateOptions(), callback);
}
@Override
public void replaceOne(final Object filter, final T replacement, final UpdateOptions options,
final SingleResultCallback callback) {
executeSingleWriteRequest(new UpdateRequest(asBson(filter), asBson(replacement), WriteRequest.Type.REPLACE)
.upsert(options.isUpsert()),
new SingleResultCallback() {
@Override
public void onResult(final BulkWriteResult result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
callback.onResult(toUpdateResult(result), null);
}
}
});
}
@Override
public void updateOne(final Object filter, final Object update, final SingleResultCallback callback) {
updateOne(filter, update, new UpdateOptions(), callback);
}
@Override
public void updateOne(final Object filter, final Object update, final UpdateOptions options,
final SingleResultCallback callback) {
update(filter, update, options, false, callback);
}
@Override
public void updateMany(final Object filter, final Object update, final SingleResultCallback callback) {
updateMany(filter, update, new UpdateOptions(), callback);
}
@Override
public void updateMany(final Object filter, final Object update, final UpdateOptions options,
final SingleResultCallback callback) {
update(filter, update, options, true, callback);
}
@Override
public void findOneAndDelete(final Object filter, final SingleResultCallback callback) {
findOneAndDelete(filter, new FindOneAndDeleteOptions(), callback);
}
@Override
public void findOneAndDelete(final Object filter, final FindOneAndDeleteOptions options, final SingleResultCallback callback) {
executor.execute(new FindAndDeleteOperation(namespace, getCodec())
.filter(asBson(filter))
.projection(asBson(options.getProjection()))
.sort(asBson(options.getSort())), callback);
}
@Override
public void findOneAndReplace(final Object filter, final T replacement, final SingleResultCallback callback) {
findOneAndReplace(filter, replacement, new FindOneAndReplaceOptions(), callback);
}
@Override
public void findOneAndReplace(final Object filter, final T replacement, final FindOneAndReplaceOptions options,
final SingleResultCallback callback) {
executor.execute(new FindAndReplaceOperation(namespace, getCodec(), asBson(replacement))
.filter(asBson(filter))
.projection(asBson(options.getProjection()))
.sort(asBson(options.getSort()))
.returnOriginal(options.getReturnOriginal())
.upsert(options.isUpsert()), callback);
}
@Override
public void findOneAndUpdate(final Object filter, final Object update, final SingleResultCallback callback) {
findOneAndUpdate(filter, update, new FindOneAndUpdateOptions(), callback);
}
@Override
public void findOneAndUpdate(final Object filter, final Object update, final FindOneAndUpdateOptions options,
final SingleResultCallback callback) {
executor.execute(new FindAndUpdateOperation(namespace, getCodec(), asBson(update))
.filter(asBson(filter))
.projection(asBson(options.getProjection()))
.sort(asBson(options.getSort()))
.returnOriginal(options.getReturnOriginal())
.upsert(options.isUpsert()), callback);
}
@Override
public void dropCollection(final SingleResultCallback callback) {
executor.execute(new DropCollectionOperation(namespace), callback);
}
@Override
public void createIndex(final Object key, final SingleResultCallback callback) {
createIndex(key, new CreateIndexOptions(), callback);
}
@Override
public void createIndex(final Object key, final CreateIndexOptions options, final SingleResultCallback callback) {
executor.execute(new CreateIndexOperation(getNamespace(), asBson(key))
.name(options.getName())
.background(options.isBackground())
.unique(options.isUnique())
.sparse(options.isSparse())
.expireAfterSeconds(options.getExpireAfterSeconds())
.version(options.getVersion())
.weights(asBson(options.getWeights()))
.defaultLanguage(options.getDefaultLanguage())
.languageOverride(options.getLanguageOverride())
.textIndexVersion(options.getTextIndexVersion())
.twoDSphereIndexVersion(options.getTwoDSphereIndexVersion())
.bits(options.getBits())
.min(options.getMin())
.max(options.getMax())
.bucketSize(options.getBucketSize()), callback);
}
@Override
public ListIndexesFluent listIndexes() {
return listIndexes(Document.class);
}
@Override
public ListIndexesFluent listIndexes(final Class clazz) {
return new ListIndexesFluentImpl(namespace, clazz, codecRegistry, readPreference, executor);
}
@Override
public void dropIndex(final String indexName, final SingleResultCallback callback) {
executor.execute(new DropIndexOperation(namespace, indexName), callback);
}
@Override
public void dropIndexes(final SingleResultCallback callback) {
dropIndex("*", callback);
}
@Override
public void renameCollection(final MongoNamespace newCollectionNamespace, final SingleResultCallback callback) {
renameCollection(newCollectionNamespace, new RenameCollectionOptions(), callback);
}
@Override
public void renameCollection(final MongoNamespace newCollectionNamespace, final RenameCollectionOptions options,
final SingleResultCallback callback) {
executor.execute(new RenameCollectionOperation(getNamespace(), newCollectionNamespace)
.dropTarget(options.isDropTarget()), callback);
}
private void delete(final Object filter, final boolean multi, final SingleResultCallback callback) {
executeSingleWriteRequest(new DeleteRequest(asBson(filter)).multi(multi), new SingleResultCallback() {
@Override
public void onResult(final BulkWriteResult result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
if (result.wasAcknowledged()) {
callback.onResult(DeleteResult.acknowledged(result.getDeletedCount()), null);
} else {
callback.onResult(DeleteResult.unacknowledged(), null);
}
}
}
});
}
private void update(final Object filter, final Object update, final UpdateOptions updateOptions, final boolean multi,
final SingleResultCallback callback) {
executeSingleWriteRequest(new UpdateRequest(asBson(filter), asBson(update), WriteRequest.Type.UPDATE)
.upsert(updateOptions.isUpsert()).multi(multi), new SingleResultCallback() {
@Override
public void onResult(final BulkWriteResult result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
callback.onResult(toUpdateResult(result), null);
}
}
});
}
private void executeSingleWriteRequest(final WriteRequest request, final SingleResultCallback callback) {
executor.execute(new MixedBulkWriteOperation(namespace, asList(request), true, writeConcern),
new SingleResultCallback() {
@Override
public void onResult(final BulkWriteResult result, final Throwable t) {
if (t instanceof MongoBulkWriteException) {
MongoBulkWriteException e = (MongoBulkWriteException) t;
if (e.getWriteErrors().isEmpty()) {
callback.onResult(null, new MongoWriteConcernException(e.getWriteConcernError(),
e.getServerAddress()));
} else {
callback.onResult(null, new MongoWriteException(new WriteError(e.getWriteErrors().get(0)),
e.getServerAddress()));
}
} else {
callback.onResult(result, t);
}
}
});
}
private UpdateResult toUpdateResult(final com.mongodb.bulk.BulkWriteResult result) {
if (result.wasAcknowledged()) {
Long modifiedCount = result.isModifiedCountAvailable() ? (long) result.getModifiedCount() : null;
BsonValue upsertedId = result.getUpserts().isEmpty() ? null : result.getUpserts().get(0).getId();
return UpdateResult.acknowledged(result.getMatchedCount(), modifiedCount, upsertedId);
} else {
return UpdateResult.unacknowledged();
}
}
private Codec getCodec() {
return getCodec(clazz);
}
private Codec getCodec(final Class clazz) {
return codecRegistry.get(clazz);
}
private BsonDocument asBson(final Object document) {
return BsonDocumentWrapper.asBsonDocument(document, codecRegistry);
}
private List createBsonDocumentList(final List pipeline) {
List aggregateList = new ArrayList(pipeline.size());
for (D obj : pipeline) {
aggregateList.add(asBson(obj));
}
return aggregateList;
}
}