Revision: 16414
          http://gate.svn.sourceforge.net/gate/?rev=16414&view=rev
Author:   valyt
Date:     2012-12-10 10:54:47 +0000 (Mon, 10 Dec 2012)
Log Message:
-----------
Added synchronous version of getDocumentsCount method in QueryRunner.

Modified Paths:
--------------
    mimir/trunk/mimir-client/src/gate/mimir/search/RemoteQueryRunner.java
    mimir/trunk/mimir-core/src/gate/mimir/search/FederatedQueryRunner.java
    mimir/trunk/mimir-core/src/gate/mimir/search/QueryRunner.java
    mimir/trunk/mimir-core/src/gate/mimir/search/RankingQueryRunnerImpl.java

Modified: mimir/trunk/mimir-client/src/gate/mimir/search/RemoteQueryRunner.java
===================================================================
--- mimir/trunk/mimir-client/src/gate/mimir/search/RemoteQueryRunner.java       
2012-12-10 02:19:06 UTC (rev 16413)
+++ mimir/trunk/mimir-client/src/gate/mimir/search/RemoteQueryRunner.java       
2012-12-10 10:54:47 UTC (rev 16414)
@@ -39,6 +39,7 @@
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.Executor;
+import java.util.concurrent.FutureTask;
 
 import org.apache.log4j.Logger;
 
@@ -164,6 +165,12 @@
    */
   private volatile long currentDocumentsCount;
   
+  /**
+   * The task that's working on collecting all the document IDs. When this 
+   * activity has finished, the precise documents count is known.
+   */
+  private volatile FutureTask<Object> docDataUpdaterFuture;
+  
   private volatile boolean closed;
   
   /**
@@ -236,11 +243,11 @@
     // start the background action
     documentsCount = -1;
     currentDocumentsCount = 0;
-    DocumentDataUpdater docDataUpdater = new DocumentDataUpdater(); 
+    docDataUpdaterFuture = new FutureTask<Object>( new DocumentDataUpdater(), 
null); 
     if(threadSource != null) {
-      threadSource.execute(docDataUpdater);
+      threadSource.execute(docDataUpdaterFuture);
     } else {
-      new Thread(docDataUpdater, 
+      new Thread(docDataUpdaterFuture, 
         DocumentDataUpdater.class.getCanonicalName()).start();
     }    
   }
@@ -306,7 +313,24 @@
     return documentsCount;
   }
 
+  
+  
   /* (non-Javadoc)
+   * @see gate.mimir.search.QueryRunner#getDocumentsCountSync()
+   */
+  @Override
+  public long getDocumentsCountSync() {
+    try{
+      docDataUpdaterFuture.get();
+    } catch(Exception e) {
+      logger.error("Exception while getting all document IDs", e);
+      throw new IllegalStateException(
+        "Exception while getting all document IDs", e);
+    }    
+    return getDocumentsCount();
+  }
+
+  /* (non-Javadoc)
    * @see gate.mimir.search.QueryRunner#getCurrentDocumentsCount()
    */
   @Override

Modified: mimir/trunk/mimir-core/src/gate/mimir/search/FederatedQueryRunner.java
===================================================================
--- mimir/trunk/mimir-core/src/gate/mimir/search/FederatedQueryRunner.java      
2012-12-10 02:19:06 UTC (rev 16413)
+++ mimir/trunk/mimir-core/src/gate/mimir/search/FederatedQueryRunner.java      
2012-12-10 10:54:47 UTC (rev 16414)
@@ -110,7 +110,20 @@
     return documentsCount;
   }
 
+  
+  
   /* (non-Javadoc)
+   * @see gate.mimir.search.QueryRunner#getDocumentsCountSync()
+   */
+  @Override
+  public long getDocumentsCountSync() {
+    for(QueryRunner subRunner : subRunners) {
+      subRunner.getDocumentsCountSync();
+    }
+    return getDocumentsCount();
+  }
+
+  /* (non-Javadoc)
    * @see gate.mimir.search.QueryRunner#getCurrentDocumentsCount()
    */
   @Override

