it.unimi.di.mg4j.search
Class DocumentIteratorBuilderVisitor

java.lang.Object
  extended by it.unimi.di.mg4j.query.nodes.AbstractQueryBuilderVisitor<DocumentIterator>
      extended by it.unimi.di.mg4j.search.DocumentIteratorBuilderVisitor
All Implemented Interfaces:
QueryBuilderVisitor<DocumentIterator>, FlyweightPrototype<QueryBuilderVisitor<DocumentIterator>>
Direct Known Subclasses:
PrecomputeIndex.ReplacingDocumentIteratorBuilderVisitor

public class DocumentIteratorBuilderVisitor
extends AbstractQueryBuilderVisitor<DocumentIterator>

A QueryBuilderVisitor that builds a DocumentIterator resolving the queries using the objects in it.unimi.di.mg4j.search.

This elementary builder visitor invokes Index.documents(CharSequence) to build the leaf index iterators. Thus, the resulting DocumentIterator should be carefully disposed after usage (every index iterator may open a file or a socket).

Prefix and MultiTerm nodes cause the creation of a MultiTermIndexIterator, in the first case by calling Index.documents(CharSequence,int) and in the second case by creating a MultiTermIndexIterator with the name and frequency equal to the maximum frequency over all terms. Other implementations might choose differently.

At construction time, you must provide a map from strings to indices that will be used to resolve Select nodes. The map may be null, in which case such nodes will cause an IllegalArgumentException. If a Select node contains an index name that does not appear in the map a NoSuchElementException will be thrown instead.

A production site will likely substitute this builder visitor with one that reuses index readers out of a pool.

Instances of this class may be safely reused by calling prepare().


Field Summary
protected  ObjectArrayList<Index> curr
          The stack of selected indices (changed by Select nodes).
protected  Index defaultIndex
          The default index.
protected  Reference2ReferenceMap<Index,Object> index2Parser
          A map associating an object with a parse(String) method to each payload-based index.
protected  Object2ReferenceMap<String,Index> indexMap
          A map associating a textual key to indices.
protected  int limit
          The limit on prefix queries provided in the constructor.
protected  int numberOfDocuments
          The number of documents (fetched from the default index).
protected  double weight
          The last seen, but still not consumed, weight, or Double.NaN.
protected  DoubleArrayList weights
          The stack of weights.
 
Constructor Summary
DocumentIteratorBuilderVisitor(Object2ReferenceMap<String,Index> indexMap, Index defaultIndex, int limit)
          Creates a new builder visitor.
DocumentIteratorBuilderVisitor(Object2ReferenceMap<String,Index> indexMap, Reference2ReferenceMap<Index,Object> index2Parser, Index defaultIndex, int limit)
          Creates a new builder visitor with additional parsers for payload-based indices.
 
Method Summary
 DocumentIteratorBuilderVisitor copy()
           
 DocumentIterator[] newArray(int len)
          Builds an array of given length of type T.
 DocumentIteratorBuilderVisitor prepare()
          No-op.
