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.DatabaseException; |
7 | |
import com.sleepycat.db.Cursor; |
8 | |
import com.sleepycat.db.DatabaseEntry; |
9 | |
import com.sleepycat.db.LockMode; |
10 | |
import com.sleepycat.db.OperationStatus; |
11 | |
|
12 | |
public class AnchorFinder implements ToTidyList.Closeable { |
13 | |
|
14 | |
private Cursor anchors; |
15 | 17 | private DatabaseEntry text_index = DbUtils.userMemDatabaseEntry(IndexOther.textIdBytesLength); |
16 | 17 | private DatabaseEntry anchor = DbUtils.userMemDatabaseEntry(IndexOther.wordTruncationLength); |
17 | |
|
18 | 17 | public AnchorFinder(IndexOther index, boolean blockmarkers) { |
19 | |
try { |
20 | 17 | anchors = (blockmarkers ? index.blockmarkOfIndex : index.anchorOfIndex). |
21 | |
openCursor(null, null); |
22 | |
} |
23 | 0 | catch (DatabaseException e) { |
24 | 0 | throw new UnexpectedExceptionException(e); |
25 | 17 | } |
26 | 17 | } |
27 | |
|
28 | |
public AnchorFinder(IndexOther index) { |
29 | 0 | this(index, false); |
30 | 0 | } |
31 | |
|
32 | |
public synchronized String anchorOfIndex(long textID, int index) |
33 | |
throws DatabaseException { |
34 | 0 | IndexOther.setTI(text_index.getData(), textID, index); |
35 | 0 | return |
36 | |
anchors.getSearchKeyRange(text_index, anchor, LockMode.DEFAULT) == OperationStatus.SUCCESS && |
37 | |
anchors.getPrev(text_index, anchor, LockMode.DEFAULT) == OperationStatus.SUCCESS && |
38 | |
IndexOther.getTI_textID(text_index.getData()) == textID ? |
39 | |
new String(anchor.getData(), 0, anchor.getSize()) : null; |
40 | |
} |
41 | |
|
42 | |
public void close() { |
43 | |
try { |
44 | 0 | anchors.close(); |
45 | |
} |
46 | 0 | catch (Exception e) {} |
47 | 0 | } |
48 | |
} |