lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r567253 - in /lucene/java/trunk: CHANGES.txt src/java/org/apache/lucene/index/FieldsReader.java src/java/org/apache/lucene/index/TermInfosReader.java src/java/org/apache/lucene/index/TermVectorsReader.java
Date Sat, 18 Aug 2007 09:42:22 GMT
Author: mikemccand
Date: Sat Aug 18 02:42:21 2007
New Revision: 567253

URL: http://svn.apache.org/viewvc?view=rev&rev=567253
Log:
LUCENE-978: ensure all streams are closed when we hit exception in constructor

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/src/java/org/apache/lucene/index/FieldsReader.java
    lucene/java/trunk/src/java/org/apache/lucene/index/TermInfosReader.java
    lucene/java/trunk/src/java/org/apache/lucene/index/TermVectorsReader.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?view=diff&rev=567253&r1=567252&r2=567253
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Sat Aug 18 02:42:21 2007
@@ -72,6 +72,10 @@
     sharing an index over NFS and using a custom deletion policy (Mike
     McCandless)
 
+11. LUCENE-978: Ensure TermInfosReader, FieldsReader, and FieldsReader
+    close any streams they had opened if an exception is hit in the
+    constructor.  (Ning Li via Mike McCandless)
+
 New features
 
  1. LUCENE-906: Elision filter for French.

