Search in sources :

Example 36 with Database

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

the class XdmResourceManagerTest method testExisting.

@Test
public void testExisting() {
    final Database database = TestHelper.getDatabase(PATHS.PATH1.getFile());
    final ResourceManager resource = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
    final XdmNodeWriteTrx wtx1 = resource.beginNodeWriteTrx();
    DocumentCreater.create(wtx1);
    assertEquals(1L, wtx1.getRevisionNumber());
    wtx1.commit();
    wtx1.close();
    resource.close();
    final ResourceManager resource2 = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
    final XdmNodeReadTrx rtx1 = resource2.beginNodeReadTrx();
    assertEquals(1L, rtx1.getRevisionNumber());
    rtx1.moveTo(12L);
    assertEquals("bar", rtx1.getValue());
    final XdmNodeWriteTrx wtx2 = resource2.beginNodeWriteTrx();
    assertEquals(2L, wtx2.getRevisionNumber());
    wtx2.moveTo(12L);
    wtx2.setValue("bar2");
    assertEquals("bar", rtx1.getValue());
    assertEquals("bar2", wtx2.getValue());
    rtx1.close();
    wtx2.commit();
    wtx2.close();
    final Database database2 = TestHelper.getDatabase(PATHS.PATH1.getFile());
    final ResourceManager resource3 = database2.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
    final XdmNodeReadTrx rtx2 = resource3.beginNodeReadTrx();
    assertEquals(2L, rtx2.getRevisionNumber());
    rtx2.moveTo(12L);
    assertEquals("bar2", rtx2.getValue());
    rtx2.close();
    resource3.close();
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) Database(org.sirix.api.Database) ResourceManager(org.sirix.api.ResourceManager) Test(org.junit.Test)

Example 37 with Database

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

the class WikipediaImport method updateShredder.

/**
 * Update shredder.
 */
private void updateShredder() throws SirixException, IOException, XMLStreamException {
    final Path path = Files.createTempDirectory("sdbtmp");
    final DatabaseConfiguration dbConf = new DatabaseConfiguration(path);
    Databases.truncateDatabase(dbConf);
    Databases.createDatabase(dbConf);
    final Database db = Databases.openDatabase(path);
    db.createResource(new ResourceConfiguration.Builder("wiki", dbConf).build());
    final ResourceManager resourceManager = db.getResourceManager(new ResourceManagerConfiguration.Builder("wiki").build());
    if (mPageEvents.peek().isStartElement() && !mPageEvents.peek().asStartElement().getName().getLocalPart().equals("root")) {
        mPageEvents.addFirst(XMLEventFactory.newInstance().createStartElement(new QName("root"), null, null));
        mPageEvents.addLast(XMLEventFactory.newInstance().createEndElement(new QName("root"), null));
    }
    final XdmNodeWriteTrx wtx = resourceManager.beginNodeWriteTrx();
    final XMLShredder shredder = new XMLShredder.Builder(wtx, XMLShredder.createQueueReader(mPageEvents), Insert.ASFIRSTCHILD).commitAfterwards().build();
    shredder.call();
    wtx.close();
    mPageEvents = new ArrayDeque<>();
    final XdmNodeReadTrx rtx = resourceManager.beginNodeReadTrx();
    rtx.moveToFirstChild();
    rtx.moveToFirstChild();
    final long nodeKey = mWtx.getNodeKey();
    try (final FMSE fmse = new FMSE()) {
        fmse.diff(mWtx, rtx);
    }
    mWtx.moveTo(nodeKey);
    rtx.close();
    resourceManager.close();
    db.close();
    Databases.truncateDatabase(dbConf);
}
Also used : Path(java.nio.file.Path) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) QName(javax.xml.namespace.QName) Database(org.sirix.api.Database) FMSE(org.sirix.diff.algorithm.fmse.FMSE) ResourceManager(org.sirix.api.ResourceManager)

Example 38 with Database

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

the class XMLShredder method main.

/**
 * Main method.
 *
 * @param args input and output files
 * @throws XMLStreamException if the XML stream isn't valid
 * @throws IOException if an I/O error occurs
 * @throws SirixException if a Sirix error occurs
 */
public static void main(final String... args) throws SirixException, IOException, XMLStreamException {
    if (args.length != 2 && args.length != 3) {
        throw new IllegalArgumentException("Usage: XMLShredder XMLFile Database [true/false] (shredder comment|PI)");
    }
    LOGWRAPPER.info("Shredding '" + args[0] + "' to '" + args[1] + "' ... ");
    final long time = System.nanoTime();
    final Path target = Paths.get(args[1]);
    final DatabaseConfiguration config = new DatabaseConfiguration(target);
    Databases.truncateDatabase(config);
    Databases.createDatabase(config);
    try (final Database db = Databases.openDatabase(target)) {
        db.createResource(new ResourceConfiguration.Builder("shredded", config).build());
        try (final ResourceManager resMgr = db.getResourceManager(new ResourceManagerConfiguration.Builder("shredded").build());
            final XdmNodeWriteTrx wtx = resMgr.beginNodeWriteTrx()) {
            final XMLEventReader reader = createFileReader(Paths.get(args[0]));
            final boolean includeCoPI = args.length == 3 ? Boolean.parseBoolean(args[2]) : false;
            final XMLShredder shredder = new XMLShredder.Builder(wtx, reader, Insert.ASFIRSTCHILD).commitAfterwards().includeComments(includeCoPI).includePIs(includeCoPI).build();
            shredder.call();
        }
    }
    LOGWRAPPER.info(" done [" + (System.nanoTime() - time) / 1000000 + " ms].");
}
Also used : Path(java.nio.file.Path) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) Database(org.sirix.api.Database) XMLEventReader(javax.xml.stream.XMLEventReader) ResourceManager(org.sirix.api.ResourceManager)

