Search in sources :

Example 1 with NodeWriteTrx

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

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

the class NodeIdRepresentation method addSubResource.

/**
 * This method is responsible to perform a POST request to a node id. This
 * method adds a new XML subtree as first child or as right sibling to the
 * node which is addressed through a node id.
 *
 * @param resourceName
 *          The name of the database, the node id belongs to.
 * @param nodeId
 *          The node id.
 * @param input
 *          The new XML subtree.
 * @param type
 *          The type which indicates if the new subtree has to be inserted as
 *          right sibling or as first child.
 * @throws JaxRxException
 *           The exception occurred.
 */
public void addSubResource(final String resourceName, final long nodeId, final InputStream input, final IDAccessType type) throws JaxRxException {
    Session session = null;
    Database database = null;
    NodeWriteTrx wtx = null;
    synchronized (resourceName) {
        boolean abort;
        if (WorkerHelper.checkExistingResource(mStoragePath, resourceName)) {
            abort = false;
            try {
                database = Databases.openDatabase(mStoragePath);
                // Creating a new session
                session = database.getSession(new SessionConfiguration.Builder(resourceName).build());
                // Creating a write transaction
                wtx = session.beginNodeWriteTrx();
                final boolean exist = wtx.moveTo(nodeId).hasMoved();
                if (exist) {
                    if (type == IDAccessType.FIRSTCHILD) {
                        WorkerHelper.shredInputStream(wtx, input, Insert.ASFIRSTCHILD);
                    } else if (type == IDAccessType.RIGHTSIBLING) {
                        WorkerHelper.shredInputStream(wtx, input, Insert.ASRIGHTSIBLING);
                    } else if (type == IDAccessType.LASTCHILD) {
                        if (wtx.moveToFirstChild().hasMoved()) {
                            long last = wtx.getNodeKey();
                            while (wtx.moveToRightSibling().hasMoved()) {
                                last = wtx.getNodeKey();
                            }
                            wtx.moveTo(last);
                            WorkerHelper.shredInputStream(wtx, input, Insert.ASRIGHTSIBLING);
                        } else {
                            throw new JaxRxException(404, NOTFOUND);
                        }
                    } else if (type == IDAccessType.LEFTSIBLING && wtx.moveToLeftSibling().hasMoved()) {
                        WorkerHelper.shredInputStream(wtx, input, Insert.ASRIGHTSIBLING);
                    }
                } else {
                    throw new JaxRxException(404, NOTFOUND);
                }
            } catch (final JaxRxException exce) {
                // NOPMD due
                // to
                // different
                // exception
                // types
                abort = true;
                throw exce;
            } catch (final Exception exce) {
                abort = true;
                throw new JaxRxException(exce);
            } finally {
                try {
                    WorkerHelper.closeWTX(abort, wtx, session, database);
                } catch (final SirixException exce) {
                    throw new JaxRxException(exce);
                }
            }
        }
    }
}
Also used : Database(org.sirix.api.Database) NodeWriteTrx(org.sirix.api.NodeWriteTrx) SirixException(org.sirix.exception.SirixException) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) JaxRxException(org.jaxrx.core.JaxRxException) Session(org.sirix.api.Session) JaxRxException(org.jaxrx.core.JaxRxException)

Example 3 with NodeWriteTrx

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

the class NodeIdRepresentation method deleteResource.

/**
 * This method is responsible to delete an XML resource addressed through a
 * unique node id (except root node id).
 *
 * @param resourceName
 *          The name of the database, which the node id belongs to.
 * @param nodeId
 *          The unique node id.
 * @throws JaxRxException
 *           The exception occurred.
 */
public void deleteResource(final String resourceName, final long nodeId) 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();
                // move to node with given rest id and deletes it
                if (wtx.moveTo(nodeId).hasMoved()) {
                    wtx.remove();
                    wtx.commit();
                } else {
                    // workerHelper.closeWTX(abort, wtx, session, database);
                    throw new JaxRxException(404, NOTFOUND);
                }
            } catch (final SirixException exce) {
                abort = true;
                throw new JaxRxException(exce);
            } finally {
                try {
                    WorkerHelper.closeWTX(abort, wtx, session, database);
                } catch (final SirixException exce) {
                    throw new JaxRxException(exce);
                }
            }
        } else {
            throw new JaxRxException(404, "DB 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 4 with NodeWriteTrx

use of org.sirix.api.NodeWriteTrx 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 5 with NodeWriteTrx

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

the class TestNodeWrapper method testGetBaseURI.

@Test
public void testGetBaseURI() throws Exception {
    // Test with xml:base specified.
    final File source = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "data" + File.separator + "testBaseURI.xml");
    final Session session = generateSession();
    final NodeWriteTrx wtx = session.beginNodeWriteTrx();
    final XMLEventReader reader = XMLShredder.createFileReader(source);
    final XMLShredder shredder = new XMLShredder.Builder(wtx, reader, Insert.ASFIRSTCHILD).commitAfterwards().build();
    shredder.call();
    wtx.close();
    final Processor proc = new Processor(false);
    final NodeInfo doc = new DocumentWrapper(session, proc.getUnderlyingConfiguration());
    doc.getNamePool().allocate("xml", "http://www.w3.org/XML/1998/namespace", "base");
    doc.getNamePool().allocate("", "", "baz");
    final NameTest test = new NameTest(Type.ELEMENT, "", "baz", doc.getNamePool());
    final AxisIterator iterator = doc.iterateAxis(Axis.DESCENDANT, test);
    final NodeInfo baz = (NodeInfo) iterator.next();
    assertEquals("http://example.org", baz.getBaseURI());
    session.close();
    mDatabase.close();
}
Also used : NameTest(net.sf.saxon.pattern.NameTest) Processor(net.sf.saxon.s9api.Processor) NodeInfo(net.sf.saxon.om.NodeInfo) NodeWriteTrx(org.sirix.api.NodeWriteTrx) XMLEventReader(javax.xml.stream.XMLEventReader) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) File(java.io.File) AxisIterator(net.sf.saxon.tree.iter.AxisIterator) Session(org.sirix.api.Session) NameTest(net.sf.saxon.pattern.NameTest) Test(org.junit.Test)

Aggregations

NodeWriteTrx (org.sirix.api.NodeWriteTrx)10 Session (org.sirix.api.Session)10 Database (org.sirix.api.Database)8 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)7 JaxRxException (org.jaxrx.core.JaxRxException)5 SirixException (org.sirix.exception.SirixException)5 Test (org.junit.Test)4 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)3 XMLShredder (org.sirix.service.xml.shredder.XMLShredder)3 IOException (java.io.IOException)2 XMLEventReader (javax.xml.stream.XMLEventReader)2 NodeInfo (net.sf.saxon.om.NodeInfo)2 NameTest (net.sf.saxon.pattern.NameTest)2 Processor (net.sf.saxon.s9api.Processor)2 ResourceConfiguration (org.sirix.access.conf.ResourceConfiguration)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Configuration (net.sf.saxon.Configuration)1