View Javadoc

1   // BSD License (http://www.galagosearch.org/license)
2   package org.galagosearch.core.retrieval.structured;
3   
4   import java.io.IOException;
5   import org.galagosearch.tupleflow.Parameters;
6   
7   /***
8    * <p>Implements the #inside operator.  The #inside operator is usually implicit
9    * in the query language, where <tt>a.b</tt> is equivalent to <tt>#inside(a b)</tt>.
10   * This is usually used to find terms that occur in fields.  For example,
11   * <tt>#1(bruce croft).author</tt>, which finds instances of "bruce croft" occurring
12   * in the author field of a document.</p>
13   *
14   * @author trevor
15   */
16  public class ExtentInsideIterator extends ExtentConjunctionIterator {
17      ExtentIterator innerIterator;
18      ExtentIterator outerIterator;
19  
20      /***
21       * <p>Constructs an #inside instance.  For <tt>#inside(a b)</tt>, this
22       * produces an extent whenever <tt>a</tt> is found inside <tt>b</tt>.</p>
23       *
24       * <p>For example, in the expression <tt>#inside(#1(white house) #extents:title())</tt>,
25       * <tt>#1(white house)</tt> is the inner iterator and <tt>#extents:title()</tt>
26       * is the outer iterator.  Whenever <tt>#1(white house)</tt> is found in the title of
27       * a document, this is a match.  The extent for <tt>#1(white house)</tt> is returned
28       * (not the extent for <tt>#extents:title()</tt> that surrounds it).</tt>
29       *
30       * @param parameters extra parameters, not used for anything.
31       * @param innerIterator The source of extents that must be inside.
32       * @param outerIterator The source of extents that must contain the inner extents.
33       * @throws java.io.IOException
34       */
35      public ExtentInsideIterator(Parameters parameters,
36              ExtentIterator innerIterator,
37              ExtentIterator outerIterator) throws IOException {
38          super(new ExtentIterator[] { innerIterator, outerIterator });
39          this.innerIterator = innerIterator;
40          this.outerIterator = outerIterator;
41          findDocument();
42      }
43  
44      /***
45       * This method is called whenever the ExtentConjunctionIterator has verified
46       * that both the inner and outer iterators match this document.  This method's job
47       * is to find all matchin extents within the document, if they exist.
48       */
49  
50      public void loadExtents() {
51          ExtentArrayIterator inner = new ExtentArrayIterator(innerIterator.extents());
52          ExtentArrayIterator outer = new ExtentArrayIterator(outerIterator.extents());
53  
54          while (!inner.isDone() && !outer.isDone()) {
55              if (outer.current().contains(inner.current())) {
56                  extents.add(inner.current());
57                  inner.next();
58              } else if (outer.current().end <= inner.current().begin) {
59                  outer.next();
60              } else {
61                  inner.next();
62              }
63          }
64      }
65  }