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 java.util.ArrayList;
6   import java.util.PriorityQueue;
7   import org.galagosearch.core.util.ExtentArray;
8   
9   /***
10   * This class is meant to be a base class for many kinds of
11   * iterators that require at least one of their children to be
12   * present in the document for a match to happen.  This class
13   * will call loadExtents once for each document that has a
14   * match on any child iterator.
15   * 
16   * @author trevor
17   */
18  public abstract class ExtentDisjunctionIterator extends ExtentIterator {
19      protected ExtentIterator[] original;
20      protected PriorityQueue<ExtentIterator> iterators;
21      protected int document;
22      protected ExtentArray extents;
23  
24      public ExtentDisjunctionIterator(ExtentIterator[] iterators) {
25          this.original = iterators;
26          this.iterators = new PriorityQueue<ExtentIterator>(iterators.length);
27  
28          this.extents = new ExtentArray();
29          this.document = 0;
30  
31          for (ExtentIterator iterator : original) {
32              if (!iterator.isDone()) {
33                  this.iterators.add(iterator);
34              }
35          }
36      }
37      
38      public abstract void loadExtents();
39  
40      public void nextDocument() throws IOException {
41          // find all iterators on the current document and move them forward
42          while (iterators.size() > 0 && iterators.peek().document() == document) {
43              ExtentIterator iter = iterators.poll();
44              iter.nextDocument();
45  
46              if (!iter.isDone()) {
47                  iterators.offer(iter);
48              }
49          }
50  
51          if (!isDone()) {
52              extents.reset();
53              loadExtents();
54          }
55      }
56  
57      public boolean isDone() {
58          return iterators.size() == 0;
59      }
60  
61      public ExtentArray extents() {
62          return extents;
63      }
64  
65      public int document() {
66          return document;
67      }
68  
69      public int count() {
70          return extents.getPosition();
71      }
72  
73      public void reset() throws IOException {
74          iterators.clear();
75          for (ExtentIterator iterator : original) {
76              iterator.reset();
77              if (!iterator.isDone()) {
78                  iterators.add(iterator);
79              }
80          }
81  
82          loadExtents();
83      }
84  }