protected  void pushWeight()
          Pushes weight, if it is not Double.NaN, or 1, otherwise, on the stack of weights; in either case, sets weight to Double.NaN.
 DocumentIterator visit(False node)
          Visits False.
 DocumentIterator visit(Prefix node)
          Visits a Prefix.
 DocumentIterator visit(Range node)
          Visits a Range.
 DocumentIterator visit(Term node)
          Visits a Term.
 DocumentIterator visit(True node)
          Visits True.
 DocumentIterator visitPost(Align node, DocumentIterator[] subNode)
          Visits an Align node after recursing into the corresponding subtree.
 DocumentIterator visitPost(And node, DocumentIterator[] subNode)
          Visits an And node after recursing into the corresponding subtree.
 DocumentIterator visitPost(Consecutive node, DocumentIterator[] subNode)
          Visits a Consecutive node after recursing into the corresponding subtree.
 DocumentIterator visitPost(Difference node, DocumentIterator[] subNode)
          Visits an Difference node after recursing into the corresponding subtree.
 DocumentIterator visitPost(LowPass node, DocumentIterator subNode)
          Visits a LowPass node after recursing into the corresponding subtree.
 DocumentIterator visitPost(MultiTerm node, DocumentIterator[] subNode)
          Visits a MultiTerm node after recursing into the corresponding subtree.
 DocumentIterator visitPost(Not node, DocumentIterator subNode)
          Visits a Not node after recursing into the corresponding subtree.
 DocumentIterator visitPost(OrderedAnd node, DocumentIterator[] subNode)
          Visits an OrderedAnd node after recursing into the corresponding subtree.
 DocumentIterator visitPost(Or node, DocumentIterator[] subNode)
          Visits an Or node after recursing into the corresponding subtree.
 DocumentIterator visitPost(Remap node, DocumentIterator subNode)
          Visits a Remap node after recursing into the corresponding subtree.
 DocumentIterator visitPost(Select node, DocumentIterator subNode)
          Visits a Select node after recursing into the corresponding subtree.
 DocumentIterator visitPost(Weight node, DocumentIterator subNode)
          Visits a Weight node after recursing into the corresponding subtree.
 boolean visitPre(Align node)
          Visits an Align node before recursing into the corresponding subtree.
 boolean visitPre(And node)
          Visits an And node before recursing into the corresponding subtree.
 boolean visitPre(Consecutive node)
          Visits a Consecutive node before recursing into the corresponding subtree.
 boolean visitPre(Difference node)
          Visits an Difference node before recursing into the corresponding subtree.
 boolean visitPre(LowPass node)
          Visits a LowPass node before recursing into the corresponding subtree.
 boolean visitPre(MultiTerm node)
          Visits a MultiTerm node before recursing into the corresponding subtree.
 boolean visitPre(Not node)
          Visits a Not node before recursing into the corresponding subtree.
 boolean visitPre(Or node)
          Visits an Or node before recursing into the corresponding subtree.
 boolean visitPre(OrderedAnd node)
          Visits an OrderedAnd node before recursing into the corresponding subtree.
 boolean visitPre(Remap node)
          Visits a Remap node before recursing into the corresponding subtree.
 boolean visitPre(Select node)
          Visits a Select node before recursing into the corresponding subtree.
 boolean visitPre(Weight node)
          Visits a Weight node before recursing into the corresponding subtree.
protected  double weight()
          Returns weight, if it is not Double.NaN, or 1, otherwise; in either case, sets weight to Double.NaN.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

indexMap

protected final Object2ReferenceMap<String,Index> indexMap
A map associating a textual key to indices.


index2Parser

protected final Reference2ReferenceMap<Index,Object> index2Parser
A map associating an object with a parse(String) method to each payload-based index.


defaultIndex

protected final Index defaultIndex
The default index.


numberOfDocuments

protected final int numberOfDocuments
The number of documents (fetched from the default index).


limit

protected final int limit
The limit on prefix queries provided in the constructor.


curr

protected ObjectArrayList<Index> curr
The stack of selected indices (changed by Select nodes).


weights

protected DoubleArrayList weights
The stack of weights.


weight

protected double weight
The last seen, but still not consumed, weight, or Double.NaN.

Constructor Detail

DocumentIteratorBuilderVisitor

public DocumentIteratorBuilderVisitor(Object2ReferenceMap<String,Index> indexMap,
                                      Index defaultIndex,
                                      int limit)
Creates a new builder visitor.

Parameters:
indexMap - a map from index names to indices, to be used in Select nodes, or null if the only used index is the default index.
defaultIndex - the default index.
limit - a limit that will be used with Prefix nodes.

DocumentIteratorBuilderVisitor

public DocumentIteratorBuilderVisitor(Object2ReferenceMap<String,Index> indexMap,
                                      Reference2ReferenceMap<Index,Object> index2Parser,
                                      Index defaultIndex,
                                      int limit)
Creates a new builder visitor with additional parsers for payload-based indices.

Parameters:
indexMap - a map from index names to indices, to be used in Select nodes, or null if the only used index is the default index.
defaultIndex - the default index.
limit - a limit that will be used with Prefix nodes.
Method Detail

pushWeight

