Search in sources :

Example 11 with StreamListener

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

the class NativeValueIndex method reindex.

public void reindex(final IStoredNode node) {
    if (node == null) {
        return;
    }
    final StreamListener listener = new ValueIndexStreamListener();
    IndexUtils.scanNode(broker, null, node, listener);
}
Also used : AbstractStreamListener(org.exist.indexing.AbstractStreamListener) StreamListener(org.exist.indexing.StreamListener)

Example 12 with StreamListener

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

the class ElementImpl method update.

/**
 * Update the contents of this element. The passed list of nodes
 * becomes the new content.
 * @param transaction the transaction
 * @param newContent the context
 * @throws DOMException in case of a DOM exception
 */
public void update(final Txn transaction, final NodeList newContent) throws DOMException {
    final NodePath path = getPath();
    // remove old child nodes
    final NodeList nodes = getAttrsAndChildNodes();
    try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
        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.REMOVE_SOME_NODES);
        final StreamListener listener;
        if (reindexRoot == null) {
            listener = indexes.getStreamListener();
        } else {
            listener = null;
            indexes.reindex(transaction, reindexRoot, ReindexMode.REMOVE_SOME_NODES);
        }
        // TODO: fix once range index has been moved to new architecture
        final IStoredNode valueReindexRoot = broker.getValueIndex().getReindexRoot(this, path);
        broker.getValueIndex().reindex(valueReindexRoot);
        IStoredNode last = this;
        int i = nodes.getLength();
        for (; i > 0; i--) {
            IStoredNode<?> child = (IStoredNode<?>) nodes.item(i - 1);
            if (child.getNodeType() == Node.ATTRIBUTE_NODE) {
                last = child;
                break;
            }
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                path.addComponent(child.getQName());
            }
            broker.removeAllNodes(transaction, child, path, listener);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                path.removeLastComponent();
            }
        }
        indexes.flush();
        indexes.setMode(ReindexMode.STORE);
        indexes.getStreamListener();
        broker.endRemove(transaction);
        children = i;
        final NodeId newNodeId = last == this ? nodeId.newChild() : last.getNodeId().nextSibling();
        // Append new content
        appendChildren(transaction, newNodeId, null, new NodeImplRef(last), path, newContent, listener);
        broker.updateNode(transaction, this, false);
        indexes.reindex(transaction, reindexRoot, ReindexMode.STORE);
        broker.getValueIndex().reindex(valueReindexRoot);
        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 13 with StreamListener

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

the class ElementImpl method insertAfter.

/**
 * Insert a list of nodes at the position following the reference
 * child.
 * @param transaction the transaction
 * @param nodes to be inserted
 * @param refChild nodes will be added after
 * @throws DOMException in case of a DOM error
 */
@Override
public void insertAfter(final Txn transaction, final NodeList nodes, final Node refChild) throws DOMException {
    if (refChild == null) {
        // TODO : use NodeImpl.UNKNOWN_NODE_IMPL_GID ? -pb
        appendChildren(null, 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<?> previous = (IStoredNode<?>) refChild;
        final IStoredNode<?> following = (IStoredNode<?>) previous.getNextSibling();
        final NodeId followingId = following == null ? null : following.getNodeId();
        final NodeId newNodeId = previous.getNodeId().insertNode(followingId);
        appendChildren(transaction, newNodeId, followingId, 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 14 with StreamListener

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

the class ElementImpl method removeChild.

@Override
public Node removeChild(final Txn transaction, final Node oldChild) throws DOMException {
    if (!(oldChild instanceof IStoredNode)) {
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "wrong node type");
    }
    final IStoredNode<?> oldNode = (IStoredNode<?>) oldChild;
    if (!oldNode.getNodeId().getParentId().equals(nodeId)) {
        throw new DOMException(DOMException.NOT_FOUND_ERR, "node is not a child of this element");
    }
    final NodePath oldPath = oldNode.getPath();
    try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
        final IndexController indexes = broker.getIndexController();
        indexes.setDocument(ownerDocument);
        final IStoredNode reindexRoot = indexes.getReindexRoot(oldNode, oldPath, false);
        indexes.setMode(ReindexMode.REMOVE_SOME_NODES);
        final StreamListener listener;
        if (reindexRoot == null) {
            listener = indexes.getStreamListener();
        } else {
            listener = null;
            indexes.reindex(transaction, reindexRoot, ReindexMode.REMOVE_SOME_NODES);
        }
        broker.removeAllNodes(transaction, oldNode, oldPath, listener);
        --children;
        if (oldChild.getNodeType() == Node.ATTRIBUTE_NODE) {
            --attributes;
        }
        broker.endRemove(transaction);
        setDirty(true);
        broker.updateNode(transaction, this, false);
        broker.flush();
        if (reindexRoot != null && !reindexRoot.getNodeId().equals(oldNode.getNodeId())) {
            indexes.reindex(transaction, reindexRoot, ReindexMode.STORE);
        }
    } catch (final EXistException e) {
        LOG.warn("Exception while inserting node: {}", e.getMessage(), e);
    }
    return oldNode;
}
Also used : IndexController(org.exist.indexing.IndexController) EXistException(org.exist.EXistException) StreamListener(org.exist.indexing.StreamListener)

Example 15 with StreamListener

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

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