Interface DocumentIteratorVisitor<T>
-
- All Known Implementing Classes:
AbstractDocumentIteratorVisitor
,BM25FScorer.Visitor
,CounterCollectionVisitor
,CounterSetupVisitor
,TermCollectionVisitor
,TrueTermsCollectionVisitor
public interface DocumentIteratorVisitor<T>
A visitor for the tree defined by aDocumentIterator
.Implementations of this interface must be reusable. The user must invoke
prepare()
before a visit so that the internal state of the visitor can be suitably set up.Document-iterator visitors can optionally return values. In this case, the
newArray(int)
method must be properly implemented. Otherwise, it can safely returnnull
: all implementations ofDocumentIterator.accept(DocumentIteratorVisitor)
andDocumentIterator.acceptOnTruePaths(DocumentIteratorVisitor)
must be prepared for this to happen. (It would be of course possible to pass around empty arrays, but visitors on document iterators usually have performance issues.)If a document-iterator visitor does not return values, it is suggested that it is parameterised on
Boolean
and that it returnsBoolean.TRUE
to denote that a visit should not end.For maximum flexibility, there is just one type of visit method for leaves, but two visits methods for internal nodes, which should be used for preorder and postorder visits, respectively.
The visit methods for leaves are actually two, as they depend on the visited leaf (more precisely, whether it is an
IndexIterator
or aMultiTermIndexIterator
. The reason for this choice is that in some cases the term-like nature of aMultiTermIndexIterator
makes it easier to write scorers that don't know about expansions (e.g.,BM25Scorer
. On the other hand, some scorers might want to delve into aMultiTermIndexIterator
and discover usingMultiTermIndexIterator.front(IndexIterator[])
which terms where actually found. Thevisit(MultiTermIndexIterator)
makes this goal easily reachable.Note that this visitor interface and that defined in
QueryBuilderVisitor
are based on different principles: in the latter case, the action of the visitor will likely be different for each type of internal node, so we have specific visit methods for each type of such nodes. In our case, the visit will most likely behave differently just for internal nodes and leaves, so we prefer a simpler interface that also let us implement more easily visitor acceptance methods (DocumentIterator.accept(DocumentIteratorVisitor)
andDocumentIterator.acceptOnTruePaths(DocumentIteratorVisitor)
).- Author:
- Sebastiano Vigna
-
-
Method Summary
Modifier and Type Method Description T[]
newArray(int len)
Builds an array of given length of typeT
.DocumentIteratorVisitor<T>
prepare()
Prepares the internal state of this visitor for a(nother) visit.T
visit(IndexIterator indexIterator)
Visits anIndexIterator
leaf.T
visit(MultiTermIndexIterator multiTermIndexIterator)
Visits aMultiTermIndexIterator
leaf.T
visit(FalseDocumentIterator falseDocumentIterator)
Visits aFalseDocumentIterator
leaf.T
visit(TrueDocumentIterator trueDocumentIterator)
Visits aTrueDocumentIterator
leaf.T
visitPost(DocumentIterator documentIterator, T[] subNodeResult)
Visits an internal node after recursing into the corresponding subtree.boolean
visitPre(DocumentIterator documentIterator)
Visits an internal node before recursing into the corresponding subtree.
-
-
-
Method Detail
-
prepare
DocumentIteratorVisitor<T> prepare()
Prepares the internal state of this visitor for a(nother) visit.By specification, it must be safe to call this method any number of times.
- Returns:
- this visitor.
-
newArray
T[] newArray(int len)
Builds an array of given length of typeT
.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 typeT
. This method must provide an array of given length that is an acceptable input for allvisitPost()
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 inArray
.- Parameters:
len
- the required array length.- Returns:
- an array of type
T
of lengthlen
, ornull
if this document iterator visitor does not return values.
-
visitPre
boolean visitPre(DocumentIterator documentIterator)
Visits an internal node before recursing into the corresponding subtree.- Parameters:
documentIterator
- the internal node to be visited.- Returns:
- true if the visit should continue.
-
visitPost
T visitPost(DocumentIterator documentIterator, T[] subNodeResult)
Visits an internal node after recursing into the corresponding subtree.- Parameters:
documentIterator
- the internal node to be visited.subNodeResult
- 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, ornull
.
-
visit
T visit(IndexIterator indexIterator) throws IOException
Visits anIndexIterator
leaf.- Parameters:
indexIterator
- the leaf to be visited.- Returns:
- an appropriate return value if the visit should continue, or
null
. - Throws:
IOException
-
visit
T visit(MultiTermIndexIterator multiTermIndexIterator) throws IOException
Visits aMultiTermIndexIterator
leaf.- Parameters:
multiTermIndexIterator
- the leaf to be visited.- Returns:
- an appropriate return value if the visit should continue, or
null
. - Throws:
IOException
-
visit
T visit(TrueDocumentIterator trueDocumentIterator) throws IOException
Visits aTrueDocumentIterator
leaf.- Parameters:
trueDocumentIterator
- the leaf to be visited.- Returns:
- an appropriate return value if the visit should continue, or
null
. - Throws:
IOException
-
visit
T visit(FalseDocumentIterator falseDocumentIterator) throws IOException
Visits aFalseDocumentIterator
leaf.- Parameters:
falseDocumentIterator
- the leaf to be visited.- Returns:
- an appropriate return value if the visit should continue, or
null
. - Throws:
IOException
-
-