cutting 2003/05/29 13:18:18 Modified: src/java/org/apache/lucene/store FSDirectory.java Log: Fix so that lock files created in /tmp are removed when a directory is re-created. This way attempts to create a new index after a crashed indexing run no longer have to manually remove lock files from /tmp. Revision Changes Path 1.20 +39 -23 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.19 retrieving revision 1.20 diff -u -r1.19 -r1.20 --- FSDirectory.java 4 May 2003 21:13:49 -0000 1.19 +++ FSDirectory.java 29 May 2003 20:18:18 -0000 1.20 @@ -156,8 +156,8 @@ private synchronized void create() throws IOException { if (!directory.exists()) - if (!directory.mkdir()) - throw new IOException("Cannot create directory: " + directory); + if (!directory.mkdir()) + throw new IOException("Cannot create directory: " + directory); String[] files = directory.list(); // clear old files for (int i = 0; i < files.length; i++) { @@ -165,6 +165,17 @@ if (!file.delete()) throw new IOException("couldn't delete " + files[i]); } + + String lockPrefix = getLockPrefix().toString(); // clear old locks + File tmpdir = new File(System.getProperty("java.io.tmpdir")); + files = tmpdir.list(); + for (int i = 0; i < files.length; i++) { + if (!files[i].startsWith(lockPrefix)) + continue; + File file = new File(tmpdir, files[i]); + if (!file.delete()) + throw new IOException("couldn't delete " + files[i]); + } } /** Returns an array of strings, one for each file in the directory. */ @@ -298,27 +309,9 @@ * @return an instance of <code>Lock</code> holding the lock */ public final Lock makeLock(String 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"); + StringBuffer buf = getLockPrefix(); + buf.append("-"); + buf.append(name); // make the lock file in tmp, where anyone can create files. final File lockFile = new File(System.getProperty("java.io.tmpdir"), @@ -345,6 +338,29 @@ return "Lock@" + lockFile; } }; + } + + private StringBuffer getLockPrefix() { + String dirName; // name to be hashed + try { + dirName = directory.getCanonicalPath(); + } catch (IOException e) { + throw new RuntimeException(e.toString()); + } + + byte digest[]; + synchronized (DIGESTER) { + digest = DIGESTER.digest(dirName.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]); + } + + return buf; } /** Closes the store to future operations. */
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]