Search in sources :

Example 1 with NodeRef

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;
    }
}
Also used : NodeRef(org.exist.storage.NativeBroker.NodeRef) BTreeException(org.exist.storage.btree.BTreeException) XMLStreamException(javax.xml.stream.XMLStreamException) NodeId(org.exist.numbering.NodeId) EmbeddedXMLStreamReader(org.exist.stax.EmbeddedXMLStreamReader) NativeBroker(org.exist.storage.NativeBroker) NodeRef(org.exist.storage.NativeBroker.NodeRef) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)1 DocumentImpl (org.exist.dom.persistent.DocumentImpl)1 NodeProxy (org.exist.dom.persistent.NodeProxy)1 NodeId (org.exist.numbering.NodeId)1 EmbeddedXMLStreamReader (org.exist.stax.EmbeddedXMLStreamReader)1 NativeBroker (org.exist.storage.NativeBroker)1 NodeRef (org.exist.storage.NativeBroker.NodeRef)1 BTreeException (org.exist.storage.btree.BTreeException)1