scottganyo 2003/08/12 08:05:03 Modified: src/java/org/apache/lucene/index IndexReader.java IndexWriter.java SegmentReader.java src/java/org/apache/lucene/store Lock.java Log: added ability to wait on any Lock. changed write.lock wait to 1 sec from none. Revision Changes Path 1.17 +3 -3 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.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- IndexReader.java 1 May 2003 19:50:17 -0000 1.16 +++ IndexReader.java 12 Aug 2003 15:05:03 -0000 1.17 @@ -100,7 +100,7 @@ /** Returns an IndexReader reading the index in the given Directory. */ public static IndexReader open(final Directory directory) throws IOException{ synchronized (directory) { // in- & inter-process sync - return (IndexReader)new Lock.With(directory.makeLock("commit.lock")) { + return (IndexReader)new Lock.With(directory.makeLock("commit.lock"), IndexWriter.COMMIT_LOCK_TIMEOUT) { public Object doBody() throws IOException { SegmentInfos infos = new SegmentInfos(); infos.read(directory); @@ -255,7 +255,7 @@ public final synchronized void delete(int docNum) throws IOException { if (writeLock == null) { Lock writeLock = directory.makeLock("write.lock"); - if (!writeLock.obtain()) // obtain write lock + if (!writeLock.obtain(IndexWriter.WRITE_LOCK_TIMEOUT)) // obtain write lock throw new IOException("Index locked for write: " + writeLock); this.writeLock = writeLock; } @@ -338,7 +338,7 @@ return directory.makeLock("write.lock").isLocked() || directory.makeLock("commit.lock").isLocked(); - + } /** 1.14 +7 -4 jakarta-lucene/src/java/org/apache/lucene/index/IndexWriter.java Index: IndexWriter.java =================================================================== RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/index/IndexWriter.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- IndexWriter.java 11 Jul 2003 22:13:13 -0000 1.13 +++ IndexWriter.java 12 Aug 2003 15:05:03 -0000 1.14 @@ -87,6 +87,9 @@ */ public class IndexWriter { + public static long WRITE_LOCK_TIMEOUT = 1000; + public static long COMMIT_LOCK_TIMEOUT = 10000; + private Directory directory; // where this index resides private Analyzer analyzer; // how to analyze text @@ -97,7 +100,7 @@ private Lock writeLock; - /** Expert: Set the Similarity implementation used by this IndexWriter. + /** Expert: Set the Similarity implementation used by this IndexWriter. * * @see Similarity#setDefault(Similarity) */ @@ -141,12 +144,12 @@ analyzer = a; Lock writeLock = directory.makeLock("write.lock"); - if (!writeLock.obtain()) // obtain write lock + if (!writeLock.obtain(WRITE_LOCK_TIMEOUT)) // obtain write lock throw new IOException("Index locked for write: " + writeLock); this.writeLock = writeLock; // save it synchronized (directory) { // in- & inter-process sync - new Lock.With(directory.makeLock("commit.lock")) { + new Lock.With(directory.makeLock("commit.lock"), COMMIT_LOCK_TIMEOUT) { public Object doBody() throws IOException { if (create) segmentInfos.write(directory); @@ -365,7 +368,7 @@ directory)); synchronized (directory) { // in- & inter-process sync - new Lock.With(directory.makeLock("commit.lock")) { + new Lock.With(directory.makeLock("commit.lock"), COMMIT_LOCK_TIMEOUT) { public Object doBody() throws IOException { segmentInfos.write(directory); // commit before deleting deleteSegments(segmentsToDelete); // delete now-unused segments 1.10 +1 -1 jakarta-lucene/src/java/org/apache/lucene/index/SegmentReader.java Index: SegmentReader.java =================================================================== RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/index/SegmentReader.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- SegmentReader.java 12 Aug 2003 09:17:53 -0000 1.9 +++ SegmentReader.java 12 Aug 2003 15:05:03 -0000 1.10 @@ -119,7 +119,7 @@ final synchronized void doClose() throws IOException { if (deletedDocsDirty) { synchronized (directory) { // in- & inter-process sync - new Lock.With(directory.makeLock("commit.lock")) { + new Lock.With(directory.makeLock("commit.lock"), IndexWriter.COMMIT_LOCK_TIMEOUT) { public Object doBody() throws IOException { deletedDocs.write(directory, segment + ".tmp"); directory.renameFile(segment + ".tmp", segment + ".del"); 1.4 +50 -25 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.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- Lock.java 1 May 2003 19:50:17 -0000 1.3 +++ Lock.java 12 Aug 2003 15:05:03 -0000 1.4 @@ -54,6 +54,8 @@ * <http://www.apache.org/>. */ +import org.apache.lucene.index.IndexWriter; + import java.io.IOException; /** An interprocess mutex lock. @@ -70,12 +72,39 @@ */ public abstract class Lock { - /** Attempt to obtain exclusive access. - * + public static long LOCK_POLL_INTERVAL = 1000; + + /** Attempt to obtain exclusive access and immediately return + * upon success or failure. * @return true iff exclusive access is obtained */ public abstract boolean obtain() throws IOException; + /** Attempt to obtain an exclusive lock within amount + * of time given. Currently polls once per second until + * lockWaitTimeout is passed. + * @param lockWaitTimeout length of time to wait in ms + * @return true if lock was obtained + * @throws IOException if lock wait times out or obtain() throws an IOException + */ + public boolean obtain(long lockWaitTimeout) throws IOException { + boolean locked = obtain(); + int maxSleepCount = (int)(lockWaitTimeout / LOCK_POLL_INTERVAL); + int sleepCount = 0; + while (!locked) { + if (++sleepCount == maxSleepCount) { + throw new IOException("Lock obtain timed out"); + } + try { + Thread.sleep(LOCK_POLL_INTERVAL); + } catch (InterruptedException e) { + throw new IOException(e.toString()); + } + locked = obtain(); + } + return locked; + } + /** Release exclusive access. */ public abstract void release(); @@ -87,12 +116,21 @@ /** Utility class for executing code with exclusive access. */ public abstract static class With { private Lock lock; - private int sleepInterval = 1000; - private int maxSleeps = 10; - + private long lockWaitTimeout; + + /** Constructs an executor that will grab the named lock. + * Defaults lockWaitTimeout to Lock.COMMIT_LOCK_TIMEOUT. + * @deprecated Kept only to avoid breaking existing code. + */ + public With(Lock lock) + { + this(lock, IndexWriter.COMMIT_LOCK_TIMEOUT); + } + /** Constructs an executor that will grab the named lock. */ - public With(Lock lock) { + public With(Lock lock, long lockWaitTimeout) { this.lock = lock; + this.lockWaitTimeout = lockWaitTimeout; } /** Code to execute with exclusive access. */ @@ -100,29 +138,16 @@ /** Calls [EMAIL PROTECTED] #doBody} while <i>lock</i> is obtained. Blocks if lock * cannot be obtained immediately. Retries to obtain lock once per second - * until it is obtained, or until it has tried ten times. */ + * until it is obtained, or until it has tried ten times. Lock is released when + * [EMAIL PROTECTED] #doBody} exits. */ public Object run() throws IOException { boolean locked = false; try { - locked = lock.obtain(); - int sleepCount = 0; - while (!locked) { - if (++sleepCount == maxSleeps) { - throw new IOException("Timed out waiting for: " + lock); - } - try { - Thread.sleep(sleepInterval); - } catch (InterruptedException e) { - throw new IOException(e.toString()); - } - locked = lock.obtain(); - } - - return doBody(); - + locked = lock.obtain(lockWaitTimeout); + return doBody(); } finally { - if (locked) - lock.release(); + if (locked) + lock.release(); } } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]