use of org.exist.storage.NativeBroker.NodeRef in project exist by eXist-db.
the class DOMFile method findValue.
/**
* Retrieve node at virtual address.
*
* @param broker the database broker
* @param node The virtual address
* @return The reference of the node
* @throws IOException if an I/O error occurs
* @throws BTreeException if an error occurs reading the tree
*/
protected long findValue(final DBBroker broker, final NodeProxy node) throws IOException, BTreeException {
if (LOG.isDebugEnabled() && !lockManager.isBtreeLocked(getLockName())) {
LOG.debug("The file doesn't own a lock");
}
final DocumentImpl doc = node.getOwnerDocument();
final NodeRef nodeRef = new NativeBroker.NodeRef(doc.getDocId(), node.getNodeId());
// first try to find the node in the index
final long pointer = findValue(nodeRef);
if (pointer == KEY_NOT_FOUND) {
// node not found in index: try to find the nearest available
// ancestor and traverse it
NodeId nodeID = node.getNodeId();
long parentPointer = KEY_NOT_FOUND;
do {
nodeID = nodeID.getParentId();
if (nodeID == null) {
SanityCheck.TRACE("Node " + node.getOwnerDocument().getDocId() + ":<null nodeID> not found.");
throw new BTreeException("Node not found.");
}
if (nodeID == NodeId.DOCUMENT_NODE) {
SanityCheck.TRACE("Node " + node.getOwnerDocument().getDocId() + ":" + nodeID + " not found.");
return KEY_NOT_FOUND;
}
final NativeBroker.NodeRef parentRef = new NativeBroker.NodeRef(doc.getDocId(), nodeID);
try {
parentPointer = findValue(parentRef);
} catch (final BTreeException bte) {
LOG.error("report me", bte);
}
} while (parentPointer == KEY_NOT_FOUND);
try {
final int thisLevel = nodeID.getTreeLevel();
// lazily initialized below
Integer childLevel = null;
final NodeProxy parent = new NodeProxy(doc, nodeID, parentPointer);
final EmbeddedXMLStreamReader reader = (EmbeddedXMLStreamReader) broker.getXMLStreamReader(parent, true);
while (reader.hasNext()) {
final int status = reader.next();
if (status != XMLStreamReader.END_ELEMENT) {
if (childLevel == null) {
childLevel = reader.getNode().getNodeType() == Node.ELEMENT_NODE ? thisLevel + 1 : thisLevel;
}
final NodeId otherId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
if (otherId.equals(node.getNodeId())) {
return reader.getCurrentPosition();
}
}
if (status == XMLStreamConstants.END_ELEMENT) {
final NodeId otherId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
final int otherLevel = otherId.getTreeLevel();
if (childLevel != null && childLevel != otherLevel && otherLevel == thisLevel) {
// exit-while
break;
}
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Node {} could not be found. Giving up. This is usually not an error.", node.getNodeId());
}
return KEY_NOT_FOUND;
} catch (final XMLStreamException e) {
SanityCheck.TRACE("Node " + node.getOwnerDocument().getDocId() + ":" + node.getNodeId() + " not found.");
throw new BTreeException("Node " + node.getNodeId() + " not found.");
}
} else {
return pointer;
}
}
Aggregations