- Added WRITE_LOCK_NAME and COMMIT_LOCK_NAME, so that their names are not

hard-coded in half a dozen places in the code.


git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150095 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Otis Gospodnetic 2003-10-13 14:22:27 +00:00
parent 59779c7455
commit 9e968c9127
2 changed files with 17 additions and 9 deletions

View File

@ -76,6 +76,9 @@ import org.apache.lucene.document.Field; // for javadoc
document in the index. These document numbers are ephemeral--they may change
as documents are added to and deleted from an index. Clients should thus not
rely on a given document having the same number between sessions.
@author Doug Cutting
@version $Id$
*/
public abstract class IndexReader {
protected IndexReader(Directory directory) {
@ -104,7 +107,9 @@ public abstract class IndexReader {
/** 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"), IndexWriter.COMMIT_LOCK_TIMEOUT) {
return (IndexReader)new Lock.With(
directory.makeLock("IndexWriter.COMMIT_LOCK_NAME"),
IndexWriter.COMMIT_LOCK_TIMEOUT) {
public Object doBody() throws IOException {
IndexReader result = null;
@ -264,7 +269,7 @@ public abstract class IndexReader {
*/
public final synchronized void delete(int docNum) throws IOException {
if (writeLock == null) {
Lock writeLock = directory.makeLock("write.lock");
Lock writeLock = directory.makeLock("IndexWriter.WRITE_LOCK_NAME");
if (!writeLock.obtain(IndexWriter.WRITE_LOCK_TIMEOUT)) // obtain write lock
throw new IOException("Index locked for write: " + writeLock);
this.writeLock = writeLock;
@ -355,8 +360,8 @@ public abstract class IndexReader {
*/
public static boolean isLocked(Directory directory) throws IOException {
return
directory.makeLock("write.lock").isLocked() ||
directory.makeLock("commit.lock").isLocked();
directory.makeLock("IndexWriter.WRITE_LOCK_NAME").isLocked() ||
directory.makeLock("IndexWriter.COMMIT_LOCK_NAME").isLocked();
}
@ -378,7 +383,7 @@ public abstract class IndexReader {
* currently accessing this index.
*/
public static void unlock(Directory directory) throws IOException {
directory.makeLock("write.lock").release();
directory.makeLock("commit.lock").release();
directory.makeLock("IndexWriter.WRITE_LOCK_NAME").release();
directory.makeLock("IndexWriter.COMMIT_LOCK_NAME").release();
}
}

View File

@ -89,6 +89,9 @@ import org.apache.lucene.analysis.Analyzer;
public class IndexWriter {
public static long WRITE_LOCK_TIMEOUT = 1000;
public static long COMMIT_LOCK_TIMEOUT = 10000;
public static final String WRITE_LOCK_NAME = "write.lock";
public static final String COMMIT_LOCK_NAME = "commit.lock";
private Directory directory; // where this index resides
private Analyzer analyzer; // how to analyze text
@ -166,13 +169,13 @@ public class IndexWriter {
directory = d;
analyzer = a;
Lock writeLock = directory.makeLock("write.lock");
Lock writeLock = directory.makeLock("IndexWriter.WRITE_LOCK_NAME");
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"), COMMIT_LOCK_TIMEOUT) {
new Lock.With(directory.makeLock("COMMIT_LOCK_NAME"), COMMIT_LOCK_TIMEOUT) {
public Object doBody() throws IOException {
if (create)
segmentInfos.write(directory);
@ -395,7 +398,7 @@ public class IndexWriter {
directory));
synchronized (directory) { // in- & inter-process sync
new Lock.With(directory.makeLock("commit.lock"), COMMIT_LOCK_TIMEOUT) {
new Lock.With(directory.makeLock("COMMIT_LOCK_NAME"), COMMIT_LOCK_TIMEOUT) {
public Object doBody() throws IOException {
segmentInfos.write(directory); // commit before deleting
deleteSegments(segmentsToDelete); // delete now-unused segments