lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gol...@apache.org
Subject cvs commit: jakarta-lucene/src/test/org/apache/lucene/index TestSegmentMerger.java TestDoc.java DocTest.java
Date Tue, 20 Apr 2004 19:33:35 GMT
goller      2004/04/20 12:33:35

  Modified:    src/java/org/apache/lucene/index SegmentMerger.java
                        IndexWriter.java
               src/test/org/apache/lucene/index TestSegmentMerger.java
                        TestDoc.java DocTest.java
  Log:
  Directory in now only closed if implicitly generated from
  File or String. Behavior is identical to IndexReader.
  
  addIndexes(IndexReader[]) no longer closes the readers.
  
  Revision  Changes    Path
  1.11      +18 -13    jakarta-lucene/src/java/org/apache/lucene/index/SegmentMerger.java
  
  Index: SegmentMerger.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/index/SegmentMerger.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- SegmentMerger.java	20 Apr 2004 13:47:58 -0000	1.10
  +++ SegmentMerger.java	20 Apr 2004 19:33:35 -0000	1.11
  @@ -88,25 +88,30 @@
      */
     final int merge() throws IOException {
       int value;
  -    try {
  -      value = mergeFields();
  -      mergeTerms();
  -      mergeNorms();
  +    
  +    value = mergeFields();
  +    mergeTerms();
  +    mergeNorms();
   
  -      if (fieldInfos.hasVectors())
  -        mergeVectors();
  -
  -    } finally {
  -      for (int i = 0; i < readers.size(); i++) {  // close readers
  -        IndexReader reader = (IndexReader) readers.elementAt(i);
  -        reader.close();
  -      }
  -    }
  +    if (fieldInfos.hasVectors())
  +      mergeVectors();
   
       if (useCompoundFile)
         createCompoundFile();
   
       return value;
  +  }
  +  
  +  /**
  +   * close all IndexReaders that have been added.
  +   * Should not be called before merge().
  +   * @throws IOException
  +   */
  +  final void closeReaders() throws IOException {
  +    for (int i = 0; i < readers.size(); i++) {  // close readers
  +      IndexReader reader = (IndexReader) readers.elementAt(i);
  +      reader.close();
  +    }
     }
   
     private final void createCompoundFile()
  
  
  
  1.30      +38 -27    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.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- IndexWriter.java	16 Apr 2004 12:44:43 -0000	1.29
  +++ IndexWriter.java	20 Apr 2004 19:33:35 -0000	1.30
  @@ -117,7 +117,8 @@
      * may also cause file handle problems.
      */
     private boolean useCompoundFile = true;
  -
  +  
  +  private boolean closeDir;
   
     /** Setting to turn on usage of a compound file. When on, multiple files
      *  for each segment are merged into a single file once the segment creation
  @@ -169,7 +170,7 @@
      */
     public IndexWriter(String path, Analyzer a, boolean create)
          throws IOException {
  -    this(FSDirectory.getDirectory(path, create), a, create);
  +    this(FSDirectory.getDirectory(path, create), a, create, true);
     }
   
     /**
  @@ -189,7 +190,7 @@
      */
     public IndexWriter(File path, Analyzer a, boolean create)
          throws IOException {
  -    this(FSDirectory.getDirectory(path, create), a, create);
  +    this(FSDirectory.getDirectory(path, create), a, create, true);
     }
   
     /**
  @@ -207,37 +208,43 @@
      *  if it does not exist, and <code>create</code> is
      *  <code>false</code>
      */
  -  public IndexWriter(Directory d, Analyzer a, final boolean create)
  +  public IndexWriter(Directory d, Analyzer a, boolean create)
          throws IOException {
  -    directory = d;
  -    analyzer = a;
  -
  -    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(IndexWriter.COMMIT_LOCK_NAME), COMMIT_LOCK_TIMEOUT)
{
  -          public Object doBody() throws IOException {
  -            if (create)
  -              segmentInfos.write(directory);
  -            else
  -              segmentInfos.read(directory);
  -            return null;
  -          }
  -        }.run();
  -    }
  +    this(d, a, create, false);
  +  }
  +  
  +  private IndexWriter(Directory d, Analyzer a, final boolean create, boolean closeDir)
  +    throws IOException {
  +      this.closeDir = closeDir;
  +      directory = d;
  +      analyzer = a;
  +
  +      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(IndexWriter.COMMIT_LOCK_NAME), COMMIT_LOCK_TIMEOUT)
{
  +            public Object doBody() throws IOException {
  +              if (create)
  +                segmentInfos.write(directory);
  +              else
  +                segmentInfos.read(directory);
  +              return null;
  +            }
  +          }.run();
  +      }
     }
   
  -  /** Flushes all changes to an index, closes all associated files, and closes
  -    the directory that the index is stored in. */
  +  /** Flushes all changes to an index and closes all associated files. */
     public synchronized void close() throws IOException {
       flushRamSegments();
       ramDirectory.close();
       writeLock.release();                          // release write lock
       writeLock = null;
  -    directory.close();
  +    if(closeDir)
  +      directory.close();
     }
   
     /** Release the write lock, if needed. */
  @@ -379,7 +386,9 @@
     }
   
     /** Merges the provided indexes into this index.
  -   * <p>After this completes, the index is optimized. */
  +   * <p>After this completes, the index is optimized. </p>
  +   * <p>The provided IndexReaders are not closed.</p>
  +   */
     public synchronized void addIndexes(IndexReader[] readers)
       throws IOException {
   
  @@ -490,6 +499,8 @@
             }
           }.run();
       }
  +    
  +    merger.closeReaders();
     }
   
     /* Some operating systems (e.g. Windows) don't permit a file to be deleted
  
  
  
  1.3       +126 -125  jakarta-lucene/src/test/org/apache/lucene/index/TestSegmentMerger.java
  
  Index: TestSegmentMerger.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/test/org/apache/lucene/index/TestSegmentMerger.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestSegmentMerger.java	29 Mar 2004 22:48:06 -0000	1.2
  +++ TestSegmentMerger.java	20 Apr 2004 19:33:35 -0000	1.3
  @@ -1,125 +1,126 @@
  -package org.apache.lucene.index;
  -
  -/**
  - * Copyright 2004 The Apache Software Foundation
  - *
  - * Licensed under the Apache License, Version 2.0 (the "License");
  - * you may not use this file except in compliance with the License.
  - * You may obtain a copy of the License at
  - *
  - *     http://www.apache.org/licenses/LICENSE-2.0
  - *
  - * Unless required by applicable law or agreed to in writing, software
  - * distributed under the License is distributed on an "AS IS" BASIS,
  - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  - * See the License for the specific language governing permissions and
  - * limitations under the License.
  - */
  -
  -import junit.framework.TestCase;
  -import org.apache.lucene.store.Directory;
  -import org.apache.lucene.store.RAMDirectory;
  -import org.apache.lucene.document.Document;
  -
  -import java.io.IOException;
  -import java.util.Collection;
  -
  -public class TestSegmentMerger extends TestCase {
  -  //The variables for the new merged segment
  -  private Directory mergedDir = new RAMDirectory();
  -  private String mergedSegment = "test";
  -  //First segment to be merged
  -  private Directory merge1Dir = new RAMDirectory();
  -  private Document doc1 = new Document();
  -  private String merge1Segment = "test-1";
  -  private SegmentReader reader1 = null;
  -  //Second Segment to be merged
  -  private Directory merge2Dir = new RAMDirectory();
  -  private Document doc2 = new Document();
  -  private String merge2Segment = "test-2";
  -  private SegmentReader reader2 = null;
  -  
  -
  -  public TestSegmentMerger(String s) {
  -    super(s);
  -  }
  -
  -  protected void setUp() {
  -    DocHelper.setupDoc(doc1);
  -    DocHelper.writeDoc(merge1Dir, merge1Segment, doc1);
  -    DocHelper.setupDoc(doc2);
  -    DocHelper.writeDoc(merge2Dir, merge2Segment, doc2);
  -    try {
  -      reader1 = new SegmentReader(new SegmentInfo(merge1Segment, 1, merge1Dir));
  -      reader2 = new SegmentReader(new SegmentInfo(merge2Segment, 1, merge2Dir));
  -    } catch (IOException e) {
  -      e.printStackTrace();                                                      
  -    }
  -
  -  }
  -
  -  protected void tearDown() {
  -
  -  }
  -
  -  public void test() {
  -    assertTrue(mergedDir != null);
  -    assertTrue(merge1Dir != null);
  -    assertTrue(merge2Dir != null);
  -    assertTrue(reader1 != null);
  -    assertTrue(reader2 != null);
  -  }
  -  
  -  public void testMerge() {                             
  -    //System.out.println("----------------TestMerge------------------");
  -    SegmentMerger merger = new SegmentMerger(mergedDir, mergedSegment, false);
  -    merger.add(reader1);
  -    merger.add(reader2);
  -    try {
  -      int docsMerged = merger.merge();
  -      assertTrue(docsMerged == 2);
  -      //Should be able to open a new SegmentReader against the new directory
  -      SegmentReader mergedReader = new SegmentReader(new SegmentInfo(mergedSegment, docsMerged,
mergedDir));
  -      assertTrue(mergedReader != null);
  -      assertTrue(mergedReader.numDocs() == 2);
  -      Document newDoc1 = mergedReader.document(0);
  -      assertTrue(newDoc1 != null);
  -      //There are 2 unstored fields on the document
  -      assertTrue(DocHelper.numFields(newDoc1) == DocHelper.numFields(doc1) - 2);
  -      Document newDoc2 = mergedReader.document(1);
  -      assertTrue(newDoc2 != null);
  -      assertTrue(DocHelper.numFields(newDoc2) == DocHelper.numFields(doc2) - 2);
  -      
  -      TermDocs termDocs = mergedReader.termDocs(new Term(DocHelper.TEXT_FIELD_2_KEY, "field"));
  -      assertTrue(termDocs != null);
  -      assertTrue(termDocs.next() == true);
  -      
  -      Collection stored = mergedReader.getIndexedFieldNames(true);
  -      assertTrue(stored != null);
  -      //System.out.println("stored size: " + stored.size());
  -      assertTrue(stored.size() == 2);
  -      
  -      TermFreqVector vector = mergedReader.getTermFreqVector(0, DocHelper.TEXT_FIELD_2_KEY);
  -      assertTrue(vector != null);
  -      String [] terms = vector.getTerms();
  -      assertTrue(terms != null);
  -      //System.out.println("Terms size: " + terms.length);
  -      assertTrue(terms.length == 3);
  -      int [] freqs = vector.getTermFrequencies();
  -      assertTrue(freqs != null);
  -      //System.out.println("Freqs size: " + freqs.length);
  -      
  -      for (int i = 0; i < terms.length; i++) {
  -        String term = terms[i];
  -        int freq = freqs[i];
  -        //System.out.println("Term: " + term + " Freq: " + freq);
  -        assertTrue(DocHelper.FIELD_2_TEXT.indexOf(term) != -1);
  -        assertTrue(DocHelper.FIELD_2_FREQS[i] == freq);
  -      }                                                
  -    } catch (IOException e) {
  -      e.printStackTrace();
  -      assertTrue(false);
  -    }
  -    //System.out.println("---------------------end TestMerge-------------------");
  -  }    
  -}
  +package org.apache.lucene.index;
  +
  +/**
  + * Copyright 2004 The Apache Software Foundation
  + *
  + * Licensed under the Apache License, Version 2.0 (the "License");
  + * you may not use this file except in compliance with the License.
  + * You may obtain a copy of the License at
  + *
  + *     http://www.apache.org/licenses/LICENSE-2.0
  + *
  + * Unless required by applicable law or agreed to in writing, software
  + * distributed under the License is distributed on an "AS IS" BASIS,
  + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  + * See the License for the specific language governing permissions and
  + * limitations under the License.
  + */
  +
  +import junit.framework.TestCase;
  +import org.apache.lucene.store.Directory;
  +import org.apache.lucene.store.RAMDirectory;
  +import org.apache.lucene.document.Document;
  +
  +import java.io.IOException;
  +import java.util.Collection;
  +
  +public class TestSegmentMerger extends TestCase {
  +  //The variables for the new merged segment
  +  private Directory mergedDir = new RAMDirectory();
  +  private String mergedSegment = "test";
  +  //First segment to be merged
  +  private Directory merge1Dir = new RAMDirectory();
  +  private Document doc1 = new Document();
  +  private String merge1Segment = "test-1";
  +  private SegmentReader reader1 = null;
  +  //Second Segment to be merged
  +  private Directory merge2Dir = new RAMDirectory();
  +  private Document doc2 = new Document();
  +  private String merge2Segment = "test-2";
  +  private SegmentReader reader2 = null;
  +  
  +
  +  public TestSegmentMerger(String s) {
  +    super(s);
  +  }
  +
  +  protected void setUp() {
  +    DocHelper.setupDoc(doc1);
  +    DocHelper.writeDoc(merge1Dir, merge1Segment, doc1);
  +    DocHelper.setupDoc(doc2);
  +    DocHelper.writeDoc(merge2Dir, merge2Segment, doc2);
  +    try {
  +      reader1 = new SegmentReader(new SegmentInfo(merge1Segment, 1, merge1Dir));
  +      reader2 = new SegmentReader(new SegmentInfo(merge2Segment, 1, merge2Dir));
  +    } catch (IOException e) {
  +      e.printStackTrace();                                                      
  +    }
  +
  +  }
  +
  +  protected void tearDown() {
  +
  +  }
  +
  +  public void test() {
  +    assertTrue(mergedDir != null);
  +    assertTrue(merge1Dir != null);
  +    assertTrue(merge2Dir != null);
  +    assertTrue(reader1 != null);
  +    assertTrue(reader2 != null);
  +  }
  +  
  +  public void testMerge() {                             
  +    //System.out.println("----------------TestMerge------------------");
  +    SegmentMerger merger = new SegmentMerger(mergedDir, mergedSegment, false);
  +    merger.add(reader1);
  +    merger.add(reader2);
  +    try {
  +      int docsMerged = merger.merge();
  +      merger.closeReaders();
  +      assertTrue(docsMerged == 2);
  +      //Should be able to open a new SegmentReader against the new directory
  +      SegmentReader mergedReader = new SegmentReader(new SegmentInfo(mergedSegment, docsMerged,
mergedDir));
  +      assertTrue(mergedReader != null);
  +      assertTrue(mergedReader.numDocs() == 2);
  +      Document newDoc1 = mergedReader.document(0);
  +      assertTrue(newDoc1 != null);
  +      //There are 2 unstored fields on the document
  +      assertTrue(DocHelper.numFields(newDoc1) == DocHelper.numFields(doc1) - 2);
  +      Document newDoc2 = mergedReader.document(1);
  +      assertTrue(newDoc2 != null);
  +      assertTrue(DocHelper.numFields(newDoc2) == DocHelper.numFields(doc2) - 2);
  +      
  +      TermDocs termDocs = mergedReader.termDocs(new Term(DocHelper.TEXT_FIELD_2_KEY, "field"));
  +      assertTrue(termDocs != null);
  +      assertTrue(termDocs.next() == true);
  +      
  +      Collection stored = mergedReader.getIndexedFieldNames(true);
  +      assertTrue(stored != null);
  +      //System.out.println("stored size: " + stored.size());
  +      assertTrue(stored.size() == 2);
  +      
  +      TermFreqVector vector = mergedReader.getTermFreqVector(0, DocHelper.TEXT_FIELD_2_KEY);
  +      assertTrue(vector != null);
  +      String [] terms = vector.getTerms();
  +      assertTrue(terms != null);
  +      //System.out.println("Terms size: " + terms.length);
  +      assertTrue(terms.length == 3);
  +      int [] freqs = vector.getTermFrequencies();
  +      assertTrue(freqs != null);
  +      //System.out.println("Freqs size: " + freqs.length);
  +      
  +      for (int i = 0; i < terms.length; i++) {
  +        String term = terms[i];
  +        int freq = freqs[i];
  +        //System.out.println("Term: " + term + " Freq: " + freq);
  +        assertTrue(DocHelper.FIELD_2_TEXT.indexOf(term) != -1);
  +        assertTrue(DocHelper.FIELD_2_FREQS[i] == freq);
  +      }                                                
  +    } catch (IOException e) {
  +      e.printStackTrace();
  +      assertTrue(false);
  +    }
  +    //System.out.println("---------------------end TestMerge-------------------");
  +  }    
  +}
  
  
  
  1.5       +2 -1      jakarta-lucene/src/test/org/apache/lucene/index/TestDoc.java
  
  Index: TestDoc.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/test/org/apache/lucene/index/TestDoc.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestDoc.java	29 Mar 2004 22:48:06 -0000	1.4
  +++ TestDoc.java	20 Apr 2004 19:33:35 -0000	1.5
  @@ -186,6 +186,7 @@
         merger.add(r1);
         merger.add(r2);
         merger.merge();
  +      merger.closeReaders();
   
         directory.close();
      }
  
  
  
  1.8       +1 -0      jakarta-lucene/src/test/org/apache/lucene/index/DocTest.java
  
  Index: DocTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/test/org/apache/lucene/index/DocTest.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- DocTest.java	29 Mar 2004 22:48:06 -0000	1.7
  +++ DocTest.java	20 Apr 2004 19:33:35 -0000	1.8
  @@ -80,6 +80,7 @@
       merger.add(r1);
       merger.add(r2);
       merger.merge();
  +    merger.closeReaders();
   
       directory.close();
     }
  
  
  

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


Mime
View raw message