use of org.sirix.access.conf.DatabaseConfiguration in project sirix by sirixdb.
the class DatabaseRepresentation method shred.
/**
* This method is responsible to save the XML file, which is in an
* {@link InputStream}, as a sirix object.
*
* @param xmlInput
* XML file wrapped in an {@link InputStream}
* @param resource
* name of the resource
* @return {@code true} if shredding process has been successful,
* {@code false} otherwise.
* @throws SirixException
* if any sirix related exception occurs
*/
public final boolean shred(final InputStream xmlInput, final String resource) throws SirixException {
boolean allOk = false;
NodeWriteTrx wtx = null;
Database database = null;
Session session = null;
boolean abort = false;
try {
final DatabaseConfiguration dbConf = new DatabaseConfiguration(mStoragePath);
Databases.createDatabase(dbConf);
database = Databases.openDatabase(dbConf.getFile());
// Shredding the database to the file as XML
final ResourceConfiguration resConf = new ResourceConfiguration.Builder(resource, dbConf).revisionsToRestore(1).build();
if (database.createResource(resConf)) {
session = database.getSession(new SessionConfiguration.Builder(resource).build());
wtx = session.beginNodeWriteTrx();
final XMLShredder shredder = new XMLShredder.Builder(wtx, RESTXMLShredder.createReader(xmlInput), Insert.ASFIRSTCHILD).commitAfterwards().build();
shredder.call();
allOk = true;
}
} catch (final Exception exce) {
abort = true;
throw new JaxRxException(exce);
} finally {
WorkerHelper.closeWTX(abort, wtx, session, database);
}
return allOk;
}
use of org.sirix.access.conf.DatabaseConfiguration in project sirix by sirixdb.
the class WikipediaImport method main.
/**
* Main method.
*
* @param args Arguments (path to xml-file /.
* @throws SirixException if anything within sirix fails
*/
public static void main(final String[] args) throws SirixException {
if (args.length != 2) {
throw new IllegalArgumentException("Usage: WikipediaImport path/to/xmlFile path/to/SirixStorage");
}
LOGWRAPPER.info("Importing wikipedia...");
final long start = System.nanoTime();
final Path xml = Paths.get(args[0]);
final Path resource = Paths.get(args[1]);
Databases.truncateDatabase(new DatabaseConfiguration(resource));
// Create necessary element nodes.
final String NSP_URI = "http://www.mediawiki.org/xml/export-0.5/";
final XMLEventFactory eventFactory = XMLEventFactory.newInstance();
final StartElement timestamp = eventFactory.createStartElement(new QName(NSP_URI, "timestamp", XMLConstants.DEFAULT_NS_PREFIX), null, null);
final StartElement page = eventFactory.createStartElement(new QName(NSP_URI, "page", XMLConstants.DEFAULT_NS_PREFIX), null, null);
final StartElement rev = eventFactory.createStartElement(new QName(NSP_URI, "revision", XMLConstants.DEFAULT_NS_PREFIX), null, null);
final StartElement id = eventFactory.createStartElement(new QName(NSP_URI, "id", XMLConstants.DEFAULT_NS_PREFIX), null, null);
final StartElement text = eventFactory.createStartElement(new QName(NSP_URI, "text", XMLConstants.DEFAULT_NS_PREFIX), null, null);
// Create list.
final List<StartElement> list = new LinkedList<StartElement>();
list.add(timestamp);
list.add(page);
list.add(rev);
list.add(id);
list.add(text);
// Invoke import.
new WikipediaImport(xml, resource).importData(DateBy.HOURS, list);
LOGWRAPPER.info(" done in " + (System.nanoTime() - start) / 1_000_000_000 + "[s].");
}
use of org.sirix.access.conf.DatabaseConfiguration in project sirix by sirixdb.
the class XMLUpdateShredder method main.
/**
* Main method.
*
* @param args input and output files
*/
public static void main(final String[] args) {
if (args.length != 2) {
throw new IllegalArgumentException("Usage: XMLShredder input.xml output.tnk");
}
LOGWRAPPER.info("Shredding '" + args[0] + "' to '" + args[1] + "' ... ");
final long time = System.currentTimeMillis();
final Path target = Paths.get(args[1]);
try {
final DatabaseConfiguration config = new DatabaseConfiguration(target);
Databases.createDatabase(config);
final Database db = Databases.openDatabase(target);
db.createResource(new ResourceConfiguration.Builder("shredded", config).build());
final ResourceManager resMgr = db.getResourceManager(new ResourceManagerConfiguration.Builder("shredded").build());
final XdmNodeWriteTrx wtx = resMgr.beginNodeWriteTrx();
final XMLEventReader reader = XMLShredder.createFileReader(Paths.get(args[0]));
final XMLUpdateShredder shredder = new XMLUpdateShredder(wtx, reader, Insert.ASFIRSTCHILD, new File(args[0]), ShredderCommit.COMMIT);
shredder.call();
wtx.close();
resMgr.close();
} catch (final SirixException | XMLStreamException | IOException e) {
LOGWRAPPER.error(e.getMessage(), e);
}
LOGWRAPPER.info(" done [" + (System.currentTimeMillis() - time) + "ms].");
}
use of org.sirix.access.conf.DatabaseConfiguration in project sirix by sirixdb.
the class SAXSerializer method main.
/**
* Main method.
*
* @param args args[0] specifies the path to the sirix storage from which to generate SAX events.
* @throws SirixException if any Sirix exception occurs
*/
public static void main(final String... args) {
final Path path = Paths.get(args[0]);
final DatabaseConfiguration config = new DatabaseConfiguration(path);
Databases.createDatabase(config);
final Database database = Databases.openDatabase(path);
database.createResource(new ResourceConfiguration.Builder("shredded", config).build());
try (final ResourceManager session = database.getResourceManager(new ResourceManagerConfiguration.Builder("shredded").build())) {
final DefaultHandler defHandler = new DefaultHandler();
final SAXSerializer serializer = new SAXSerializer(session, defHandler, session.getMostRecentRevisionNumber());
serializer.call();
}
}
use of org.sirix.access.conf.DatabaseConfiguration in project sirix by sirixdb.
the class XMLSerializer method main.
/**
* Main method.
*
* @param args args[0] specifies the input-TT file/folder; args[1] specifies the output XML file.
* @throws Exception any exception
*/
public static void main(final String... args) throws Exception {
if (args.length < 2 || args.length > 3) {
throw new IllegalArgumentException("Usage: XMLSerializer input-TT output.xml");
}
LOGWRAPPER.info("Serializing '" + args[0] + "' to '" + args[1] + "' ... ");
final long time = System.nanoTime();
final Path target = Paths.get(args[1]);
SirixFiles.recursiveRemove(target);
Files.createDirectories(target.getParent());
Files.createFile(target);
final Path databaseFile = Paths.get(args[0]);
final DatabaseConfiguration config = new DatabaseConfiguration(databaseFile);
Databases.createDatabase(config);
try (final Database db = Databases.openDatabase(databaseFile)) {
db.createResource(new ResourceConfiguration.Builder("shredded", config).build());
final ResourceManager resMgr = db.getResourceManager(new ResourceManagerConfiguration.Builder("shredded").build());
try (final FileOutputStream outputStream = new FileOutputStream(target.toFile())) {
final XMLSerializer serializer = XMLSerializer.newBuilder(resMgr, outputStream).emitXMLDeclaration().build();
serializer.call();
}
}
LOGWRAPPER.info(" done [" + (System.nanoTime() - time) / 1_000_000 + "ms].");
}
Aggregations