Package org.apache.lucene.index
Class PersistentSnapshotDeletionPolicy
- java.lang.Object
-
- org.apache.lucene.index.IndexDeletionPolicy
-
- org.apache.lucene.index.SnapshotDeletionPolicy
-
- org.apache.lucene.index.PersistentSnapshotDeletionPolicy
-
public class PersistentSnapshotDeletionPolicy extends SnapshotDeletionPolicy
ASnapshotDeletionPolicy
which adds a persistence layer so that snapshots can be maintained across the life of an application. The snapshots are persisted in aDirectory
and are committed as soon assnapshot()
orrelease(IndexCommit)
is called.NOTE: Sharing
PersistentSnapshotDeletionPolicy
s that write to the same directory acrossIndexWriter
s will corrupt snapshots. You should make sure everyIndexWriter
has its ownPersistentSnapshotDeletionPolicy
and that they all write to a differentDirectory
. It is OK to use the same Directory that holds the index.This class adds a
release(long)
method to release commits from a previous snapshot'sIndexCommit.getGeneration()
.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
CODEC_NAME
private Directory
dir
private long
nextWriteGen
static java.lang.String
SNAPSHOTS_PREFIX
Prefix used for the save file.private static int
VERSION_CURRENT
private static int
VERSION_START
-
Fields inherited from class org.apache.lucene.index.SnapshotDeletionPolicy
indexCommits, lastCommit, refCounts
-
-
Constructor Summary
Constructors Constructor Description PersistentSnapshotDeletionPolicy(IndexDeletionPolicy primary, Directory dir)
PersistentSnapshotDeletionPolicy
wraps anotherIndexDeletionPolicy
to enable flexible snapshotting, passingIndexWriterConfig.OpenMode.CREATE_OR_APPEND
by default.PersistentSnapshotDeletionPolicy(IndexDeletionPolicy primary, Directory dir, IndexWriterConfig.OpenMode mode)
PersistentSnapshotDeletionPolicy
wraps anotherIndexDeletionPolicy
to enable flexible snapshotting.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
clearPriorSnapshots()
java.lang.String
getLastSaveFile()
Returns the file name the snapshots are currently saved to, or null if no snapshots have been saved.private void
loadPriorSnapshots()
Reads the snapshots information from the givenDirectory
.private void
persist()
void
release(long gen)
Deletes a snapshotted commit by generation.void
release(IndexCommit commit)
Deletes a snapshotted commit.IndexCommit
snapshot()
Snapshots the last commit.-
Methods inherited from class org.apache.lucene.index.SnapshotDeletionPolicy
getIndexCommit, getSnapshotCount, getSnapshots, incRef, onCommit, onInit, releaseGen
-
-
-
-
Field Detail
-
SNAPSHOTS_PREFIX
public static final java.lang.String SNAPSHOTS_PREFIX
Prefix used for the save file.- See Also:
- Constant Field Values
-
VERSION_START
private static final int VERSION_START
- See Also:
- Constant Field Values
-
VERSION_CURRENT
private static final int VERSION_CURRENT
- See Also:
- Constant Field Values
-
CODEC_NAME
private static final java.lang.String CODEC_NAME
- See Also:
- Constant Field Values
-
nextWriteGen
private long nextWriteGen
-
dir
private final Directory dir
-
-
Constructor Detail
-
PersistentSnapshotDeletionPolicy
public PersistentSnapshotDeletionPolicy(IndexDeletionPolicy primary, Directory dir) throws java.io.IOException
PersistentSnapshotDeletionPolicy
wraps anotherIndexDeletionPolicy
to enable flexible snapshotting, passingIndexWriterConfig.OpenMode.CREATE_OR_APPEND
by default.- Parameters:
primary
- theIndexDeletionPolicy
that is used on non-snapshotted commits. Snapshotted commits, by definition, are not deleted until explicitly released viarelease(org.apache.lucene.index.IndexCommit)
.dir
- theDirectory
which will be used to persist the snapshots information.- Throws:
java.io.IOException
-
PersistentSnapshotDeletionPolicy
public PersistentSnapshotDeletionPolicy(IndexDeletionPolicy primary, Directory dir, IndexWriterConfig.OpenMode mode) throws java.io.IOException
PersistentSnapshotDeletionPolicy
wraps anotherIndexDeletionPolicy
to enable flexible snapshotting.- Parameters:
primary
- theIndexDeletionPolicy
that is used on non-snapshotted commits. Snapshotted commits, by definition, are not deleted until explicitly released viarelease(org.apache.lucene.index.IndexCommit)
.dir
- theDirectory
which will be used to persist the snapshots information.mode
- specifies whether a new index should be created, deleting all existing snapshots information (immediately), or open an existing index, initializing the class with the snapshots information.- Throws:
java.io.IOException
-
-
Method Detail
-
snapshot
public IndexCommit snapshot() throws java.io.IOException
Snapshots the last commit. Once this method returns, the snapshot information is persisted in the directory.- Overrides:
snapshot
in classSnapshotDeletionPolicy
- Returns:
- the
IndexCommit
that was snapshotted. - Throws:
java.io.IOException
- See Also:
SnapshotDeletionPolicy.snapshot()
-
release
public void release(IndexCommit commit) throws java.io.IOException
Deletes a snapshotted commit. Once this method returns, the snapshot information is persisted in the directory.- Overrides:
release
in classSnapshotDeletionPolicy
- Parameters:
commit
- the commit previously returned bySnapshotDeletionPolicy.snapshot()
- Throws:
java.io.IOException
- See Also:
SnapshotDeletionPolicy.release(org.apache.lucene.index.IndexCommit)
-
release
public void release(long gen) throws java.io.IOException
Deletes a snapshotted commit by generation. Once this method returns, the snapshot information is persisted in the directory.- Throws:
java.io.IOException
- See Also:
IndexCommit.getGeneration()
,SnapshotDeletionPolicy.release(org.apache.lucene.index.IndexCommit)
-
persist
private void persist() throws java.io.IOException
- Throws:
java.io.IOException
-
clearPriorSnapshots
private void clearPriorSnapshots() throws java.io.IOException
- Throws:
java.io.IOException
-
getLastSaveFile
public java.lang.String getLastSaveFile()
Returns the file name the snapshots are currently saved to, or null if no snapshots have been saved.
-
loadPriorSnapshots
private void loadPriorSnapshots() throws java.io.IOException
Reads the snapshots information from the givenDirectory
. This method can be used if the snapshots information is needed, however you cannot instantiate the deletion policy (because e.g., some other process keeps a lock on the snapshots directory).- Throws:
java.io.IOException
-
-