Example usage for java.io IOException initCause

List of usage examples for java.io IOException initCause

Introduction

In this page you can find the example usage for java.io IOException initCause.

Prototype

public synchronized Throwable initCause(Throwable cause) 

Source Link

Document

Initializes the cause of this throwable to the specified value.

Usage

From source file:org.apache.lucene.index.IndexWriter.java

/** Just like {@link #forceMerge(int)}, except you can
 *  specify whether the call should block until
 *  all merging completes.  This is only meaningful with a
 *  {@link MergeScheduler} that is able to run merges in
 *  background threads.//w w  w.jav a 2  s. c  o  m
 *
 *  <p><b>NOTE</b>: if this method hits an OutOfMemoryError
 *  you should immediately close the writer.  See <a
 *  href="#OOME">above</a> for details.</p>
 */
public void forceMerge(int maxNumSegments, boolean doWait) throws IOException {
    ensureOpen();

    if (maxNumSegments < 1)
        throw new IllegalArgumentException("maxNumSegments must be >= 1; got " + maxNumSegments);

    if (infoStream.isEnabled("IW")) {
        infoStream.message("IW", "forceMerge: index now " + segString());
        infoStream.message("IW", "now flush at forceMerge");
    }

    flush(true, true);

    synchronized (this) {
        resetMergeExceptions();
        segmentsToMerge.clear();
        for (SegmentCommitInfo info : segmentInfos) {
            segmentsToMerge.put(info, Boolean.TRUE);
        }
        mergeMaxNumSegments = maxNumSegments;

        // Now mark all pending & running merges for forced
        // merge:
        for (final MergePolicy.OneMerge merge : pendingMerges) {
            merge.maxNumSegments = maxNumSegments;
            segmentsToMerge.put(merge.info, Boolean.TRUE);
        }

        for (final MergePolicy.OneMerge merge : runningMerges) {
            merge.maxNumSegments = maxNumSegments;
            segmentsToMerge.put(merge.info, Boolean.TRUE);
        }
    }

    maybeMerge(MergeTrigger.EXPLICIT, maxNumSegments);

    if (doWait) {
        synchronized (this) {
            while (true) {

                if (hitOOM) {
                    throw new IllegalStateException(
                            "this writer hit an OutOfMemoryError; cannot complete forceMerge");
                }

                if (mergeExceptions.size() > 0) {
                    // Forward any exceptions in background merge
                    // threads to the current thread:
                    final int size = mergeExceptions.size();
                    for (int i = 0; i < size; i++) {
                        final MergePolicy.OneMerge merge = mergeExceptions.get(i);
                        if (merge.maxNumSegments != -1) {
                            IOException err = new IOException(
                                    "background merge hit exception: " + merge.segString(directory));
                            final Throwable t = merge.getException();
                            if (t != null)
                                err.initCause(t);
                            throw err;
                        }
                    }
                }

                if (maxNumSegmentsMergesPending())
                    doWait();
                else
                    break;
            }
        }

        // If close is called while we are still
        // running, throw an exception so the calling
        // thread will know merging did not
        // complete
        ensureOpen();
    }
    // NOTE: in the ConcurrentMergeScheduler case, when
    // doWait is false, we can return immediately while
    // background threads accomplish the merging
}