Search in sources :

Example 86 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class VersioningTest method test2.

/**
 * Test revisioning.
 *
 * @throws SirixException if anything in Sirix fails
 */
public void test2() throws SirixException {
    try (final ResourceManager manager = mDatabase.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build())) {
        XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx();
        wtx.insertElementAsFirstChild(new QNm("foo"));
        wtx.commit();
        wtx.insertElementAsFirstChild(new QNm("foo"));
        wtx.commit();
        wtx.insertElementAsFirstChild(new QNm("foo"));
        wtx.commit();
        wtx.insertElementAsFirstChild(new QNm("foo"));
        wtx.commit();
        wtx.insertElementAsFirstChild(new QNm("foo"));
        wtx.commit();
        wtx.close();
        try (final XdmNodeReadTrx rtx = manager.beginNodeReadTrx()) {
            assertTrue(rtx.moveToFirstChild().hasMoved());
            assertTrue(rtx.moveToFirstChild().hasMoved());
            assertTrue(rtx.moveToFirstChild().hasMoved());
            assertTrue(rtx.moveToFirstChild().hasMoved());
            assertTrue(rtx.moveToFirstChild().hasMoved());
        }
    }
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) QNm(org.brackit.xquery.atomic.QNm) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) ResourceManager(org.sirix.api.ResourceManager)

Example 87 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class VersioningTest method setBaaaz.

/**
 * Set the last {@link QNm} in the first node page.
 *
 * @param wtx {@link XdmNodeWriteTrx} instance
 * @throws SirixException if inserting elements fails
 */
private void setBaaaz(final XdmNodeWriteTrx wtx) throws SirixException {
    wtx.moveToDocumentRoot();
    wtx.moveToFirstChild();
    for (int i = 0; i < Constants.NDP_NODE_COUNT - 3; i++) {
        wtx.moveToFirstChild();
    }
    wtx.setName(new QNm("baaaz"));
}
Also used : QNm(org.brackit.xquery.atomic.QNm)

Example 88 with QNm

use of org.brackit.xquery.atomic.QNm 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();
    }
}
Also used : QNm(org.brackit.xquery.atomic.QNm) PINode(org.sirix.node.PINode) SirixUsageException(org.sirix.exception.SirixUsageException) SirixDeweyID(org.sirix.node.SirixDeweyID) StructNode(org.sirix.node.interfaces.StructNode)

Example 89 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class ModificationVisitor method modify.

/**
 * Determines if a node must be modified. If yes, it is deleted and {@code true} is returned. If
 * it must not be deleted {@code false} is returned. The transaction is moved accordingly in case
 * of a remove-operation such that the {@link DescendantAxis} can move to the next node after a
 * delete occurred.
 *
 * @param node the node to check and possibly delete
 * @return {@code true} if node has been deleted, {@code false} otherwise
 */
private VisitResult modify(final ImmutableNode node) {
    assert node != null;
    if (mNodeIndex % MODIFY_EVERY == 0) {
        mNodeIndex = 1;
        try {
            switch(mRandom.nextInt(4)) {
                case 0:
                    final QNm insert = new QNm("testInsert");
                    final long key = mWtx.getNodeKey();
                    mWtx.insertElementAsLeftSibling(insert);
                    boolean moved = mWtx.moveTo(key).hasMoved();
                    assert moved;
                    return VisitResultType.CONTINUE;
                case 1:
                    if (mWtx.getKind() == Kind.TEXT) {
                        mWtx.setValue("testUpdate");
                    } else if (mWtx.getKind() == Kind.ELEMENT) {
                        mWtx.setName(new QNm("testUpdate"));
                    }
                    return VisitResultType.CONTINUE;
                case 2:
                    return delete();
                case 3:
                    mWtx.replaceNode("<foo/>");
                    return VisitResultType.CONTINUE;
            }
        } catch (final SirixException | IOException | XMLStreamException e) {
            LOGWRAPPER.error(e.getMessage(), e);
            return VisitResultType.TERMINATE;
        }
    } else {
        mNodeIndex++;
        return VisitResultType.CONTINUE;
    }
    return VisitResultType.CONTINUE;
}
Also used : QNm(org.brackit.xquery.atomic.QNm) XMLStreamException(javax.xml.stream.XMLStreamException) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException)

Aggregations

QNm (org.brackit.xquery.atomic.QNm)89 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)20 Test (org.junit.Test)18 QueryException (org.brackit.xquery.QueryException)15 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)15 DBNode (org.sirix.xquery.node.DBNode)12 QName (javax.xml.namespace.QName)11 PathSummaryReader (org.sirix.index.path.summary.PathSummaryReader)11 IndexController (org.sirix.access.IndexController)10 IndexDef (org.sirix.index.IndexDef)10 Item (org.brackit.xquery.xdm.Item)7 Axis (org.sirix.api.Axis)7 DescendantAxis (org.sirix.axis.DescendantAxis)7 SirixException (org.sirix.exception.SirixException)7 Attribute (javax.xml.stream.events.Attribute)5 Namespace (javax.xml.stream.events.Namespace)5 DocumentException (org.brackit.xquery.xdm.DocumentException)5 SirixIOException (org.sirix.exception.SirixIOException)5 Ignore (org.junit.Ignore)4 SirixUsageException (org.sirix.exception.SirixUsageException)4