Example usage for io.netty.channel ChannelProgressivePromise cause

List of usage examples for io.netty.channel ChannelProgressivePromise cause

Introduction

In this page you can find the example usage for io.netty.channel ChannelProgressivePromise cause.

Prototype

Throwable cause();

Source Link

Document

Returns the cause of the failed I/O operation if the I/O operation has failed.

Usage

From source file:com.addthis.hydra.data.query.engine.QueryEngine.java

License:Apache License

/**
 * see above./*www .  j  a va  2  s.c o  m*/
 */
private void tableSearch(LinkedList<DataTreeNode> stack, FieldValueList prefix, QueryElement[] path,
        int pathIndex, DataChannelOutput sink, int collect, ChannelProgressivePromise queryPromise)
        throws QueryException {
    if (queryPromise.isDone()) {
        log.debug("Query promise completed during processing");
        if (queryPromise.isCancelled()) {
            throw (CancellationException) queryPromise.cause();
        }
        throw new QueryException("Query closed during processing");
    }

    DataTreeNode root = stack != null ? stack.peek() : null;
    if (log.isDebugEnabled()) {
        log.debug("root={} pre={} path={} idx={} res={} coll={}", root, prefix, Arrays.toString(path),
                pathIndex, sink, collect);
    }

    if (Thread.currentThread().isInterrupted()) {
        QueryException exception = new QueryException("query interrupted");
        log.warn("Query closed due to thread interruption:\n", exception);
        throw exception;
    }
    if (pathIndex >= path.length) {
        log.debug("pathIndex>path.length, return root={}", root);
        if (!queryPromise.isDone()) {
            sink.send(prefix.createBundle(sink));
        }
        return;
    }
    QueryElement next = path[pathIndex];
    Iterator<DataTreeNode> iter = root != null ? next.matchNodes(tree, stack)
            : next.emptyok() ? Iterators.<DataTreeNode>emptyIterator() : null;
    if (iter == null) {
        return;
    }
    try {
        int skip = next.skip();
        int limit = next.limit();
        if (next.flatten()) {
            int count = 0;
            while (iter.hasNext() && (next.limit() == 0 || limit > 0)) {
                // Check for interruptions or cancellations
                if (Thread.currentThread().isInterrupted()) {
                    QueryException exception = new QueryException("query interrupted");
                    log.warn("Query closed due to thread interruption:\n", exception);
                    throw exception;
                }
                if (queryPromise.isDone()) {
                    if (iter instanceof ClosableIterator) {
                        ((ClosableIterator<DataTreeNode>) iter).close();
                    }

                    log.debug("Query promise completed during processing. root={}", root);
                    if (queryPromise.isCancelled()) {
                        throw (CancellationException) queryPromise.cause();
                    }
                    throw new QueryException("Query closed during processing, root=" + root);
                }

                DataTreeNode tn = iter.next();
                if (tn == null && !next.emptyok()) {
                    break;
                }
                if (next.hasData()) {
                    if (skip > 0) {
                        skip--;
                        continue;
                    }
                    count += next.update(prefix, tn);
                    limit--;
                }
            }
            if (!queryPromise.isDone()) {
                tableSearch(null, prefix, path, pathIndex + 1, sink, collect + count, queryPromise);
            }
            prefix.pop(count);
            return;
        }
        while (iter.hasNext() && (next.limit() == 0 || limit > 0)) {
            // Check for interruptions or cancellations
            if (Thread.currentThread().isInterrupted()) {
                QueryException exception = new QueryException("query interrupted");
                log.warn("Query closed due to thread interruption", exception);
                throw exception;
            }
            if (queryPromise.isDone()) {
                break;
            }
            if (queryPromise.isDone()) {
                if (iter instanceof ClosableIterator) {
                    ((ClosableIterator<DataTreeNode>) iter).close();
                }

                log.debug("Query promise completed during processing. root={}", root);
                if (queryPromise.isCancelled()) {
                    throw (CancellationException) queryPromise.cause();
                }
                throw new QueryException("Query closed during processing, root=" + root);
            }

            DataTreeNode tn = iter.next();
            if (next.hasData()) {
                if (tn == null && !next.emptyok()) {
                    return;
                }
                if (skip > 0) {
                    skip--;
                    continue;
                }
                int count = next.update(prefix, tn);
                if (count > 0) {
                    if (!queryPromise.isDone()) {
                        tableSearch(stack, tn, prefix, path, pathIndex + 1, sink, collect + count,
                                queryPromise);
                    }
                    prefix.pop(count);
                    limit--;
                }
            } else {
                if (skip > 0) {
                    skip--;
                    continue;
                }
                if (!queryPromise.isDone()) {
                    tableSearch(stack, tn, prefix, path, pathIndex + 1, sink, collect, queryPromise);
                }
                limit--;
            }
        }
    } finally {
        if (log.isDebugEnabled()) {
            log.debug("CLOSING: root={} pre={} path={} idx={} res={} coll={}", root, prefix,
                    Arrays.toString(path), pathIndex, sink, collect);
        }

        if (iter instanceof ClosableIterator) {
            ((ClosableIterator<DataTreeNode>) iter).close();
        }
    }
}