View Javadoc

1   package org.paneris.bibliomania.fti;
2   
3   import org.melati.poem.transaction.ToTidyList;
4   import org.melati.util.UnexpectedExceptionException;
5   
6   import com.sleepycat.db.Db;
7   import com.sleepycat.db.DbException;
8   import com.sleepycat.db.Dbc;
9   import com.sleepycat.db.Dbt;
10  
11  public class AnchorFinder implements ToTidyList.Closeable {
12  
13    private Dbc anchors;
14    private Dbt text_index = DbUtils.userMemDbt(IndexOther.tiBytesLength);
15    private Dbt anchor = DbUtils.userMemDbt(IndexOther.wordTruncationLength);
16  
17    public AnchorFinder(IndexOther index, boolean blockmarkers) {
18      try {
19        anchors = (blockmarkers ? index.blockmarkOfIndex : index.anchorOfIndex).
20                      cursor(null, 0);
21      }
22      catch (DbException e) {
23        throw new UnexpectedExceptionException(e);
24      }
25    }
26  
27    public AnchorFinder(IndexOther index) {
28      this(index, false);
29    }
30  
31    public synchronized String anchorOfIndex(long textID, int index)
32        throws DbException {
33      IndexOther.setTI(text_index.getData(), textID, index);
34      return
35          anchors.get(text_index, anchor, Db.DB_SET_RANGE) == 0 &&
36          anchors.get(text_index, anchor, Db.DB_PREV) == 0 &&
37          IndexOther.getTI_textID(text_index.getData()) == textID ?
38            new String(anchor.getData(), 0, anchor.getSize()) : null;
39    }
40  
41    public void close() {
42      try {
43        anchors.close();
44      }
45      catch (Exception e) {}
46    }
47  }