protected void pushWeight()
Pushes weight, if it is not Double.NaN, or 1, otherwise, on the stack of weights; in either case, sets weight to Double.NaN.


weight

protected double weight()
Returns weight, if it is not Double.NaN, or 1, otherwise; in either case, sets weight to Double.NaN.

Returns:
weight, if it is not Double.NaN, or 1, otherwise.

copy

public DocumentIteratorBuilderVisitor copy()

prepare

public DocumentIteratorBuilderVisitor prepare()
Description copied from class: AbstractQueryBuilderVisitor
No-op.

Specified by:
prepare in interface QueryBuilderVisitor<DocumentIterator>
Overrides:
prepare in class AbstractQueryBuilderVisitor<DocumentIterator>
Returns:
this visitor.

newArray

public DocumentIterator[] newArray(int len)
Description copied from interface: QueryBuilderVisitor
Builds an array of given length of type T.

Because of erasure, generic classes in Java cannot allocate arrays of generic types. This impossibility can be a problem if for some reason the visitPost() methods expect an actual array of type T. This method must provide an array of given length that is an acceptable input for all visitPost() methods.

Note that by declaring an implementing class of this interface that has a sole constructor accepting an argument of type Class<T>, you will force the user to provide the class of the generic type, opening the way for the reflective methods in Array.

Parameters:
len - the required array length.
Returns:
an array of type T of length len.

visit

public DocumentIterator visit(Term node)
                       throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits a Term.

Parameters:
node - the leaf to be visited.
Returns:
true if the visit should continue.
Throws:
QueryBuilderVisitorException

visit

public DocumentIterator visit(Prefix node)
                       throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits a Prefix.

Parameters:
node - the leaf to be visited.
Returns:
an appropriate return value (usually, the object built using the elements in subNodeResult) if the visit should continue, or null.
Throws:
QueryBuilderVisitorException

visit

public DocumentIterator visit(Range node)
                       throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits a Range.

Parameters:
node - the leaf to be visited.
Returns:
true if the visit should continue.
Throws:
QueryBuilderVisitorException

visitPre

public boolean visitPre(And node)
                 throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits an And node before recursing into the corresponding subtree.

Specified by:
visitPre in interface QueryBuilderVisitor<DocumentIterator>
Overrides:
visitPre in class AbstractQueryBuilderVisitor<DocumentIterator>
Parameters:
node - the node to be visited.
Returns:
true if the visit should continue.
Throws:
QueryBuilderVisitorException

visitPost

public DocumentIterator visitPost(And node,
                                  DocumentIterator[] subNode)
                           throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits an And node after recursing into the corresponding subtree.

Parameters:
node - the internal node to be visited.
subNode - the array of results returned by subnodes.
Returns:
an appropriate return value (usually, the object built using the elements in subNodeResult) if the visit should continue, or null.
Throws:
QueryBuilderVisitorException

visitPre

public boolean visitPre(Consecutive node)
                 throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits a Consecutive node before recursing into the corresponding subtree.

Specified by:
visitPre in interface QueryBuilderVisitor<DocumentIterator>
Overrides:
visitPre in class AbstractQueryBuilderVisitor<DocumentIterator>
Parameters:
node - the node to be visited.
Returns:
true if the visit should continue.
Throws:
QueryBuilderVisitorException

visitPost

public DocumentIterator visitPost(Consecutive node,
                                  DocumentIterator[] subNode)
                           throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits a Consecutive node after recursing into the corresponding subtree.

Parameters:
node - the internal node to be visited.
subNode - the array of results returned by subnodes.
Returns:
an appropriate return value (usually, the object built using the elements in subNodeResult) if the visit should continue, or null.
Throws:
QueryBuilderVisitorException

visitPre

public boolean visitPre(LowPass node)
                 throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits a LowPass node before recursing into the corresponding subtree.

Specified by:
visitPre in interface QueryBuilderVisitor<DocumentIterator>
Overrides:
visitPre in class AbstractQueryBuilderVisitor<DocumentIterator>
Parameters:
node - the node to be visited.
Returns:
true if the visit should continue.
Throws:
QueryBuilderVisitorException

visitPost

