| 1 | |
package org.paneris.bibliomania.fti; |
| 2 | |
|
| 3 | |
import org.melati.poem.util.Order; |
| 4 | |
import org.melati.poem.util.SortUtils; |
| 5 | |
|
| 6 | |
import com.sleepycat.db.DatabaseException; |
| 7 | |
|
| 8 | |
public class AndSearchResults implements SearchResults { |
| 9 | 0 | private static final Order leastCommonFirst = new Order() { |
| 10 | |
public boolean lessOrEqual(Object a, Object b) { |
| 11 | 0 | return ((SearchResults)a).frequency() < ((SearchResults)b).frequency(); |
| 12 | |
} |
| 13 | |
}; |
| 14 | |
|
| 15 | |
private SearchResults[] streams; |
| 16 | 0 | private long textID = -1; |
| 17 | |
private boolean grouped; |
| 18 | |
private TextSearchResults occurrences; |
| 19 | |
private int frequency; |
| 20 | |
|
| 21 | |
private void construct() { |
| 22 | 0 | frequency = Integer.MAX_VALUE; |
| 23 | 0 | for (int w = 0; w < streams.length; ++w) |
| 24 | 0 | frequency = Math.min(streams[w].frequency(), frequency); |
| 25 | |
|
| 26 | 0 | SortUtils.insertionSort(leastCommonFirst, streams); |
| 27 | 0 | } |
| 28 | |
|
| 29 | |
AndSearchResults(IndexOther index, String[] words, boolean grouped) |
| 30 | 0 | throws FTIException, DatabaseException { |
| 31 | 0 | this.grouped = grouped; |
| 32 | 0 | streams = new TextStream[words.length]; |
| 33 | 0 | for (int w = 0; w < words.length; ++w) |
| 34 | 0 | streams[w] = new TextStream(index, words[w]); |
| 35 | |
|
| 36 | 0 | if (grouped) { |
| 37 | 0 | TextStream[] s = new TextStream[streams.length]; |
| 38 | 0 | System.arraycopy(streams, 0, s, 0, streams.length); |
| 39 | 0 | occurrences = new GroupTextSearchResults(s); |
| 40 | 0 | } else |
| 41 | 0 | occurrences = new OrTextSearchResults(streams); |
| 42 | |
|
| 43 | 0 | construct(); |
| 44 | 0 | } |
| 45 | |
|
| 46 | 0 | AndSearchResults(SearchResults[] streams) throws FTIException { |
| 47 | 0 | this.grouped = false; |
| 48 | 0 | this.streams = streams; |
| 49 | 0 | construct(); |
| 50 | 0 | occurrences = new OrTextSearchResults(streams); |
| 51 | 0 | } |
| 52 | |
|
| 53 | |
public int frequency() { |
| 54 | 0 | return frequency; |
| 55 | |
} |
| 56 | |
|
| 57 | |
public void gotoText(long textIdP) throws DatabaseException { |
| 58 | 0 | if (streams.length == 0) |
| 59 | 0 | this.textID = -1; |
| 60 | |
else |
| 61 | |
do { |
| 62 | 0 | int lastW = -1; |
| 63 | 0 | for (int w = 0; w < streams.length;) { |
| 64 | 0 | if (w == lastW) |
| 65 | 0 | ++w; |
| 66 | |
else { |
| 67 | 0 | SearchResults stream = streams[w]; |
| 68 | |
|
| 69 | 0 | stream.gotoText(textIdP); |
| 70 | |
|
| 71 | 0 | if (stream.currentTextID() == -1) { |
| 72 | 0 | this.textID = -1; |
| 73 | 0 | return; |
| 74 | |
} |
| 75 | |
|
| 76 | 0 | if (stream.currentTextID() != textIdP) { |
| 77 | 0 | textIdP = stream.currentTextID(); |
| 78 | 0 | lastW = w; |
| 79 | 0 | w = 0; |
| 80 | |
} else |
| 81 | 0 | ++w; |
| 82 | 0 | } |
| 83 | |
} |
| 84 | |
|
| 85 | 0 | this.textID = textIdP++; |
| 86 | 0 | occurrences.init(); |
| 87 | 0 | } while (grouped && occurrences.currentWordIndex() == -1); |
| 88 | 0 | } |
| 89 | |
|
| 90 | |
public int hitWordsCount() { |
| 91 | 0 | return occurrences.hitWordsCount(); |
| 92 | |
} |
| 93 | |
|
| 94 | |
public void init() { |
| 95 | 0 | occurrences.init(); |
| 96 | 0 | } |
| 97 | |
|
| 98 | |
public long currentTextID() { |
| 99 | 0 | return textID; |
| 100 | |
} |
| 101 | |
|
| 102 | |
public void skipToNextHit() { |
| 103 | 0 | occurrences.skipToNextHit(); |
| 104 | 0 | } |
| 105 | |
|
| 106 | |
public void skipToWordIndex(int wordIndex) { |
| 107 | 0 | occurrences.skipToWordIndex(wordIndex); |
| 108 | 0 | } |
| 109 | |
|
| 110 | |
public int currentWordIndex() { |
| 111 | 0 | return occurrences.currentWordIndex(); |
| 112 | |
} |
| 113 | |
|
| 114 | |
public int currentOffset() { |
| 115 | 0 | return occurrences.currentOffset(); |
| 116 | |
} |
| 117 | |
|
| 118 | |
public void close() { |
| 119 | 0 | for (int i = 0; i < streams.length; ++i) |
| 120 | 0 | streams[i].close(); |
| 121 | 0 | occurrences.close(); |
| 122 | 0 | } |
| 123 | |
} |