Search in sources :

Example 11 with SirixUsageException

use of org.sirix.exception.SirixUsageException in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method setName.

@Override
public XdmNodeWriteTrx setName(final QNm name) throws SirixException {
    checkNotNull(name);
    acquireLock();
    try {
        if (getCurrentNode() instanceof NameNode) {
            if (!getName().equals(name)) {
                checkAccessAndCommit();
                NameNode node = (NameNode) mNodeReader.getCurrentNode();
                final long oldHash = node.hashCode();
                // Create new keys for mapping.
                final int prefixKey = name.getPrefix() != null && !name.getPrefix().isEmpty() ? getPageTransaction().createNameKey(name.getPrefix(), node.getKind()) : -1;
                final int localNameKey = name.getLocalName() != null && !name.getLocalName().isEmpty() ? getPageTransaction().createNameKey(name.getLocalName(), node.getKind()) : -1;
                final int uriKey = name.getNamespaceURI() != null && !name.getNamespaceURI().isEmpty() ? getPageTransaction().createNameKey(name.getNamespaceURI(), Kind.NAMESPACE) : -1;
                // Adapt path summary.
                if (mBuildPathSummary) {
                    mPathSummaryWriter.adaptPathForChangedNode(node, name, uriKey, prefixKey, localNameKey, OPType.SETNAME);
                }
                // Remove old keys from mapping.
                final Kind nodeKind = node.getKind();
                final int oldPrefixKey = node.getPrefixKey();
                final int oldLocalNameKey = node.getLocalNameKey();
                final int oldUriKey = node.getURIKey();
                final NamePage page = ((NamePage) getPageTransaction().getActualRevisionRootPage().getNamePageReference().getPage());
                page.removeName(oldPrefixKey, nodeKind);
                page.removeName(oldLocalNameKey, nodeKind);
                page.removeName(oldUriKey, Kind.NAMESPACE);
                // Set new keys for current node.
                node = (NameNode) getPageTransaction().prepareEntryForModification(node.getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
                node.setLocalNameKey(localNameKey);
                node.setURIKey(uriKey);
                node.setPrefixKey(prefixKey);
                node.setPathNodeKey(mBuildPathSummary ? mPathSummaryWriter.getNodeKey() : 0);
                mNodeReader.setCurrentNode(node);
                adaptHashedWithUpdate(oldHash);
            }
            return this;
        } else {
            throw new SirixUsageException("setName is not allowed if current node is not an INameNode implementation!");
        }
    } finally {
        unLock();
    }
}
Also used : NameNode(org.sirix.node.interfaces.NameNode) PageKind(org.sirix.page.PageKind) Kind(org.sirix.node.Kind) NamePage(org.sirix.page.NamePage) SirixUsageException(org.sirix.exception.SirixUsageException)

Example 12 with SirixUsageException

use of org.sirix.exception.SirixUsageException in project sirix by sirixdb.

the class XdmResourceManager method beginPageWriteTrx.

@Override
public synchronized PageWriteTrx<Long, Record, UnorderedKeyValuePage> beginPageWriteTrx(@Nonnegative final int revision) throws SirixException {
    assertAccess(revision);
    // Make sure not to exceed available number of write transactions.
    try {
        if (!mWriteSemaphore.tryAcquire(20, TimeUnit.SECONDS)) {
            throw new SirixUsageException("No write transaction available, please close the write transaction first.");
        }
    } catch (final InterruptedException e) {
        throw new SirixThreadedException(e);
    }
    final long currentPageTrxID = mPageTrxIDCounter.incrementAndGet();
    final int lastRev = mLastCommittedUberPage.get().getRevisionNumber();
    final PageWriteTrx<Long, Record, UnorderedKeyValuePage> pageWtx = createPageWriteTransaction(currentPageTrxID, lastRev, lastRev, Abort.NO);
    // Remember page transaction for debugging and safe close.
    if (mPageTrxMap.put(currentPageTrxID, pageWtx) != null) {
        throw new SirixThreadedException("ID generation is bogus because of duplicate ID.");
    }
    return pageWtx;
}
Also used : SirixThreadedException(org.sirix.exception.SirixThreadedException) AtomicLong(java.util.concurrent.atomic.AtomicLong) Record(org.sirix.node.interfaces.Record) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) SirixUsageException(org.sirix.exception.SirixUsageException)

Example 13 with SirixUsageException

use of org.sirix.exception.SirixUsageException in project sirix by sirixdb.

