Search in sources :

Example 1 with AttributeNode

use of org.sirix.node.AttributeNode in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method insertAttribute.

@Override
public XdmNodeWriteTrx insertAttribute(final QNm name, final String value, final Movement move) throws SirixException {
    checkNotNull(value);
    if (!XMLToken.isValidQName(checkNotNull(name))) {
        throw new IllegalArgumentException("The QName is not valid!");
    }
    acquireLock();
    try {
        if (getCurrentNode().getKind() == Kind.ELEMENT) {
            checkAccessAndCommit();
            /*
         * Update value in case of the same attribute name is found but the attribute to insert has
         * a different value (otherwise an exception is thrown because of a duplicate attribute
         * which would otherwise be inserted!).
         */
            final ElementNode element = (ElementNode) getCurrentNode();
            final Optional<Long> attKey = element.getAttributeKeyByName(name);
            if (attKey.isPresent()) {
                moveTo(attKey.get());
                final QNm qName = getName();
                if (name.equals(qName)) {
                    if (getValue().equals(value)) {
                        throw new SirixUsageException("Duplicate attribute!");
                    } else {
                        setValue(value);
                    }
                }
                moveToParent();
            }
            // Get the path node key.
            final long pathNodeKey = mNodeReader.mResourceManager.getResourceConfig().mPathSummary ? mPathSummaryWriter.getPathNodeKey(name, Kind.ATTRIBUTE) : 0;
            final byte[] attValue = getBytes(value);
            final Optional<SirixDeweyID> id = newAttributeID();
            final long elementKey = getCurrentNode().getNodeKey();
            final AttributeNode node = mNodeFactory.createAttributeNode(elementKey, name, attValue, pathNodeKey, id);
            final Node parentNode = (Node) getPageTransaction().prepareEntryForModification(node.getParentKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
            ((ElementNode) parentNode).insertAttribute(node.getNodeKey(), node.getPrefixKey() + node.getLocalNameKey());
            mNodeReader.setCurrentNode(node);
            adaptHashesWithAdd();
            // Index text value.
            mIndexController.notifyChange(ChangeType.INSERT, node, pathNodeKey);
            if (move == Movement.TOPARENT) {
                moveToParent();
            }
            return this;
        } else {
            throw new SirixUsageException("Insert is not allowed if current node is not an ElementNode!");
        }
    } finally {
        unLock();
    }
}
Also used : AttributeNode(org.sirix.node.AttributeNode) 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) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) ElementNode(org.sirix.node.ElementNode) SirixUsageException(org.sirix.exception.SirixUsageException) SirixDeweyID(org.sirix.node.SirixDeweyID) QNm(org.brackit.xquery.atomic.QNm)

Example 2 with AttributeNode

use of org.sirix.node.AttributeNode in project sirix by sirixdb.

the class NodeFactoryImpl method createAttributeNode.

@Override
public AttributeNode createAttributeNode(@Nonnegative final long parentKey, @Nonnull final QNm name, @Nonnull final byte[] value, @Nonnegative final long pathNodeKey, final Optional<SirixDeweyID> id) throws SirixIOException {
    final long revision = mPageWriteTrx.getRevisionNumber();
    final int uriKey = mPageWriteTrx.createNameKey(name.getNamespaceURI(), Kind.NAMESPACE);
    final int prefixKey = name.getPrefix() != null && !name.getPrefix().isEmpty() ? mPageWriteTrx.createNameKey(name.getPrefix(), Kind.ATTRIBUTE) : -1;
    final int localNameKey = mPageWriteTrx.createNameKey(name.getLocalName(), Kind.ATTRIBUTE);
    final NodeDelegate nodeDel = new NodeDelegate(mPageWriteTrx.getActualRevisionRootPage().getMaxNodeKey() + 1, parentKey, 0, revision, id);
    final NameNodeDelegate nameDel = new NameNodeDelegate(nodeDel, uriKey, prefixKey, localNameKey, pathNodeKey);
    final ValNodeDelegate valDel = new ValNodeDelegate(nodeDel, value, false);
    return (AttributeNode) mPageWriteTrx.createEntry(nodeDel.getNodeKey(), new AttributeNode(nodeDel, nameDel, valDel, name), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
}
Also used : AttributeNode(org.sirix.node.AttributeNode) ValNodeDelegate(org.sirix.node.delegates.ValNodeDelegate) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) NodeDelegate(org.sirix.node.delegates.NodeDelegate) StructNodeDelegate(org.sirix.node.delegates.StructNodeDelegate) NameNodeDelegate(org.sirix.node.delegates.NameNodeDelegate) ValNodeDelegate(org.sirix.node.delegates.ValNodeDelegate) NameNodeDelegate(org.sirix.node.delegates.NameNodeDelegate)

Aggregations

AttributeNode (org.sirix.node.AttributeNode)2 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)2 QNm (org.brackit.xquery.atomic.QNm)1 SirixUsageException (org.sirix.exception.SirixUsageException)1 CommentNode (org.sirix.node.CommentNode)1 ElementNode (org.sirix.node.ElementNode)1 NamespaceNode (org.sirix.node.NamespaceNode)1 PINode (org.sirix.node.PINode)1 SirixDeweyID (org.sirix.node.SirixDeweyID)1 TextNode (org.sirix.node.TextNode)1 NameNodeDelegate (org.sirix.node.delegates.NameNodeDelegate)1 NodeDelegate (org.sirix.node.delegates.NodeDelegate)1 StructNodeDelegate (org.sirix.node.delegates.StructNodeDelegate)1 ValNodeDelegate (org.sirix.node.delegates.ValNodeDelegate)1 NameNode (org.sirix.node.interfaces.NameNode)1 Node (org.sirix.node.interfaces.Node)1 StructNode (org.sirix.node.interfaces.StructNode)1 ValueNode (org.sirix.node.interfaces.ValueNode)1 ImmutableNode (org.sirix.node.interfaces.immutable.ImmutableNode)1