Search in sources :

Example 16 with IndexController

use of org.exist.indexing.IndexController in project exist by eXist-db.

the class ElementImpl method insertBefore.

/**
 * Insert a list of nodes at the position before the reference
 * child.
 */
@Override
public void insertBefore(final Txn transaction, final NodeList nodes, final Node refChild) throws DOMException {
    if (refChild == null) {
        // TODO : use NodeImpl.UNKNOWN_NODE_IMPL_GID ? -pb
        appendChildren(transaction, nodes, -1);
        return;
    } else if (!(refChild instanceof IStoredNode)) {
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "wrong node type");
    }
    try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
        final NodePath path = getPath();
        final IndexController indexes = broker.getIndexController();
        // May help getReindexRoot() to make some useful things
        indexes.setDocument(ownerDocument);
        final IStoredNode reindexRoot = indexes.getReindexRoot(this, path, true, true);
        indexes.setMode(ReindexMode.STORE);
        final StreamListener listener;
        if (reindexRoot == null) {
            listener = indexes.getStreamListener();
        } else {
            listener = null;
        }
        final IStoredNode<?> following = (IStoredNode<?>) refChild;
        final IStoredNode<?> previous = (IStoredNode<?>) following.getPreviousSibling();
        if (previous == null) {
            // there's no sibling node before the new node
            final NodeId newId = following.getNodeId().insertBefore();
            appendChildren(transaction, newId, following.getNodeId(), new NodeImplRef(this), path, nodes, listener);
        } else {
            // insert the new node between the preceding and following sibling
            final NodeId newId = previous.getNodeId().insertNode(following.getNodeId());
            appendChildren(transaction, newId, following.getNodeId(), new NodeImplRef(getLastNode(previous)), path, nodes, listener);
        }
        setDirty(true);
        broker.updateNode(transaction, this, true);
        indexes.reindex(transaction, reindexRoot, ReindexMode.STORE);
        broker.flush();
    } catch (final EXistException e) {
        LOG.warn("Exception while inserting node: {}", e.getMessage(), e);
    }
}
Also used : IndexController(org.exist.indexing.IndexController) NodeId(org.exist.numbering.NodeId) EXistException(org.exist.EXistException) StreamListener(org.exist.indexing.StreamListener)

Example 17 with IndexController

use of org.exist.indexing.IndexController in project exist by eXist-db.

the class ElementImpl method removeAppendAttributes.

public void removeAppendAttributes(final Txn transaction, final NodeList removeList, final NodeList appendList) {
    try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
        final IndexController indexes = broker.getIndexController();
        if (removeList != null) {
            try {
                for (int i = 0; i < removeList.getLength(); i++) {
                    final Node oldChild = removeList.item(i);
                    if (!(oldChild instanceof IStoredNode)) {
                        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Wrong node type");
                    }
                    final IStoredNode<?> old = (IStoredNode<?>) oldChild;
                    if (!old.getNodeId().isChildOf(nodeId)) {
                        throw new DOMException(DOMException.NOT_FOUND_ERR, "node " + old.getNodeId().getParentId() + " is not a child of element " + nodeId);
                    }
                    final NodePath oldPath = old.getPath();
                    // remove old custom indexes
                    indexes.reindex(transaction, old, ReindexMode.REMOVE_SOME_NODES);
                    broker.removeNode(transaction, old, oldPath, null);
                    children--;
                    attributes--;
                }
            } finally {
                broker.endRemove(transaction);
            }
        }
        final NodePath path = getPath();
        indexes.setDocument(ownerDocument, ReindexMode.STORE);
        final IStoredNode reindexRoot = indexes.getReindexRoot(this, path, true, true);
        final StreamListener listener = reindexRoot == null ? indexes.getStreamListener() : null;
        if (children == 0) {
            appendChildren(transaction, nodeId.newChild(), null, new NodeImplRef(this), path, appendList, listener);
        } else {
            if (attributes == 0) {
                final IStoredNode<?> firstChild = (IStoredNode<?>) getFirstChild();
                final NodeId newNodeId = firstChild.getNodeId().insertBefore();
                appendChildren(transaction, newNodeId, firstChild.getNodeId(), new NodeImplRef(this), path, appendList, listener);
            } else {
                final AttribVisitor visitor = new AttribVisitor();
                accept(visitor);
                final NodeId firstChildId = visitor.firstChild == null ? null : visitor.firstChild.getNodeId();
                final NodeId newNodeId = visitor.lastAttrib.getNodeId().insertNode(firstChildId);
                appendChildren(transaction, newNodeId, firstChildId, new NodeImplRef(visitor.lastAttrib), path, appendList, listener);
            }
            setDirty(true);
        }
        attributes += appendList.getLength();
        broker.updateNode(transaction, this, true);
        broker.flush();
        indexes.reindex(transaction, reindexRoot, ReindexMode.STORE);
    } catch (final EXistException e) {
        LOG.warn("Exception while inserting node: {}", e.getMessage(), e);
    }
}
Also used : IndexController(org.exist.indexing.IndexController) EXistException(org.exist.EXistException) NodeId(org.exist.numbering.NodeId) StreamListener(org.exist.indexing.StreamListener)

Aggregations

IndexController (org.exist.indexing.IndexController)17 EXistException (org.exist.EXistException)12 StreamListener (org.exist.indexing.StreamListener)9 NodeId (org.exist.numbering.NodeId)4 MatchListener (org.exist.indexing.MatchListener)3 Txn (org.exist.storage.txn.Txn)3 QName (org.exist.dom.QName)2 Receiver (org.exist.util.serializer.Receiver)2 SAXResult (javax.xml.transform.sax.SAXResult)1 PermissionDeniedException (org.exist.security.PermissionDeniedException)1 ReceiverToSAX (org.exist.util.serializer.ReceiverToSAX)1 SAXSerializer (org.exist.util.serializer.SAXSerializer)1 XmldbURI (org.exist.xmldb.XmldbURI)1 org.w3c.dom (org.w3c.dom)1