lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r602055 - in /lucene/java/trunk/src: java/org/apache/lucene/index/SegmentInfos.java test/org/apache/lucene/index/TestIndexReader.java
Date Fri, 07 Dec 2007 10:13:13 GMT
Author: mikemccand
Date: Fri Dec  7 02:13:11 2007
New Revision: 602055

URL: http://svn.apache.org/viewvc?rev=602055&view=rev
Log:
LUCENE-1082: fix NPE in IndexReader.lastModified(*) methods

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/index/SegmentInfos.java
    lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReader.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/index/SegmentInfos.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/SegmentInfos.java?rev=602055&r1=602054&r2=602055&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/SegmentInfos.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/SegmentInfos.java Fri Dec  7 02:13:11
2007
@@ -528,41 +528,43 @@
           // a stale cache (NFS) we have a better chance of
           // getting the right generation.
           long genB = -1;
-          for(int i=0;i<defaultGenFileRetryCount;i++) {
-            IndexInput genInput = null;
-            try {
-              genInput = directory.openInput(IndexFileNames.SEGMENTS_GEN);
-            } catch (FileNotFoundException e) {
-              message("segments.gen open: FileNotFoundException " + e);
-              break;
-            } catch (IOException e) {
-              message("segments.gen open: IOException " + e);
-            }
-
-            if (genInput != null) {
+          if (directory != null) {
+            for(int i=0;i<defaultGenFileRetryCount;i++) {
+              IndexInput genInput = null;
               try {
-                int version = genInput.readInt();
-                if (version == FORMAT_LOCKLESS) {
-                  long gen0 = genInput.readLong();
-                  long gen1 = genInput.readLong();
-                  message("fallback check: " + gen0 + "; " + gen1);
-                  if (gen0 == gen1) {
-                    // The file is consistent.
-                    genB = gen0;
-                    break;
+                genInput = directory.openInput(IndexFileNames.SEGMENTS_GEN);
+              } catch (FileNotFoundException e) {
+                message("segments.gen open: FileNotFoundException " + e);
+                break;
+              } catch (IOException e) {
+                message("segments.gen open: IOException " + e);
+              }
+
+              if (genInput != null) {
+                try {
+                  int version = genInput.readInt();
+                  if (version == FORMAT_LOCKLESS) {
+                    long gen0 = genInput.readLong();
+                    long gen1 = genInput.readLong();
+                    message("fallback check: " + gen0 + "; " + gen1);
+                    if (gen0 == gen1) {
+                      // The file is consistent.
+                      genB = gen0;
+                      break;
+                    }
                   }
+                } catch (IOException err2) {
+                  // will retry
+                } finally {
+                  genInput.close();
                 }
-              } catch (IOException err2) {
+              }
+              try {
+                Thread.sleep(defaultGenFileRetryPauseMsec);
+              } catch (InterruptedException e) {
                 // will retry
-              } finally {
-                genInput.close();
               }
             }
-            try {
-              Thread.sleep(defaultGenFileRetryPauseMsec);
-            } catch (InterruptedException e) {
-              // will retry
-            }
           }
 
           message(IndexFileNames.SEGMENTS_GEN + " check: genB=" + genB);
@@ -655,8 +657,14 @@
             String prevSegmentFileName = IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS,
                                                                                "",
                                                                                gen-1);
-            
-            if (directory.fileExists(prevSegmentFileName)) {
+
+            final boolean prevExists;
+            if (directory != null)
+              prevExists = directory.fileExists(prevSegmentFileName);
+            else
+              prevExists = new File(fileDirectory, prevSegmentFileName).exists();
+
+            if (prevExists) {
               message("fallback to prior segment file '" + prevSegmentFileName + "'");
               try {
                 Object v = doBody(prevSegmentFileName);

Modified: lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReader.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReader.java?rev=602055&r1=602054&r2=602055&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReader.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReader.java Fri Dec  7 02:13:11
2007
@@ -595,26 +595,51 @@
 
     public void testLastModified() throws IOException {
       assertFalse(IndexReader.indexExists("there_is_no_such_index"));
-      Directory dir = new MockRAMDirectory();
-      assertFalse(IndexReader.indexExists(dir));
-      IndexWriter writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
-      addDocumentWithFields(writer);
-      assertTrue(IndexReader.isLocked(dir));		// writer open, so dir is locked
-      writer.close();
-      assertTrue(IndexReader.indexExists(dir));
-      IndexReader reader = IndexReader.open(dir);
-      assertFalse(IndexReader.isLocked(dir));		// reader only, no lock
-      long version = IndexReader.lastModified(dir);
-      reader.close();
-      // modify index and check version has been
-      // incremented:
-      writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
-      addDocumentWithFields(writer);
-      writer.close();
-      reader = IndexReader.open(dir);
-      assertTrue("old lastModified is " + version + "; new lastModified is " + IndexReader.lastModified(dir),
version <= IndexReader.lastModified(dir));
-      reader.close();
-      dir.close();
+      final File fileDir = new File(System.getProperty("tempDir"), "testIndex");
+      for(int i=0;i<2;i++) {
+        try {
+          final Directory dir;
+          if (0 == i)
+            dir = new MockRAMDirectory();
+          else
+            dir = getDirectory();
+          assertFalse(IndexReader.indexExists(dir));
+          IndexWriter writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
+          addDocumentWithFields(writer);
+          assertTrue(IndexReader.isLocked(dir));		// writer open, so dir is locked
+          writer.close();
+          assertTrue(IndexReader.indexExists(dir));
+          IndexReader reader = IndexReader.open(dir);
+          assertFalse(IndexReader.isLocked(dir));		// reader only, no lock
+          long version = IndexReader.lastModified(dir);
+          if (i == 1) {
+            long version2 = IndexReader.lastModified(fileDir);
+            assertEquals(version, version2);
+          }
+          reader.close();
+          // modify index and check version has been
+          // incremented:
+          while(true) {
+            try {
+              Thread.sleep(1000);
+              break;
+            } catch (InterruptedException ie) {
+              Thread.currentThread().interrupt();
+            }
+          }
+
+          writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
+          addDocumentWithFields(writer);
+          writer.close();
+          reader = IndexReader.open(dir);
+          assertTrue("old lastModified is " + version + "; new lastModified is " + IndexReader.lastModified(dir),
version <= IndexReader.lastModified(dir));
+          reader.close();
+          dir.close();
+        } finally {
+          if (i == 1)
+            _TestUtil.rmDir(fileDir);
+        }
+      }
     }
 
     public void testVersion() throws IOException {



Mime
View raw message