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 private DatabaseEntry text_index = DbUtils.userMemDatabaseEntry(IndexOther.textIdBytesLength); 16 private DatabaseEntry anchor = DbUtils.userMemDatabaseEntry(IndexOther.wordTruncationLength); 17 18 public AnchorFinder(IndexOther index, boolean blockmarkers) { 19 try { 20 anchors = (blockmarkers ? index.blockmarkOfIndex : index.anchorOfIndex). 21 openCursor(null, null); 22 } 23 catch (DatabaseException e) { 24 throw new UnexpectedExceptionException(e); 25 } 26 } 27 28 public AnchorFinder(IndexOther index) { 29 this(index, false); 30 } 31 32 public synchronized String anchorOfIndex(long textID, int index) 33 throws DatabaseException { 34 IndexOther.setTI(text_index.getData(), textID, index); 35 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 anchors.close(); 45 } 46 catch (Exception e) {} 47 } 48 }