View Javadoc

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