Return-Path: Delivered-To: apmail-jakarta-lucene-dev-archive@apache.org Received: (qmail 66220 invoked from network); 25 Apr 2003 02:07:05 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 25 Apr 2003 02:07:05 -0000 Received: (qmail 4902 invoked by uid 97); 25 Apr 2003 02:09:10 -0000 Delivered-To: qmlist-jakarta-archive-lucene-dev@nagoya.betaversion.org Received: (qmail 4895 invoked from network); 25 Apr 2003 02:09:10 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 25 Apr 2003 02:09:10 -0000 Received: (qmail 65933 invoked by uid 500); 25 Apr 2003 02:07:03 -0000 Mailing-List: contact lucene-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Lucene Developers List" Reply-To: "Lucene Developers List" Delivered-To: mailing list lucene-dev@jakarta.apache.org Received: (qmail 65920 invoked from network); 25 Apr 2003 02:07:02 -0000 Received: from rwcrmhc52.attbi.com (216.148.227.88) by daedalus.apache.org with SMTP; 25 Apr 2003 02:07:02 -0000 Received: from bacalao (h005018069d5d.ne.client2.attbi.com[24.61.47.204]) by rwcrmhc52.attbi.com (rwcrmhc52) with SMTP id <2003042502071105200nr487e>; Fri, 25 Apr 2003 02:07:12 +0000 Subject: Code contribution for locking index directories From: Michael Cafarella To: lucene-dev@jakarta.apache.org Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Mailer: Ximian Evolution 1.0.8 (1.0.8-11) Date: 24 Apr 2003 22:08:57 -0400 Message-Id: <1051236542.2976.248.camel@bacalao> Mime-Version: 1.0 X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N 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