Package org.apache.lucene.index

# Class KeepLastNCommitsDeletionPolicy

java.lang.Object
org.apache.lucene.index.IndexDeletionPolicy
org.apache.lucene.index.KeepLastNCommitsDeletionPolicy

public class KeepLastNCommitsDeletionPolicy extends IndexDeletionPolicy
This `IndexDeletionPolicy` implementation keeps the last N commits and removes all prior commits after a new commit is done. This policy can be useful for maintaining a history of recent commits while still managing index size.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `KeepLastNCommitsDeletionPolicy(int numCommitsToKeep)`
  Constructor that sets the number of commits to keep.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `onCommit(List<? extends IndexCommit> commits)`
  Deletes all but the last N commits.
  `void`
  `onInit(List<? extends IndexCommit> commits)`
  This is called once when a writer is first instantiated to give the policy a chance to remove old commit points.

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

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

- ## Constructor Details

  - ### KeepLastNCommitsDeletionPolicy

    public KeepLastNCommitsDeletionPolicy(int numCommitsToKeep)
    Constructor that sets the number of commits to keep.
    Parameters:
      `numCommitsToKeep` - the number of most recent commits to retain
    Throws:
      `IllegalArgumentException` - if numCommitsToKeep is not positive

- ## Method Details

  - ### onInit

    public void onInit(List<? extends IndexCommit> commits) throws IOException
    Description copied from class: `IndexDeletionPolicy`
    This is called once when a writer is first instantiated to give the policy a chance to remove old commit points.

    The writer locates all index commits present in the index directory and calls this method. The policy may choose to delete some of the commit points, doing so by calling method `delete()` of `IndexCommit`.

    Note: the last CommitPoint is the most recent one, i.e. the "front index state". Be careful not to delete it, unless you know for sure what you are doing, and unless you can afford to lose the index content while doing that.

    Specified by:
      `onInit` in class `IndexDeletionPolicy`
    Parameters:
      `commits` - List of current `point-in-time commits`, sorted by age (the 0th one is the oldest commit). Note that for a new index this method is invoked with an empty list.
    Throws:
      `IOException`

  - ### onCommit

    public void onCommit(List<? extends IndexCommit> commits) throws IOException
    Deletes all but the last N commits.
    Specified by:
      `onCommit` in class `IndexDeletionPolicy`
    Parameters:
      `commits` - List of `IndexCommit`, sorted by age (the 0th one is the oldest commit).
    Throws:
      `IOException`