| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MoveIterators |
|
| 0.0;0 |
| 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 | 0 | 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 | 56 | if (iterators.length == 0) { |
| 23 | 0 | return Integer.MAX_VALUE; |
| 24 | } | |
| 25 | 56 | if (iterators[0].isDone()) { |
| 26 | 20 | return Integer.MAX_VALUE; |
| 27 | } | |
| 28 | 36 | int currentTarget = iterators[0].document(); |
| 29 | 36 | boolean allMatch = false; |
| 30 | ||
| 31 | retry: | |
| 32 | 80 | while (!allMatch) { |
| 33 | 52 | allMatch = true; |
| 34 | ||
| 35 | 124 | for (ExtentIterator iterator : iterators) { |
| 36 | 96 | if (iterator.isDone()) { |
| 37 | 0 | return Integer.MAX_VALUE; |
| 38 | } | |
| 39 | 96 | 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 | 96 | if (currentTarget > thisDocument) { |
| 45 | 28 | iterator.skipToDocument(currentTarget); |
| 46 | 28 | if (iterator.isDone()) { |
| 47 | 8 | return Integer.MAX_VALUE; |
| 48 | } | |
| 49 | 20 | 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 | 88 | if (currentTarget < thisDocument) { |
| 57 | 16 | allMatch = false; |
| 58 | 16 | currentTarget = thisDocument; |
| 59 | 16 | continue retry; |
| 60 | } | |
| 61 | } | |
| 62 | } | |
| 63 | ||
| 64 | 28 | return currentTarget; |
| 65 | } | |
| 66 | ||
| 67 | public static boolean allSameDocument(ExtentIterator[] iterators) { | |
| 68 | 8 | if (iterators.length == 0) { |
| 69 | 0 | return true; |
| 70 | } | |
| 71 | 8 | int document = iterators[0].document(); |
| 72 | ||
| 73 | 20 | for (ExtentIterator iterator : iterators) { |
| 74 | 16 | if (document != iterator.document()) { |
| 75 | 4 | return false; |
| 76 | } | |
| 77 | } | |
| 78 | ||
| 79 | 4 | return true; |
| 80 | } | |
| 81 | ||
| 82 | public static int findMaximumDocument(ExtentIterator[] iterators) { | |
| 83 | 20 | int maximumDocument = 0; |
| 84 | ||
| 85 | 56 | for (ExtentIterator iterator : iterators) { |
| 86 | 40 | if (iterator.isDone()) { |
| 87 | 4 | return Integer.MAX_VALUE; |
| 88 | } | |
| 89 | 36 | maximumDocument = Math.max(maximumDocument, iterator.document()); |
| 90 | } | |
| 91 | ||
| 92 | 16 | return maximumDocument; |
| 93 | } | |
| 94 | ||
| 95 | public static int findMinimumDocument(ExtentIterator[] iterators) { | |
| 96 | 0 | int minimumDocument = Integer.MAX_VALUE; |
| 97 | ||
| 98 | 0 | for (ExtentIterator iterator : iterators) { |
| 99 | 0 | minimumDocument = Math.min(minimumDocument, iterator.document()); |
| 100 | } | |
| 101 | ||
| 102 | 0 | return minimumDocument; |
| 103 | } | |
| 104 | ||
| 105 | public static int findMinimumCandidate(ScoreIterator[] iterators) { | |
| 106 | 0 | int minimumDocument = Integer.MAX_VALUE; |
| 107 | ||
| 108 | 0 | for (ScoreIterator iterator : iterators) { |
| 109 | 0 | minimumDocument = Math.min(minimumDocument, iterator.nextCandidate()); |
| 110 | } | |
| 111 | ||
| 112 | 0 | return minimumDocument; |
| 113 | } | |
| 114 | } |