Search in sources :

Example 71 with Axis

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

the class DatabaseRepresentation method getModificHistory.

/**
 * This method reads the existing database, and offers all modifications of
 * the two given revisions
 *
 * @param resourceName
 *          The name of the existing database.
 * @param revisionRange
 *          Contains the range of revisions
 * @param nodeid
 *          To response the resource with a restid for each node (
 *          <code>true</code>) or without ( <code>false</code>).
 * @param output
 *          The OutputStream reference which have to be modified and returned
 * @param wrap
 *          <code>true</code> if the results have to be wrapped.
 *          <code>false</code> otherwise.
 * @return The {@link OutputStream} containing the result
 * @throws SirixException
 * @throws WebApplicationException
 *           The Exception occurred.
 */
public OutputStream getModificHistory(// NOPMD this method needs alls these
final String resourceName, // functions
final String revisionRange, final boolean nodeid, final OutputStream output, final boolean wrap) throws JaxRxException, SirixException {
    // extract both revision from given String value
    final StringTokenizer tokenizer = new StringTokenizer(revisionRange, "-");
    final int revision1 = Integer.valueOf(tokenizer.nextToken());
    final int revision2 = Integer.valueOf(tokenizer.nextToken());
    if (revision1 < revision2 && revision2 <= getLastRevision(resourceName)) {
        // variables for highest rest-id in respectively revision
        long maxRestidRev1 = 1;
        long maxRestidRev2 = 1;
        // Connection to sirix, creating a session
        Database database = null;
        Axis axis = null;
        NodeReadTrx rtx = null;
        Session session = null;
        // List for all restIds of modifications
        final List<Long> modificRestids = new LinkedList<Long>();
        // List of all restIds of revision 1
        final List<Long> restIdsRev1 = new LinkedList<Long>();
        try {
            database = Databases.openDatabase(mStoragePath);
            session = database.getSession(new SessionConfiguration.Builder(resourceName).build());
            // get highest rest-id from given revision 1
            rtx = session.beginNodeReadTrx(revision1);
            axis = new XPathAxis(rtx, ".//*");
            while (axis.hasNext()) {
                axis.next();
                if (rtx.getNodeKey() > maxRestidRev1) {
                    maxRestidRev1 = rtx.getNodeKey();
                }
                // stores all restids from revision 1 into a list
                restIdsRev1.add(rtx.getNodeKey());
            }
            rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
            rtx.close();
            // get highest rest-id from given revision 2
            rtx = session.beginNodeReadTrx(revision2);
            axis = new XPathAxis(rtx, ".//*");
            while (axis.hasNext()) {
                axis.next();
                final Long nodeKey = rtx.getNodeKey();
                if (nodeKey > maxRestidRev2) {
                    maxRestidRev2 = rtx.getNodeKey();
                }
                if (nodeKey > maxRestidRev1) {
                    /*
						 * writes all restids of revision 2 higher than the highest restid
						 * of revision 1 into the list
						 */
                    modificRestids.add(nodeKey);
                }
                /*
					 * removes all restids from restIdsRev1 that appears in revision 2 all
					 * remaining restids in the list can be seen as deleted nodes
					 */
                restIdsRev1.remove(nodeKey);
            }
            rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
            rtx.close();
            rtx = session.beginNodeReadTrx(revision1);
            // linked list for holding unique restids from revision 1
            final List<Long> restIdsRev1New = new LinkedList<Long>();
            /*
				 * Checks if a deleted node has a parent node that was deleted too. If
				 * so, only the parent node is stored in new list to avoid double print
				 * of node modification
				 */
            for (Long nodeKey : restIdsRev1) {
                rtx.moveTo(nodeKey);
                final long parentKey = rtx.getParentKey();
                if (!restIdsRev1.contains(parentKey)) {
                    restIdsRev1New.add(nodeKey);
                }
            }
            rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
            rtx.close();
            if (wrap) {
                output.write(beginResult.getBytes());
            }
            /*
				 * Shred modified restids from revision 2 to xml fragment Just
				 * modifications done by post commands
				 */
            rtx = session.beginNodeReadTrx(revision2);
            for (Long nodeKey : modificRestids) {
                rtx.moveTo(nodeKey);
                WorkerHelper.serializeXML(session, output, false, nodeid, nodeKey, revision2).call();
            }
            rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
            rtx.close();
            /*
				 * Shred modified restids from revision 1 to xml fragment Just
				 * modifications done by put and deletes
				 */
            rtx = session.beginNodeReadTrx(revision1);
            for (Long nodeKey : restIdsRev1New) {
                rtx.moveTo(nodeKey);
                WorkerHelper.serializeXML(session, output, false, nodeid, nodeKey, revision1).call();
            }
            if (wrap) {
                output.write(endResult.getBytes());
            }
            rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
        } catch (final Exception globExcep) {
            throw new JaxRxException(globExcep);
        } finally {
            WorkerHelper.closeRTX(rtx, session, database);
        }
    } else {
        throw new JaxRxException(400, "Bad user request");
    }
    return output;
}
Also used : LinkedList(java.util.LinkedList) JaxRxException(org.jaxrx.core.JaxRxException) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) StringTokenizer(java.util.StringTokenizer) NodeReadTrx(org.sirix.api.NodeReadTrx) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) Database(org.sirix.api.Database) Axis(org.sirix.api.Axis) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) Session(org.sirix.api.Session) JaxRxException(org.jaxrx.core.JaxRxException)