public DocumentIterator visitPost(LowPass node,
                                  DocumentIterator subNode)
Description copied from interface: QueryBuilderVisitor
Visits a LowPass node after recursing into the corresponding subtree.

Parameters:
node - the internal node to be visited.
subNode - the of result returned by the sole subnode.
Returns:
an appropriate return value (usually, the object built using subNodeResult) if the visit should continue, or null.

visitPre

public boolean visitPre(Not node)
                 throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits a Not node before recursing into the corresponding subtree.

Specified by:
visitPre in interface QueryBuilderVisitor<DocumentIterator>
Overrides:
visitPre in class AbstractQueryBuilderVisitor<DocumentIterator>
Parameters:
node - the node to be visited.
Returns:
true if the visit should continue.
Throws:
QueryBuilderVisitorException

visitPost

public DocumentIterator visitPost(Not node,
                                  DocumentIterator subNode)
                           throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits a Not node after recursing into the corresponding subtree.

Parameters:
node - the internal node to be visited.
subNode - the of result returned by the sole subnode.
Returns:
an appropriate return value (usually, the object built using subNodeResult) if the visit should continue, or null.
Throws:
QueryBuilderVisitorException

visitPre

public boolean visitPre(Or node)
                 throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits an Or node before recursing into the corresponding subtree.

Specified by:
visitPre in interface QueryBuilderVisitor<DocumentIterator>
Overrides:
visitPre in class AbstractQueryBuilderVisitor<DocumentIterator>
Parameters:
node - the node to be visited.
Returns:
true if the visit should continue.
Throws:
QueryBuilderVisitorException

visitPost

public DocumentIterator visitPost(Or node,
                                  DocumentIterator[] subNode)
                           throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits an Or node after recursing into the corresponding subtree.

Parameters:
node - the internal node to be visited.
Returns:
an appropriate return value (usually, the object built using the elements in subNodeResult) if the visit should continue, or null.
Throws:
QueryBuilderVisitorException

visitPre

public boolean visitPre(OrderedAnd node)
                 throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits an OrderedAnd node before recursing into the corresponding subtree.

Specified by:
visitPre in interface QueryBuilderVisitor<DocumentIterator>
Overrides:
visitPre in class AbstractQueryBuilderVisitor<DocumentIterator>
Parameters:
node - the node to be visited.
Returns:
true if the visit should continue.
Throws:
QueryBuilderVisitorException

visitPost

public DocumentIterator visitPost(OrderedAnd node,
                                  DocumentIterator[] subNode)
                           throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits an OrderedAnd node after recursing into the corresponding subtree.

Parameters:
node - the internal node to be visited.
subNode - the array of results returned by subnodes.
Returns:
an appropriate return value (usually, the object built using the elements in subNodeResult) if the visit should continue, or null.
Throws:
QueryBuilderVisitorException

visitPre

public boolean visitPre(Align node)
                 throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits an Align node before recursing into the corresponding subtree.

Specified by:
visitPre in interface QueryBuilderVisitor<DocumentIterator>
Overrides:
visitPre in class AbstractQueryBuilderVisitor<DocumentIterator>
Parameters:
node - the node to be visited.
Returns:
true if the visit should continue.
Throws:
QueryBuilderVisitorException

visitPost

public DocumentIterator visitPost(Align node,
                                  DocumentIterator[] subNode)
                           throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits an Align node after recursing into the corresponding subtree.

Parameters:
node - the internal node to be visited.
subNode - the array of results returned by subnodes.
Returns:
an appropriate return value (usually, the object built using the elements in subNodeResult) if the visit should continue, or null.
Throws:
QueryBuilderVisitorException

visitPre

public boolean visitPre(Difference node)
                 throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits an Difference node before recursing into the corresponding subtree.

Specified by:
visitPre in interface QueryBuilderVisitor<DocumentIterator>
Overrides:
visitPre in class AbstractQueryBuilderVisitor<DocumentIterator>
Parameters:
node - the node to be visited.
Returns:
true if the visit should continue.
Throws:
QueryBuilderVisitorException

visitPost

