Search in sources :

Example 6 with Database

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

the class NodeIdRepresentation method serializeAT.

/**
 * This method serializes requested resource with an access type.
 *
 * @param resource
 *          The requested resource
 * @param nodeId
 *          The node id of the requested resource.
 * @param revision
 *          The revision of the requested resource.
 * @param doNodeId
 *          Specifies whether the node id's have to be shown in the result.
 * @param output
 *          The output stream to be written.
 * @param wrapResult
 *          Specifies whether the result has to be wrapped with a result
 *          element.
 * @param accessType
 *          The {@link IDAccessType} which indicates the access to a special
 *          node.
 */
private void serializeAT(final String resource, final long nodeId, final Integer revision, final boolean doNodeId, final OutputStream output, final boolean wrapResult, final IDAccessType accessType) {
    if (WorkerHelper.checkExistingResource(mStoragePath, resource)) {
        Session session = null;
        Database database = null;
        NodeReadTrx rtx = null;
        try {
            database = Databases.openDatabase(mStoragePath);
            session = database.getSession(new SessionConfiguration.Builder(resource).build());
            if (revision == null) {
                rtx = session.beginNodeReadTrx();
            } else {
                rtx = session.beginNodeReadTrx(revision);
            }
            if (rtx.moveTo(nodeId).hasMoved()) {
                switch(accessType) {
                    case FIRSTCHILD:
                        if (!rtx.moveToFirstChild().hasMoved())
                            throw new JaxRxException(404, NOTFOUND);
                        break;
                    case LASTCHILD:
                        if (rtx.moveToFirstChild().hasMoved()) {
                            long last = rtx.getNodeKey();
                            while (rtx.moveToRightSibling().hasMoved()) {
                                last = rtx.getNodeKey();
                            }
                            rtx.moveTo(last);
                        } else {
                            throw new JaxRxException(404, NOTFOUND);
                        }
                        break;
                    case RIGHTSIBLING:
                        if (!rtx.moveToRightSibling().hasMoved())
                            throw new JaxRxException(404, NOTFOUND);
                        break;
                    case LEFTSIBLING:
                        if (!rtx.moveToLeftSibling().hasMoved())
                            throw new JaxRxException(404, NOTFOUND);
                        break;
                    // nothing to do;
                    default:
                }
                if (wrapResult) {
                    output.write(BEGINRESULT);
                    final XMLSerializerProperties props = new XMLSerializerProperties();
                    final XMLSerializerBuilder builder = new XMLSerializerBuilder(session, rtx.getNodeKey(), output, props);
                    if (doNodeId) {
                        builder.emitRESTful().emitIDs();
                    }
                    final XMLSerializer serializer = builder.build();
                    serializer.call();
                    output.write(ENDRESULT);
                } else {
                    final XMLSerializerProperties props = new XMLSerializerProperties();
                    final XMLSerializerBuilder builder = new XMLSerializerBuilder(session, rtx.getNodeKey(), output, props);
                    if (doNodeId) {
                        builder.emitRESTful().emitIDs();
                    }
                    final XMLSerializer serializer = builder.build();
                    serializer.call();
                }
            } else {
                throw new JaxRxException(404, NOTFOUND);
            }
        } catch (final SirixException ttExcep) {
            throw new JaxRxException(ttExcep);
        } catch (final IOException ioExcep) {
            throw new JaxRxException(ioExcep);
        } catch (final Exception globExcep) {
            if (globExcep instanceof JaxRxException) {
                // types
                throw (JaxRxException) globExcep;
            } else {
                throw new JaxRxException(globExcep);
            }
        } finally {
            try {
                WorkerHelper.closeRTX(rtx, session, database);
            } catch (final SirixException exce) {
                throw new JaxRxException(exce);
            }
        }
    } else {
        throw new JaxRxException(404, "Resource does not exist");
    }
}
Also used : XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) IOException(java.io.IOException) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) JaxRxException(org.jaxrx.core.JaxRxException) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) XMLSerializerProperties(org.sirix.service.xml.serialize.XMLSerializerProperties) NodeReadTrx(org.sirix.api.NodeReadTrx) Database(org.sirix.api.Database) SirixException(org.sirix.exception.SirixException) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) Session(org.sirix.api.Session) JaxRxException(org.jaxrx.core.JaxRxException)

Example 7 with Database

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

the class NodeIdRepresentation method modifyResource.

/**
 * This method is responsible to modify the XML resource, which is addressed
 * through a unique node id.
 *
 * @param resourceName
 *          The name of the database, where the node id belongs to.
 * @param nodeId
 *          The node id.
 * @param newValue
 *          The new value of the node that has to be replaced.
 * @throws JaxRxException
 *           The exception occurred.
 */
public void modifyResource(final String resourceName, final long nodeId, final InputStream newValue) throws JaxRxException {
    synchronized (resourceName) {
        Session session = null;
        Database database = null;
        NodeWriteTrx wtx = null;
        boolean abort = false;
        if (WorkerHelper.checkExistingResource(mStoragePath, resourceName)) {
            try {
                database = Databases.openDatabase(mStoragePath);
                // Creating a new session
                session = database.getSession(new SessionConfiguration.Builder(resourceName).build());
                // Creating a write transaction
                wtx = session.beginNodeWriteTrx();
                if (wtx.moveTo(nodeId).hasMoved()) {
                    final long parentKey = wtx.getParentKey();
                    wtx.remove();
                    wtx.moveTo(parentKey);
                    WorkerHelper.shredInputStream(wtx, newValue, Insert.ASFIRSTCHILD);
                } else {
                    // workerHelper.closeWTX(abort, wtx, session, database);
                    throw new JaxRxException(404, NOTFOUND);
                }
            } catch (final SirixException exc) {
                abort = true;
                throw new JaxRxException(exc);
            } finally {
                try {
                    WorkerHelper.closeWTX(abort, wtx, session, database);
                } catch (final SirixException exce) {
                    throw new JaxRxException(exce);
                }
            }
        } else {
            throw new JaxRxException(404, "Requested resource not found");
        }
    }
}
Also used : Database(org.sirix.api.Database) NodeWriteTrx(org.sirix.api.NodeWriteTrx) SirixException(org.sirix.exception.SirixException) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) Session(org.sirix.api.Session) JaxRxException(org.jaxrx.core.JaxRxException)

Example 8 with Database

use of org.sirix.api.Database 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 9 with Database

use of org.sirix.api.Database 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 10 with Database

use of org.sirix.api.Database 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

Database (org.sirix.api.Database)55 ResourceManager (org.sirix.api.ResourceManager)30 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)22 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)19 Test (org.junit.Test)18 SirixException (org.sirix.exception.SirixException)18 Session (org.sirix.api.Session)16 IOException (java.io.IOException)13 Path (java.nio.file.Path)13 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)13 XMLSerializerBuilder (org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder)12 JaxRxException (org.jaxrx.core.JaxRxException)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 NodeWriteTrx (org.sirix.api.NodeWriteTrx)8 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)7 WebApplicationException (javax.ws.rs.WebApplicationException)6 NodeReadTrx (org.sirix.api.NodeReadTrx)6 XMLSerializer (org.sirix.service.xml.serialize.XMLSerializer)6 XMLEventReader (javax.xml.stream.XMLEventReader)5 DocumentException (org.brackit.xquery.xdm.DocumentException)5