use of com.mongodb.async.SingleResultCallback in project mongo-java-driver by mongodb.
the class MongoClientImpl method createOperationExecutor.
private static AsyncOperationExecutor createOperationExecutor(final MongoClientSettings settings, final Cluster cluster) {
return new AsyncOperationExecutor() {
@Override
public <T> void execute(final AsyncReadOperation<T> operation, final ReadPreference readPreference, final SingleResultCallback<T> callback) {
notNull("operation", operation);
notNull("readPreference", readPreference);
notNull("callback", callback);
final SingleResultCallback<T> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
final AsyncReadBinding binding = getReadWriteBinding(readPreference, cluster);
operation.executeAsync(binding, new SingleResultCallback<T>() {
@Override
public void onResult(final T result, final Throwable t) {
try {
errHandlingCallback.onResult(result, t);
} finally {
binding.release();
}
}
});
}
@Override
public <T> void execute(final AsyncWriteOperation<T> operation, final SingleResultCallback<T> callback) {
notNull("operation", operation);
notNull("callback", callback);
final AsyncWriteBinding binding = getReadWriteBinding(ReadPreference.primary(), cluster);
operation.executeAsync(binding, new SingleResultCallback<T>() {
@Override
public void onResult(final T result, final Throwable t) {
try {
errorHandlingCallback(callback, LOGGER).onResult(result, t);
} finally {
binding.release();
}
}
});
}
};
}
use of com.mongodb.async.SingleResultCallback in project mongo-java-driver by mongodb.
the class MongoCollectionImpl method insertMany.
@Override
public void insertMany(final List<? extends TDocument> documents, final InsertManyOptions options, final SingleResultCallback<Void> callback) {
notNull("documents", documents);
List<InsertRequest> requests = new ArrayList<InsertRequest>(documents.size());
for (TDocument document : documents) {
if (document == null) {
throw new IllegalArgumentException("documents can not contain a null value");
}
if (getCodec() instanceof CollectibleCodec) {
document = ((CollectibleCodec<TDocument>) getCodec()).generateIdIfAbsentFromDocument(document);
}
requests.add(new InsertRequest(documentToBsonDocument(document)));
}
executor.execute(new MixedBulkWriteOperation(namespace, requests, options.isOrdered(), writeConcern).bypassDocumentValidation(options.getBypassDocumentValidation()), errorHandlingCallback(new SingleResultCallback<BulkWriteResult>() {
@Override
public void onResult(final BulkWriteResult result, final Throwable t) {
callback.onResult(null, t);
}
}, LOGGER));
}
use of com.mongodb.async.SingleResultCallback in project mongo-java-driver by mongodb.
the class GridFSBucketImpl method readAndWriteInputStream.
private void readAndWriteInputStream(final AsyncInputStream source, final GridFSUploadStream uploadStream, final ByteBuffer buffer, final SingleResultCallback<Void> callback) {
buffer.clear();
source.read(buffer, new SingleResultCallback<Integer>() {
@Override
public void onResult(final Integer result, final Throwable t) {
if (t != null) {
if (t instanceof IOException) {
uploadStream.abort(new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable abortException) {
if (abortException != null) {
callback.onResult(null, abortException);
} else {
callback.onResult(null, new MongoGridFSException("IOException when reading from the InputStream", t));
}
}
});
} else {
callback.onResult(null, t);
}
} else if (result > 0) {
buffer.flip();
uploadStream.write(buffer, new SingleResultCallback<Integer>() {
@Override
public void onResult(final Integer result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
readAndWriteInputStream(source, uploadStream, buffer, callback);
}
}
});
} else {
uploadStream.close(callback);
}
}
});
}
use of com.mongodb.async.SingleResultCallback in project mongo-java-driver by mongodb.
the class GridFSUploadStreamImpl method close.
@Override
public void close(final SingleResultCallback<Void> callback) {
notNull("callback", callback);
final SingleResultCallback<Void> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
boolean alreadyClosed = false;
synchronized (closeAndWritingLock) {
alreadyClosed = closed;
closed = true;
}
if (alreadyClosed) {
errHandlingCallback.onResult(null, null);
return;
} else if (!getAndSetWritingLock()) {
callbackIsWritingException(errHandlingCallback);
return;
}
writeChunk(new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable t) {
if (t != null) {
releaseWritingLock();
errHandlingCallback.onResult(null, t);
} else {
GridFSFile gridFSFile = new GridFSFile(fileId, filename, lengthInBytes, chunkSizeBytes, new Date(), toHex(md5.digest()), metadata);
filesCollection.insertOne(gridFSFile, new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable t) {
buffer = null;
releaseWritingLock();
errHandlingCallback.onResult(result, t);
}
});
}
}
});
}
use of com.mongodb.async.SingleResultCallback in project mongo-java-driver by mongodb.
the class GridFSUploadStreamImpl method writeChunk.
private void writeChunk(final SingleResultCallback<Void> callback) {
if (md5 == null) {
callback.onResult(null, new MongoGridFSException("No MD5 message digest available, cannot upload file"));
} else if (bufferOffset > 0) {
chunksCollection.insertOne(new Document("files_id", fileId).append("n", chunkIndex).append("data", getData()), new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
md5.update(buffer);
chunkIndex++;
bufferOffset = 0;
callback.onResult(null, null);
}
}
});
} else {
callback.onResult(null, null);
}
}
Aggregations