1 | |
package org.paneris.bibliomania.fti; |
2 | |
|
3 | |
import org.melati.poem.transaction.ToTidyList; |
4 | |
|
5 | |
import com.sleepycat.db.DatabaseException; |
6 | |
import com.sleepycat.db.LockMode; |
7 | |
import com.sleepycat.db.MemoryException; |
8 | |
import com.sleepycat.db.Cursor; |
9 | |
import com.sleepycat.db.DatabaseEntry; |
10 | |
import com.sleepycat.db.OperationStatus; |
11 | |
|
12 | |
class TextStream extends TextSearchResultsBase |
13 | |
implements SearchResults, ToTidyList.Closeable { |
14 | |
public final String word; |
15 | |
public final int wordID; |
16 | |
public final int frequency; |
17 | |
public long textID; |
18 | |
private final Cursor texts; |
19 | |
private final DatabaseEntry word_text; |
20 | |
private final DatabaseEntry occurrencesData; |
21 | 0 | private final WordTextSearchResults occurrences = |
22 | |
new WordTextSearchResults(); |
23 | |
|
24 | 0 | TextStream(IndexOther index, String word) throws FTIException, DatabaseException { |
25 | 0 | this.word = word; |
26 | |
|
27 | 0 | synchronized (index.idOfWord) { |
28 | 0 | wordID = index.idOfWord(word); |
29 | 0 | frequency = IndexOther.getWC_count(index.idOfWordData.getData()); |
30 | 0 | } |
31 | |
|
32 | 0 | texts = index.occurrencesOfWordInText.openCursor(null, null); |
33 | |
|
34 | 0 | word_text = DbUtils.userMemDatabaseEntry(IndexOther.wtBytesLength); |
35 | 0 | occurrencesData = DbUtils.userMemDatabaseEntry(500); |
36 | 0 | } |
37 | |
|
38 | |
public int frequency() { |
39 | 0 | return frequency; |
40 | |
} |
41 | |
|
42 | |
public void gotoText(long textIdP) throws DatabaseException { |
43 | 0 | if (IndexOther.debug) System.err.println(word + ": gotoText(" + textIdP + ")"); |
44 | |
|
45 | |
OperationStatus result; |
46 | |
for (;;) { |
47 | |
try { |
48 | 0 | IndexOther.setWT(word_text.getData(), wordID, textIdP); |
49 | 0 | result = texts.getSearchKeyRange(word_text, occurrencesData, LockMode.DEFAULT); |
50 | 0 | break; |
51 | |
} |
52 | 0 | catch (MemoryException e) { |
53 | 0 | occurrencesData.setData( |
54 | |
new byte[occurrencesData.getData().length * 2]); |
55 | 0 | occurrencesData.setUserBuffer(occurrencesData.getData().length, true); |
56 | 0 | } |
57 | |
} |
58 | |
|
59 | 0 | if (result != OperationStatus.SUCCESS) { |
60 | 0 | if (IndexOther.debug) System.err.println("nope: " + result); |
61 | 0 | this.textID = -1; |
62 | |
} |
63 | |
else { |
64 | 0 | if (IndexOther.getWT_wordID(word_text.getData()) != wordID) { |
65 | 0 | this.textID = -1; |
66 | 0 | if (IndexOther.debug) System.err.println("wrong word"); |
67 | |
} |
68 | |
else { |
69 | 0 | this.textID = IndexOther.getWT_textID(word_text.getData()); |
70 | 0 | if (IndexOther.debug) System.err.println("yep: " + this.textID); |
71 | |
} |
72 | |
} |
73 | 0 | } |
74 | |
|
75 | |
public long currentTextID() { |
76 | 0 | return textID; |
77 | |
} |
78 | |
|
79 | |
public int hitWordsCount() { |
80 | 0 | return occurrences.hitWordsCount(); |
81 | |
} |
82 | |
|
83 | |
public void init() { |
84 | 0 | occurrences.init(occurrencesData.getData(), |
85 | |
occurrencesData.getSize()); |
86 | 0 | } |
87 | |
|
88 | |
public void skipToNextHit() { |
89 | 0 | if (textID != -1) |
90 | 0 | occurrences.skipToNextHit(); |
91 | 0 | } |
92 | |
|
93 | |
public int currentWordIndex() { |
94 | 0 | return textID == -1 ? -1 : occurrences.currentWordIndex(); |
95 | |
} |
96 | |
|
97 | |
public int currentOffset() { |
98 | 0 | return textID == -1 ? -1 : occurrences.currentOffset(); |
99 | |
} |
100 | |
|
101 | |
public void close() { |
102 | |
try { |
103 | 0 | texts.close(); |
104 | |
} |
105 | 0 | catch (Exception e) {} |
106 | 0 | } |
107 | |
} |