org.apache.lucene.index
Class PersistentSnapshotDeletionPolicy

java.lang.Object
  extended by org.apache.lucene.index.SnapshotDeletionPolicy
      extended by org.apache.lucene.index.PersistentSnapshotDeletionPolicy
All Implemented Interfaces:
IndexDeletionPolicy

public class PersistentSnapshotDeletionPolicy
extends SnapshotDeletionPolicy

A SnapshotDeletionPolicy which adds a persistence layer so that snapshots can be maintained across the life of an application. The snapshots are persisted in a Directory and are committed as soon as snapshot(String) or release(String) is called.

NOTE: this class receives a Directory to persist the data into a Lucene index. It is highly recommended to use a dedicated directory (and on stable storage as well) for persisting the snapshots' information, and not reuse the content index directory, or otherwise conflicts and index corruptions will occur.

NOTE: you should call close() when you're done using this class for safetyness (it will close the IndexWriter instance used).


Nested Class Summary
 
Nested classes/interfaces inherited from class org.apache.lucene.index.SnapshotDeletionPolicy
SnapshotDeletionPolicy.SnapshotCommitPoint
 
Field Summary
 
Fields inherited from class org.apache.lucene.index.SnapshotDeletionPolicy
lastCommit
 
Constructor Summary
PersistentSnapshotDeletionPolicy(IndexDeletionPolicy primary, Directory dir, IndexWriterConfig.OpenMode mode, Version matchVersion)
          PersistentSnapshotDeletionPolicy wraps another IndexDeletionPolicy to enable flexible snapshotting.
 
Method Summary
 void close()
          Closes the index which writes the snapshots to the directory.
 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.
static Map<String,String> readSnapshotsInfo(Directory dir)
          Reads the snapshots information from the given Directory.
 void release(String id)
          Deletes a snapshotted commit by ID.
 IndexCommit snapshot(String id)
          Snapshots the last commit using the given ID.
 
Methods inherited from class org.apache.lucene.index.SnapshotDeletionPolicy
checkSnapshotted, getSnapshot, getSnapshots, isSnapshotted, onCommit, registerSnapshotInfo, wrapCommits
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PersistentSnapshotDeletionPolicy

public PersistentSnapshotDeletionPolicy(IndexDeletionPolicy primary,
                                        Directory dir,
                                        IndexWriterConfig.OpenMode mode,
                                        Version matchVersion)
                                 throws CorruptIndexException,
                                        LockObtainFailedException,
                                        IOException
PersistentSnapshotDeletionPolicy wraps another IndexDeletionPolicy to enable flexible snapshotting.

Parameters:
primary - the IndexDeletionPolicy that is used on non-snapshotted commits. Snapshotted commits, by definition, are not deleted until explicitly released via release(String).
dir - the Directory 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.
matchVersion - specifies the Version that should be used when opening the IndexWriter.
Throws:
CorruptIndexException
LockObtainFailedException
IOException
Method Detail

readSnapshotsInfo

public static Map<String,String> readSnapshotsInfo(Directory dir)
                                            throws IOException
Reads the snapshots information from the given Directory. This method does 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:
IOException

onInit

public void onInit(List<? extends IndexCommit> commits)
            throws IOException
Description copied from interface: 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 interface IndexDeletionPolicy
Overrides:
onInit in class SnapshotDeletionPolicy
Parameters:
commits - List of current point-in-time commits, sorted by age (the 0th one is the oldest commit).
Throws:
IOException

snapshot

public IndexCommit snapshot(String id)
                     throws IOException
Snapshots the last commit using the given ID. Once this method returns, the snapshot information is persisted in the directory.

Overrides:
snapshot in class SnapshotDeletionPolicy
Parameters:
id - a unique identifier of the commit that is being snapshotted.
Returns:
the IndexCommit that was snapshotted.
Throws:
IOException
See Also:
SnapshotDeletionPolicy.snapshot(String)

release

public void release(String id)
             throws IOException
Deletes a snapshotted commit by ID. Once this method returns, the snapshot information is committed to the directory.

Overrides:
release in class SnapshotDeletionPolicy
Parameters:
id - a unique identifier of the commit that is un-snapshotted.
Throws:
IOException
See Also:
SnapshotDeletionPolicy.release(String)

close

public void close()
           throws CorruptIndexException,
                  IOException
Closes the index which writes the snapshots to the directory.

Throws:
CorruptIndexException
IOException


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