use of org.sirix.node.SirixDeweyID 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.SirixDeweyID 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.SirixDeweyID 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.SirixDeweyID in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method newRightSiblingID.
/**
* Get an optional right sibling {@link SirixDeweyID} reference.
*
* @return optional right sibling {@link SirixDeweyID} reference
* @throws SirixException if generating an ID fails
*/
private Optional<SirixDeweyID> newRightSiblingID() throws SirixException {
Optional<SirixDeweyID> id = Optional.<SirixDeweyID>empty();
if (mDeweyIDsStored) {
final SirixDeweyID currID = mNodeReader.getCurrentNode().getDeweyID().get();
if (mNodeReader.hasRightSibling()) {
mNodeReader.moveToRightSibling();
id = Optional.of(SirixDeweyID.newBetween(currID, mNodeReader.getCurrentNode().getDeweyID().get()));
mNodeReader.moveToLeftSibling();
} else {
id = Optional.of(SirixDeweyID.newBetween(currID, null));
}
}
return id;
}
use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method insertAttribute.
@Override
public XdmNodeWriteTrx insertAttribute(final QNm name, final String value, final Movement move) throws SirixException {
checkNotNull(value);
if (!XMLToken.isValidQName(checkNotNull(name))) {
throw new IllegalArgumentException("The QName is not valid!");
}
acquireLock();
try {
if (getCurrentNode().getKind() == Kind.ELEMENT) {
checkAccessAndCommit();
/*
* Update value in case of the same attribute name is found but the attribute to insert has
* a different value (otherwise an exception is thrown because of a duplicate attribute
* which would otherwise be inserted!).
*/
final ElementNode element = (ElementNode) getCurrentNode();
final Optional<Long> attKey = element.getAttributeKeyByName(name);
if (attKey.isPresent()) {
moveTo(attKey.get());
final QNm qName = getName();
if (name.equals(qName)) {
if (getValue().equals(value)) {
throw new SirixUsageException("Duplicate attribute!");
} else {
setValue(value);
}
}
moveToParent();
}
// Get the path node key.
final long pathNodeKey = mNodeReader.mResourceManager.getResourceConfig().mPathSummary ? mPathSummaryWriter.getPathNodeKey(name, Kind.ATTRIBUTE) : 0;
final byte[] attValue = getBytes(value);
final Optional<SirixDeweyID> id = newAttributeID();
final long elementKey = getCurrentNode().getNodeKey();
final AttributeNode node = mNodeFactory.createAttributeNode(elementKey, name, attValue, pathNodeKey, id);
final Node parentNode = (Node) getPageTransaction().prepareEntryForModification(node.getParentKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
((ElementNode) parentNode).insertAttribute(node.getNodeKey(), node.getPrefixKey() + node.getLocalNameKey());
mNodeReader.setCurrentNode(node);
adaptHashesWithAdd();
// Index text value.
mIndexController.notifyChange(ChangeType.INSERT, node, pathNodeKey);
if (move == Movement.TOPARENT) {
moveToParent();
}
return this;
} else {
throw new SirixUsageException("Insert is not allowed if current node is not an ElementNode!");
}
} finally {
unLock();
}
}
Aggregations