org.apache.lucene.index

## Class ConcurrentMergeScheduler

- java.lang.Object
- - org.apache.lucene.index.MergeScheduler
  - - org.apache.lucene.index.ConcurrentMergeScheduler

- All Implemented Interfaces:
    Closeable, AutoCloseable

      public class ConcurrentMergeScheduler
      extends MergeScheduler

  A `MergeScheduler` that runs each merge using a separate thread.

  Specify the max number of threads that may run at once with `setMaxThreadCount(int)`.

  Separately specify the maximum number of simultaneous merges with `setMaxMergeCount(int)`. If the number of merges exceeds the max number of threads then the largest merges are paused until one of the smaller merges completes.

  If more than `getMaxMergeCount()` merges are requested then this class will forcefully throttle the incoming threads by pausing until one more more merges complete.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `protected class` | `ConcurrentMergeScheduler.MergeThread` Runs a merge thread, which may run one or more merges in sequence.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected static Comparator<ConcurrentMergeScheduler.MergeThread>` | `compareByMergeDocCount` Sorts `ConcurrentMergeScheduler.MergeThread`s; larger merges come first.
    `protected Directory` | `dir` `Directory` that holds the index.
    `protected int` | `mergeThreadCount` How many `ConcurrentMergeScheduler.MergeThread`s have kicked off (this is use to name them).
    `protected List<ConcurrentMergeScheduler.MergeThread>` | `mergeThreads` List of currently active `ConcurrentMergeScheduler.MergeThread`s.
    `protected IndexWriter` | `writer` `IndexWriter` that owns this instance.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ConcurrentMergeScheduler()` Sole constructor, with all settings set to default values.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()` Close this MergeScheduler.
    `protected void` | `doMerge(MergePolicy.OneMerge merge)` Does the actual merge, by calling `IndexWriter.merge(org.apache.lucene.index.MergePolicy.OneMerge)`
    `int` | `getMaxMergeCount()` See `setMaxMergeCount(int)`.
    `int` | `getMaxThreadCount()` Returns `maxThreadCount`.
    `protected ConcurrentMergeScheduler.MergeThread` | `getMergeThread(IndexWriter writer, MergePolicy.OneMerge merge)` Create and return a new MergeThread
    `int` | `getMergeThreadPriority()` Return the priority that merge threads run at.
    `protected void` | `handleMergeException(Throwable exc)` Called when an exception is hit in a background merge thread
    `void` | `merge(IndexWriter writer)` Run the merges provided by `IndexWriter.getNextMerge()`.
    `protected int` | `mergeThreadCount()` Returns the number of merge threads that are alive.
    `protected void` | `message(String message)` Outputs the given message - this method assumes `verbose()` was called and returned true.
    `void` | `setMaxMergeCount(int count)` Sets the max # simultaneous merges that are allowed.
    `void` | `setMaxThreadCount(int count)` Sets the max # simultaneous merge threads that should be running at once.
    `void` | `setMergeThreadPriority(int pri)` Set the base priority that merge threads run at.
    `void` | `sync()` Wait for any running merge threads to finish.
    `String` | `toString()`
    `protected void` | `updateMergeThreads()` Called whenever the running merges have changed, to pause & unpause threads.
    `protected boolean` | `verbose()` Returns true if verbosing is enabled.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### mergeThreads

          protected List<ConcurrentMergeScheduler.MergeThread> mergeThreads

      List of currently active `ConcurrentMergeScheduler.MergeThread`s.

    - #### dir

          protected Directory dir

      `Directory` that holds the index.

    - #### writer

          protected IndexWriter writer

      `IndexWriter` that owns this instance.

    - #### mergeThreadCount

          protected int mergeThreadCount

      How many `ConcurrentMergeScheduler.MergeThread`s have kicked off (this is use to name them).

    - #### compareByMergeDocCount

          protected static final Comparator<ConcurrentMergeScheduler.MergeThread> compareByMergeDocCount

      Sorts `ConcurrentMergeScheduler.MergeThread`s; larger merges come first.

  - ### Constructor Detail

    - #### ConcurrentMergeScheduler

          public ConcurrentMergeScheduler()

      Sole constructor, with all settings set to default values.

  - ### Method Detail

    - #### setMaxThreadCount

          public void setMaxThreadCount(int count)

      Sets the max # simultaneous merge threads that should be running at once. This must be <= `setMaxMergeCount(int)`.

    - #### getMaxThreadCount

          public int getMaxThreadCount()

      Returns `maxThreadCount`.
      See Also:
        `setMaxThreadCount(int)`

    - #### setMaxMergeCount

          public void setMaxMergeCount(int count)

      Sets the max # simultaneous merges that are allowed. If a merge is necessary yet we already have this many threads running, the incoming thread (that is calling add/updateDocument) will block until a merge thread has completed. Note that we will only run the smallest `setMaxThreadCount(int)` merges at a time.

    - #### getMaxMergeCount

          public int getMaxMergeCount()

      See `setMaxMergeCount(int)`.

    - #### getMergeThreadPriority

          public int getMergeThreadPriority()

      Return the priority that merge threads run at. By default the priority is 1 plus the priority of (ie, slightly higher priority than) the first thread that calls merge.

    - #### setMergeThreadPriority

          public void setMergeThreadPriority(int pri)

      Set the base priority that merge threads run at. Note that CMS may increase priority of some merge threads beyond this base priority. It's best not to set this any higher than Thread.MAX_PRIORITY-maxThreadCount, so that CMS has room to set relative priority among threads.

    - #### updateMergeThreads

          protected void updateMergeThreads()

      Called whenever the running merges have changed, to pause & unpause threads. This method sorts the merge threads by their merge size in descending order and then pauses/unpauses threads from first to last -- that way, smaller merges are guaranteed to run before larger ones.

    - #### verbose

          protected boolean verbose()

      Returns true if verbosing is enabled. This method is usually used in conjunction with `message(String)`, like that:

           if (verbose()) {
             message("your message");
           }

    - #### message

          protected void message(String message)

      Outputs the given message - this method assumes `verbose()` was called and returned true.

    - #### close

          public void close()

      Description copied from class: `MergeScheduler`
      Close this MergeScheduler.
      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Specified by:
        `close` in class `MergeScheduler`

    - #### sync

          public void sync()

      Wait for any running merge threads to finish. This call is not interruptible as used by `close()`.

    - #### mergeThreadCount

          protected int mergeThreadCount()

      Returns the number of merge threads that are alive. Note that this number is ≤ `mergeThreads` size.

    - #### merge

          public void merge(IndexWriter writer)
                     throws IOException

      Description copied from class: `MergeScheduler`
      Run the merges provided by `IndexWriter.getNextMerge()`.
      Specified by:
        `merge` in class `MergeScheduler`
      Throws:
        `IOException`

    - #### doMerge

          protected void doMerge(MergePolicy.OneMerge merge)
                          throws IOException

      Does the actual merge, by calling `IndexWriter.merge(org.apache.lucene.index.MergePolicy.OneMerge)`
      Throws:
        `IOException`

    - #### getMergeThread

          protected ConcurrentMergeScheduler.MergeThread getMergeThread(IndexWriter writer,
                                                            MergePolicy.OneMerge merge)
                                                                 throws IOException

      Create and return a new MergeThread
      Throws:
        `IOException`

    - #### handleMergeException

          protected void handleMergeException(Throwable exc)

      Called when an exception is hit in a background merge thread

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

Copyright © 2000-2013 Apache Software Foundation. All Rights Reserved.