github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/index/deleter.go (about)

     1  package index
     2  
     3  // index/IndexDeletionPolicy.java
     4  
     5  /*
     6  Expert: policy for deletion of stale index commits.
     7  
     8  Implement this interface, and pass it to one of the IndexWriter or
     9  IndexReader constructors, to customize when older point-in-time-commits
    10  are deleted from the index directory. The default deletion policy is
    11  KeepOnlyLastCommitDeletionPolicy, which always remove old commits as
    12  soon as a new commit is done (this matches the behavior before 2.2).
    13  
    14  One expected use case for this ( and the reason why it was first
    15  created) is to work around problems with an index directory accessed
    16  via filesystems like NFS because NFS does not provide the "delete on
    17  last close" semantics that Lucene's "point in time" search normally
    18  relies on. By implementing a custom deletion policy, such as "a
    19  commit is only removed once it has been stale for more than X
    20  minutes", you can give your readers time to refresh to the new commit
    21  before IndexWriter removes the old commits. Note that doing so will
    22  increase the storage requirements of the index. See [LUCENE-710] for
    23  details.
    24  
    25  Implementers of sub-classes should make sure that Clone() returns an
    26  independent instance able to work with any other IndexWriter or
    27  Directory instance.
    28  */
    29  type IndexDeletionPolicy interface {
    30  	/*
    31  		This is called once when a writer is first instantiated to give the
    32  		policy a chance to remove old commit points.
    33  
    34  		The writer locates all index commits present in the index directory
    35  		and calls this method. The policy may choose to delete some of the
    36  		commit points, doing so by calling method delete() of IndexCommit.
    37  
    38  		Note: the last CommitPoint is the most recent one, i.e. the "front
    39  		index state". Be careful not to delete it, unless you know for sure
    40  		what you are doing, and unless you can afford to lose the index
    41  		content while doing that.
    42  	*/
    43  	onInit(commits []IndexCommit) error
    44  	/*
    45  	  This is called each time the writer completed a commit. This
    46  	  gives the policy a chance to remove old commit points with each
    47  	  commit.
    48  
    49  	  The policy may now choose to delete old commit points by calling
    50  	  method Delete() of IndexCommit.
    51  
    52  	  This method is only called when Commit() or Close() is called, or
    53  	  possibly not at all if the Rollback() is called.
    54  
    55  	  Note: the last CommitPoint is the most recent one, i.e. the
    56  	  "front index state". Be careful not to delete it, unless you know
    57  	  for sure what you are doing, and unless you can afford to lose
    58  	  the index content while doing that.
    59  	*/
    60  	onCommit(commits []IndexCommit) error
    61  }
    62  
    63  // index/NoDeletionPolicy.java
    64  
    65  // An IndexDeletionPolicy which keeps all index commits around, never
    66  // deleting them. This class is a singleton and can be accessed by
    67  // referencing INSTANCE.
    68  type NoDeletionPolicy bool
    69  
    70  func (p NoDeletionPolicy) onCommit(commits []IndexCommit) error { return nil }
    71  func (p NoDeletionPolicy) onInit(commits []IndexCommit) error   { return nil }
    72  func (p NoDeletionPolicy) Clone() IndexDeletionPolicy           { return p }
    73  
    74  const NO_DELETION_POLICY = NoDeletionPolicy(true)
    75  
    76  // index/KeepOnlyLastCommitDeletionPolicy.java
    77  
    78  /*
    79  This IndexDeletionPolicy implementation that keeps only the most
    80  recent commit and immediately removes all prior commits after a new
    81  commit is done. This is the default deletion policy.
    82  */
    83  type KeepOnlyLastCommitDeletionPolicy bool
    84  
    85  // Deletes all commits except the most recent one.
    86  func (p KeepOnlyLastCommitDeletionPolicy) onInit(commits []IndexCommit) error {
    87  	return p.onCommit(commits)
    88  }
    89  
    90  // Deletes all commits except the most recent one.
    91  func (p KeepOnlyLastCommitDeletionPolicy) onCommit(commits []IndexCommit) error {
    92  	// Note that len(commits) should normally be 2 (if not called by
    93  	// onInit above).
    94  	for i, limit := 0, len(commits); i < limit-1; i++ {
    95  		commits[i].Delete()
    96  	}
    97  	return nil
    98  }
    99  
   100  func (p KeepOnlyLastCommitDeletionPolicy) Clone() IndexDeletionPolicy {
   101  	return p
   102  }
   103  
   104  const DEFAULT_DELETION_POLICY = KeepOnlyLastCommitDeletionPolicy(true)