Search in sources :

Example 6 with SingleResultCallback

use of com.mongodb.async.SingleResultCallback in project mongo-java-driver by mongodb.

the class InternalStreamConnectionInitializer method initializeConnectionDescriptionAsync.

private void initializeConnectionDescriptionAsync(final InternalConnection internalConnection, final SingleResultCallback<ConnectionDescription> callback) {
    executeCommandAsync("admin", createIsMasterCommand(), internalConnection, new SingleResultCallback<BsonDocument>() {

        @Override
        public void onResult(final BsonDocument isMasterResult, final Throwable t) {
            if (t != null) {
                callback.onResult(null, t);
            } else {
                executeCommandAsync("admin", new BsonDocument("buildinfo", new BsonInt32(1)), internalConnection, new SingleResultCallback<BsonDocument>() {

                    @Override
                    public void onResult(final BsonDocument buildInfoResult, final Throwable t) {
                        if (t != null) {
                            callback.onResult(null, t);
                        } else {
                            ConnectionId connectionId = internalConnection.getDescription().getConnectionId();
                            callback.onResult(createConnectionDescription(connectionId, isMasterResult, buildInfoResult), null);
                        }
                    }
                });
            }
        }
    });
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) SingleResultCallback(com.mongodb.async.SingleResultCallback)

Example 7 with SingleResultCallback

use of com.mongodb.async.SingleResultCallback in project mongo-java-driver by mongodb.

the class FindOperation method exceptionTransformingCallback.

private static <T> SingleResultCallback<T> exceptionTransformingCallback(final SingleResultCallback<T> callback) {
    return new SingleResultCallback<T>() {

        @Override
        public void onResult(final T result, final Throwable t) {
            if (t != null) {
                if (t instanceof MongoCommandException) {
                    MongoCommandException commandException = (MongoCommandException) t;
                    callback.onResult(result, new MongoQueryException(commandException.getServerAddress(), commandException.getErrorCode(), commandException.getErrorMessage()));
                } else {
                    callback.onResult(result, t);
                }
            } else {
                callback.onResult(result, null);
            }
        }
    };
}
Also used : MongoCommandException(com.mongodb.MongoCommandException) SingleResultCallback(com.mongodb.async.SingleResultCallback) MongoQueryException(com.mongodb.MongoQueryException)

Example 8 with SingleResultCallback

use of com.mongodb.async.SingleResultCallback in project mongo-java-driver by mongodb.

the class GridFSBucketImpl method delete.

@Override
public void delete(final BsonValue id, final SingleResultCallback<Void> callback) {
    notNull("id", id);
    notNull("callback", callback);
    final SingleResultCallback<Void> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
    filesCollection.deleteOne(new BsonDocument("_id", id), new SingleResultCallback<DeleteResult>() {

        @Override
        public void onResult(final DeleteResult filesResult, final Throwable t) {
            if (t != null) {
                errHandlingCallback.onResult(null, t);
            } else {
                chunksCollection.deleteMany(new BsonDocument("files_id", id), new SingleResultCallback<DeleteResult>() {

                    @Override
                    public void onResult(final DeleteResult chunksResult, final Throwable t) {
                        if (t != null) {
                            errHandlingCallback.onResult(null, t);
                        } else if (filesResult.wasAcknowledged() && filesResult.getDeletedCount() == 0) {
                            errHandlingCallback.onResult(null, new MongoGridFSException(format("No file found with the ObjectId: %s", id)));
                        } else {
                            errHandlingCallback.onResult(null, null);
                        }
                    }
                });
            }
        }
    });
}
Also used : BsonDocument(org.bson.BsonDocument) MongoGridFSException(com.mongodb.MongoGridFSException) SingleResultCallback(com.mongodb.async.SingleResultCallback) DeleteResult(com.mongodb.client.result.DeleteResult)

Example 9 with SingleResultCallback

use of com.mongodb.async.SingleResultCallback in project mongo-java-driver by mongodb.

the class InternalStreamConnection method openAsync.

@Override
public void openAsync(final SingleResultCallback<Void> callback) {
    isTrue("Open already called", stream == null, callback);
    try {
        stream = streamFactory.create(serverId.getAddress());
    } catch (Throwable t) {
        callback.onResult(null, t);
        return;
    }
    stream.openAsync(new AsyncCompletionHandler<Void>() {

        @Override
        public void completed(final Void aVoid) {
            connectionInitializer.initializeAsync(InternalStreamConnection.this, new SingleResultCallback<ConnectionDescription>() {

                @Override
                public void onResult(final ConnectionDescription result, final Throwable t) {
                    if (t != null) {
                        close();
                        callback.onResult(null, t);
                    } else {
                        description = result;
                        opened.set(true);
                        connectionListener.connectionOpened(new ConnectionOpenedEvent(getId()));
                        if (LOGGER.isInfoEnabled()) {
                            LOGGER.info(format("Opened connection [%s] to %s", getId(), serverId.getAddress()));
                        }
                        callback.onResult(null, null);
                    }
                }
            });
        }

        @Override
        public void failed(final Throwable t) {
            callback.onResult(null, t);
        }
    });
}
Also used : ConnectionOpenedEvent(com.mongodb.event.ConnectionOpenedEvent) SingleResultCallback(com.mongodb.async.SingleResultCallback)

Example 10 with SingleResultCallback

use of com.mongodb.async.SingleResultCallback in project mongo-java-driver by mongodb.

the class MixedBulkWriteOperation method executeAsync.

@Override
public void executeAsync(final AsyncWriteBinding binding, final SingleResultCallback<BulkWriteResult> callback) {
    withConnection(binding, new AsyncCallableWithConnection() {

        @Override
        public void call(final AsyncConnection connection, final Throwable t) {
            final SingleResultCallback<BulkWriteResult> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
            if (t != null) {
                errHandlingCallback.onResult(null, t);
            } else {
                validateWriteRequests(connection, bypassDocumentValidation, writeRequests, writeConcern, new AsyncCallableWithConnection() {

                    @Override
                    public void call(final AsyncConnection connection, final Throwable t) {
                        if (t != null) {
                            releasingCallback(errHandlingCallback, connection).onResult(null, t);
                        } else {
                            Iterator<Run> runs = getRunGenerator(connection.getDescription()).iterator();
                            executeRunsAsync(runs, connection, new BulkWriteBatchCombiner(connection.getDescription().getServerAddress(), ordered, writeConcern), errHandlingCallback);
                        }
                    }
                });
            }
        }
    });
}
Also used : AsyncConnection(com.mongodb.connection.AsyncConnection) SingleResultCallback(com.mongodb.async.SingleResultCallback) AsyncCallableWithConnection(com.mongodb.operation.OperationHelper.AsyncCallableWithConnection) BulkWriteBatchCombiner(com.mongodb.connection.BulkWriteBatchCombiner)

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