Search in sources :

Example 1 with DatabaseConfiguration

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;
}
Also used : DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) NodeWriteTrx(org.sirix.api.NodeWriteTrx) Database(org.sirix.api.Database) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) RESTXMLShredder(org.sirix.service.jaxrx.util.RESTXMLShredder) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) JaxRxException(org.jaxrx.core.JaxRxException) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Session(org.sirix.api.Session) ResourceConfiguration(org.sirix.access.conf.ResourceConfiguration) JaxRxException(org.jaxrx.core.JaxRxException)

Example 2 with DatabaseConfiguration

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].");
}
Also used : Path(java.nio.file.Path) StartElement(javax.xml.stream.events.StartElement) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) XMLEventFactory(javax.xml.stream.XMLEventFactory) QName(javax.xml.namespace.QName) LinkedList(java.util.LinkedList)

Example 3 with DatabaseConfiguration

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].");
}
Also used : Path(java.nio.file.Path) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) ResourceManager(org.sirix.api.ResourceManager) SirixIOException(org.sirix.exception.SirixIOException) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) Database(org.sirix.api.Database) XMLEventReader(javax.xml.stream.XMLEventReader) SirixException(org.sirix.exception.SirixException) File(java.io.File)

Example 4 with DatabaseConfiguration

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();
    }
}
Also used : Path(java.nio.file.Path) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) Database(org.sirix.api.Database) ResourceManager(org.sirix.api.ResourceManager) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 5 with DatabaseConfiguration

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].");
}
Also used : Path(java.nio.file.Path) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) FileOutputStream(java.io.FileOutputStream) Database(org.sirix.api.Database) ResourceManager(org.sirix.api.ResourceManager)

Aggregations

DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)24 Database (org.sirix.api.Database)19 ResourceManager (org.sirix.api.ResourceManager)11 Path (java.nio.file.Path)10 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)7 DocumentException (org.brackit.xquery.xdm.DocumentException)5 SirixRuntimeException (org.sirix.exception.SirixRuntimeException)5 XMLEventReader (javax.xml.stream.XMLEventReader)4 SirixException (org.sirix.exception.SirixException)4 IOException (java.io.IOException)3 QName (javax.xml.namespace.QName)3 ResourceConfiguration (org.sirix.access.conf.ResourceConfiguration)3 XMLShredder (org.sirix.service.xml.shredder.XMLShredder)3 LinkedList (java.util.LinkedList)2 XMLEventFactory (javax.xml.stream.XMLEventFactory)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 StartElement (javax.xml.stream.events.StartElement)2 SubtreeListener (org.brackit.xquery.node.parser.SubtreeListener)2 AbstractTemporalNode (org.brackit.xquery.xdm.AbstractTemporalNode)2 Ignore (org.junit.Ignore)2