Example usage for com.mongodb.async AsyncBatchCursor close

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

Introduction

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

Prototype

@Override
    void close();

Source Link

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/*  w  ww .j a  v  a 2  s  .com*/
        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);
}