use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method insertNamespace.
@Override
public XdmNodeWriteTrx insertNamespace(final QNm name, final Movement move) throws SirixException {
if (!XMLToken.isValidQName(checkNotNull(name))) {
throw new IllegalArgumentException("The QName is not valid!");
}
acquireLock();
try {
if (getCurrentNode().getKind() == Kind.ELEMENT) {
checkAccessAndCommit();
for (int i = 0, namespCount = ((ElementNode) getCurrentNode()).getNamespaceCount(); i < namespCount; i++) {
moveToNamespace(i);
final QNm qName = getName();
if (name.getPrefix().equals(qName.getPrefix())) {
throw new SirixUsageException("Duplicate namespace!");
}
moveToParent();
}
final long pathNodeKey = mBuildPathSummary ? mPathSummaryWriter.getPathNodeKey(name, Kind.NAMESPACE) : 0;
final long elementKey = getCurrentNode().getNodeKey();
final Optional<SirixDeweyID> id = newNamespaceID();
final NamespaceNode node = mNodeFactory.createNamespaceNode(elementKey, name, pathNodeKey, id);
final Node parentNode = (Node) getPageTransaction().prepareEntryForModification(node.getParentKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
((ElementNode) parentNode).insertNamespace(node.getNodeKey());
mNodeReader.setCurrentNode(node);
adaptHashesWithAdd();
if (move == Movement.TOPARENT) {
moveToParent();
}
return this;
} else {
throw new SirixUsageException("Insert is not allowed if current node is not an ElementNode!");
}
} finally {
unLock();
}
}
use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.
the class PageUtils method createTree.
/**
* Create the initial tree structure.
*
* @param reference reference from revision root
* @param pageKind the page kind
*/
public static void createTree(@Nonnull PageReference reference, final PageKind pageKind, final int index, final PageReadTrx pageReadTrx, final TransactionIntentLog log) {
Page page = null;
// Level page count exponent from the configuration.
final int[] levelPageCountExp = pageReadTrx.getUberPage().getPageCountExp(pageKind);
// Remaining levels.
for (int i = 0, l = levelPageCountExp.length; i < l; i++) {
page = new IndirectPage();
log.put(reference, new PageContainer(page, page));
reference = page.getReference(0);
}
// Create new record page.
final UnorderedKeyValuePage ndp = new UnorderedKeyValuePage(Fixed.ROOT_PAGE_KEY.getStandardProperty(), pageKind, Constants.NULL_ID_LONG, pageReadTrx);
// Create a {@link DocumentRootNode}.
final Optional<SirixDeweyID> id = pageReadTrx.getResourceManager().getResourceConfig().mDeweyIDsStored ? Optional.of(SirixDeweyID.newRootID()) : Optional.empty();
final NodeDelegate nodeDel = new NodeDelegate(Fixed.DOCUMENT_NODE_KEY.getStandardProperty(), Fixed.NULL_NODE_KEY.getStandardProperty(), Fixed.NULL_NODE_KEY.getStandardProperty(), 0, id);
final StructNodeDelegate strucDel = new StructNodeDelegate(nodeDel, Fixed.NULL_NODE_KEY.getStandardProperty(), Fixed.NULL_NODE_KEY.getStandardProperty(), Fixed.NULL_NODE_KEY.getStandardProperty(), 0, 0);
ndp.setEntry(0L, new DocumentRootNode(nodeDel, strucDel));
log.put(reference, new PageContainer(ndp, ndp));
}
use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.
the class XdmNodeReadTrxImpl method getRightSiblingDeweyID.
@Override
public Optional<SirixDeweyID> getRightSiblingDeweyID() {
if (mResourceManager.getResourceConfig().mDeweyIDsStored) {
final StructNode node = getStructuralNode();
final long nodeKey = node.getNodeKey();
Optional<SirixDeweyID> deweyID = Optional.<SirixDeweyID>empty();
if (node.hasRightSibling()) {
// Right sibling node.
deweyID = moveTo(node.getRightSiblingKey()).get().getDeweyID();
}
moveTo(nodeKey);
return deweyID;
}
return Optional.<SirixDeweyID>empty();
}
use of org.sirix.node.SirixDeweyID 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.SirixDeweyID in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method computeNewDeweyIDs.
/**
* Compute the new DeweyIDs.
*
* @throws SirixException if anything went wrong
*/
private void computeNewDeweyIDs() throws SirixException {
SirixDeweyID id = null;
if (hasLeftSibling() && hasRightSibling()) {
id = SirixDeweyID.newBetween(getLeftSiblingDeweyID().get(), getRightSiblingDeweyID().get());
} else if (hasLeftSibling()) {
id = SirixDeweyID.newBetween(getLeftSiblingDeweyID().get(), null);
} else if (hasRightSibling()) {
id = SirixDeweyID.newBetween(null, getRightSiblingDeweyID().get());
} else {
id = mNodeReader.getParentDeweyID().get().getNewChildID();
}
assert id != null;
final long nodeKey = mNodeReader.getCurrentNode().getNodeKey();
final StructNode root = (StructNode) getPageTransaction().prepareEntryForModification(nodeKey, PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
root.setDeweyID(Optional.of(id));
if (root.hasFirstChild()) {
final Node firstChild = (Node) getPageTransaction().prepareEntryForModification(root.getFirstChildKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
firstChild.setDeweyID(Optional.of(id.getNewChildID()));
int previousLevel = getDeweyID().get().getLevel();
mNodeReader.moveTo(firstChild.getNodeKey());
int attributeNr = 0;
int nspNr = 0;
for (@SuppressWarnings("unused") final long key : LevelOrderAxis.newBuilder(this).includeNonStructuralNodes().build()) {
Optional<SirixDeweyID> deweyID = Optional.<SirixDeweyID>empty();
if (isAttribute()) {
final long attNodeKey = mNodeReader.getNodeKey();
if (attributeNr == 0) {
deweyID = Optional.of(mNodeReader.getParentDeweyID().get().getNewAttributeID());
} else {
mNodeReader.moveTo(attributeNr - 1);
deweyID = Optional.of(SirixDeweyID.newBetween(mNodeReader.getNode().getDeweyID().get(), null));
}
mNodeReader.moveTo(attNodeKey);
attributeNr++;
} else if (isNamespace()) {
final long nspNodeKey = mNodeReader.getNodeKey();
if (nspNr == 0) {
deweyID = Optional.of(mNodeReader.getParentDeweyID().get().getNewNamespaceID());
} else {
mNodeReader.moveTo(nspNr - 1);
deweyID = Optional.of(SirixDeweyID.newBetween(mNodeReader.getNode().getDeweyID().get(), null));
}
mNodeReader.moveTo(nspNodeKey);
nspNr++;
} else {
attributeNr = 0;
nspNr = 0;
if (previousLevel + 1 == getDeweyID().get().getLevel()) {
if (mNodeReader.hasLeftSibling()) {
deweyID = Optional.of(SirixDeweyID.newBetween(getLeftSiblingDeweyID().get(), null));
} else {
deweyID = Optional.of(getParentDeweyID().get().getNewChildID());
}
} else {
previousLevel++;
deweyID = Optional.of(getParentDeweyID().get().getNewChildID());
}
}
final Node node = (Node) getPageTransaction().prepareEntryForModification(mNodeReader.getCurrentNode().getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
node.setDeweyID(deweyID);
}
mNodeReader.moveTo(nodeKey);
}
}
Aggregations