the class FMSE method emitInsert.

/**
 * Emit an insert operation.
 *
 * @param parent parent of the current {@link Node} implementation reference to insert
 * @param child the current node to insert
 * @param pos position of the insert
 * @param wtx {@link XdmNodeWriteTrx} implementation reference on old revision
 * @param rtx {@link XdmNodeReadTrx} implementation reference on new revision
 * @return inserted {@link Node} implementation reference
 * @throws SirixException if anything in sirix fails
 */
private long emitInsert(final long child, final long parent, final int pos, final XdmNodeWriteTrx wtx, final XdmNodeReadTrx rtx) {
    assert child >= 0;
    assert parent >= 0;
    assert wtx != null;
    assert rtx != null;
    // Determines if node has been already inserted (for subtrees).
    if (mAlreadyInserted.get(child) != null) {
        // actually child'
        return child;
    }
    wtx.moveTo(parent);
    rtx.moveTo(child);
    try {
        switch(rtx.getKind()) {
            case ATTRIBUTE:
                try {
                    wtx.insertAttribute(rtx.getName(), rtx.getValue());
                } catch (final SirixUsageException e) {
                    mTotalMatching.remove(wtx.getNodeKey());
                    wtx.setValue(rtx.getValue());
                }
                process(wtx.getNodeKey(), rtx.getNodeKey());
                break;
            case NAMESPACE:
                // Note that the insertion is right (localPart as prefix).
                try {
                    wtx.insertNamespace(new QNm(rtx.getName().getNamespaceURI(), rtx.getName().getLocalName(), ""));
                } catch (final SirixUsageException e) {
                    mTotalMatching.remove(wtx.getNodeKey());
                }
                process(wtx.getNodeKey(), rtx.getNodeKey());
                break;
            default:
                // In case of other node types.
                long oldKey = 0;
                if (pos == 0) {
                    switch(rtx.getKind()) {
                        case ELEMENT:
                            oldKey = wtx.copySubtreeAsFirstChild(rtx).getNodeKey();
                            break;
                        case TEXT:
                            // is inserted.
                            if (wtx.hasFirstChild()) {
                                wtx.moveToFirstChild();
                                if (wtx.getKind() == Kind.TEXT) {
                                    mTotalMatching.remove(wtx.getNodeKey());
                                    wtx.remove();
                                }
                                wtx.moveTo(parent);
                            }
                            oldKey = wtx.insertTextAsFirstChild(rtx.getValue()).getNodeKey();
                            break;
                        default:
                    }
                } else {
                    assert wtx.hasFirstChild();
                    wtx.moveToFirstChild();
                    for (int i = 0; i < pos - 1; i++) {
                        assert wtx.hasRightSibling();
                        wtx.moveToRightSibling();
                    }
                    // Remove right sibl. text node if a text node already exists.
                    removeRightSiblingTextNode(wtx);
                    switch(rtx.getKind()) {
                        case ELEMENT:
                            oldKey = wtx.copySubtreeAsRightSibling(rtx).getNodeKey();
                            break;
                        case TEXT:
                            oldKey = wtx.insertTextAsRightSibling(rtx.getValue()).getNodeKey();
                            break;
                        default:
                            // Already inserted.
                            throw new IllegalStateException("Child should be already inserted!");
                    }
                }
                // Mark all nodes in subtree as inserted.
                wtx.moveTo(oldKey);
                rtx.moveTo(child);
                for (final Axis oldAxis = new DescendantAxis(wtx, IncludeSelf.YES), newAxis = new DescendantAxis(rtx, IncludeSelf.YES); oldAxis.hasNext() && newAxis.hasNext(); ) {
                    oldAxis.next();
                    newAxis.next();
                    final XdmNodeReadTrx oldRtx = oldAxis.getTrx();
                    final XdmNodeReadTrx newRtx = newAxis.getTrx();
                    process(oldRtx.getNodeKey(), newRtx.getNodeKey());
                    final long newNodeKey = newRtx.getNodeKey();
                    final long oldNodeKey = oldRtx.getNodeKey();
                    if (newRtx.getKind() == Kind.ELEMENT) {
                        assert newRtx.getKind() == oldRtx.getKind();
                        if (newRtx.getAttributeCount() > 0) {
                            for (int i = 0, attCount = newRtx.getAttributeCount(); i < attCount; i++) {
                                rtx.moveToAttribute(i);
                                for (int j = 0, oldAttCount = oldRtx.getAttributeCount(); i < oldAttCount; j++) {
                                    wtx.moveToAttribute(j);
                                    if (wtx.getName().equals(rtx.getName())) {
                                        process(oldAxis.getTrx().getNodeKey(), newAxis.getTrx().getNodeKey());
                                        break;
                                    }
                                    oldAxis.getTrx().moveTo(oldNodeKey);
                                }
                                newAxis.getTrx().moveTo(newNodeKey);
                            }
                        }
                        if (newRtx.getNamespaceCount() > 0) {
                            for (int i = 0, nspCount = newRtx.getNamespaceCount(); i < nspCount; i++) {
                                rtx.moveToNamespace(i);
                                for (int j = 0, oldNspCount = oldRtx.getNamespaceCount(); j < oldNspCount; j++) {
                                    wtx.moveToNamespace(j);
                                    if (wtx.getName().getNamespaceURI().equals(rtx.getName().getNamespaceURI()) && wtx.getName().getPrefix().equals(wtx.getName().getPrefix())) {
                                        process(wtx.getNodeKey(), rtx.getNodeKey());
                                        break;
                                    }
                                    oldAxis.getTrx().moveTo(oldNodeKey);
                                }
                                newAxis.getTrx().moveTo(newNodeKey);
                            }
                        }
                    }
                    newAxis.getTrx().moveTo(newNodeKey);
                }
        }
    } catch (final SirixException e) {
        LOGWRAPPER.error(e.getMessage(), e);
    }
    return wtx.getNodeKey();
}
Also used : QNm(org.brackit.xquery.atomic.QNm) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) SirixException(org.sirix.exception.SirixException) SirixUsageException(org.sirix.exception.SirixUsageException) VisitorDescendantAxis(org.sirix.axis.visitor.VisitorDescendantAxis) DescendantAxis(org.sirix.axis.DescendantAxis) Axis(org.sirix.api.Axis) VisitorDescendantAxis(org.sirix.axis.visitor.VisitorDescendantAxis) AbstractAxis(org.sirix.axis.AbstractAxis) ChildAxis(org.sirix.axis.ChildAxis) PostOrderAxis(org.sirix.axis.PostOrderAxis) DescendantAxis(org.sirix.axis.DescendantAxis) LevelOrderAxis(org.sirix.axis.LevelOrderAxis)

