Search in sources :

Example 16 with StreamListener

use of org.exist.indexing.StreamListener 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

StreamListener (org.exist.indexing.StreamListener)16 EXistException (org.exist.EXistException)9 IndexController (org.exist.indexing.IndexController)9 NodeId (org.exist.numbering.NodeId)4 INodeIterator (org.exist.storage.dom.INodeIterator)4 NodeList (org.w3c.dom.NodeList)4 PermissionDeniedException (org.exist.security.PermissionDeniedException)3 Database (org.exist.Database)2 LockException (org.exist.util.LockException)2 XmldbURI (org.exist.xmldb.XmldbURI)2 PooledObject (org.apache.commons.pool2.PooledObject)1 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)1 Indexer (org.exist.Indexer)1 AbstractStreamListener (org.exist.indexing.AbstractStreamListener)1 DOMTransaction (org.exist.storage.dom.DOMTransaction)1 TerminatedException (org.exist.xquery.TerminatedException)1 SAXException (org.xml.sax.SAXException)1