lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject cvs commit: jakarta-lucene/src/java/org/apache/lucene/index IndexReader.java IndexWriter.java SegmentReader.java SegmentsReader.java
Date Fri, 08 Feb 2002 19:39:42 GMT
cutting     02/02/08 11:39:42

  Modified:    src/java/org/apache/lucene/index IndexReader.java
                        IndexWriter.java SegmentReader.java
                        SegmentsReader.java
  Log:
  obtain write.lock while deleting documents
  
  Revision  Changes    Path
  1.7       +35 -4     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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- IndexReader.java	21 Jan 2002 17:07:23 -0000	1.6
  +++ IndexReader.java	8 Feb 2002 19:39:42 -0000	1.7
  @@ -75,7 +75,12 @@
     rely on a given document having the same number between sessions. */
   
   abstract public class IndexReader {
  -  protected IndexReader() {}
  +  protected IndexReader(Directory directory) {
  +    this.directory = directory;
  +  }
  +
  +  Directory directory;
  +  private Lock writeLock;
   
     /** Returns an IndexReader reading the index in an FSDirectory in the named
     path. */
  @@ -102,7 +107,7 @@
   	    SegmentReader[] readers = new SegmentReader[infos.size()];
   	    for (int i = 0; i < infos.size(); i++)
   	      readers[i] = new SegmentReader(infos.info(i), i==infos.size()-1);
  -	    return new SegmentsReader(readers);
  +	    return new SegmentsReader(directory, readers);
   	  }
   	}.run();
       }
  @@ -240,7 +245,16 @@
       method will result in an error.  The presence of this document may still be
       reflected in the {@link #docFreq} statistic, though
       this will be corrected eventually as the index is further modified.  */
  -  abstract public void delete(int docNum) throws IOException;
  +  public synchronized final void delete(int docNum) throws IOException {
  +    if (writeLock == null) {
  +      Lock writeLock = directory.makeLock("write.lock");
  +      if (!writeLock.obtain())			  // obtain write lock
  +        throw new IOException("Index locked for write: " + writeLock);
  +      this.writeLock = writeLock;
  +    }
  +    doDelete(docNum);
  +  }
  +  abstract void doDelete(int docNum) throws IOException;
   
     /** Deletes all documents containing <code>term</code>.
       This is useful if one uses a document field to hold a unique ID string for
  @@ -267,7 +281,24 @@
      * Also saves any new deletions to disk.
      * No other methods should be called after this has been called.
      */
  -    abstract public void close() throws IOException;
  +  public final synchronized void close() throws IOException {
  +    doClose();
  +    if (writeLock != null) {
  +      writeLock.release();  // release write lock
  +      writeLock = null;
  +    }
  +  }
  +
  +  /** Implements close. */
  +  abstract void doClose() throws IOException;
  +
  +  /** Release the write lock, if needed. */
  +  protected final void finalize() throws IOException {
  +    if (writeLock != null) {
  +      writeLock.release();                        // release write lock
  +      writeLock = null;
  +    }
  +  }
   
     /**
      * Returns <code>true</code> iff the index in the named directory is
  
  
  
  1.6       +14 -2     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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- IndexWriter.java	29 Jan 2002 16:30:16 -0000	1.5
  +++ IndexWriter.java	8 Feb 2002 19:39:42 -0000	1.6
  @@ -91,6 +91,8 @@
     private SegmentInfos segmentInfos = new SegmentInfos(); // the segments
     private final Directory ramDirectory = new RAMDirectory(); // for temp segs
   
  +  private Lock writeLock;
  +
     /** Constructs an IndexWriter for the index in <code>path</code>.  Text will
       be analyzed with <code>a</code>.  If <code>create</code> is
true, then a
       new, empty index will be created in <code>path</code>, replacing the index
  @@ -119,8 +121,9 @@
       analyzer = a;
   
       Lock writeLock = directory.makeLock("write.lock");
  -    if (!writeLock.obtain())			  // obtain write lock
  +    if (!writeLock.obtain())                      // 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")) {
  @@ -140,8 +143,17 @@
     public final synchronized void close() throws IOException {
       flushRamSegments();
       ramDirectory.close();
  -    directory.makeLock("write.lock").release();  // release write lock
  +    writeLock.release();                          // release write lock
  +    writeLock = null;
       directory.close();
  +  }
  +
  +  /** Release the write lock, if needed. */
  +  protected final void finalize() throws IOException {
  +    if (writeLock != null) {
  +      writeLock.release();                        // release write lock
  +      writeLock = null;
  +    }
     }
   
     /** Returns the number of documents currently in this index. */
  
  
  
  1.4       +3 -4      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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SegmentReader.java	21 Jan 2002 17:07:23 -0000	1.3
  +++ SegmentReader.java	8 Feb 2002 19:39:42 -0000	1.4
  @@ -66,7 +66,6 @@
   import org.apache.lucene.document.Document;
   
   final class SegmentReader extends IndexReader {
  -  Directory directory;
     private boolean closeDirectory = false;
     private String segment;
   
  @@ -97,7 +96,7 @@
   
     SegmentReader(SegmentInfo si)
          throws IOException {
  -    directory = si.dir;
  +    super(si.dir);
       segment = si.name;
   
       fieldInfos = new FieldInfos(directory, segment + ".fnm");
  @@ -115,7 +114,7 @@
       openNorms();
     }
     
  -  public final synchronized void close() throws IOException {
  +  final synchronized void doClose() throws IOException {
       if (deletedDocsDirty) {
         synchronized (directory) {		  // in- & inter-process sync
   	new Lock.With(directory.makeLock("commit.lock")) {
  @@ -147,7 +146,7 @@
       return si.dir.fileExists(si.name + ".del");
     }
   
  -  public final synchronized void delete(int docNum) throws IOException {
  +  final synchronized void doDelete(int docNum) throws IOException {
       if (deletedDocs == null)
         deletedDocs = new BitVector(maxDoc());
       deletedDocsDirty = true;
  
  
  
  1.4       +4 -3      jakarta-lucene/src/java/org/apache/lucene/index/SegmentsReader.java
  
  Index: SegmentsReader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/index/SegmentsReader.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SegmentsReader.java	21 Jan 2002 17:07:23 -0000	1.3
  +++ SegmentsReader.java	8 Feb 2002 19:39:42 -0000	1.4
  @@ -67,7 +67,8 @@
     private int maxDoc = 0;
     private int numDocs = -1;
   
  -  SegmentsReader(SegmentReader[] r) throws IOException {
  +  SegmentsReader(Directory directory, SegmentReader[] r) throws IOException {
  +    super(directory);
       readers = r;
       starts = new int[readers.length + 1];	  // build starts array
       for (int i = 0; i < readers.length; i++) {
  @@ -101,7 +102,7 @@
       return readers[i].isDeleted(n - starts[i]);	  // dispatch to segment reader
     }
   
  -  public synchronized final void delete(int n) throws IOException {
  +  synchronized final void doDelete(int n) throws IOException {
       numDocs = -1;				  // invalidate cache
       int i = readerIndex(n);			  // find segment num
       readers[i].delete(n - starts[i]);		  // dispatch to segment reader
  @@ -159,7 +160,7 @@
       return new SegmentsTermPositions(readers, starts);
     }
   
  -  public final void close() throws IOException {
  +  final synchronized void doClose() throws IOException {
       for (int i = 0; i < readers.length; i++)
         readers[i].close();
     }
  
  
  

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


Mime
View raw message