Example usage for com.mongodb.async AsyncBatchCursor next

List of usage examples for com.mongodb.async AsyncBatchCursor next

Introduction

In this page you can find the example usage for com.mongodb.async AsyncBatchCursor next.

Prototype

void next(SingleResultCallback<List<T>> callback);

Source Link

Document

Returns the next batch of results.

Usage

From source file:com.mastfrog.asyncpromises.mongo.CollectionPromises.java

License:Open Source License

AsyncPromise<Bson, List<T>> find(final FindBuilderImpl<T, ?> builder, final FindReceiver<List<T>> withResults) {
    AsyncPromise<Bson, AsyncBatchCursor<T>> m = AsyncPromise.create(new Logic<Bson, AsyncBatchCursor<T>>() {
        @Override//from   ww w  .  j  av  a  2  s  . co m
        public void run(Bson data, Trigger<AsyncBatchCursor<T>> next, PromiseContext context) throws Exception {
            builder.apply(collection.find(data)).batchCursor(new SRC<>(next));
        }
    });
    final ContinueTrigger cont = new ContinueTrigger();
    AsyncPromise<AsyncBatchCursor<T>, List<T>> cursorPromise = AsyncPromise
            .create(new Logic<AsyncBatchCursor<T>, List<T>>() {
                @Override
                public void run(final AsyncBatchCursor<T> cursor, final Trigger<List<T>> next,
                        final PromiseContext context) throws Exception {
                    cursor.next(new SRC<>(new Trigger<List<T>>() {
                        @Override
                        public void trigger(List<T> obj, Throwable thrown) {
                            if (thrown != null) {
                                cursor.close();
                                next.trigger(obj, thrown);
                                return;
                            }
                            if (obj != null) {
                                try {
                                    withResults.withResults(obj, cont, context);
                                } catch (Exception ex) {
                                    next.trigger(obj, ex);
                                    return;
                                }
                            }
                            if (cont.get()) {
                                cursor.next(new SRC<List<T>>(this));
                            } else {
                                cursor.close();
                                next.trigger(obj, thrown);
                            }
                        }
                    }));
                }
            });
    return m.then(cursorPromise);
}