lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sh...@apache.org
Subject svn commit: r1201677 - /lucene/dev/branches/branch_3x/lucene/contrib/facet/src/test/org/apache/lucene/facet/FacetTestBase.java
Date Mon, 14 Nov 2011 12:15:57 GMT
Author: shaie
Date: Mon Nov 14 12:15:57 2011
New Revision: 1201677

URL: http://svn.apache.org/viewvc?rev=1201677&view=rev
Log:
LUCENE-3269: speed up facet tests by creating indexing once per testcase

Modified:
    lucene/dev/branches/branch_3x/lucene/contrib/facet/src/test/org/apache/lucene/facet/FacetTestBase.java

Modified: lucene/dev/branches/branch_3x/lucene/contrib/facet/src/test/org/apache/lucene/facet/FacetTestBase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/contrib/facet/src/test/org/apache/lucene/facet/FacetTestBase.java?rev=1201677&r1=1201676&r2=1201677&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/contrib/facet/src/test/org/apache/lucene/facet/FacetTestBase.java
(original)
+++ lucene/dev/branches/branch_3x/lucene/contrib/facet/src/test/org/apache/lucene/facet/FacetTestBase.java
Mon Nov 14 12:15:57 2011
@@ -28,6 +28,7 @@ import org.apache.lucene.index.TermEnum;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.store.Directory;
 
+import org.apache.lucene.util.IOUtils;
 import org.apache.lucene.util.LuceneTestCase;
 import org.apache.lucene.util._TestUtil;
 import org.apache.lucene.facet.index.CategoryDocumentBuilder;
@@ -43,6 +44,8 @@ import org.apache.lucene.facet.taxonomy.
 import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
 
 /**
  * Licensed to the Apache Software Foundation (ASF) under one or more
@@ -64,14 +67,17 @@ import org.apache.lucene.facet.taxonomy.
 /** Base faceted search test. */
 public abstract class FacetTestBase extends LuceneTestCase {
   
-  /** Documents text field. */
-  protected static final String CONTENT_FIELD = "content";
+  /** Holds a search and taxonomy Directories pair. */
+  private static final class SearchTaxoDirPair {
+    Directory searchDir, taxoDir;
+    SearchTaxoDirPair() {}
+  }
   
-  /** Directory for the index */
-  protected Directory indexDir;
+  private static HashMap<Integer, SearchTaxoDirPair> dirsPerPartitionSize;
+  private static File TEST_DIR;
   
-  /** Directory for the taxonomy */
-  protected Directory taxoDir;
+  /** Documents text field. */
+  protected static final String CONTENT_FIELD = "content";
   
   /** taxonomy Reader for the test. */
   protected TaxonomyReader taxoReader;
@@ -82,6 +88,19 @@ public abstract class FacetTestBase exte
   /** Searcher for the test. */
   protected IndexSearcher searcher;
   
+  @BeforeClass
+  public static void beforeClassFacetTestBase() throws Exception {
+    TEST_DIR = _TestUtil.getTempDir("facets");
+    dirsPerPartitionSize = new HashMap<Integer, FacetTestBase.SearchTaxoDirPair>();

+  }
+  
+  @AfterClass
+  public static void afterClassFacetTestBase() throws Exception {
+    for (SearchTaxoDirPair pair : dirsPerPartitionSize.values()) {
+      IOUtils.close(pair.searchDir, pair.taxoDir);
+    }
+  }
+  
   /** documents text (for the text field). */
   private static final String[] DEFAULT_CONTENT = {
       "the white car is the one I want.",
@@ -120,34 +139,39 @@ public abstract class FacetTestBase exte
   }
 
   /** Prepare index (in RAM/Disk) with some documents and some facets */
-  protected final void initIndex(int partitionSize, boolean onDisk) throws Exception {
+  protected final void initIndex(int partitionSize, boolean forceDisk) throws Exception {
     if (VERBOSE) {
-      System.out.println("Partition Size: " + partitionSize+"  onDisk: "+onDisk);
+      System.out.println("Partition Size: " + partitionSize+"  forceDisk: "+forceDisk);
     }
 
-    if (onDisk) {
-      File indexFile = _TestUtil.getTempDir("index");
-      indexDir = newFSDirectory(indexFile);
-      taxoDir = newFSDirectory(new File(indexFile,"facets"));
-    } else { 
-      indexDir = newDirectory();
-      taxoDir = newDirectory();
+    SearchTaxoDirPair pair = dirsPerPartitionSize.get(Integer.valueOf(partitionSize));
+    if (pair == null) {
+      pair = new SearchTaxoDirPair();
+      if (forceDisk) {
+        pair.searchDir = newFSDirectory(new File(TEST_DIR, "index"));
+        pair.taxoDir = newFSDirectory(new File(TEST_DIR, "taxo"));
+      } else {
+        pair.searchDir = newDirectory();
+        pair.taxoDir = newDirectory();
+      }
+      
+      RandomIndexWriter iw = new RandomIndexWriter(random, pair.searchDir, getIndexWriterConfig(getAnalyzer()));
+      TaxonomyWriter taxo = new DirectoryTaxonomyWriter(pair.taxoDir, OpenMode.CREATE);
+      
+      populateIndex(iw, taxo, getFacetIndexingParams(partitionSize));
+      
+      // commit changes (taxonomy prior to search index for consistency)
+      taxo.commit();
+      iw.commit();
+      taxo.close();
+      iw.close();
+      
+      dirsPerPartitionSize.put(Integer.valueOf(partitionSize), pair);
     }
     
-    RandomIndexWriter iw = new RandomIndexWriter(random, indexDir, getIndexWriterConfig(getAnalyzer()));
-    TaxonomyWriter taxo = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);
-    
-    populateIndex(iw, taxo, getFacetIndexingParams(partitionSize));
-    
-    // commit changes (taxonomy prior to search index for consistency)
-    taxo.commit();
-    iw.commit();
-    taxo.close();
-    iw.close();
-    
     // prepare for searching
-    taxoReader = new DirectoryTaxonomyReader(taxoDir);
-    indexReader = IndexReader.open(indexDir);
+    taxoReader = new DirectoryTaxonomyReader(pair.taxoDir);
+    indexReader = IndexReader.open(pair.searchDir);
     searcher = newSearcher(indexReader);
   }
   
@@ -205,16 +229,10 @@ public abstract class FacetTestBase exte
   /** Close all indexes */
   protected void closeAll() throws Exception {
     // close and nullify everything
-    taxoReader.close();
+    IOUtils.close(taxoReader, indexReader, searcher);
     taxoReader = null;
-    indexReader.close();
     indexReader = null;
-    searcher.close();
     searcher = null;
-    indexDir.close();
-    indexDir = null;
-    taxoDir.close();
-    taxoDir = null;
   }
   
   /**



Mime
View raw message