ConstQuery.java

  1. /*
  2.  *  ConstQuery.java
  3.  *
  4.  *  Copyright (c) 2007-2011, The University of Sheffield.
  5.  *
  6.  *  This file is part of GATE Mímir (see http://gate.ac.uk/family/mimir.html),
  7.  *  and is free software, licenced under the GNU Lesser General Public License,
  8.  *  Version 3, June 2007 (also included with this distribution as file
  9.  *  LICENCE-LGPL3.html).
  10.  *
  11.  *  Valentin Tablan, 22 Nov 2012
  12.  *
  13.  *  $Id: ConstQuery.java 16782 2013-08-14 08:40:44Z valyt $
  14.  */
  15. package gate.mimir.search.query;

  16. import gate.mimir.search.QueryEngine;

  17. import it.unimi.di.big.mg4j.index.Index;
  18. import it.unimi.di.big.mg4j.search.visitor.DocumentIteratorVisitor;
  19. import it.unimi.dsi.fastutil.objects.ReferenceArraySet;
  20. import it.unimi.dsi.fastutil.objects.ReferenceSet;

  21. import java.io.IOException;

  22. /**
  23.  * A query that returns a pre-defined (constant) list of document IDs. This
  24.  * query type does not support positions, so it returns no hits.
  25.  * One example usage for this type of query is as an operand to an AND operator
  26.  * in order to restrict the results to a given set of documents.
  27.  */
  28. public class ConstQuery implements QueryNode {
  29.  
  30.   /**
  31.    * Serialization ID.
  32.    */
  33.   private static final long serialVersionUID = 4259330863001338150L;

  34.   /**
  35.    * Executor implementation for {@link ConstQuery}. It returns document IDs
  36.    * from the predefined list, and no positions.
  37.    */
  38.   public static class ConstQueryExecutor extends AbstractQueryExecutor {
  39.    
  40.     public ConstQueryExecutor(ConstQuery qNode, QueryEngine engine) {
  41.       super(engine, qNode);
  42.       this.documentIds = qNode.documentIds;
  43.       latestDocumentPosition = -1;
  44.     }
  45.    
  46.     /**
  47.      * The position in the constant list of document IDs of the latest returned
  48.      * document.
  49.      */
  50.     private int latestDocumentPosition;
  51.    
  52.     /**
  53.      * The predefined list of document IDs.
  54.      */
  55.     private long[] documentIds;
  56.    
  57.     private ReferenceSet<Index> indices;
  58.    
  59.     /* (non-Javadoc)
  60.      * @see gate.mimir.search.query.QueryExecutor#nextDocument(long)
  61.      */
  62.     @Override
  63.     public long nextDocument(long greaterThan) throws IOException {
  64.       do {
  65.         latestDocumentPosition++;
  66.       } while(latestDocumentPosition < documentIds.length &&
  67.               documentIds[latestDocumentPosition] <= greaterThan);
  68.       if(latestDocumentPosition < documentIds.length) {
  69.         latestDocument = documentIds[latestDocumentPosition];
  70.       } else {
  71.         latestDocument = -1;
  72.       }
  73.       return latestDocument;
  74.     }

  75.     /**
  76.      * This query executor type does not support positions, so it always returns
  77.      * null.
  78.      * @see gate.mimir.search.query.QueryExecutor#nextHit()
  79.      */
  80.     @Override
  81.     public Binding nextHit() throws IOException {
  82.       return null;
  83.     }

  84.     /**
  85.      * Returns an empty set of indices.
  86.      * @see it.unimi.di.big.mg4j.search.DocumentIterator#indices()
  87.      */
  88.     @Override
  89.     public ReferenceSet<Index> indices() {
  90.       if(indices == null) {
  91.         indices = new ReferenceArraySet<Index>();
  92.       }
  93.       return indices;
  94.     }

  95.     /**
  96.      * Always returns null.
  97.      * @see it.unimi.di.big.mg4j.search.DocumentIterator#accept(it.unimi.di.big.mg4j.search.visitor.DocumentIteratorVisitor)
  98.      */
  99.     @Override
  100.     public <T> T accept(DocumentIteratorVisitor<T> visitor) throws IOException {
  101.       return null;
  102.     }

  103.     /**
  104.      * Always returns null
  105.      * @see it.unimi.di.big.mg4j.search.DocumentIterator#acceptOnTruePaths(it.unimi.di.big.mg4j.search.visitor.DocumentIteratorVisitor)
  106.      */
  107.     @Override
  108.     public <T> T acceptOnTruePaths(DocumentIteratorVisitor<T> visitor)
  109.       throws IOException {
  110.       return null;
  111.     }
  112.   }
  113.  
  114.   /* (non-Javadoc)
  115.    * @see gate.mimir.search.query.QueryNode#getQueryExecutor(gate.mimir.search.QueryEngine)
  116.    */
  117.   @Override
  118.   public QueryExecutor getQueryExecutor(QueryEngine engine) throws IOException {
  119.     return new ConstQueryExecutor(this, engine);
  120.   }
  121.  
  122.  
  123.   /**
  124.    * Creates a new ConstQuery query.
  125.    * @param documentIds the document IDs (in ascending order) that should be
  126.    * returned when this query is executed.
  127.    */
  128.   public ConstQuery(long[] documentIds) {
  129.     this.documentIds = documentIds;
  130.   }

  131.   private long[] documentIds;

  132.   /**
  133.    * @return the documentIds
  134.    */
  135.   public long[] getDocumentIds() {
  136.     return documentIds;
  137.   }
  138.  
  139. }