You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by Michael Cafarella <mi...@attbi.com> on 2003/04/25 04:08:57 UTC

Code contribution for locking index directories

  Hi there,

  I had a problem with Lucene when trying to
load read-only index dirs.  Obviously, Lucene
can't create a lock file because the dir is
read-only.  In this case, Lucene can't just
charge ahead: there might be another process 
that has write-permission to the directory,
and will change things underneath the 
read-only process.

  I changed org.apache.lucene.store.FSDirectory 
so that it creates the lock file in /tmp (or 
whatever system-specific equivalent you have).  
The name of the lock file is a hashcode computed
over the full path of the Lucene index.
This way, all readers of the same index
should try to make a lock in /tmp, where
all writers should have permissions.
Different Lucene index dirs will have 
different hashcode names.

  Here are the diffs:
  --Mike C.

===================================================================
RCS file:
/home/cvspublic/jakarta-lucene/src/java/org/apache/lucene/store/FSDirectory.java,v
retrieving revision 1.17
diff -r1.17 FSDirectory.java
62a63
> import java.security.*;
86a88,97
>   private static MessageDigest DIGESTER = null;
>
>   static {
>     try {
>       DIGESTER = MessageDigest.getInstance("MD5");
>     } catch (NoSuchAlgorithmException e) {
>         throw new RuntimeException(e);
>     }
>   }
>
270a282,287
>   /**
>    * 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'};
>
283c300,343
<     final File lockFile = new File(directory, name);
---
>     // We don't want to actually create the lock
>     // file in the given directory, but we DO want
>     // the dir-specific path + the lock name.
>     File theoreticalLockFile = new File(directory, name);
>
>     //
>     // We can use it to generate a unique lockfile
>     // name, which we'll put in the TMP directory of
>     // the machine.  That way, the process does not require
>     // write-access to the Lucene directory itself.
>     // (Some other, more privileged, process might
>     // change the files out from under a reader, so
>     // we still need to lock it even for read-only.)
>     //
>
>     // First, hash the theoreticalLockFile to a string.
>     // We need to generate the hash out of the theoreticalLockFile,
>     // and then convert the hash to a hex str.
>     File tmpLock = null;
>     String lockname = null;
>     while (lockname == null) {
>         try {
>             StringBuffer buf = new StringBuffer();
>             byte digest[] =
DIGESTER.digest(theoreticalLockFile.getCanonicalPath().getBytes());
>             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]);
>             }
>
>             // Next, obtain the name of the TMP dir.
>             // Clear away the file we unfortunately need to create.
>             tmpLock = File.createTempFile("toBeALock", "" +
System.currentTimeMillis());
>             tmpLock.delete();
>
>             // Finally, assign the lockname
>             lockname = buf.toString();
>         } catch (IOException ie) {
>         }
>     }
>
>     // Finally, make a lock file where we need it.
>     final File lockFile = new File(tmpLock.getParent(), lockname);
>








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