You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by ot...@apache.org on 2003/01/04 18:13:40 UTC

cvs commit: jakarta-lucene/src/test/org/apache/lucene/index TestIndexReader.java

otis        2003/01/04 09:13:40

  Modified:    src/java/org/apache/lucene/index IndexReader.java
                        SegmentReader.java SegmentsReader.java
  Added:       src/test/org/apache/lucene/index TestIndexReader.java
  Log:
  - Applied patches from http://nagoya.apache.org/bugzilla/show_bug.cgi?id=14900
    All unit tests still pass.
  PR:            14900
  Submitted by:  Peter Mularien
  Reviewed by:   Otis
  
  Revision  Changes    Path
  1.12      +10 -0     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.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- IndexReader.java	7 Nov 2002 05:55:39 -0000	1.11
  +++ IndexReader.java	4 Jan 2003 17:13:39 -0000	1.12
  @@ -56,6 +56,8 @@
   
   import java.io.IOException;
   import java.io.File;
  +import java.util.Collection;
  +
   import org.apache.lucene.store.Directory;
   import org.apache.lucene.store.FSDirectory;
   import org.apache.lucene.store.Lock;
  @@ -301,6 +303,14 @@
         writeLock = null;
       }
     }
  +
  +    /**
  +     * Return a list of all unique field names which exist in the index pointed to by
  +     * this IndexReader.
  +     * @return Collection of Strings indicating the names of the fields
  +     * @throws IOException if there is a problem with accessing the index
  +     */
  +    public abstract Collection getFieldNames() throws IOException;
   
     /**
      * Returns <code>true</code> iff the index in the named directory is
  
  
  
  1.7       +20 -7     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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- SegmentReader.java	7 Nov 2002 05:55:39 -0000	1.6
  +++ SegmentReader.java	4 Jan 2003 17:13:39 -0000	1.7
  @@ -55,15 +55,17 @@
    */
   
   import java.io.IOException;
  -import java.util.Hashtable;
  +import java.util.Collection;
   import java.util.Enumeration;
  +import java.util.HashSet;
  +import java.util.Hashtable;
  +import java.util.Set;
   import java.util.Vector;
   
  -import org.apache.lucene.util.BitVector;
  -import org.apache.lucene.store.Directory;
  -import org.apache.lucene.store.Lock;
  -import org.apache.lucene.store.InputStream;
   import org.apache.lucene.document.Document;
  +import org.apache.lucene.store.InputStream;
  +import org.apache.lucene.store.Lock;
  +import org.apache.lucene.util.BitVector;
   
   final class SegmentReader extends IndexReader {
     private boolean closeDirectory = false;
  @@ -73,7 +75,7 @@
     private FieldsReader fieldsReader;
   
     TermInfosReader tis;
  -  
  +
     BitVector deletedDocs = null;
     private boolean deletedDocsDirty = false;
   
  @@ -113,7 +115,7 @@
       proxStream = directory.openFile(segment + ".prx");
       openNorms();
     }
  -  
  +
     final synchronized void doClose() throws IOException {
       if (deletedDocsDirty) {
         synchronized (directory) {		  // in- & inter-process sync
  @@ -271,4 +273,15 @@
         }
       }
     }
  +
  +    // javadoc inherited
  +    public Collection getFieldNames() throws IOException {
  +        // maintain a unique set of field names
  +        Set fieldSet = new HashSet();
  +        for (int i = 0; i < fieldInfos.size(); i++) {
  +            FieldInfo fi = fieldInfos.fieldInfo(i);
  +            fieldSet.add(fi.name);
  +        }
  +        return fieldSet;
  +    }
   }
  
  
  
  1.10      +22 -2     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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- SegmentsReader.java	7 Nov 2002 05:55:39 -0000	1.9
  +++ SegmentsReader.java	4 Jan 2003 17:13:39 -0000	1.10
  @@ -55,10 +55,14 @@
    */
   
   import java.io.IOException;
  +import java.util.Collection;
  +import java.util.HashSet;
   import java.util.Hashtable;
  +import java.util.Iterator;
  +import java.util.Set;
   
  -import org.apache.lucene.store.Directory;
   import org.apache.lucene.document.Document;
  +import org.apache.lucene.store.Directory;
   
   /**
    * FIXME: Describe class <code>SegmentsReader</code> here.
  @@ -174,6 +178,22 @@
       for (int i = 0; i < readers.length; i++)
         readers[i].close();
     }
  +
  +    // javadoc inherited
  +    public Collection getFieldNames() throws IOException {
  +        // maintain a unique set of field names
  +        Set fieldSet = new HashSet();
  +        for (int i = 0; i < readers.length; i++) {
  +            SegmentReader reader = readers[i];
  +            Collection names = reader.getFieldNames();
  +            // iterate through the field names and add them to the set
  +            for (Iterator iterator = names.iterator(); iterator.hasNext();) {
  +                String s = (String) iterator.next();
  +                fieldSet.add(s);
  +            }
  +        }
  +        return fieldSet;
  +    }
   }
   
   class SegmentsTermEnum extends TermEnum {
  
  
  
  1.1                  jakarta-lucene/src/test/org/apache/lucene/index/TestIndexReader.java
  
  Index: TestIndexReader.java
  ===================================================================
  package org.apache.lucene.index;
  
  import junit.framework.TestCase;
  import org.apache.lucene.store.RAMDirectory;
  import org.apache.lucene.analysis.standard.StandardAnalyzer;
  import org.apache.lucene.document.Document;
  import org.apache.lucene.document.Field;
  
  import java.util.Collection;
  import java.io.IOException;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Lucene" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Lucene", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  public class TestIndexReader extends TestCase
  {
      /**
       * Tests the IndexReader.getFieldNames implementation
       * @throws Exception on error
       */
      public void testGetFieldNames() throws Exception
      {
          RAMDirectory d = new RAMDirectory();
          // set up writer
          IndexWriter writer = new IndexWriter(d, new StandardAnalyzer(), true);
          addDocumentWithFields(writer);
          writer.close();
          // set up reader
          IndexReader reader = IndexReader.open(d);
          Collection fieldNames = reader.getFieldNames();
          assertTrue(fieldNames.contains("keyword"));
          assertTrue(fieldNames.contains("text"));
          assertTrue(fieldNames.contains("unindexed"));
          assertTrue(fieldNames.contains("unstored"));
          // add more documents
          writer = new IndexWriter(d, new StandardAnalyzer(), false);
          // want to get some more segments here
          for (int i=0;i<5*writer.mergeFactor;i++)
          {
              addDocumentWithFields(writer);
          }
          // new fields are in some different segments (we hope)
          for (int i=0;i<5*writer.mergeFactor;i++)
          {
              addDocumentWithDifferentFields(writer);
          }
          writer.close();
          // verify fields again
          reader = IndexReader.open(d);
          fieldNames = reader.getFieldNames();
          assertTrue(fieldNames.contains("keyword"));
          assertTrue(fieldNames.contains("text"));
          assertTrue(fieldNames.contains("unindexed"));
          assertTrue(fieldNames.contains("unstored"));
          assertTrue(fieldNames.contains("keyword2"));
          assertTrue(fieldNames.contains("text2"));
          assertTrue(fieldNames.contains("unindexed2"));
          assertTrue(fieldNames.contains("unstored2"));
      }
  
      private void addDocumentWithFields(IndexWriter writer) throws IOException
      {
          Document doc = new Document();
          doc.add(Field.Keyword("keyword","test1"));
          doc.add(Field.Text("text","test1"));
          doc.add(Field.UnIndexed("unindexed","test1"));
          doc.add(Field.UnStored("unstored","test1"));
          writer.addDocument(doc);
      }
  
      private void addDocumentWithDifferentFields(IndexWriter writer) throws IOException
      {
          Document doc = new Document();
          doc.add(Field.Keyword("keyword2","test1"));
          doc.add(Field.Text("text2","test1"));
          doc.add(Field.UnIndexed("unindexed2","test1"));
          doc.add(Field.UnStored("unstored2","test1"));
          writer.addDocument(doc);
      }
  }
  
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>