Search in sources :

Example 46 with XdmNodeWriteTrx

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

the class DiffTestHelper method setUpSeventh.

static void setUpSeventh(final Holder holder) throws SirixException, IOException, XMLStreamException {
    final XdmNodeWriteTrx wtx = holder.getWriter();
    DocumentCreater.create(wtx);
    wtx.commit();
    final XdmNodeReadTrx rtx = holder.getResourceManager().beginNodeReadTrx(1);
    rtx.moveTo(1);
    wtx.moveTo(5);
    wtx.replaceNode(rtx);
    wtx.commit();
    rtx.close();
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx)

Example 47 with XdmNodeWriteTrx

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

the class FMSETest method test.

/**
 * Test a folder of XML files.
 *
 * @param FOLDER path string
 * @throws Exception if any exception occurs
 */
private void test(final String FOLDER) throws Exception {
    Database database = TestHelper.getDatabase(PATHS.PATH1.getFile());
    ResourceManager resource = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
    final Path folder = Paths.get(FOLDER);
    final List<Path> list = Files.list(folder).filter(path -> path.getFileName().endsWith(".xml")).collect(toList());
    // Sort files list according to file names.
    list.sort((first, second) -> {
        final String firstName = first.getFileName().toString().substring(0, first.getFileName().toString().indexOf('.'));
        final String secondName = second.getFileName().toString().substring(0, second.getFileName().toString().indexOf('.'));
        if (Integer.parseInt(firstName) < Integer.parseInt(secondName)) {
            return -1;
        } else if (Integer.parseInt(firstName) > Integer.parseInt(secondName)) {
            return +1;
        } else {
            return 0;
        }
    });
    boolean first = true;
    // Shredder files.
    for (final Path file : list) {
        if (file.getFileName().endsWith(".xml")) {
            if (first) {
                first = false;
                try (final XdmNodeWriteTrx wtx = resource.beginNodeWriteTrx()) {
                    final XMLShredder shredder = new XMLShredder.Builder(wtx, XMLShredder.createFileReader(file), Insert.ASFIRSTCHILD).commitAfterwards().build();
                    shredder.call();
                }
            } else {
                FMSEImport.main(new String[] { PATHS.PATH1.getFile().toAbsolutePath().toString(), file.toAbsolutePath().toString() });
            }
            resource.close();
            resource = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
            final OutputStream out = new ByteArrayOutputStream();
            final XMLSerializer serializer = new XMLSerializerBuilder(resource, out).build();
            serializer.call();
            final StringBuilder sBuilder = TestHelper.readFile(file, false);
            final Diff diff = new Diff(sBuilder.toString(), out.toString());
            final DetailedDiff detDiff = new DetailedDiff(diff);
            @SuppressWarnings("unchecked") final List<Difference> differences = detDiff.getAllDifferences();
            for (final Difference difference : differences) {
                System.err.println("***********************");
                System.err.println(difference);
                System.err.println("***********************");
            }
            assertTrue("pieces of XML are similar " + diff, diff.similar());
            assertTrue("but are they identical? " + diff, diff.identical());
        }
    }
    database.close();
}
Also used : Path(java.nio.file.Path) XMLUnit(org.custommonkey.xmlunit.XMLUnit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Difference(org.custommonkey.xmlunit.Difference) XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) XMLTestCase(org.custommonkey.xmlunit.XMLTestCase) PATHS(org.sirix.TestHelper.PATHS) ResourceManagerConfiguration(org.sirix.access.conf.ResourceManagerConfiguration) TestHelper(org.sirix.TestHelper) After(org.junit.After) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Path(java.nio.file.Path) Before(org.junit.Before) OutputStream(java.io.OutputStream) SirixException(org.sirix.exception.SirixException) Files(java.nio.file.Files) IOException(java.io.IOException) Test(org.junit.Test) FMSEImport(org.sirix.diff.service.FMSEImport) File(java.io.File) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) Insert(org.sirix.service.xml.shredder.Insert) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) ResourceManager(org.sirix.api.ResourceManager) Paths(java.nio.file.Paths) Database(org.sirix.api.Database) Diff(org.custommonkey.xmlunit.Diff) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Diff(org.custommonkey.xmlunit.Diff) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ResourceManagerConfiguration(org.sirix.access.conf.ResourceManagerConfiguration) ResourceManager(org.sirix.api.ResourceManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Difference(org.custommonkey.xmlunit.Difference) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) Database(org.sirix.api.Database) XMLShredder(org.sirix.service.xml.shredder.XMLShredder)

Example 48 with XdmNodeWriteTrx

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

the class PostOrderTest method testIterateDocumentSecond.

