it.unimi.dsi.mg4j.search
Class DocumentIteratorBuilderVisitor

java.lang.Object
  extended by it.unimi.dsi.mg4j.query.nodes.AbstractQueryBuilderVisitor<DocumentIterator>
      extended by it.unimi.dsi.mg4j.search.DocumentIteratorBuilderVisitor
All Implemented Interfaces:
FlyweightPrototype<QueryBuilderVisitor<DocumentIterator>>, QueryBuilderVisitor<DocumentIterator>

public class DocumentIteratorBuilderVisitor
extends AbstractQueryBuilderVisitor<DocumentIterator>

A QueryBuilderVisitor that builds a DocumentIterator resolving the queries using the objects in it.unimi.dsi.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 callying 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().


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.
 DocumentIterator visit(Prefix node)
          Visits a Prefix.
 DocumentIterator visit(Range node)
          Visits a Range.
 DocumentIterator visit(Term node)
          Visits a Term.
 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(Select node, DocumentIterator subNode)
          Visits a Select node after recursing into the corresponding subtree.
 boolean visitPre(Select node)
          Visits a Select node before recursing into the corresponding subtree.
 
Methods inherited from class it.unimi.dsi.mg4j.query.nodes.AbstractQueryBuilderVisitor
visitPre, visitPre, visitPre, visitPre, visitPre, visitPre, visitPre, visitPre, visitPre
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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

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 subNode) 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

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 subNode) if the visit should continue, or null.
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 subNode) if the visit should continue, or null.
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 subNode) if the visit should continue, or null.

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 subNode) if the visit should continue, or null.
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 subNode) if the visit should continue, or null.
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 subNode) if the visit should continue, or null.
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 subNode) if the visit should continue, or null.
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 subNode) if the visit should continue, or null.

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 subNode) 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 subNode) if the visit should continue, or null.