Search in sources :

Example 6 with XdmNodeWriteTrx

use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.

the class DBNode method append.

private DBNode append(XdmNodeReadTrx rtx, Node<?> child) throws DocumentException {
    try {
        SubtreeBuilder builder = null;
        if (rtx.hasFirstChild()) {
            rtx.moveToLastChild();
            builder = new SubtreeBuilder(mCollection, (XdmNodeWriteTrx) rtx, Insert.ASRIGHTSIBLING, Collections.<SubtreeListener<? super AbstractTemporalNode<DBNode>>>emptyList());
        } else {
            builder = new SubtreeBuilder(mCollection, (XdmNodeWriteTrx) rtx, Insert.ASFIRSTCHILD, Collections.<SubtreeListener<? super AbstractTemporalNode<DBNode>>>emptyList());
        }
        child.parse(builder);
        rtx.moveTo(builder.getStartNodeKey());
    } catch (final SirixException e) {
        throw new DocumentException(e);
    }
    return new DBNode(rtx, mCollection);
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) SubtreeListener(org.brackit.xquery.node.parser.SubtreeListener) DocumentException(org.brackit.xquery.xdm.DocumentException) AbstractTemporalNode(org.brackit.xquery.xdm.AbstractTemporalNode) SirixException(org.sirix.exception.SirixException)

Example 7 with XdmNodeWriteTrx

use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.

the class DBNode method getWtx.

private XdmNodeWriteTrx getWtx() {
    final ResourceManager resource = mRtx.getResourceManager();
    final XdmNodeWriteTrx wtx;
    if (resource.getAvailableNodeWriteTrx() == 0) {
        wtx = resource.getXdmNodeWriteTrx().get();
    } else {
        wtx = resource.beginNodeWriteTrx();
    }
    if (mRtx.getRevisionNumber() < resource.getMostRecentRevisionNumber())
        wtx.revertTo(mRtx.getRevisionNumber());
    wtx.moveTo(mNodeKey);
    return wtx;
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) ResourceManager(org.sirix.api.ResourceManager)

Example 8 with XdmNodeWriteTrx

use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.

the class DBStore method create.

public TemporalCollection<?> create(final String collName, final Optional<String> optResName, final SubtreeParser parser) throws DocumentException {
    final DatabaseConfiguration dbConf = new DatabaseConfiguration(mLocation.resolve(collName));
    try {
        Databases.truncateDatabase(dbConf);
        Databases.createDatabase(dbConf);
        try (final Database database = Databases.openDatabase(dbConf.getFile())) {
            mDatabases.add(database);
            final String resName = optResName.isPresent() ? optResName.get() : new StringBuilder(3).append("resource").append(database.listResources().size() + 1).toString();
            database.createResource(ResourceConfiguration.newBuilder(resName, dbConf).useDeweyIDs(true).useTextCompression(true).buildPathSummary(true).storageType(mStorageType).build());
            final DBCollection collection = new DBCollection(collName, database);
            mCollections.put(database, collection);
            try (final ResourceManager resource = database.getResourceManager(new ResourceManagerConfiguration.Builder(resName).build());
                final XdmNodeWriteTrx wtx = resource.beginNodeWriteTrx()) {
                parser.parse(new SubtreeBuilder(collection, wtx, Insert.ASFIRSTCHILD, Collections.<SubtreeListener<? super AbstractTemporalNode<DBNode>>>emptyList()));
                wtx.commit();
            }
            return collection;
        }
    } catch (final SirixException e) {
        throw new DocumentException(e.getCause());
    }
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) SubtreeListener(org.brackit.xquery.node.parser.SubtreeListener) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) AbstractTemporalNode(org.brackit.xquery.xdm.AbstractTemporalNode) ResourceManager(org.sirix.api.ResourceManager) DocumentException(org.brackit.xquery.xdm.DocumentException) Database(org.sirix.api.Database) SirixException(org.sirix.exception.SirixException)

Example 9 with XdmNodeWriteTrx

use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.

the class Commit method execute.

@Override
public Sequence execute(StaticContext sctx, QueryContext ctx, Sequence[] args) throws QueryException {
    final DBNode doc = ((DBNode) args[0]);
    if (doc.getTrx() instanceof XdmNodeWriteTrx) {
        final XdmNodeWriteTrx wtx = (XdmNodeWriteTrx) doc.getTrx();
        final long revision = wtx.getRevisionNumber();
        wtx.commit();
        return new Int64(revision);
    } else {
        final ResourceManager manager = doc.getTrx().getResourceManager();
        final XdmNodeWriteTrx wtx;
        if (manager.getAvailableNodeWriteTrx() == 0) {
            wtx = manager.getXdmNodeWriteTrx().get();
        } else {
            wtx = manager.beginNodeWriteTrx();
        }
        final int revision = doc.getTrx().getRevisionNumber();
        if (revision < manager.getMostRecentRevisionNumber()) {
            wtx.revertTo(doc.getTrx().getRevisionNumber());
        }
        wtx.commit();
        return new Int64(revision);
    }
}
Also used : DBNode(org.sirix.xquery.node.DBNode) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) Int64(org.brackit.xquery.atomic.Int64) ResourceManager(org.sirix.api.ResourceManager)

Example 10 with XdmNodeWriteTrx

use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.

the class Rollback method execute.

@Override
public Sequence execute(StaticContext sctx, QueryContext ctx, Sequence[] args) throws QueryException {
    final DBNode doc = ((DBNode) args[0]);
    if (doc.getTrx() instanceof XdmNodeWriteTrx) {
        final XdmNodeWriteTrx wtx = (XdmNodeWriteTrx) doc.getTrx();
        final long revision = wtx.getRevisionNumber();
        wtx.rollback();
        return new Int64(revision);
    } else {
        throw new QueryException(new QNm("The transaction is not a write transaction!"));
    }
}
Also used : DBNode(org.sirix.xquery.node.DBNode) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) QNm(org.brackit.xquery.atomic.QNm) QueryException(org.brackit.xquery.QueryException) Int64(org.brackit.xquery.atomic.Int64)

Aggregations

XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)101 Test (org.junit.Test)61 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)48 ResourceManager (org.sirix.api.ResourceManager)33 Database (org.sirix.api.Database)22 QNm (org.brackit.xquery.atomic.QNm)15 SirixException (org.sirix.exception.SirixException)15 Path (java.nio.file.Path)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 DocumentException (org.brackit.xquery.xdm.DocumentException)7 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)7 DescendantAxis (org.sirix.axis.DescendantAxis)7 XMLSerializerBuilder (org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder)7 IOException (java.io.IOException)5 NonStructuralWrapperAxis (org.sirix.axis.NonStructuralWrapperAxis)5 SirixIOException (org.sirix.exception.SirixIOException)5 SirixDeweyID (org.sirix.node.SirixDeweyID)5 DBNode (org.sirix.xquery.node.DBNode)5 XMLEventReader (javax.xml.stream.XMLEventReader)4 QueryException (org.brackit.xquery.QueryException)4