Example 14 with SirixUsageException

use of org.sirix.exception.SirixUsageException in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method insertElementAsFirstChild.

@Override
public XdmNodeWriteTrx insertElementAsFirstChild(final QNm name) throws SirixException {
    if (!XMLToken.isValidQName(checkNotNull(name))) {
        throw new IllegalArgumentException("The QName is not valid!");
    }
    acquireLock();
    try {
        final Kind kind = mNodeReader.getCurrentNode().getKind();
        if (kind == Kind.ELEMENT || kind == Kind.DOCUMENT) {
            checkAccessAndCommit();
            final long parentKey = mNodeReader.getCurrentNode().getNodeKey();
            final long leftSibKey = Fixed.NULL_NODE_KEY.getStandardProperty();
            final long rightSibKey = ((StructNode) mNodeReader.getCurrentNode()).getFirstChildKey();
            final long pathNodeKey = mBuildPathSummary ? mPathSummaryWriter.getPathNodeKey(name, Kind.ELEMENT) : 0;
            final Optional<SirixDeweyID> id = newFirstChildID();
            final ElementNode node = mNodeFactory.createElementNode(parentKey, leftSibKey, rightSibKey, 0, name, pathNodeKey, id);
            mNodeReader.setCurrentNode(node);
            adaptForInsert(node, InsertPos.ASFIRSTCHILD, PageKind.RECORDPAGE);
            mNodeReader.setCurrentNode(node);
            adaptHashesWithAdd();
            return this;
        } else {
            throw new SirixUsageException("Insert is not allowed if current node is not an ElementNode!");
        }
    } finally {
        unLock();
    }
}
Also used : PageKind(org.sirix.page.PageKind) Kind(org.sirix.node.Kind) SirixDeweyID(org.sirix.node.SirixDeweyID) ElementNode(org.sirix.node.ElementNode) SirixUsageException(org.sirix.exception.SirixUsageException) StructNode(org.sirix.node.interfaces.StructNode)

Example 15 with SirixUsageException

use of org.sirix.exception.SirixUsageException in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method moveSubtreeToFirstChild.

