public class SnapshotDeletionPolicy extends IndexDeletionPolicy
IndexDeletionPolicy
that wraps any other
IndexDeletionPolicy
and adds the ability to hold and later release
snapshots of an index. While a snapshot is held, the IndexWriter
will
not remove any files associated with it even if the index is otherwise being
actively, arbitrarily changed. Because we wrap another arbitrary
IndexDeletionPolicy
, this gives you the freedom to continue using
whatever IndexDeletionPolicy
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
.
Modifier and Type | Class and Description |
---|---|
private class |
SnapshotDeletionPolicy.SnapshotCommitPoint
Wraps a provided
IndexCommit and prevents it
from being deleted. |
Modifier and Type | Field and Description |
---|---|
protected java.util.Map<java.lang.Long,IndexCommit> |
indexCommits
Used to map gen to IndexCommit.
|
private boolean |
initCalled
Used to detect misuse
|
protected IndexCommit |
lastCommit
Most recently committed
IndexCommit . |
private IndexDeletionPolicy |
primary
Wrapped
IndexDeletionPolicy |
protected java.util.Map<java.lang.Long,java.lang.Integer> |
refCounts
Records how many snapshots are held against each
commit generation
|
Constructor and Description |
---|
SnapshotDeletionPolicy(IndexDeletionPolicy primary)
Sole constructor, taking the incoming
IndexDeletionPolicy to wrap. |
Modifier and Type | Method and Description |
---|---|
IndexCommit |
getIndexCommit(long gen)
Retrieve an
IndexCommit from its generation;
returns null if this IndexCommit is not currently
snapshotted |
int |
getSnapshotCount()
Returns the total number of snapshots currently held.
|
java.util.List<IndexCommit> |
getSnapshots()
Returns all IndexCommits held by at least one snapshot.
|
protected void |
incRef(IndexCommit ic)
Increments the refCount for this
IndexCommit . |
void |
onCommit(java.util.List<? extends IndexCommit> commits)
This is called each time the writer completed a commit.
|
void |
onInit(java.util.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.
|
private java.util.List<IndexCommit> |
wrapCommits(java.util.List<? extends IndexCommit> commits)
Wraps each
IndexCommit as a SnapshotDeletionPolicy.SnapshotCommitPoint . |
protected final java.util.Map<java.lang.Long,java.lang.Integer> refCounts
protected final java.util.Map<java.lang.Long,IndexCommit> indexCommits
private final IndexDeletionPolicy primary
IndexDeletionPolicy
protected IndexCommit lastCommit
IndexCommit
.private boolean initCalled
public SnapshotDeletionPolicy(IndexDeletionPolicy primary)
IndexDeletionPolicy
to wrap.public void onCommit(java.util.List<? extends IndexCommit> commits) throws java.io.IOException
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()
of IndexCommit
.
This method is only called when IndexWriter.commit()
or IndexWriter.close()
is
called, or possibly not at all if the IndexWriter.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.
onCommit
in class IndexDeletionPolicy
commits
- List of IndexCommit
,
sorted by age (the 0th one is the oldest commit).java.io.IOException
public void onInit(java.util.List<? extends IndexCommit> commits) throws java.io.IOException
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.
onInit
in class IndexDeletionPolicy
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.java.io.IOException
public void release(IndexCommit commit) throws java.io.IOException
commit
- the commit previously returned by snapshot()
java.io.IOException
protected void releaseGen(long gen) throws java.io.IOException
java.io.IOException
protected void incRef(IndexCommit ic)
IndexCommit
.public IndexCommit snapshot() throws java.io.IOException
IndexDeletionPolicy
is used). The
snapshot can be removed by calling release(IndexCommit)
followed
by a call to IndexWriter.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.
IndexCommit
that was snapshotted.java.lang.IllegalStateException
- if this index does not have any commits yetjava.io.IOException
public java.util.List<IndexCommit> getSnapshots()
public int getSnapshotCount()
public IndexCommit getIndexCommit(long gen)
IndexCommit
from its generation;
returns null if this IndexCommit is not currently
snapshottedprivate java.util.List<IndexCommit> wrapCommits(java.util.List<? extends IndexCommit> commits)
IndexCommit
as a SnapshotDeletionPolicy.SnapshotCommitPoint
.