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();
}
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();
}
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();
}
}
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);
}
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;
}
Aggregations