lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject cvs commit: jakarta-lucene/src/test/org/apache/lucene ThreadSafetyTest.java
Date Thu, 01 May 2003 19:50:18 GMT
cutting     2003/05/01 12:50:18

  Modified:    .        CHANGES.txt
               src/java/org/apache/lucene/index IndexReader.java
               src/java/org/apache/lucene/store FSDirectory.java Lock.java
                        RAMDirectory.java
               src/test/org/apache/lucene ThreadSafetyTest.java
  Log:
  Put lock files in /tmp instead of with the index.
  
  Revision  Changes    Path
  1.48      +6 -1      jakarta-lucene/CHANGES.txt
  
  Index: CHANGES.txt
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/CHANGES.txt,v
  retrieving revision 1.47
  retrieving revision 1.48
  diff -u -r1.47 -r1.48
  --- CHANGES.txt	1 May 2003 01:09:15 -0000	1.47
  +++ CHANGES.txt	1 May 2003 19:50:17 -0000	1.48
  @@ -7,6 +7,11 @@
    1. Added getFieldNames(boolean) to IndexReader, SegmentReader, and
       SegmentsReader. (Julien Nioche via otis)  
   
  + 2. Changed file locking to place lock files in
  +    System.getProperty("java.io.tmpdir"), where all users are
  +    permitted to write files.  This way folks can open and correctly
  +    lock indexes which are read-only to them.
  +
   
   1.3 RC1
   
  
  
  
  1.16      +7 -4      jakarta-lucene/src/java/org/apache/lucene/index/IndexReader.java
  
  Index: IndexReader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/index/IndexReader.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- IndexReader.java	1 May 2003 01:09:15 -0000	1.15
  +++ IndexReader.java	1 May 2003 19:50:17 -0000	1.16
  @@ -335,7 +335,10 @@
      * @throws IOException if there is a problem with accessing the index
      */
       public static boolean isLocked(Directory directory) throws IOException {
  -      return directory.fileExists("write.lock");
  +      return
  +        directory.makeLock("write.lock").isLocked() ||
  +        directory.makeLock("commit.lock").isLocked();
  +        
       }
   
     /**
  @@ -345,7 +348,7 @@
      * @throws IOException if there is a problem with accessing the index
      */
       public static boolean isLocked(String directory) throws IOException {
  -      return (new File(directory, "write.lock")).exists();
  +      return isLocked(FSDirectory.getDirectory(directory, false));
       }
   
      /**
  @@ -356,7 +359,7 @@
       * currently accessing this index.
       */
       public static void unlock(Directory directory) throws IOException {
  -      directory.deleteFile("write.lock");
  -      directory.deleteFile("commit.lock");
  +      directory.makeLock("write.lock").release();
  +      directory.makeLock("commit.lock").release();
       }
   }
  
  
  
  1.18      +51 -2     jakarta-lucene/src/java/org/apache/lucene/store/FSDirectory.java
  
  Index: FSDirectory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/store/FSDirectory.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- FSDirectory.java	19 Mar 2003 03:20:53 -0000	1.17
  +++ FSDirectory.java	1 May 2003 19:50:17 -0000	1.18
  @@ -60,6 +60,8 @@
   import java.io.FileInputStream;
   import java.io.FileOutputStream;
   import java.util.Hashtable;
  +import java.security.MessageDigest;
  +import java.security.NoSuchAlgorithmException;
   
   import org.apache.lucene.util.Constants;
   
  @@ -84,6 +86,16 @@
     private static final boolean DISABLE_LOCKS =
         Boolean.getBoolean("disableLuceneLocks") || Constants.JAVA_1_1;
   
  +  private static MessageDigest DIGESTER;
  + 
  +  static {
  +    try {
  +      DIGESTER = MessageDigest.getInstance("MD5");
  +    } catch (NoSuchAlgorithmException e) {
  +        throw new RuntimeException(e);
  +    }
  +  }
  +
     /** A buffer optionally used in renameTo method */
     private byte[] buffer = null;
   
  @@ -268,6 +280,12 @@
       return new FSInputStream(new File(directory, name));
     }
   
  +  /**
  +   * So we can do some byte-to-hexchar conversion below
  +   */
  +  private static final char[] HEX_DIGITS =
  +  {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  +
     /** Constructs a {@link Lock} with the specified name.  Locks are implemented
      * with {@link File#createNewFile() }.
      *
  @@ -280,18 +298,49 @@
      * @return an instance of <code>Lock</code> holding the lock
      */
     public final Lock makeLock(String name) {
  -    final File lockFile = new File(directory, name);
  +    // the fully-qualified file name which uniquely identifies this lock 
  +    String fullName;
  +    try {
  +      fullName = new File(directory, name).getCanonicalPath();
  +    } catch (IOException e) {
  +      throw new RuntimeException(e.toString());
  +    }
  +
  +    // hash full name to create the tmp file name
  +    byte digest[];
  +    synchronized (DIGESTER) {
  +      digest = DIGESTER.digest(fullName.getBytes());
  +    }
  +    StringBuffer buf = new StringBuffer();
  +    buf.append("lucene-");
  +    for (int i = 0; i < digest.length; i++) {
  +      int b = digest[i];
  +      buf.append(HEX_DIGITS[(b >> 4) & 0xf]);
  +      buf.append(HEX_DIGITS[b & 0xf]);
  +    }
  +    buf.append(".lock");
  +
  +    // make the lock file in tmp, where anyone can create files.
  +    final File lockFile = new File(System.getProperty("java.io.tmpdir"),
  +                                   buf.toString());
  +
       return new Lock() {
         public boolean obtain() throws IOException {
           if (DISABLE_LOCKS)
             return true;
  -            return lockFile.createNewFile();
  +        return lockFile.createNewFile();
         }
         public void release() {
           if (DISABLE_LOCKS)
             return;
           lockFile.delete();
         }
  +      public boolean isLocked() {
  +        if (DISABLE_LOCKS)
  +          return false;
  +        return lockFile.exists();
  +      }
  +
         public String toString() {
           return "Lock@" + lockFile;
         }
  
  
  
  1.3       +5 -0      jakarta-lucene/src/java/org/apache/lucene/store/Lock.java
  
  Index: Lock.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/store/Lock.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Lock.java	14 Feb 2002 00:58:46 -0000	1.2
  +++ Lock.java	1 May 2003 19:50:17 -0000	1.3
  @@ -79,6 +79,11 @@
     /** Release exclusive access. */
     public abstract void release();
   
  +  /** Returns true if the resource is currently locked.  Note that one must
  +   * still call {@link #obtain()} before using the resource. */
  +  public abstract boolean isLocked();
  +
  +
     /** Utility class for executing code with exclusive access. */
     public abstract static class With {
       private Lock lock;
  
  
  
  1.8       +4 -1      jakarta-lucene/src/java/org/apache/lucene/store/RAMDirectory.java
  
  Index: RAMDirectory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/store/RAMDirectory.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- RAMDirectory.java	14 Jan 2003 03:41:05 -0000	1.7
  +++ RAMDirectory.java	1 May 2003 19:50:17 -0000	1.8
  @@ -199,6 +199,9 @@
   	public void release() {
   	  deleteFile(name);
   	}
  +	public boolean isLocked() {
  +	  return fileExists(name);
  +	}
         };
     }
   
  
  
  
  1.5       +2 -0      jakarta-lucene/src/test/org/apache/lucene/ThreadSafetyTest.java
  
  Index: ThreadSafetyTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/test/org/apache/lucene/ThreadSafetyTest.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ThreadSafetyTest.java	29 Jan 2003 17:43:13 -0000	1.4
  +++ ThreadSafetyTest.java	1 May 2003 19:50:18 -0000	1.5
  @@ -164,6 +164,8 @@
   	add = true;
       }
   
  +    IndexReader.unlock(FSDirectory.getDirectory("index", false));
  +
       if (!readOnly) {
         IndexWriter writer = new IndexWriter("index", ANALYZER, !add);
         
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-dev-help@jakarta.apache.org


Mime
View raw message