@Override
public XdmNodeWriteTrx moveSubtreeToFirstChild(@Nonnegative final long fromKey) throws SirixException, IllegalArgumentException {
    acquireLock();
    try {
        Preconditions.checkArgument(fromKey >= 0 && fromKey <= getMaxNodeKey(), "Argument must be a valid node key!");
        Preconditions.checkArgument(fromKey != getCurrentNode().getNodeKey(), "Can't move itself to right sibling of itself!");
        @SuppressWarnings("unchecked") final Optional<? extends Node> node = (Optional<? extends Node>) getPageTransaction().getRecord(fromKey, PageKind.RECORDPAGE, -1);
        if (!node.isPresent()) {
            throw new IllegalStateException("Node to move must exist!");
        }
        final Node nodeToMove = node.get();
        if (nodeToMove instanceof StructNode && getCurrentNode().getKind() == Kind.ELEMENT) {
            // Safe to cast (because IStructNode is a subtype of INode).
            checkAncestors(nodeToMove);
            checkAccessAndCommit();
            final ElementNode nodeAnchor = (ElementNode) getCurrentNode();
            // Check that it's not already the first child.
            if (nodeAnchor.getFirstChildKey() != nodeToMove.getNodeKey()) {
                final StructNode toMove = (StructNode) nodeToMove;
                // Adapt index-structures (before move).
                adaptSubtreeForMove(toMove, ChangeType.DELETE);
                // Adapt hashes.
                adaptHashesForMove(toMove);
                // Adapt pointers and merge sibling text nodes.
                adaptForMove(toMove, nodeAnchor, InsertPos.ASFIRSTCHILD);
                mNodeReader.moveTo(toMove.getNodeKey());
                adaptHashesWithAdd();
                // Adapt path summary.
                if (mBuildPathSummary && toMove instanceof NameNode) {
                    final NameNode moved = (NameNode) toMove;
                    mPathSummaryWriter.adaptPathForChangedNode(moved, getName(), moved.getURIKey(), moved.getPrefixKey(), moved.getLocalNameKey(), OPType.MOVED);
                }
                // Adapt index-structures (after move).
                adaptSubtreeForMove(toMove, ChangeType.INSERT);
                // Compute and assign new DeweyIDs.
                if (mDeweyIDsStored) {
                    computeNewDeweyIDs();
                }
            }
            return this;
        } else {
            throw new SirixUsageException("Move is not allowed if moved node is not an ElementNode and the node isn't inserted at an element node!");
        }
    } finally {
        unLock();
    }
}
Also used : NameNode(org.sirix.node.interfaces.NameNode) Optional(java.util.Optional) TextNode(org.sirix.node.TextNode) CommentNode(org.sirix.node.CommentNode) PINode(org.sirix.node.PINode) Node(org.sirix.node.interfaces.Node) ValueNode(org.sirix.node.interfaces.ValueNode) AttributeNode(org.sirix.node.AttributeNode) ImmutableNode(org.sirix.node.interfaces.immutable.ImmutableNode) StructNode(org.sirix.node.interfaces.StructNode) ElementNode(org.sirix.node.ElementNode) NamespaceNode(org.sirix.node.NamespaceNode) NameNode(org.sirix.node.interfaces.NameNode) ElementNode(org.sirix.node.ElementNode) SirixUsageException(org.sirix.exception.SirixUsageException) StructNode(org.sirix.node.interfaces.StructNode)

Aggregations

SirixUsageException (org.sirix.exception.SirixUsageException)22 StructNode (org.sirix.node.interfaces.StructNode)13 SirixDeweyID (org.sirix.node.SirixDeweyID)10 ElementNode (org.sirix.node.ElementNode)8 TextNode (org.sirix.node.TextNode)7 Node (org.sirix.node.interfaces.Node)6 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)6 CommentNode (org.sirix.node.CommentNode)5 PINode (org.sirix.node.PINode)5 NameNode (org.sirix.node.interfaces.NameNode)5 ValueNode (org.sirix.node.interfaces.ValueNode)5 ImmutableNode (org.sirix.node.interfaces.immutable.ImmutableNode)5 QNm (org.brackit.xquery.atomic.QNm)4 SirixThreadedException (org.sirix.exception.SirixThreadedException)4 AttributeNode (org.sirix.node.AttributeNode)4 NamespaceNode (org.sirix.node.NamespaceNode)4 Optional (java.util.Optional)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Axis (org.sirix.api.Axis)2 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)2