Hi,

a patch similar to this was proposed a year ago but then got lost. I will 
apply it unless someone objects. It fixes the problem that you cannot 
safely use IndexReader.getCurrentVersion() to detect changes, as the 
version number is reset to 0 if the index is re-created.

Also see 
http://www.mail-archive.com/lucene-dev%40jakarta.apache.org/msg06143.html

Regards
 Daniel

-- 
http://www.danielnaber.de
Index: /home/dnaber/workspace/LuceneSVN/src/java/org/apache/lucene/index/IndexReader.java
===================================================================
--- /home/dnaber/workspace/LuceneSVN/src/java/org/apache/lucene/index/IndexReader.java	(Revision 178894)
+++ /home/dnaber/workspace/LuceneSVN/src/java/org/apache/lucene/index/IndexReader.java	(Arbeitskopie)
@@ -224,6 +224,7 @@
    * @param directory where the index resides.
    * @return version number.
    * @throws IOException if segments file cannot be read
+   * @deprecated use [EMAIL PROTECTED] #isCurrent()} instead
    */
   public static long getCurrentVersion(String directory) throws IOException {
     return getCurrentVersion(new File(directory));
@@ -236,6 +237,7 @@
    * @param directory where the index resides.
    * @return version number.
    * @throws IOException if segments file cannot be read
+   * @deprecated use [EMAIL PROTECTED] #isCurrent()} instead
    */
   public static long getCurrentVersion(File directory) throws IOException {
     Directory dir = FSDirectory.getDirectory(directory, false);
@@ -251,12 +253,27 @@
    * @param directory where the index resides.
    * @return version number.
    * @throws IOException if segments file cannot be read.
+   * @deprecated use [EMAIL PROTECTED] #isCurrent()} instead
    */
   public static long getCurrentVersion(Directory directory) throws IOException {
     return SegmentInfos.readCurrentVersion(directory);
   }
 
   /**
+   * Check whether this IndexReader still works on a current version of the index.
+   * If this is not the case you will need to re-open the IndexReader to
+   * make sure you see the latest changes made to the index.
+   * 
+   * @throws IOException
+   */
+  public boolean isCurrent() throws IOException {
+    if (SegmentInfos.readCurrentVersion(directory) > segmentInfos.getVersion()) {
+      return false;
+    }
+    return true;
+  }
+
+  /**
    *  Return an array of term frequency vectors for the specified document.
    *  The array contains a vector for each vectorized field in the document.
    *  Each vector contains terms and frequencies for all terms in a given vectorized field.
Index: /home/dnaber/workspace/LuceneSVN/src/java/org/apache/lucene/index/SegmentInfos.java
===================================================================
--- /home/dnaber/workspace/LuceneSVN/src/java/org/apache/lucene/index/SegmentInfos.java	(Revision 171375)
+++ /home/dnaber/workspace/LuceneSVN/src/java/org/apache/lucene/index/SegmentInfos.java	(Arbeitskopie)
@@ -29,7 +29,11 @@
   public static final int FORMAT = -1;
   
   public int counter = 0;    // used to name new segments
-  private long version = 0; //counts how often the index has been changed by adding or deleting docs
+  /**
+   * counts how often the index has been changed by adding or deleting docs.
+   * starting with the current time in milliseconds forces to create unique version numbers.
+   */
+  private long version = System.currentTimeMillis();
 
   public final SegmentInfo info(int i) {
     return (SegmentInfo) elementAt(i);
@@ -59,7 +63,7 @@
       
       if(format >= 0){    // in old format the version number may be at the end of the file
         if (input.getFilePointer() >= input.length())
-          version = 0; // old file format without version number
+          version = System.currentTimeMillis(); // old file format without version number
         else
           version = input.readLong(); // read version
       }

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to