Search in sources :

Example 51 with XdmNodeWriteTrx

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

the class XdmResourceManager method close.

@Override
public synchronized void close() {
    if (!mClosed) {
        // Close all open node transactions.
        for (XdmNodeReadTrx rtx : mNodeReaderMap.values()) {
            if (rtx instanceof XdmNodeWriteTrx) {
                ((XdmNodeWriteTrx) rtx).rollback();
            }
            rtx.close();
            rtx = null;
        }
        // Close all open node page transactions.
        for (PageReadTrx rtx : mNodePageTrxMap.values()) {
            rtx.close();
            rtx = null;
        }
        // Close all open page transactions.
        for (PageReadTrx rtx : mPageTrxMap.values()) {
            rtx.close();
            rtx = null;
        }
        // Immediately release all ressources.
        mLastCommittedUberPage = null;
        mNodeReaderMap.clear();
        mPageTrxMap.clear();
        mNodePageTrxMap.clear();
        mResourceStore.closeResource(mResourceConfig.getResource());
        mFac.close();
        mClosed = true;
    }
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) PageReadTrx(org.sirix.api.PageReadTrx)

Example 52 with XdmNodeWriteTrx

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

the class ResourceTransactionUsage method main.

public static void main(final String[] args) {
    final Path file = LOCATION.resolve("db");
    final DatabaseConfiguration config = new DatabaseConfiguration(file);
    if (Files.exists(file)) {
        Databases.truncateDatabase(config);
    }
    Databases.createDatabase(config);
    try (final Database database = Databases.openDatabase(file)) {
        database.createResource(new ResourceConfiguration.Builder("resource", config).build());
        try (final ResourceManager resource = database.getResourceManager(new ResourceManagerConfiguration.Builder("resource").build());
            final XdmNodeWriteTrx wtx = resource.beginNodeWriteTrx()) {
            wtx.insertSubtreeAsFirstChild(XMLShredder.createFileReader(LOCATION.resolve("input.xml")));
            wtx.moveTo(2);
            wtx.moveSubtreeToFirstChild(4).commit();
            final OutputStream out = new ByteArrayOutputStream();
            new XMLSerializer.XMLSerializerBuilder(resource, out).prettyPrint().build().call();
            System.out.println(out);
        }
    } catch (final SirixException | IOException | XMLStreamException e) {
    // LOG or do anything, the database is closed properly.
    }
}
Also used : Path(java.nio.file.Path) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResourceManager(org.sirix.api.ResourceManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) Database(org.sirix.api.Database) SirixException(org.sirix.exception.SirixException)

Example 53 with XdmNodeWriteTrx

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

the class FileHierarchyWalker method parseDir.

/**
 * Parse a directory and create a simple XML representation.
 *
 * @param path path to directory from which to shredder all content into sirix
 * @param database sirix {@IDatabase} to shred into
 * @param visitor an optional visitor
 * @throws SirixException if any sirix operation fails
 * @throws IOException if an I/O error occurs
 * @throws NullPointerException if one of the arguments is {@code null}
 */
public static Map<Path, org.sirix.fs.FileSystemPath> parseDir(final Path path, final Database database, Optional<Visitor<XdmNodeWriteTrx>> visitor) throws SirixException, IOException {
    checkNotNull(visitor);
    checkNotNull(path);
    try (final ResourceManager resource = checkNotNull(database).getResourceManager(new ResourceManagerConfiguration.Builder("shredded").build());
        final XdmNodeWriteTrx wtx = resource.beginNodeWriteTrx()) {
        final Builder builder = new Builder(wtx);
        if (visitor.isPresent()) {
            builder.setVisitor(visitor.get());
        }
        Map<Path, org.sirix.fs.FileSystemPath> index = Collections.emptyMap();
        try (final HierarchyFileVisitor fileVisitor = HierarchyFileVisitor.getInstance(builder)) {
            Files.walkFileTree(path, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, fileVisitor);
            index = fileVisitor.getIndex();
        }
        return index;
    }
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) Path(java.nio.file.Path) Builder(org.sirix.fs.HierarchyFileVisitor.Builder) ResourceManager(org.sirix.api.ResourceManager)

