use of org.sirix.exception.SirixUsageException 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.exception.SirixUsageException 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.exception.SirixUsageException 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();
}
}
use of org.sirix.exception.SirixUsageException 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.exception.SirixUsageException in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method remove.
@Override
public XdmNodeWriteTrx remove() throws SirixException {
checkAccessAndCommit();
acquireLock();
try {
if (getCurrentNode().getKind() == Kind.DOCUMENT) {
throw new SirixUsageException("Document root can not be removed.");
} else if (getCurrentNode() instanceof StructNode) {
final StructNode node = (StructNode) mNodeReader.getCurrentNode();
// Remove subtree.
for (final Axis axis = new PostOrderAxis(this); axis.hasNext(); ) {
axis.next();
// Remove name.
removeName();
// Remove namespaces and attributes.
removeNonStructural();
// Remove text value.
removeValue();
// Then remove node.
getPageTransaction().removeEntry(getCurrentNode().getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
}
// Adapt hashes and neighbour nodes as well as the name from the
// NamePage mapping if it's not a text node.
mNodeReader.setCurrentNode(node);
adaptHashesWithRemove();
adaptForRemove(node, PageKind.RECORDPAGE);
mNodeReader.setCurrentNode(node);
// Remove the name of subtree-root.
if (node.getKind() == Kind.ELEMENT) {
removeName();
}
// of text merges.
if (mNodeReader.hasRightSibling() && moveTo(node.getRightSiblingKey()).hasMoved()) {
} else if (node.hasLeftSibling()) {
moveTo(node.getLeftSiblingKey());
} else {
moveTo(node.getParentKey());
}
} else if (getCurrentNode().getKind() == Kind.ATTRIBUTE) {
final ImmutableNode node = mNodeReader.getCurrentNode();
final ElementNode parent = (ElementNode) getPageTransaction().prepareEntryForModification(node.getParentKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
parent.removeAttribute(node.getNodeKey());
adaptHashesWithRemove();
getPageTransaction().removeEntry(node.getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
removeName();
mIndexController.notifyChange(ChangeType.DELETE, getNode(), parent.getPathNodeKey());
moveToParent();
} else if (getCurrentNode().getKind() == Kind.NAMESPACE) {
final ImmutableNode node = mNodeReader.getCurrentNode();
final ElementNode parent = (ElementNode) getPageTransaction().prepareEntryForModification(node.getParentKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
parent.removeNamespace(node.getNodeKey());
adaptHashesWithRemove();
getPageTransaction().removeEntry(node.getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
removeName();
moveToParent();
}
return this;
} finally {
unLock();
}
}
Aggregations