Class SnapshotDeletionPolicy
- java.lang.Object
-
- org.apache.lucene.index.IndexDeletionPolicy
-
- org.apache.lucene.index.SnapshotDeletionPolicy
-
- All Implemented Interfaces:
Cloneable
- Direct Known Subclasses:
PersistentSnapshotDeletionPolicy
public class SnapshotDeletionPolicy extends IndexDeletionPolicy
AnIndexDeletionPolicy
that wraps any otherIndexDeletionPolicy
and adds the ability to hold and later release snapshots of an index. While a snapshot is held, theIndexWriter
will not remove any files associated with it even if the index is otherwise being actively, arbitrarily changed. Because we wrap another arbitraryIndexDeletionPolicy
, this gives you the freedom to continue using whateverIndexDeletionPolicy
you would normally want to use with your index.This class maintains all snapshots in-memory, and so the information is not persisted and not protected against system failures. If persistence is important, you can use
PersistentSnapshotDeletionPolicy
.
-
-
Field Summary
Fields Modifier and Type Field Description protected Map<Long,IndexCommit>
indexCommits
Used to map gen to IndexCommit.protected IndexCommit
lastCommit
Most recently committedIndexCommit
.protected Map<Long,Integer>
refCounts
Records how many snapshots are held against each commit generation
-
Constructor Summary
Constructors Constructor Description SnapshotDeletionPolicy(IndexDeletionPolicy primary)
Sole constructor, taking the incomingIndexDeletionPolicy
to wrap.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description IndexDeletionPolicy
clone()
IndexCommit
getIndexCommit(long gen)
Retrieve anIndexCommit
from its generation; returns null if this IndexCommit is not currently snapshottedint
getSnapshotCount()
Returns the total number of snapshots currently held.List<IndexCommit>
getSnapshots()
Returns all IndexCommits held by at least one snapshot.protected void
incRef(IndexCommit ic)
Increments the refCount for thisIndexCommit
.void
onCommit(List<? extends IndexCommit> commits)
This is called each time the writer completed a commit.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.void
release(IndexCommit commit)
Release a snapshotted commit.protected void
releaseGen(long gen)
Release a snapshot by generation.IndexCommit
snapshot()
Snapshots the last commit and returns it.
-
-
-
Field Detail
-
refCounts
protected Map<Long,Integer> refCounts
Records how many snapshots are held against each commit generation
-
indexCommits
protected Map<Long,IndexCommit> indexCommits
Used to map gen to IndexCommit.
-
lastCommit
protected IndexCommit lastCommit
Most recently committedIndexCommit
.
-
-
Constructor Detail
-
SnapshotDeletionPolicy
public SnapshotDeletionPolicy(IndexDeletionPolicy primary)
Sole constructor, taking the incomingIndexDeletionPolicy
to wrap.
-
-
Method Detail
-
onCommit
public void onCommit(List<? extends IndexCommit> commits) throws IOException
Description copied from class:IndexDeletionPolicy
This is called each time the writer completed a commit. This gives the policy a chance to remove old commit points with each commit.
The policy may now choose to delete old commit points by calling method
delete()
ofIndexCommit
.This method is only called when
IndexWriter.commit()
orIndexWriter.close()
is called, or possibly not at all if theIndexWriter.rollback()
is called.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:
onCommit
in classIndexDeletionPolicy
- Parameters:
commits
- List ofIndexCommit
, sorted by age (the 0th one is the oldest commit).- Throws:
IOException
-
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()
ofIndexCommit
.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 classIndexDeletionPolicy
- Parameters:
commits
- List of currentpoint-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
-
release
public void release(IndexCommit commit) throws IOException
Release a snapshotted commit.- Parameters:
commit
- the commit previously returned bysnapshot()
- Throws:
IOException
-
releaseGen
protected void releaseGen(long gen) throws IOException
Release a snapshot by generation.- Throws:
IOException
-
incRef
protected void incRef(IndexCommit ic)
Increments the refCount for thisIndexCommit
.
-
snapshot
public IndexCommit snapshot() throws IOException
Snapshots the last commit and returns it. Once a commit is 'snapshotted,' it is protected from deletion (as long as thisIndexDeletionPolicy
is used). The snapshot can be removed by callingrelease(IndexCommit)
followed by a call toIndexWriter.deleteUnusedFiles()
.NOTE: while the snapshot is held, the files it references will not be deleted, which will consume additional disk space in your index. If you take a snapshot at a particularly bad time (say just before you call forceMerge) then in the worst case this could consume an extra 1X of your total index size, until you release the snapshot.
- Returns:
- the
IndexCommit
that was snapshotted. - Throws:
IllegalStateException
- if this index does not have any commits yetIOException
-
getSnapshots
public List<IndexCommit> getSnapshots()
Returns all IndexCommits held by at least one snapshot.
-
getSnapshotCount
public int getSnapshotCount()
Returns the total number of snapshots currently held.
-
getIndexCommit
public IndexCommit getIndexCommit(long gen)
Retrieve anIndexCommit
from its generation; returns null if this IndexCommit is not currently snapshotted
-
clone
public IndexDeletionPolicy clone()
- Overrides:
clone
in classIndexDeletionPolicy
-
-