Example 54 with XdmNodeWriteTrx

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

the class VersioningTest method test1.

/**
 * Test revisioning.
 *
 * @throws SirixException if anything in Sirix fails
 */
public void test1() throws SirixException {
    try (final ResourceManager manager = mDatabase.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build())) {
        XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx();
        for (int i = 0; i < Constants.NDP_NODE_COUNT - 1; i++) {
            wtx.insertElementAsFirstChild(new QNm("foo"));
        }
        wtx.commit();
        assertTrue(wtx.getNodeKey() == Constants.NDP_NODE_COUNT - 1);
        wtx.close();
        wtx = manager.beginNodeWriteTrx();
        setBaaaz(wtx);
        setFooBar(wtx);
        setFoooo(wtx);
        wtx.moveTo(Constants.NDP_NODE_COUNT - 1);
        fillNodePage(wtx);
        wtx.commit();
        wtx.close();
        wtx = manager.beginNodeWriteTrx();
        wtx.moveTo((Constants.NDP_NODE_COUNT << 1) - 1);
        fillNodePage(wtx);
        wtx.commit();
        wtx.close();
        wtx = manager.beginNodeWriteTrx();
        wtx.moveTo((Constants.NDP_NODE_COUNT * 3) - 1);
        fillNodePage(wtx);
        wtx.commit();
        wtx.close();
        wtx = manager.beginNodeWriteTrx();
        wtx.moveTo((Constants.NDP_NODE_COUNT << 2) - 1);
        fillNodePage(wtx);
        wtx.commit();
        wtx.close();
        try (final XdmNodeReadTrx rtx = manager.beginNodeReadTrx()) {
            assertTrue(rtx.moveToFirstChild().hasMoved());
            assertEquals(new QNm("foobar"), rtx.getName());
            assertTrue(rtx.moveToFirstChild().hasMoved());
            assertEquals(new QNm("foooo"), rtx.getName());
            for (int i = 0; i < Constants.NDP_NODE_COUNT - 4; i++) {
                assertTrue(rtx.moveToFirstChild().hasMoved());
            }
            assertEquals(new QNm("baaaz"), rtx.getName());
        }
    }
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) QNm(org.brackit.xquery.atomic.QNm) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) ResourceManager(org.sirix.api.ResourceManager)

Example 55 with XdmNodeWriteTrx

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

the class VersioningTest method test.

/**
 * Test revisioning.
 *
 * @throws SirixException if anything in Sirix fails
 */
public void test() throws SirixException {
    try (final ResourceManager manager = mDatabase.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build())) {
        try (final XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx()) {
            for (int i = 0; i < Constants.NDP_NODE_COUNT - 1; i++) {
                wtx.insertElementAsFirstChild(new QNm("foo"));
            }
            wtx.commit();
            assertTrue(wtx.getNodeKey() == Constants.NDP_NODE_COUNT - 1);
            fillNodePage(wtx);
            wtx.commit();
            assertTrue(wtx.getNodeKey() == (Constants.NDP_NODE_COUNT << 1) - 1);
            fillNodePage(wtx);
            wtx.commit();
            assertTrue(wtx.getNodeKey() == (Constants.NDP_NODE_COUNT * 3) - 1);
            fillNodePage(wtx);
            wtx.commit();
            assertTrue(wtx.getNodeKey() == (Constants.NDP_NODE_COUNT << 2) - 1);
            fillNodePage(wtx);
            wtx.commit();
            assertTrue(wtx.getNodeKey() == (Constants.NDP_NODE_COUNT * 5) - 1);
            try (final XdmNodeReadTrx rtx = manager.beginNodeReadTrx()) {
                for (int i = 0; i < Constants.NDP_NODE_COUNT - 1; i++) {
                    assertTrue(rtx.moveToFirstChild().hasMoved());
                }
                move(rtx);
                move(rtx);
                move(rtx);
                move(rtx);
            }
        }
    }
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) QNm(org.brackit.xquery.atomic.QNm) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) ResourceManager(org.sirix.api.ResourceManager)

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