use of org.sirix.node.interfaces.immutable.ImmutableNode in project sirix by sirixdb.
the class XdmNodeReadTrxImpl method moveTo.
@Override
public Move<? extends XdmNodeReadTrx> moveTo(final long nodeKey) {
assertNotClosed();
// Remember old node and fetch new one.
final ImmutableNode oldNode = mCurrentNode;
Optional<? extends Record> newNode;
try {
// Immediately return node from item list if node key negative.
if (nodeKey < 0) {
if (mItemList.size() > 0) {
newNode = mItemList.getItem(nodeKey);
} else {
newNode = Optional.empty();
}
} else {
final Optional<? extends Record> node = mPageReadTrx.getRecord(nodeKey, PageKind.RECORDPAGE, -1);
newNode = node;
}
} catch (final SirixIOException e) {
newNode = Optional.empty();
}
if (newNode.isPresent()) {
mCurrentNode = (Node) newNode.get();
return Move.moved(this);
} else {
mCurrentNode = oldNode;
return Move.notMoved();
}
}
use of org.sirix.node.interfaces.immutable.ImmutableNode in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method rollingUpdate.
/**
* Adapting the structure with a rolling hash for all ancestors only with update.
*
* @param oldHash pOldHash to be removed
* @throws SirixIOException if anything weird happened
*/
private void rollingUpdate(final long oldHash) throws SirixIOException {
final ImmutableNode newNode = getCurrentNode();
final long hash = newNode.hashCode();
final long newNodeHash = hash;
long resultNew = hash;
// go the path to the root
do {
final Node node = (Node) getPageTransaction().prepareEntryForModification(mNodeReader.getCurrentNode().getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
if (node.getNodeKey() == newNode.getNodeKey()) {
resultNew = node.getHash() - oldHash;
resultNew = resultNew + newNodeHash;
} else {
resultNew = node.getHash() - oldHash * PRIME;
resultNew = resultNew + newNodeHash * PRIME;
}
node.setHash(resultNew);
} while (moveTo(mNodeReader.getCurrentNode().getParentKey()).hasMoved());
mNodeReader.setCurrentNode(newNode);
}
use of org.sirix.node.interfaces.immutable.ImmutableNode 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.immutable.ImmutableNode in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method addHashAndDescendantCount.
/**
* Add a hash and the descendant count.
*/
private void addHashAndDescendantCount() throws SirixIOException {
switch(mHashKind) {
case ROLLING:
// Setup.
final ImmutableNode startNode = getCurrentNode();
final long oldDescendantCount = mNodeReader.getStructuralNode().getDescendantCount();
final long descendantCount = oldDescendantCount == 0 ? 1 : oldDescendantCount + 1;
// Set start node.
final long hashToAdd = mHash.hashLong(startNode.hashCode()).asLong();
Node node = (Node) getPageTransaction().prepareEntryForModification(mNodeReader.getCurrentNode().getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
node.setHash(hashToAdd);
// Set parent node.
if (startNode.hasParent()) {
moveToParent();
node = (Node) getPageTransaction().prepareEntryForModification(mNodeReader.getCurrentNode().getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
node.setHash(node.getHash() + hashToAdd * PRIME);
setAddDescendants(startNode, node, descendantCount);
}
mNodeReader.setCurrentNode(startNode);
break;
case POSTORDER:
postorderAdd();
break;
default:
}
}
use of org.sirix.node.interfaces.immutable.ImmutableNode in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method postorderAdd.
/**
* Adapting the structure with a rolling hash for all ancestors only with insert.
*
* @throws SirixIOException if anything weird happened
*/
private void postorderAdd() throws SirixIOException {
// start with hash to add
final ImmutableNode startNode = getCurrentNode();
// long for adapting the hash of the parent
long hashCodeForParent = 0;
// adapting the parent if the current node is no structural one.
if (!(startNode instanceof StructNode)) {
final Node node = (Node) getPageTransaction().prepareEntryForModification(mNodeReader.getCurrentNode().getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
node.setHash(mHash.hashLong(mNodeReader.getCurrentNode().hashCode()).asLong());
moveTo(mNodeReader.getCurrentNode().getParentKey());
}
// Cursor to root
StructNode cursorToRoot;
do {
cursorToRoot = (StructNode) getPageTransaction().prepareEntryForModification(mNodeReader.getCurrentNode().getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
hashCodeForParent = mNodeReader.getCurrentNode().hashCode() + hashCodeForParent * PRIME;
// Caring about attributes and namespaces if node is an element.
if (cursorToRoot.getKind() == Kind.ELEMENT) {
final ElementNode currentElement = (ElementNode) cursorToRoot;
// setting the attributes and namespaces
final int attCount = ((ElementNode) cursorToRoot).getAttributeCount();
for (int i = 0; i < attCount; i++) {
moveTo(currentElement.getAttributeKey(i));
hashCodeForParent = mNodeReader.getCurrentNode().hashCode() + hashCodeForParent * PRIME;
}
final int nspCount = ((ElementNode) cursorToRoot).getNamespaceCount();
for (int i = 0; i < nspCount; i++) {
moveTo(currentElement.getNamespaceKey(i));
hashCodeForParent = mNodeReader.getCurrentNode().hashCode() + hashCodeForParent * PRIME;
}
moveTo(cursorToRoot.getNodeKey());
}
// Caring about the children of a node
if (moveTo(mNodeReader.getStructuralNode().getFirstChildKey()).hasMoved()) {
do {
hashCodeForParent = mNodeReader.getCurrentNode().getHash() + hashCodeForParent * PRIME;
} while (moveTo(mNodeReader.getStructuralNode().getRightSiblingKey()).hasMoved());
moveTo(mNodeReader.getStructuralNode().getParentKey());
}
// setting hash and resetting hash
cursorToRoot.setHash(hashCodeForParent);
hashCodeForParent = 0;
} while (moveTo(cursorToRoot.getParentKey()).hasMoved());
mNodeReader.setCurrentNode(startNode);
}
Aggregations