You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@lucene.apache.org by mi...@apache.org on 2008/07/10 11:27:46 UTC

svn commit: r675485 - in /lucene/java/trunk/src: java/org/apache/lucene/store/ test/org/apache/lucene/index/ test/org/apache/lucene/store/

Author: mikemccand
Date: Thu Jul 10 02:27:44 2008
New Revision: 675485

URL: http://svn.apache.org/viewvc?rev=675485&view=rev
Log:
LUCENE-1331: catch if FSDirectory is used after being closed

Added:
    lucene/java/trunk/src/test/org/apache/lucene/store/TestDirectory.java   (with props)
Modified:
    lucene/java/trunk/src/java/org/apache/lucene/store/Directory.java
    lucene/java/trunk/src/java/org/apache/lucene/store/FSDirectory.java
    lucene/java/trunk/src/java/org/apache/lucene/store/RAMDirectory.java
    lucene/java/trunk/src/test/org/apache/lucene/index/TestDoc.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/store/Directory.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/store/Directory.java?rev=675485&r1=675484&r2=675485&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/store/Directory.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/store/Directory.java Thu Jul 10 02:27:44 2008
@@ -38,6 +38,8 @@
  */
 public abstract class Directory {
 
+  volatile boolean isOpen = true;
+
   /** Holds the LockFactory instance (implements locking for
    * this Directory instance). */
   protected LockFactory lockFactory;
@@ -210,4 +212,12 @@
       if(closeDirSrc)
         src.close();
   }
+
+  /**
+   * @throws AlreadyClosedException if this Directory is closed
+   */
+  protected final void ensureOpen() throws AlreadyClosedException {
+    if (!isOpen)
+      throw new AlreadyClosedException("this Directory is closed");
+  }
 }

Modified: lucene/java/trunk/src/java/org/apache/lucene/store/FSDirectory.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/store/FSDirectory.java?rev=675485&r1=675484&r2=675485&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/store/FSDirectory.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/store/FSDirectory.java Thu Jul 10 02:27:44 2008
@@ -317,17 +317,20 @@
 
   /** Returns an array of strings, one for each Lucene index file in the directory. */
   public String[] list() {
+    ensureOpen();
     return directory.list(IndexFileNameFilter.getFilter());
   }
 
   /** Returns true iff a file with the given name exists. */
   public boolean fileExists(String name) {
+    ensureOpen();
     File file = new File(directory, name);
     return file.exists();
   }
 
   /** Returns the time the named file was last modified. */
   public long fileModified(String name) {
+    ensureOpen();
     File file = new File(directory, name);
     return file.lastModified();
   }