Modified: lucene/java/trunk/src/java/org/apache/lucene/index/FieldsReader.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/FieldsReader.java?view=diff&rev=567253&r1=567252&r2=567253
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/FieldsReader.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/FieldsReader.java Sat Aug 18 02:42:21
2007
@@ -66,23 +66,38 @@
   }
 
   FieldsReader(Directory d, String segment, FieldInfos fn, int readBufferSize, int docStoreOffset,
int size) throws IOException {
-    fieldInfos = fn;
+    boolean success = false;
 
-    cloneableFieldsStream = d.openInput(segment + ".fdt", readBufferSize);
-    fieldsStream = (IndexInput)cloneableFieldsStream.clone();
-    indexStream = d.openInput(segment + ".fdx", readBufferSize);
-
-    if (docStoreOffset != -1) {
-      // We read only a slice out of this shared fields file
-      this.docStoreOffset = docStoreOffset;
-      this.size = size;
-
-      // Verify the file is long enough to hold all of our
-      // docs
-      assert ((int) (indexStream.length()/8)) >= size + this.docStoreOffset;
-    } else {
-      this.docStoreOffset = 0;
-      this.size = (int) (indexStream.length() >> 3);
+    try {
+      fieldInfos = fn;
+
+      cloneableFieldsStream = d.openInput(segment + ".fdt", readBufferSize);
+      fieldsStream = (IndexInput) cloneableFieldsStream.clone();
+      indexStream = d.openInput(segment + ".fdx", readBufferSize);
+
+      if (docStoreOffset != -1) {
+        // We read only a slice out of this shared fields file
+        this.docStoreOffset = docStoreOffset;
+        this.size = size;
+
+        // Verify the file is long enough to hold all of our
+        // docs
+        assert ((int) (indexStream.length() / 8)) >= size + this.docStoreOffset;
+      } else {
+        this.docStoreOffset = 0;
+        this.size = (int) (indexStream.length() >> 3);
+      }
+
+      success = true;
+    } finally {
+      // With lock-less commits, it's entirely possible (and
+      // fine) to hit a FileNotFound exception above. In
+      // this case, we want to explicitly close any subset
+      // of things that were opened so that we don't have to
+      // wait for a GC to do so.
+      if (!success) {
+        close();
+      }
     }
   }
 
@@ -103,9 +118,15 @@
    */
   final void close() throws IOException {
     if (!closed) {
-      fieldsStream.close();
-      cloneableFieldsStream.close();
-      indexStream.close();
+      if (fieldsStream != null) {
+        fieldsStream.close();
+      }
+      if (cloneableFieldsStream != null) {
+        cloneableFieldsStream.close();
+      }
+      if (indexStream != null) {
+        indexStream.close();
+      }
       IndexInput localFieldsStream = (IndexInput) fieldsStreamTL.get();
       if (localFieldsStream != null) {
         localFieldsStream.close();

Modified: lucene/java/trunk/src/java/org/apache/lucene/index/TermInfosReader.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/TermInfosReader.java?view=diff&rev=567253&r1=567252&r2=567253
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/TermInfosReader.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/TermInfosReader.java Sat Aug 18 02:42:21
2007
@@ -48,17 +48,31 @@
 
   TermInfosReader(Directory dir, String seg, FieldInfos fis, int readBufferSize)
        throws CorruptIndexException, IOException {
-    directory = dir;
-    segment = seg;
-    fieldInfos = fis;
+    boolean success = false;
 
-    origEnum = new SegmentTermEnum(directory.openInput(segment + ".tis", readBufferSize),
-                                   fieldInfos, false);
-    size = origEnum.size;
+    try {
+      directory = dir;
+      segment = seg;
+      fieldInfos = fis;
 
-    indexEnum =
-      new SegmentTermEnum(directory.openInput(segment + ".tii", readBufferSize),
-			  fieldInfos, true);
+      origEnum = new SegmentTermEnum(directory.openInput(segment + ".tis",
+          readBufferSize), fieldInfos, false);
+      size = origEnum.size;
+
+      indexEnum = new SegmentTermEnum(directory.openInput(segment + ".tii",
+          readBufferSize), fieldInfos, true);
+
+      success = true;
+    } finally {
+      // With lock-less commits, it's entirely possible (and
+      // fine) to hit a FileNotFound exception above. In
+      // this case, we want to explicitly close any subset
+      // of things that were opened so that we don't have to
+      // wait for a GC to do so.
+      if (!success) {
+        close();
+      }
+    }
   }
 
   public int getSkipInterval() {

Modified: lucene/java/trunk/src/java/org/apache/lucene/index/TermVectorsReader.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/TermVectorsReader.java?view=diff&rev=567253&r1=567252&r2=567253
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/TermVectorsReader.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/TermVectorsReader.java Sat Aug 18 02:42:21
2007
@@ -53,26 +53,40 @@
     
   TermVectorsReader(Directory d, String segment, FieldInfos fieldInfos, int readBufferSize,
int docStoreOffset, int size)
     throws CorruptIndexException, IOException {
-    if (d.fileExists(segment + TermVectorsWriter.TVX_EXTENSION)) {
-      tvx = d.openInput(segment + TermVectorsWriter.TVX_EXTENSION, readBufferSize);
-      checkValidFormat(tvx);
-      tvd = d.openInput(segment + TermVectorsWriter.TVD_EXTENSION, readBufferSize);
-      tvdFormat = checkValidFormat(tvd);
-      tvf = d.openInput(segment + TermVectorsWriter.TVF_EXTENSION, readBufferSize);
-      tvfFormat = checkValidFormat(tvf);
-      if (-1 == docStoreOffset) {
-        this.docStoreOffset = 0;
-        this.size = (int) (tvx.length() >> 3);
-      } else {
-        this.docStoreOffset = docStoreOffset;
-        this.size = size;
-        // Verify the file is long enough to hold all of our
-        // docs
-        assert ((int) (tvx.length()/8)) >= size + docStoreOffset;
+    boolean success = false;
+
+    try {
+      if (d.fileExists(segment + TermVectorsWriter.TVX_EXTENSION)) {
+        tvx = d.openInput(segment + TermVectorsWriter.TVX_EXTENSION, readBufferSize);
+        checkValidFormat(tvx);
+        tvd = d.openInput(segment + TermVectorsWriter.TVD_EXTENSION, readBufferSize);
+        tvdFormat = checkValidFormat(tvd);
+        tvf = d.openInput(segment + TermVectorsWriter.TVF_EXTENSION, readBufferSize);
+        tvfFormat = checkValidFormat(tvf);
+        if (-1 == docStoreOffset) {
+          this.docStoreOffset = 0;
+          this.size = (int) (tvx.length() >> 3);
+        } else {
+          this.docStoreOffset = docStoreOffset;
+          this.size = size;
+          // Verify the file is long enough to hold all of our
+          // docs
+          assert ((int) (tvx.length() / 8)) >= size + docStoreOffset;
+        }
       }
-    }
 
-    this.fieldInfos = fieldInfos;
+      this.fieldInfos = fieldInfos;
+      success = true;
+    } finally {
+      // With lock-less commits, it's entirely possible (and
+      // fine) to hit a FileNotFound exception above. In
+      // this case, we want to explicitly close any subset
+      // of things that were opened so that we don't have to
+      // wait for a GC to do so.
+      if (!success) {
+        close();
+      }
+    }
   }
   
   private int checkValidFormat(IndexInput in) throws CorruptIndexException, IOException



Mime
View raw message