private BIN searchForBIN(DatabaseImpl db, byte[] mainKey, byte[] dupKey)
throws DatabaseException {
/* Search for this IN */
Tree tree = db.getTree();
IN in = tree.search
(mainKey, SearchType.NORMAL, -1, null, CacheMode.UNCHANGED);
/* Couldn't find a BIN, return null */
if (in == null) {
return null;
}
/* This is not a duplicate, we're done. */
if (dupKey == null) {
return (BIN) in;
}
/* We need to descend down into a duplicate tree. */
DIN duplicateRoot = null;
boolean duplicateRootIsLatched = false;
DBIN duplicateBin = null;
BIN bin = (BIN) in;
boolean binIsLatched = true;
try {
int index = bin.findEntry(mainKey, false, true);
if (index >= 0) {
Node node = null;
if (!bin.isEntryKnownDeleted(index)) {
/*
* If fetchTarget returns null, a deleted LN was cleaned.
*/
node = bin.fetchTarget(index);
}
if (node == null) {
bin.releaseLatch();
binIsLatched = false;
return null;
}
if (node.containsDuplicates()) {
/* It's a duplicate tree. */
duplicateRoot = (DIN) node;
duplicateRoot.latch(CacheMode.UNCHANGED);
duplicateRootIsLatched = true;
bin.releaseLatch();
binIsLatched = false;
duplicateBin = (DBIN) tree.searchSubTree
(duplicateRoot, dupKey, SearchType.NORMAL, -1, null,
CacheMode.UNCHANGED);
duplicateRootIsLatched = false;
return duplicateBin;