Example 39 with Database

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

the class FMSEImport method dataImport.

/**
 * Import the data.
 *
 * @param resOldRev {@link File} for old revision (sirix resource)
 * @param resNewRev {@link File} for new revision (XML resource)
 */
private void dataImport(final Path resOldRev, @Nonnull final Path resNewRev) {
    try {
        final Path newRevTarget = Files.createTempDirectory(resNewRev.toAbsolutePath().toString());
        if (Files.exists(newRevTarget)) {
            SirixFiles.recursiveRemove(newRevTarget);
        }
        shredder(checkNotNull(resNewRev), newRevTarget);
        try (final Database databaseOld = Databases.openDatabase(resOldRev);
            final ResourceManager resMgrOld = databaseOld.getResourceManager(new ResourceManagerConfiguration.Builder("shredded").build());
            final XdmNodeWriteTrx wtx = resMgrOld.beginNodeWriteTrx();
            final Database databaseNew = Databases.openDatabase(newRevTarget);
            final ResourceManager resourceNew = databaseNew.getResourceManager(new ResourceManagerConfiguration.Builder("shredded").build());
            final XdmNodeReadTrx rtx = resourceNew.beginNodeReadTrx();
            final FMSE fmes = new FMSE()) {
            fmes.diff(wtx, rtx);
        }
    } catch (final SirixException | IOException | XMLStreamException e) {
        LOGWRAPPER.error(e.getMessage(), e);
    }
}
Also used : Path(java.nio.file.Path) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) XMLStreamException(javax.xml.stream.XMLStreamException) Database(org.sirix.api.Database) FMSE(org.sirix.diff.algorithm.fmse.FMSE) SirixException(org.sirix.exception.SirixException) ResourceManager(org.sirix.api.ResourceManager) IOException(java.io.IOException)

Example 40 with Database

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

the class FMSEImport method shredder.

/**
 * Shredder new revision as temporal resource.
 *
 * @param resNewRev {@link File} reference for new revision (XML resource)
 * @param newRev {@link File} reference for shreddered new revision (sirix resource)
 * @throws SirixException if sirix fails to shredder the file
 * @throws IOException if file couldn't be read
 * @throws XMLStreamException if XML document isn't well formed
 * @throws NullPointerException if {@code resNewRev} or {@code newRev} is {@code null}
 */
private void shredder(final Path resNewRev, @Nonnull Path newRev) throws SirixException, IOException, XMLStreamException {
    assert resNewRev != null;
    assert newRev != null;
    final DatabaseConfiguration conf = new DatabaseConfiguration(newRev);
    Databases.truncateDatabase(conf);
    Databases.createDatabase(conf);
    try (final Database db = Databases.openDatabase(newRev)) {
        db.createResource(new ResourceConfiguration.Builder("shredded", conf).build());
        try (final ResourceManager resMgr = db.getResourceManager(new ResourceManagerConfiguration.Builder("shredded").build());
            final XdmNodeWriteTrx wtx = resMgr.beginNodeWriteTrx()) {
            final XMLEventReader fileReader = XMLShredder.createFileReader(resNewRev);
            final XMLShredder shredder = new XMLShredder.Builder(wtx, fileReader, Insert.ASFIRSTCHILD).commitAfterwards().build();
            shredder.call();
        }
    }
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) Database(org.sirix.api.Database) XMLEventReader(javax.xml.stream.XMLEventReader) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) ResourceManager(org.sirix.api.ResourceManager)

Aggregations

Database (org.sirix.api.Database)55 ResourceManager (org.sirix.api.ResourceManager)30 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)22 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)19 Test (org.junit.Test)18 SirixException (org.sirix.exception.SirixException)18 Session (org.sirix.api.Session)16 IOException (java.io.IOException)13 Path (java.nio.file.Path)13 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)13 XMLSerializerBuilder (org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder)12 JaxRxException (org.jaxrx.core.JaxRxException)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 NodeWriteTrx (org.sirix.api.NodeWriteTrx)8 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)7 WebApplicationException (javax.ws.rs.WebApplicationException)6 NodeReadTrx (org.sirix.api.NodeReadTrx)6 XMLSerializer (org.sirix.service.xml.serialize.XMLSerializer)6 XMLEventReader (javax.xml.stream.XMLEventReader)5 DocumentException (org.brackit.xquery.xdm.DocumentException)5