public DocumentIterator visitPost(Difference node,
                                  DocumentIterator[] subNode)
Description copied from interface: QueryBuilderVisitor
Visits an Difference node after recursing into the corresponding subtree.

Parameters:
node - the internal node to be visited.
subNode - the array of results returned by subnodes.
Returns:
an appropriate return value (usually, the object built using the elements in subNodeResult) if the visit should continue, or null.

visitPre

public boolean visitPre(MultiTerm node)
                 throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits a MultiTerm node before recursing into the corresponding subtree.

Specified by:
visitPre in interface QueryBuilderVisitor<DocumentIterator>
Overrides:
visitPre in class AbstractQueryBuilderVisitor<DocumentIterator>
Parameters:
node - the node to be visited.
Returns:
true if the visit should continue.
Throws:
QueryBuilderVisitorException

visitPost

public DocumentIterator visitPost(MultiTerm node,
                                  DocumentIterator[] subNode)
                           throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits a MultiTerm node after recursing into the corresponding subtree.

Parameters:
node - the internal node to be visited.
subNode - the of result returned by the sole subnode.
Returns:
an appropriate return value (usually, the object built using subNodeResult) if the visit should continue, or null.
Throws:
QueryBuilderVisitorException

visitPre

public boolean visitPre(Select node)
                 throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits a Select node before recursing into the corresponding subtree.

Specified by:
visitPre in interface QueryBuilderVisitor<DocumentIterator>
Overrides:
visitPre in class AbstractQueryBuilderVisitor<DocumentIterator>
Parameters:
node - the node to be visited.
Returns:
true if the visit should continue.
Throws:
QueryBuilderVisitorException

visitPost

public DocumentIterator visitPost(Select node,
                                  DocumentIterator subNode)
Description copied from interface: QueryBuilderVisitor
Visits a Select node after recursing into the corresponding subtree.

Parameters:
node - the internal node to be visited.
subNode - the of result returned by the sole subnode.
Returns:
an appropriate return value (usually, the object built using subNodeResult) if the visit should continue, or null.

visitPre

public boolean visitPre(Remap node)
                 throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits a Remap node before recursing into the corresponding subtree.

Specified by:
visitPre in interface QueryBuilderVisitor<DocumentIterator>
Overrides:
visitPre in class AbstractQueryBuilderVisitor<DocumentIterator>
Parameters:
node - the node to be visited.
Returns:
true if the visit should continue.
Throws:
QueryBuilderVisitorException

visitPost

public DocumentIterator visitPost(Remap node,
                                  DocumentIterator subNode)
Description copied from interface: QueryBuilderVisitor
Visits a Remap node after recursing into the corresponding subtree.

Parameters:
node - the internal node to be visited.
subNode - the of result returned by the sole subnode.
Returns:
an appropriate return value (usually, the object built using subNodeResult) if the visit should continue, or null.

visitPre

public boolean visitPre(Weight node)
                 throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits a Weight node before recursing into the corresponding subtree.

Specified by:
visitPre in interface QueryBuilderVisitor<DocumentIterator>
Overrides:
visitPre in class AbstractQueryBuilderVisitor<DocumentIterator>
Parameters:
node - the node to be visited.
Returns:
true if the visit should continue.
Throws:
QueryBuilderVisitorException

visitPost

public DocumentIterator visitPost(Weight node,
                                  DocumentIterator subNode)
Description copied from interface: QueryBuilderVisitor
Visits a Weight node after recursing into the corresponding subtree.

Parameters:
node - the internal node to be visited.
subNode - the of result returned by the sole subnode.
Returns:
an appropriate return value (usually, the object built using subNodeResult) if the visit should continue, or null.

visit

public DocumentIterator visit(True node)
                       throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits True.

Parameters:
node - the leaf to be visited.
Returns:
true if the visit should continue.
Throws:
QueryBuilderVisitorException

visit

public DocumentIterator visit(False node)
                       throws QueryBuilderVisitorException
Description copied from interface: QueryBuilderVisitor
Visits False.

Parameters:
node - the leaf to be visited.
Returns:
true if the visit should continue.
Throws:
QueryBuilderVisitorException