use of org.sirix.node.ElementNode 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();
}
}
use of org.sirix.node.ElementNode 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();
}
}
use of org.sirix.node.ElementNode in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method insertElementAsRightSibling.
@Override
public XdmNodeWriteTrx insertElementAsRightSibling(final QNm name) throws SirixException {
if (!XMLToken.isValidQName(checkNotNull(name))) {
throw new IllegalArgumentException("The QName is not valid!");
}
acquireLock();
try {
if (getCurrentNode() instanceof StructNode && !isDocumentRoot()) {
checkAccessAndCommit();
final long key = getCurrentNode().getNodeKey();
moveToParent();
final long pathNodeKey = mBuildPathSummary ? mPathSummaryWriter.getPathNodeKey(name, Kind.ELEMENT) : 0;
moveTo(key);
final long parentKey = getCurrentNode().getParentKey();
final long leftSibKey = getCurrentNode().getNodeKey();
final long rightSibKey = ((StructNode) getCurrentNode()).getRightSiblingKey();
final Optional<SirixDeweyID> id = newRightSiblingID();
final ElementNode node = mNodeFactory.createElementNode(parentKey, leftSibKey, rightSibKey, 0, name, pathNodeKey, id);
mNodeReader.setCurrentNode(node);
adaptForInsert(node, InsertPos.ASRIGHTSIBLING, PageKind.RECORDPAGE);
mNodeReader.setCurrentNode(node);
adaptHashesWithAdd();
return this;
} else {
throw new SirixUsageException("Insert is not allowed if current node is not an StructuralNode (either Text or Element)!");
}
} finally {
unLock();
}
}
use of org.sirix.node.ElementNode in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method moveSubtreeToRightSibling.
@Override
public XdmNodeWriteTrx moveSubtreeToRightSibling(@Nonnegative final long fromKey) throws SirixException {
acquireLock();
try {
if (fromKey < 0 || fromKey > getMaxNodeKey()) {
throw new IllegalArgumentException("Argument must be a valid node key!");
}
if (fromKey == getCurrentNode().getNodeKey()) {
throw new IllegalArgumentException("Can't move itself to first child of itself!");
}
// Save: Every node in the "usual" node page is of type Node.
@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() instanceof StructNode) {
final StructNode toMove = (StructNode) nodeToMove;
checkAncestors(toMove);
checkAccessAndCommit();
final StructNode nodeAnchor = (StructNode) getCurrentNode();
if (nodeAnchor.getRightSiblingKey() != nodeToMove.getNodeKey()) {
final long parentKey = toMove.getParentKey();
// Adapt hashes.
adaptHashesForMove(toMove);
// Adapt pointers and merge sibling text nodes.
adaptForMove(toMove, nodeAnchor, InsertPos.ASRIGHTSIBLING);
mNodeReader.moveTo(toMove.getNodeKey());
adaptHashesWithAdd();
// Adapt path summary.
if (mBuildPathSummary && toMove instanceof NameNode) {
final NameNode moved = (NameNode) toMove;
final OPType type = moved.getParentKey() == parentKey ? OPType.MOVEDSAMELEVEL : OPType.MOVED;
mPathSummaryWriter.adaptPathForChangedNode(moved, getName(), moved.getURIKey(), moved.getPrefixKey(), moved.getLocalNameKey(), type);
}
// Recompute DeweyIDs if they are used.
if (mDeweyIDsStored) {
computeNewDeweyIDs();
}
}
return this;
} else {
throw new SirixUsageException("Move is not allowed if moved node is not an ElementNode or TextNode and the node isn't inserted at an ElementNode or TextNode!");
}
} finally {
unLock();
}
}
use of org.sirix.node.ElementNode in project sirix by sirixdb.
the class NodeFactoryImpl method createElementNode.
@Override
public ElementNode createElementNode(@Nonnegative final long parentKey, @Nonnegative final long leftSibKey, @Nonnegative final long rightSibKey, final long hash, @Nonnull final QNm name, @Nonnegative final long pathNodeKey, final Optional<SirixDeweyID> id) throws SirixIOException {
final int uriKey = name.getNamespaceURI() != null && !name.getNamespaceURI().isEmpty() ? mPageWriteTrx.createNameKey(name.getNamespaceURI(), Kind.NAMESPACE) : -1;
final int prefixKey = name.getPrefix() != null && !name.getPrefix().isEmpty() ? mPageWriteTrx.createNameKey(name.getPrefix(), Kind.ELEMENT) : -1;
final int localNameKey = mPageWriteTrx.createNameKey(name.getLocalName(), Kind.ELEMENT);
final long revision = mPageWriteTrx.getRevisionNumber();
final NodeDelegate nodeDel = new NodeDelegate(mPageWriteTrx.getActualRevisionRootPage().getMaxNodeKey() + 1, parentKey, 0, revision, id);
final StructNodeDelegate structDel = new StructNodeDelegate(nodeDel, Fixed.NULL_NODE_KEY.getStandardProperty(), rightSibKey, leftSibKey, 0, 0);
final NameNodeDelegate nameDel = new NameNodeDelegate(nodeDel, uriKey, prefixKey, localNameKey, pathNodeKey);
return (ElementNode) mPageWriteTrx.createEntry(nodeDel.getNodeKey(), new ElementNode(structDel, nameDel, new ArrayList<Long>(), HashBiMap.<Long, Long>create(), new ArrayList<Long>(), name), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
}
Aggregations