use of org.sirix.node.TextNode in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method insertTextAsFirstChild.
@Override
public XdmNodeWriteTrx insertTextAsFirstChild(final String value) throws SirixException {
checkNotNull(value);
acquireLock();
try {
if (getCurrentNode() instanceof StructNode && !value.isEmpty()) {
checkAccessAndCommit();
final long pathNodeKey = getCurrentNode().getNodeKey();
final long parentKey = getCurrentNode().getNodeKey();
final long leftSibKey = Fixed.NULL_NODE_KEY.getStandardProperty();
final long rightSibKey = ((StructNode) getCurrentNode()).getFirstChildKey();
// Update value in case of adjacent text nodes.
if (hasNode(rightSibKey)) {
moveTo(rightSibKey);
if (getCurrentNode().getKind() == Kind.TEXT) {
setValue(new StringBuilder(value).append(getValue()).toString());
adaptHashedWithUpdate(getCurrentNode().getHash());
return this;
}
moveTo(parentKey);
}
// Insert new text node if no adjacent text nodes are found.
final byte[] textValue = getBytes(value);
final Optional<SirixDeweyID> id = newFirstChildID();
final TextNode node = mNodeFactory.createTextNode(parentKey, leftSibKey, rightSibKey, textValue, mCompression, id);
// Adapt local nodes and hashes.
mNodeReader.setCurrentNode(node);
adaptForInsert(node, InsertPos.ASFIRSTCHILD, PageKind.RECORDPAGE);
mNodeReader.setCurrentNode(node);
adaptHashesWithAdd();
// Index text value.
mIndexController.notifyChange(ChangeType.INSERT, node, pathNodeKey);
return this;
} else {
throw new SirixUsageException("Insert is not allowed if current node is not an ElementNode or TextNode!");
}
} finally {
unLock();
}
}
use of org.sirix.node.TextNode in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method insertTextAsLeftSibling.
@Override
public XdmNodeWriteTrx insertTextAsLeftSibling(final String value) throws SirixException {
checkNotNull(value);
acquireLock();
try {
if (getCurrentNode() instanceof StructNode && getCurrentNode().getKind() != Kind.DOCUMENT && !value.isEmpty()) {
checkAccessAndCommit();
final long parentKey = getCurrentNode().getParentKey();
final long leftSibKey = ((StructNode) getCurrentNode()).getLeftSiblingKey();
final long rightSibKey = getCurrentNode().getNodeKey();
// Update value in case of adjacent text nodes.
final StringBuilder builder = new StringBuilder();
if (getCurrentNode().getKind() == Kind.TEXT) {
builder.append(value);
}
builder.append(getValue());
if (!value.equals(builder.toString())) {
setValue(builder.toString());
return this;
}
if (hasNode(leftSibKey)) {
moveTo(leftSibKey);
final StringBuilder valueBuilder = new StringBuilder();
if (getCurrentNode().getKind() == Kind.TEXT) {
valueBuilder.append(getValue()).append(builder);
}
if (!value.equals(valueBuilder.toString())) {
setValue(valueBuilder.toString());
return this;
}
}
// Insert new text node if no adjacent text nodes are found.
moveTo(rightSibKey);
final byte[] textValue = getBytes(builder.toString());
final Optional<SirixDeweyID> id = newLeftSiblingID();
final TextNode node = mNodeFactory.createTextNode(parentKey, leftSibKey, rightSibKey, textValue, mCompression, id);
// Adapt local nodes and hashes.
mNodeReader.setCurrentNode(node);
adaptForInsert(node, InsertPos.ASLEFTSIBLING, PageKind.RECORDPAGE);
mNodeReader.setCurrentNode(node);
adaptHashesWithAdd();
// Get the path node key.
final long pathNodeKey = moveToParent().get().isElement() ? getNameNode().getPathNodeKey() : -1;
mNodeReader.setCurrentNode(node);
// Index text value.
mIndexController.notifyChange(ChangeType.INSERT, node, pathNodeKey);
return this;
} else {
throw new SirixUsageException("Insert is not allowed if current node is not an Element- or Text-node!");
}
} finally {
unLock();
}
}
use of org.sirix.node.TextNode in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method insertTextAsRightSibling.
@Override
public XdmNodeWriteTrx insertTextAsRightSibling(final String value) throws SirixException {
checkNotNull(value);
acquireLock();
try {
if (getCurrentNode() instanceof StructNode && getCurrentNode().getKind() != Kind.DOCUMENT && !value.isEmpty()) {
checkAccessAndCommit();
final long parentKey = getCurrentNode().getParentKey();
final long leftSibKey = getCurrentNode().getNodeKey();
final long rightSibKey = ((StructNode) getCurrentNode()).getRightSiblingKey();
// Update value in case of adjacent text nodes.
final StringBuilder builder = new StringBuilder();
if (getCurrentNode().getKind() == Kind.TEXT) {
builder.append(getValue());
}
builder.append(value);
if (!value.equals(builder.toString())) {
setValue(builder.toString());
return this;
}
if (hasNode(rightSibKey)) {
moveTo(rightSibKey);
if (getCurrentNode().getKind() == Kind.TEXT) {
builder.append(getValue());
}
if (!value.equals(builder.toString())) {
setValue(builder.toString());
return this;
}
}
// Insert new text node if no adjacent text nodes are found.
moveTo(leftSibKey);
final byte[] textValue = getBytes(builder.toString());
final Optional<SirixDeweyID> id = newRightSiblingID();
final TextNode node = mNodeFactory.createTextNode(parentKey, leftSibKey, rightSibKey, textValue, mCompression, id);
// Adapt local nodes and hashes.
mNodeReader.setCurrentNode(node);
adaptForInsert(node, InsertPos.ASRIGHTSIBLING, PageKind.RECORDPAGE);
mNodeReader.setCurrentNode(node);
adaptHashesWithAdd();
// Get the path node key.
final long pathNodeKey = moveToParent().get().isElement() ? getNameNode().getPathNodeKey() : -1;
mNodeReader.setCurrentNode(node);
// Index text value.
mIndexController.notifyChange(ChangeType.INSERT, node, pathNodeKey);
return this;
} else {
throw new SirixUsageException("Insert is not allowed if current node is not an Element- or Text-node or value is empty!");
}
} finally {
unLock();
}
}
use of org.sirix.node.TextNode in project sirix by sirixdb.
the class NodeFactoryImpl method createTextNode.
@Override
public TextNode createTextNode(@Nonnegative final long parentKey, @Nonnegative final long leftSibKey, @Nonnegative final long rightSibKey, @Nonnull final byte[] value, final boolean isCompressed, final Optional<SirixDeweyID> id) throws SirixIOException {
final long revision = mPageWriteTrx.getRevisionNumber();
final NodeDelegate nodeDel = new NodeDelegate(mPageWriteTrx.getActualRevisionRootPage().getMaxNodeKey() + 1, parentKey, 0, revision, id);
final boolean compression = isCompressed && value.length > 10;
final byte[] compressedValue = compression ? Compression.compress(value, Deflater.HUFFMAN_ONLY) : value;
final ValNodeDelegate valDel = new ValNodeDelegate(nodeDel, compressedValue, compression);
final StructNodeDelegate structDel = new StructNodeDelegate(nodeDel, Fixed.NULL_NODE_KEY.getStandardProperty(), rightSibKey, leftSibKey, 0, 0);
return (TextNode) mPageWriteTrx.createEntry(nodeDel.getNodeKey(), new TextNode(valDel, structDel), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
}
use of org.sirix.node.TextNode 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