Search in sources :

Example 1 with SingleResultCallback

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();
                    }
                }
            });
        }
    };
}
Also used : ReadPreference(com.mongodb.ReadPreference) AsyncOperationExecutor(com.mongodb.operation.AsyncOperationExecutor) AsyncReadBinding(com.mongodb.binding.AsyncReadBinding) SingleResultCallback(com.mongodb.async.SingleResultCallback) AsyncWriteBinding(com.mongodb.binding.AsyncWriteBinding) AsyncWriteOperation(com.mongodb.operation.AsyncWriteOperation) AsyncReadOperation(com.mongodb.operation.AsyncReadOperation)

Example 2 with SingleResultCallback

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));
}
Also used : CollectibleCodec(org.bson.codecs.CollectibleCodec) InsertRequest(com.mongodb.bulk.InsertRequest) ArrayList(java.util.ArrayList) SingleResultCallback(com.mongodb.async.SingleResultCallback) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) MixedBulkWriteOperation(com.mongodb.operation.MixedBulkWriteOperation)

Example 3 with SingleResultCallback

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);
            }
        }
    });
}
Also used : MongoGridFSException(com.mongodb.MongoGridFSException) SingleResultCallback(com.mongodb.async.SingleResultCallback) IOException(java.io.IOException)

Example 4 with SingleResultCallback

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);
                    }
                });
            }
        }
    });
}
Also used : GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) SingleResultCallback(com.mongodb.async.SingleResultCallback) Date(java.util.Date)

Example 5 with SingleResultCallback

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);
    }
}
Also used : MongoGridFSException(com.mongodb.MongoGridFSException) SingleResultCallback(com.mongodb.async.SingleResultCallback) Document(org.bson.Document)

Aggregations

SingleResultCallback (com.mongodb.async.SingleResultCallback)10 MongoGridFSException (com.mongodb.MongoGridFSException)3 BsonDocument (org.bson.BsonDocument)2 MongoCommandException (com.mongodb.MongoCommandException)1 MongoQueryException (com.mongodb.MongoQueryException)1 ReadPreference (com.mongodb.ReadPreference)1 AsyncReadBinding (com.mongodb.binding.AsyncReadBinding)1 AsyncWriteBinding (com.mongodb.binding.AsyncWriteBinding)1 BulkWriteResult (com.mongodb.bulk.BulkWriteResult)1 InsertRequest (com.mongodb.bulk.InsertRequest)1 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)1 DeleteResult (com.mongodb.client.result.DeleteResult)1 AsyncConnection (com.mongodb.connection.AsyncConnection)1 BulkWriteBatchCombiner (com.mongodb.connection.BulkWriteBatchCombiner)1 ConnectionOpenedEvent (com.mongodb.event.ConnectionOpenedEvent)1 AsyncOperationExecutor (com.mongodb.operation.AsyncOperationExecutor)1 AsyncReadOperation (com.mongodb.operation.AsyncReadOperation)1 AsyncWriteOperation (com.mongodb.operation.AsyncWriteOperation)1 MixedBulkWriteOperation (com.mongodb.operation.MixedBulkWriteOperation)1 AsyncCallableWithConnection (com.mongodb.operation.OperationHelper.AsyncCallableWithConnection)1