View Javadoc

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    private final WordTextSearchResults occurrences =
22        new WordTextSearchResults();
23  
24    TextStream(IndexOther index, String word) throws FTIException, DatabaseException {
25      this.word = word;
26      // hack
27      synchronized (index.idOfWord) {
28        wordID = index.idOfWord(word);
29        frequency = IndexOther.getWC_count(index.idOfWordData.getData());
30      }
31  
32      texts = index.occurrencesOfWordInText.openCursor(null, null);
33  
34      word_text = DbUtils.userMemDatabaseEntry(IndexOther.wtBytesLength);
35      occurrencesData = DbUtils.userMemDatabaseEntry(500);
36    }
37  
38    public int frequency() {
39      return frequency;
40    }
41  
42    public void gotoText(long textIdP) throws DatabaseException {
43      if (IndexOther.debug) System.err.println(word + ": gotoText(" + textIdP + ")");
44  
45      OperationStatus result;
46      for (;;) {
47        try {
48          IndexOther.setWT(word_text.getData(), wordID, textIdP);
49          result = texts.getSearchKeyRange(word_text, occurrencesData, LockMode.DEFAULT);
50          break;
51        }
52        catch (MemoryException e) {
53          occurrencesData.setData(
54              new byte[occurrencesData.getData().length * 2]);
55          occurrencesData.setUserBuffer(occurrencesData.getData().length, true);
56        }
57      }
58  
59      if (result != OperationStatus.SUCCESS) {
60        if (IndexOther.debug) System.err.println("nope: " + result);
61        this.textID = -1;
62      }
63      else {
64        if (IndexOther.getWT_wordID(word_text.getData()) != wordID) {
65          this.textID = -1;
66          if (IndexOther.debug) System.err.println("wrong word");
67        }
68        else {
69          this.textID = IndexOther.getWT_textID(word_text.getData());
70          if (IndexOther.debug) System.err.println("yep: " + this.textID);
71        }
72      }
73    }
74  
75    public long currentTextID() {
76      return textID;
77    }
78  
79    public int hitWordsCount() {
80      return occurrences.hitWordsCount();
81    }
82  
83    public void init() {
84      occurrences.init(occurrencesData.getData(),
85                       occurrencesData.getSize());
86    }
87  
88    public void skipToNextHit() {
89      if (textID != -1)
90        occurrences.skipToNextHit();
91    }
92  
93    public int currentWordIndex() {
94      return textID == -1 ? -1 : occurrences.currentWordIndex();
95    }
96  
97    public int currentOffset() {
98      return textID == -1 ? -1 : occurrences.currentOffset();
99    }
100 
101   public void close() {
102     try {
103       texts.close();
104     }
105     catch (Exception e) {}
106   }
107 }