@Test
public void testIterateDocumentSecond() throws SirixException, IOException, XMLStreamException {
    try (final XdmNodeWriteTrx wtx = holder.getResourceManager().beginNodeWriteTrx()) {
        wtx.moveTo(11);
        wtx.insertSubtreeAsFirstChild(XMLShredder.createStringReader(DocumentCreater.XML_WITHOUT_XMLDECL));
        wtx.commit();
        wtx.moveToDocumentRoot();
        wtx.moveToFirstChild();
        final long key = wtx.getNodeKey();
        AbsAxisTest.testIAxisConventions(new PostOrderAxis(wtx, IncludeSelf.YES), new long[] { 4L, 6L, 7L, 5L, 8L, 17L, 19L, 20L, 18L, 21L, 24L, 25L, 22L, 26L, 14L, 11L, 12L, 9L, 13L, 1L });
        new IteratorTester<Long>(ITERATIONS, IteratorFeature.UNMODIFIABLE, ImmutableList.of(4L, 6L, 7L, 5L, 8L, 17L, 19L, 20L, 18L, 21L, 24L, 25L, 22L, 26L, 14L, 11L, 12L, 9L, 13L, 1L), null) {

            @Override
            protected Iterator<Long> newTargetIterator() {
                wtx.moveTo(key);
                return new PostOrderAxis(wtx, IncludeSelf.YES);
            }
        }.test();
        wtx.moveToDocumentRoot();
        wtx.moveToFirstChild();
        final long secondKey = wtx.getNodeKey();
        AbsAxisTest.testIAxisConventions(new PostOrderAxis(wtx), new long[] { 4L, 6L, 7L, 5L, 8L, 17L, 19L, 20L, 18L, 21L, 24L, 25L, 22L, 26L, 14L, 11L, 12L, 9L, 13L });
        new IteratorTester<Long>(ITERATIONS, IteratorFeature.UNMODIFIABLE, ImmutableList.of(4L, 6L, 7L, 5L, 8L, 17L, 19L, 20L, 18L, 21L, 24L, 25L, 22L, 26L, 14L, 11L, 12L, 9L, 13L), null) {

            @Override
            protected Iterator<Long> newTargetIterator() {
                wtx.moveTo(secondKey);
                return new PostOrderAxis(wtx);
            }
        }.test();
    }
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) Iterator(java.util.Iterator) Test(org.junit.Test)

Example 49 with XdmNodeWriteTrx

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

the class XMLShredderTest method testShreddingLargeText.

@Test
public void testShreddingLargeText() throws Exception {
    final Database database = TestHelper.getDatabase(PATHS.PATH2.getFile());
    final ResourceManager manager = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
    final XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx();
    final XMLShredder shredder = new XMLShredder.Builder(wtx, XMLShredder.createFileReader(XML3), Insert.ASFIRSTCHILD).commitAfterwards().build();
    shredder.call();
    wtx.close();
    final XdmNodeReadTrx rtx = manager.beginNodeReadTrx();
    assertTrue(rtx.moveToFirstChild().hasMoved());
    assertTrue(rtx.moveToFirstChild().hasMoved());
    final StringBuilder tnkBuilder = new StringBuilder();
    do {
        tnkBuilder.append(rtx.getValue());
    } while (rtx.moveToRightSibling().hasMoved());
    final String tnkString = tnkBuilder.toString();
    rtx.close();
    manager.close();
    final XMLEventReader validater = XMLShredder.createFileReader(XML3);
    final StringBuilder xmlBuilder = new StringBuilder();
    while (validater.hasNext()) {
        final XMLEvent event = validater.nextEvent();
        switch(event.getEventType()) {
            case XMLStreamConstants.CHARACTERS:
                final String text = event.asCharacters().getData().trim();
                if (text.length() > 0) {
                    xmlBuilder.append(text);
                }
                break;
        }
    }
    assertEquals(xmlBuilder.toString(), tnkString);
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) Database(org.sirix.api.Database) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) ResourceManager(org.sirix.api.ResourceManager) Test(org.junit.Test)

Example 50 with XdmNodeWriteTrx

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

the class TransactionImpl method commit.

@Override
public Transaction commit() {
    int i = 0;
    for (boolean failure = false; i < resourceTrxs.size() && !failure; i++) {
        final XdmNodeWriteTrx trx = resourceTrxs.get(i);
        try {
            trx.commit();
        } catch (final Exception e) {
            trx.rollback();
            failure = true;
        }
    }
    if (i < resourceTrxs.size()) {
        for (int j = 0; j < i; j++) {
            final XdmNodeWriteTrx trx = resourceTrxs.get(i);
            trx.truncateTo(trx.getRevisionNumber() - 1);
        }
        for (; i < resourceTrxs.size(); i++) {
            final XdmNodeWriteTrx trx = resourceTrxs.get(i);
            trx.rollback();
        }
    }
    resourceTrxs.clear();
    trxMgr.closeTransaction(this);
    return this;
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx)

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