use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method pi.
/**
* Processing instruction.
*
* @param target target PI
* @param content content of PI
* @param insert insertion location
* @throws SirixException if any unexpected error occurs
*/
private XdmNodeWriteTrx pi(final String target, final String content, final Insert insert) throws SirixException {
final byte[] targetBytes = getBytes(target);
if (!XMLToken.isNCName(checkNotNull(targetBytes))) {
throw new IllegalArgumentException("The target is not valid!");
}
if (content.contains("?>-")) {
throw new SirixUsageException("The content must not contain '?>-'");
}
acquireLock();
try {
if (getCurrentNode() instanceof StructNode) {
checkAccessAndCommit();
// Insert new processing instruction node.
final byte[] processingContent = getBytes(content);
long parentKey = 0;
long leftSibKey = 0;
long rightSibKey = 0;
InsertPos pos = InsertPos.ASFIRSTCHILD;
Optional<SirixDeweyID> id = Optional.<SirixDeweyID>empty();
switch(insert) {
case ASFIRSTCHILD:
parentKey = getCurrentNode().getNodeKey();
leftSibKey = Fixed.NULL_NODE_KEY.getStandardProperty();
rightSibKey = ((StructNode) getCurrentNode()).getFirstChildKey();
id = newFirstChildID();
break;
case ASRIGHTSIBLING:
parentKey = getCurrentNode().getParentKey();
leftSibKey = getCurrentNode().getNodeKey();
rightSibKey = ((StructNode) getCurrentNode()).getRightSiblingKey();
pos = InsertPos.ASRIGHTSIBLING;
id = newRightSiblingID();
break;
case ASLEFTSIBLING:
parentKey = getCurrentNode().getParentKey();
leftSibKey = ((StructNode) getCurrentNode()).getLeftSiblingKey();
rightSibKey = getCurrentNode().getNodeKey();
pos = InsertPos.ASLEFTSIBLING;
id = newLeftSiblingID();
break;
default:
throw new IllegalStateException("Insert location not known!");
}
final QNm targetName = new QNm(target);
final long pathNodeKey = mBuildPathSummary ? mPathSummaryWriter.getPathNodeKey(targetName, Kind.PROCESSING_INSTRUCTION) : 0;
final PINode node = mNodeFactory.createPINode(parentKey, leftSibKey, rightSibKey, targetName, processingContent, mCompression, pathNodeKey, id);
// Adapt local nodes and hashes.
mNodeReader.setCurrentNode(node);
adaptForInsert(node, pos, PageKind.RECORDPAGE);
mNodeReader.setCurrentNode(node);
adaptHashesWithAdd();
return this;
} else {
throw new SirixUsageException("Current node must be a structural node!");
}
} finally {
unLock();
}
}
use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method comment.
/**
* Comment node.
*
* @param value value of comment
* @param insert insertion location
* @throws SirixException if any unexpected error occurs
*/
private XdmNodeWriteTrx comment(final String value, final Insert insert) throws SirixException {
// Produces a NPE if value is null (what we want).
if (value.contains("--")) {
throw new SirixUsageException("Character sequence \"--\" is not allowed in comment content!");
}
if (value.endsWith("-")) {
throw new SirixUsageException("Comment content must not end with \"-\"!");
}
acquireLock();
try {
if (getCurrentNode() instanceof StructNode && (getCurrentNode().getKind() != Kind.DOCUMENT || (getCurrentNode().getKind() == Kind.DOCUMENT && insert == Insert.ASFIRSTCHILD))) {
checkAccessAndCommit();
// Insert new comment node.
final byte[] commentValue = getBytes(value);
long parentKey = 0;
long leftSibKey = 0;
long rightSibKey = 0;
InsertPos pos = InsertPos.ASFIRSTCHILD;
Optional<SirixDeweyID> id = Optional.<SirixDeweyID>empty();
switch(insert) {
case ASFIRSTCHILD:
parentKey = getCurrentNode().getNodeKey();
leftSibKey = Fixed.NULL_NODE_KEY.getStandardProperty();
rightSibKey = ((StructNode) getCurrentNode()).getFirstChildKey();
id = newFirstChildID();
break;
case ASRIGHTSIBLING:
parentKey = getCurrentNode().getParentKey();
leftSibKey = getCurrentNode().getNodeKey();
rightSibKey = ((StructNode) getCurrentNode()).getRightSiblingKey();
pos = InsertPos.ASRIGHTSIBLING;
id = newRightSiblingID();
break;
case ASLEFTSIBLING:
parentKey = getCurrentNode().getParentKey();
leftSibKey = ((StructNode) getCurrentNode()).getLeftSiblingKey();
rightSibKey = getCurrentNode().getNodeKey();
pos = InsertPos.ASLEFTSIBLING;
id = newLeftSiblingID();
break;
default:
throw new IllegalStateException("Insert location not known!");
}
final CommentNode node = mNodeFactory.createCommentNode(parentKey, leftSibKey, rightSibKey, commentValue, mCompression, id);
// Adapt local nodes and hashes.
mNodeReader.setCurrentNode(node);
adaptForInsert(node, pos, PageKind.RECORDPAGE);
mNodeReader.setCurrentNode(node);
adaptHashesWithAdd();
return this;
} else {
throw new SirixUsageException("Current node must be a structural node!");
}
} finally {
unLock();
}
}
use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method newLeftSiblingID.
/**
* Get an optional left sibling {@link SirixDeweyID} reference.
*
* @return optional left sibling {@link SirixDeweyID} reference
* @throws SirixException if generating an ID fails
*/
private Optional<SirixDeweyID> newLeftSiblingID() throws SirixException {
Optional<SirixDeweyID> id = Optional.<SirixDeweyID>empty();
if (mDeweyIDsStored) {
final SirixDeweyID currID = mNodeReader.getCurrentNode().getDeweyID().get();
if (mNodeReader.hasLeftSibling()) {
mNodeReader.moveToLeftSibling();
id = Optional.of(SirixDeweyID.newBetween(mNodeReader.getCurrentNode().getDeweyID().get(), currID));
mNodeReader.moveToRightSibling();
} else {
id = Optional.of(SirixDeweyID.newBetween(null, currID));
}
}
return id;
}
use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method insertElementAsRightSibling.
@Override
public XdmNodeWriteTrx insertElementAsRightSibling(final QNm name) throws SirixException {
if (!XMLToken.isValidQName(checkNotNull(name))) {
throw new IllegalArgumentException("The QName is not valid!");
}
acquireLock();
try {
if (getCurrentNode() instanceof StructNode && !isDocumentRoot()) {
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 = getCurrentNode().getNodeKey();
final long rightSibKey = ((StructNode) getCurrentNode()).getRightSiblingKey();
final Optional<SirixDeweyID> id = newRightSiblingID();
final ElementNode node = mNodeFactory.createElementNode(parentKey, leftSibKey, rightSibKey, 0, name, pathNodeKey, id);
mNodeReader.setCurrentNode(node);
adaptForInsert(node, InsertPos.ASRIGHTSIBLING, 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.SirixDeweyID in project sirix by sirixdb.
the class PageWriteTrxImpl method removeEntry.
@Override
public void removeEntry(final Long recordKey, @Nonnull final PageKind pageKind, final int index, final Optional<UnorderedKeyValuePage> keyValuePage) throws SirixIOException {
mPageRtx.assertNotClosed();
final long nodePageKey = mPageRtx.pageKey(recordKey);
final PageContainer cont = prepareRecordPage(nodePageKey, index, pageKind);
final Optional<Record> node = getRecord(recordKey, pageKind, index);
if (node.isPresent()) {
final Record nodeToDel = node.get();
final Node delNode = new DeletedNode(new NodeDelegate(nodeToDel.getNodeKey(), -1, -1, -1, Optional.<SirixDeweyID>empty()));
((UnorderedKeyValuePage) cont.getModified()).setEntry(delNode.getNodeKey(), delNode);
((UnorderedKeyValuePage) cont.getComplete()).setEntry(delNode.getNodeKey(), delNode);
} else {
throw new IllegalStateException("Node not found!");
}
}
Aggregations