use of org.sirix.node.interfaces.StructNode 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();
}
}
Aggregations