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