@@ -340,18 +343,21 @@
 
   /** Set the modified time of an existing file to now. */
   public void touchFile(String name) {
+    ensureOpen();
     File file = new File(directory, name);
     file.setLastModified(System.currentTimeMillis());
   }
 
   /** Returns the length in bytes of a file in the directory. */
   public long fileLength(String name) {
+    ensureOpen();
     File file = new File(directory, name);
     return file.length();
   }
 
   /** Removes an existing file in the directory. */
   public void deleteFile(String name) throws IOException {
+    ensureOpen();
     File file = new File(directory, name);
     if (!file.delete())
       throw new IOException("Cannot delete " + file);
@@ -363,6 +369,7 @@
    */
   public synchronized void renameFile(String from, String to)
       throws IOException {
+    ensureOpen();
     File old = new File(directory, from);
     File nu = new File(directory, to);
 
@@ -427,7 +434,7 @@
   /** Creates a new, empty file in the directory with the given name.
       Returns a stream writing this file. */
   public IndexOutput createOutput(String name) throws IOException {
-
+    ensureOpen();
     File file = new File(directory, name);
     if (file.exists() && !file.delete())          // delete existing, if any
       throw new IOException("Cannot overwrite: " + file);
@@ -436,6 +443,7 @@
   }
 
   public void sync(String name) throws IOException {
+    ensureOpen();
     File fullFile = new File(directory, name);
     boolean success = false;
     int retryCount = 0;
@@ -470,11 +478,13 @@
 
   // Inherit javadoc
   public IndexInput openInput(String name) throws IOException {
+    ensureOpen();
     return openInput(name, BufferedIndexInput.BUFFER_SIZE);
   }
 
   // Inherit javadoc
   public IndexInput openInput(String name, int bufferSize) throws IOException {
+    ensureOpen();
     return new FSIndexInput(new File(directory, name), bufferSize);
   }
 
@@ -486,6 +496,7 @@
 
   
   public String getLockID() {
+    ensureOpen();
     String dirName;                               // name to be hashed
     try {
       dirName = directory.getCanonicalPath();
@@ -510,7 +521,8 @@
 
   /** Closes the store to future operations. */
   public synchronized void close() {
-    if (--refCount <= 0) {
+    if (isOpen && --refCount <= 0) {
+      isOpen = false;
       synchronized (DIRECTORIES) {
         DIRECTORIES.remove(directory);
       }
@@ -518,6 +530,7 @@
   }
 
   public File getFile() {
+    ensureOpen();
     return directory;
   }
 

Modified: lucene/java/trunk/src/java/org/apache/lucene/store/RAMDirectory.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/store/RAMDirectory.java?rev=675485&r1=675484&r2=675485&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/store/RAMDirectory.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/store/RAMDirectory.java Thu Jul 10 02:27:44 2008
@@ -237,15 +237,7 @@
 
   /** Closes the store to future operations, releasing associated memory. */
   public void close() {
+    isOpen = false;
     fileMap = null;
   }
-
-  /**
-   * @throws AlreadyClosedException if this IndexReader is closed
-   */
-  protected final void ensureOpen() throws AlreadyClosedException {
-    if (fileMap == null) {
-      throw new AlreadyClosedException("this RAMDirectory is closed");
-    }
-  }
 }

Modified: lucene/java/trunk/src/test/org/apache/lucene/index/TestDoc.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/index/TestDoc.java?rev=675485&r1=675484&r2=675485&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/index/TestDoc.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/index/TestDoc.java Thu Jul 10 02:27:44 2008
@@ -22,11 +22,9 @@
 
 
 import org.apache.lucene.analysis.SimpleAnalyzer;
-import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.store.FSDirectory;
 import org.apache.lucene.store.Directory;
 import org.apache.lucene.document.Document;
-import org.apache.lucene.search.Similarity;
 import org.apache.lucene.demo.FileDocument;
 
 import java.io.*;
@@ -115,7 +113,6 @@
       SegmentInfo si2 = indexDoc(writer, "test2.txt");
       printSegment(out, si2);
       writer.close();
-      directory.close();
 
       SegmentInfo siMerge = merge(si1, si2, "merge", false);
       printSegment(out, siMerge);
@@ -126,6 +123,7 @@
       SegmentInfo siMerge3 = merge(siMerge, siMerge2, "merge3", false);
       printSegment(out, siMerge3);
       
+      directory.close();
       out.close();
       sw.close();
       String multiFileOutput = sw.getBuffer().toString();
@@ -143,7 +141,6 @@
       si2 = indexDoc(writer, "test2.txt");
       printSegment(out, si2);
       writer.close();
-      directory.close();
 
       siMerge = merge(si1, si2, "merge", true);
       printSegment(out, siMerge);
@@ -154,6 +151,7 @@
       siMerge3 = merge(siMerge, siMerge2, "merge3", true);
       printSegment(out, siMerge3);
       
+      directory.close();
       out.close();
       sw.close();
       String singleFileOutput = sw.getBuffer().toString();

Added: lucene/java/trunk/src/test/org/apache/lucene/store/TestDirectory.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/store/TestDirectory.java?rev=675485&view=auto
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/store/TestDirectory.java (added)
+++ lucene/java/trunk/src/test/org/apache/lucene/store/TestDirectory.java Thu Jul 10 02:27:44 2008
@@ -0,0 +1,42 @@
+package org.apache.lucene.store;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.lucene.util.LuceneTestCase;
+
+public class TestDirectory extends LuceneTestCase {
+
+  public void testDetectClose() throws Throwable {
+    Directory dir = new RAMDirectory();
+    dir.close();
+    try {
+      dir.createOutput("test");
+      fail("did not hit expected exception");
+    } catch (AlreadyClosedException ace) {
+    }
+
+    dir = FSDirectory.getDirectory(System.getProperty("tempDir"));
+    dir.close();
+    try {
+      dir.createOutput("test");
+      fail("did not hit expected exception");
+    } catch (AlreadyClosedException ace) {
+    }
+  }
+}
+

Propchange: lucene/java/trunk/src/test/org/apache/lucene/store/TestDirectory.java
------------------------------------------------------------------------------
    svn:eol-style = native