Example 72 with Axis

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

the class FileSystemWatcher method findNode.

/**
 * Find node corresponding to the path. The {@link XdmNodeWriteTrx} globally used is moved to the
 * found node.
 *
 * @param query xpath expression
 * @throws SirixXPathException if expression isn't valid
 * @throws NullPointerException if {@code pXPath} is {@code null}
 */
private void findNode(final String query) throws SirixXPathException {
    final Axis axis = new XPathAxis(mWtx, checkNotNull(query));
    int countResults = 0;
    long resultNodeKey = Fixed.NULL_NODE_KEY.getStandardProperty();
    while (axis.hasNext()) {
        resultNodeKey = axis.next();
        countResults++;
        assert countResults == 1 : "At maximum one item should be found!";
    }
    mWtx.moveTo(resultNodeKey);
}
Also used : XPathAxis(org.sirix.service.xml.xpath.XPathAxis) Axis(org.sirix.api.Axis) XPathAxis(org.sirix.service.xml.xpath.XPathAxis)

Example 73 with Axis

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

the class NodeCompTest method testAtomize.

@Test
public void testAtomize() throws SirixXPathException {
    Axis axis = new LiteralExpr(holder.getReader(), -2);
    axis.hasNext();
    axis.next();
    AtomicValue[] value = comparator.atomize(axis);
    assertEquals(value.length, 1);
    assertEquals(holder.getReader().getNodeKey(), value[0].getNodeKey());
    assertEquals("xs:integer", value[0].getType());
    try {
        axis = new DescendantAxis(holder.getReader());
        axis.hasNext();
        comparator.atomize(axis);
    } catch (SirixXPathException e) {
        assertEquals("err:XPTY0004 The type is not appropriate the expression or" + " the typedoes not match a required type as specified by the " + "matching rules.", e.getMessage());
    }
}
Also used : SirixXPathException(org.sirix.exception.SirixXPathException) LiteralExpr(org.sirix.service.xml.xpath.expr.LiteralExpr) AtomicValue(org.sirix.service.xml.xpath.AtomicValue) DescendantAxis(org.sirix.axis.DescendantAxis) Axis(org.sirix.api.Axis) DescendantAxis(org.sirix.axis.DescendantAxis) Test(org.junit.Test)

Aggregations

Axis (org.sirix.api.Axis)73 DescendantAxis (org.sirix.axis.DescendantAxis)39 FilterAxis (org.sirix.axis.filter.FilterAxis)38 ChildAxis (org.sirix.axis.ChildAxis)23 UnionAxis (org.sirix.service.xml.xpath.expr.UnionAxis)19 DupFilterAxis (org.sirix.service.xml.xpath.filter.DupFilterAxis)19 ForAxis (org.sirix.axis.ForAxis)17 PredicateFilterAxis (org.sirix.axis.filter.PredicateFilterAxis)17 ExceptAxis (org.sirix.service.xml.xpath.expr.ExceptAxis)17 IfAxis (org.sirix.service.xml.xpath.expr.IfAxis)17 IntersectAxis (org.sirix.service.xml.xpath.expr.IntersectAxis)17 RangeAxis (org.sirix.service.xml.xpath.expr.RangeAxis)17 SequenceAxis (org.sirix.service.xml.xpath.expr.SequenceAxis)17 VariableAxis (org.sirix.service.xml.xpath.expr.VariableAxis)17 AddOpAxis (org.sirix.service.xml.xpath.operators.AddOpAxis)17 DivOpAxis (org.sirix.service.xml.xpath.operators.DivOpAxis)17 IDivOpAxis (org.sirix.service.xml.xpath.operators.IDivOpAxis)17 ModOpAxis (org.sirix.service.xml.xpath.operators.ModOpAxis)17 MulOpAxis (org.sirix.service.xml.xpath.operators.MulOpAxis)17 SubOpAxis (org.sirix.service.xml.xpath.operators.SubOpAxis)17