View Javadoc

1   // BSD License (http://www.galagosearch.org/license)
2   package org.galagosearch.core.retrieval.structured;
3   
4   import java.io.IOException;
5   
6   /***
7    *
8    * @author trevor
9    */
10  public class MoveIterators {
11      /***
12       * Moves all iterators in the array to the same document.  This method will only
13       * stop at documents where all of the iterators have a match.
14       *
15       * This code assumes that the most selective iterator (the one with the fewest document
16       * matches) is the first one.
17       *
18       * @return The document number that the iterators are now pointing to, or Integer.MAX_VALUE
19       *         if one of the iterators is now done.
20       */
21      public static int moveAllToSameDocument(ExtentIterator[] iterators) throws IOException {
22          if (iterators.length == 0) {
23              return Integer.MAX_VALUE;
24          }
25          if (iterators[0].isDone()) {
26              return Integer.MAX_VALUE;
27          }
28          int currentTarget = iterators[0].document();
29          boolean allMatch = false;
30  
31          retry:
32          while (!allMatch) {
33              allMatch = true;
34  
35              for (ExtentIterator iterator : iterators) {
36                  if (iterator.isDone()) {
37                      return Integer.MAX_VALUE;
38                  }
39                  int thisDocument = iterator.document();
40  
41                  // this iterator points somewhere before our
42                  // current target document, so try to move forward to
43                  // the target
44                  if (currentTarget > thisDocument) {
45                      iterator.skipToDocument(currentTarget);
46                      if (iterator.isDone()) {
47                          return Integer.MAX_VALUE;
48                      }
49                      thisDocument = iterator.document();
50                  }
51  
52                  // this iterator points after the target document,
53                  // so the target document is not a match.
54                  // we break and try again because we don't want to 
55                  // touch the longest iterators if we can help it.
56                  if (currentTarget < thisDocument) {
57                      allMatch = false;
58                      currentTarget = thisDocument;
59                      continue retry;
60                  }
61              }
62          }
63  
64          return currentTarget;
65      }
66  
67      public static boolean allSameDocument(ExtentIterator[] iterators) {
68          if (iterators.length == 0) {
69              return true;
70          }
71          int document = iterators[0].document();
72  
73          for (ExtentIterator iterator : iterators) {
74              if (document != iterator.document()) {
75                  return false;
76              }
77          }
78  
79          return true;
80      }
81  
82      public static int findMaximumDocument(ExtentIterator[] iterators) {
83          int maximumDocument = 0;
84  
85          for (ExtentIterator iterator : iterators) {
86              if (iterator.isDone()) {
87                  return Integer.MAX_VALUE;
88              }
89              maximumDocument = Math.max(maximumDocument, iterator.document());
90          }
91  
92          return maximumDocument;
93      }
94  
95      public static int findMinimumDocument(ExtentIterator[] iterators) {
96          int minimumDocument = Integer.MAX_VALUE;
97  
98          for (ExtentIterator iterator : iterators) {
99              minimumDocument = Math.min(minimumDocument, iterator.document());
100         }
101 
102         return minimumDocument;
103     }
104 
105     public static int findMinimumCandidate(ScoreIterator[] iterators) {
106         int minimumDocument = Integer.MAX_VALUE;
107 
108         for (ScoreIterator iterator : iterators) {
109             minimumDocument = Math.min(minimumDocument, iterator.nextCandidate());
110         }
111 
112         return minimumDocument;
113     }
114 }