Modified: mimir/trunk/mimir-core/src/gate/mimir/search/QueryRunner.java
===================================================================
--- mimir/trunk/mimir-core/src/gate/mimir/search/QueryRunner.java       
2012-12-10 02:19:06 UTC (rev 16413)
+++ mimir/trunk/mimir-core/src/gate/mimir/search/QueryRunner.java       
2012-12-10 10:54:47 UTC (rev 16414)
@@ -56,6 +56,14 @@
   public long getDocumentsCount();
 
   /**
+   * Synchronous version of {@link #getDocumentsCount()} that waits if 
necessary
+   * before returning the correct result (instead of returning  <code>-1</code>
+   * of the value is not yet known).
+   * @return the total number of documents found to match the query.
+   */
+  public long getDocumentsCountSync();
+  
+  /**
    * Gets the number of result documents found so far. After the search 
    * completes, the result returned by this call is identical to that of 
    * {@link #getDocumentsCount()}. 

Modified: 
mimir/trunk/mimir-core/src/gate/mimir/search/RankingQueryRunnerImpl.java
===================================================================
--- mimir/trunk/mimir-core/src/gate/mimir/search/RankingQueryRunnerImpl.java    
2012-12-10 02:19:06 UTC (rev 16413)
+++ mimir/trunk/mimir-core/src/gate/mimir/search/RankingQueryRunnerImpl.java    
2012-12-10 10:54:47 UTC (rev 16414)
@@ -15,20 +15,14 @@
  */
 package gate.mimir.search;
 
-import gate.mimir.DocumentMetadataHelper;
 import gate.mimir.index.IndexException;
 import gate.mimir.search.query.Binding;
 import gate.mimir.search.query.QueryExecutor;
 import gate.mimir.search.query.QueryNode;
 import gate.mimir.search.score.MimirScorer;
-import it.unimi.dsi.fastutil.doubles.DoubleArrayList;
 import it.unimi.dsi.fastutil.doubles.DoubleBigArrayBigList;
-import it.unimi.dsi.fastutil.ints.IntArrayList;
-import it.unimi.dsi.fastutil.ints.IntList;
-import it.unimi.dsi.fastutil.longs.LongArrayList;
 import it.unimi.dsi.fastutil.longs.LongBigArrayBigList;
 import it.unimi.dsi.fastutil.longs.LongBigList;
-import it.unimi.dsi.fastutil.longs.LongList;
 import it.unimi.dsi.fastutil.objects.Object2ObjectAVLTreeMap;
 import it.unimi.dsi.fastutil.objects.ObjectArrayList;
 import it.unimi.dsi.fastutil.objects.ObjectBigArrayBigList;
@@ -45,6 +39,7 @@
 import java.util.Set;
 import java.util.SortedMap;
 import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.FutureTask;
 import java.util.concurrent.LinkedBlockingQueue;
@@ -324,6 +319,12 @@
   protected volatile boolean allDocIdsCollected = false;
   
   /**
+   * The task that's working on collecting all the document IDs. When this 
+   * activity has finished, the precise documents count is known.
+   */
+  protected volatile FutureTask<Object> docIdCollectorFuture;
+  
+  /**
    * Creates a query runner in ranking mode.
    * @param qNode the {@link QueryNode} for the query being executed.
    * @param scorer the {@link MimirScorer} to use for ranking.
@@ -370,16 +371,14 @@
 
     // queue a job for collecting all document ids
     try {
+      docIdCollectorFuture = new FutureTask<Object>(new DocIdsCollector(), 
null);
+      backgroundTasks.put(docIdCollectorFuture);
       if(!ranking) {
         // if not ranking, the doc IDs collector will all collect the
         // hits for the first docBlockSize number of documents
-        FutureTask<Object> future = new FutureTask<Object>(new 
DocIdsCollector(), null);
         synchronized(hitCollectors) {
-          hitCollectors.put(new long[]{0, docBlockSize}, future);
+          hitCollectors.put(new long[]{0, docBlockSize}, docIdCollectorFuture);
         }
-        backgroundTasks.put(future);
-      } else {
-        backgroundTasks.put(new DocIdsCollector());
       }
     } catch(InterruptedException e) {
       Thread.currentThread().interrupt();
@@ -395,6 +394,24 @@
     if(allDocIdsCollected) return documentIds.size64();
     else return -1;
   }
+  
+  /**
+   * Synchronous version of {@link #getDocumentsCount()} that waits if 
necessary
+   * before returning the correct result (instead of returning  <code>-1</code>
+   * of the value is not yet known).
+   * @return the total number of documents found to match the query.
+   */
+  @Override
+  public long getDocumentsCountSync() {
+    try {
+      docIdCollectorFuture.get();
+    } catch(Exception e) {
+      logger.error("Exception while getting all document IDs", e);
+      throw new IllegalStateException(
+        "Exception while getting all document IDs", e);
+    }
+    return getDocumentsCount();
+  }
 
   /* (non-Javadoc)
    * @see gate.mimir.search.QueryRunner#getCurrentDocumentsCount()

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial
Remotely access PCs and mobile devices and provide instant support
Improve your efficiency, and focus on delivering more value-add services
Discover what IT Professionals Know. Rescue delivers
http://p.sf.net/sfu/logmein_12329d2d
_______________________________________________
GATE-cvs mailing list
GATE-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gate-cvs

Reply via email to