use of org.sirix.node.interfaces.StructNode in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method addParentHash.
/**
* Add a hash.
*
* @param startNode start node
*/
private void addParentHash(final ImmutableNode startNode) throws SirixIOException {
switch(mHashKind) {
case ROLLING:
final long hashToAdd = mHash.hashLong(startNode.hashCode()).asLong();
final Node node = (Node) getPageTransaction().prepareEntryForModification(mNodeReader.getCurrentNode().getNodeKey(), PageKind.RECORDPAGE, -1, Optional.empty());
node.setHash(node.getHash() + hashToAdd * PRIME);
if (startNode instanceof StructNode) {
((StructNode) node).setDescendantCount(((StructNode) node).getDescendantCount() + ((StructNode) startNode).getDescendantCount() + 1);
}
break;
case POSTORDER:
break;
default:
}
}
use of org.sirix.node.interfaces.StructNode 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.interfaces.StructNode in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method setAddDescendants.
/**
* Set new descendant count of ancestor after an add-operation.
*
* @param startNode the node which has been removed
* @param nodeToModify node to modify
* @param descendantCount the descendantCount to add
*/
private void setAddDescendants(final ImmutableNode startNode, final Node nodeToModifiy, @Nonnegative final long descendantCount) {
assert startNode != null;
assert descendantCount >= 0;
assert nodeToModifiy != null;
if (startNode instanceof StructNode) {
final StructNode node = (StructNode) nodeToModifiy;
final long oldDescendantCount = node.getDescendantCount();
node.setDescendantCount(oldDescendantCount + descendantCount);
}
}
use of org.sirix.node.interfaces.StructNode in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method insertElementAsLeftSibling.
@Override
public XdmNodeWriteTrx insertElementAsLeftSibling(final QNm name) throws SirixException {
if (!XMLToken.isValidQName(checkNotNull(name))) {
throw new IllegalArgumentException("The QName is not valid!");
}
acquireLock();
try {
if (getCurrentNode() instanceof StructNode && getCurrentNode().getKind() != Kind.DOCUMENT) {
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 = ((StructNode) getCurrentNode()).getLeftSiblingKey();
final long rightSibKey = getCurrentNode().getNodeKey();
final Optional<SirixDeweyID> id = newLeftSiblingID();
final ElementNode node = mNodeFactory.createElementNode(parentKey, leftSibKey, rightSibKey, 0, name, pathNodeKey, id);
mNodeReader.setCurrentNode(node);
adaptForInsert(node, InsertPos.ASLEFTSIBLING, 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.interfaces.StructNode 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();
}
}
Aggregations