Search in sources :

Example 41 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class ProcessFileSystemAttributes method processFile.

@Override
public void processFile(final XdmNodeWriteTrx trx, Path path, final Optional<BasicFileAttributes> attrs) {
    if (Files.exists(path)) {
        final String file = path.getFileName().toString();
        final int index = file.lastIndexOf('.');
        if (index > 0) {
            final String suffix = file.substring(index + 1);
            if (!suffix.isEmpty()) {
                try {
                    trx.insertAttribute(new QNm("suffix"), file.substring(index + 1));
                    trx.moveToParent();
                } catch (SirixException e) {
                    LOGWRAPPER.error(e.getMessage(), e);
                }
            }
        }
    }
}
Also used : QNm(org.brackit.xquery.atomic.QNm) SirixException(org.sirix.exception.SirixException)

Example 42 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class WikipediaImport method importData.

/**
 * Import data.
 *
 * @param dateRange
 *        <p>
 *        Date range, the following values are possible:
 *        </p>
 *        <dl>
 *        <dt>h</dt>
 *        <dd>hourly revisions</dd>
 *        <dt>d</dt>
 *        <dd>daily revisions</dd>
 *        <dt>w</dt>
 *        <dd>weekly revisions (currently unsupported)</dd>
 *        <dt>m</dt>
 *        <dd>monthly revisions</dd>
 *        </dl>
 *
 * @param data
 *        <p>
 *        List of {@link StartElement}s with the following meaning:
 *        </p>
 *        <dl>
 *        <dt>Zero index</dt>
 *        <dd>Timestamp start tag {@link StartElement}.</dd>
 *        <dt>First index</dt>
 *        <dd>Page start tag {@link StartElement}.</dd>
 *        <dt>Second index</dt>
 *        <dd>Revision start tag {@link StartElement}.</dd>
 *        <dt>Third index</dt>
 *        <dd>Page-ID start tag {@link StartElement}.</dd>
 *        <dt>Fourth index</dt>
 *        <dd>Revision text start tag {@link StartElement}.</dd>
 *        </dl>
 */
@Override
public void importData(final DateBy dateRange, final List<StartElement> data) {
    // Some checks.
    checkNotNull(dateRange);
    checkNotNull(data);
    checkArgument(data.size() == 5, "data must have 5 elements!");
    final StartElement timestamp = data.get(0);
    final StartElement page = data.get(1);
    final StartElement rev = data.get(2);
    final StartElement id = data.get(3);
    // final StartElement text = pData.get(4);
    // Initialize variables.
    mFound = false;
    mIsRev = false;
    boolean isFirst = true;
    try {
        while (mReader.hasNext()) {
            final XMLEvent event = mReader.nextEvent();
            if (isWhitespace(event)) {
                continue;
            }
            // Add event to page or revision metadata list if it's not a
            // whitespace.
            mPageEvents.add(event);
            switch(event.getEventType()) {
                case XMLStreamConstants.START_ELEMENT:
                    if (checkStAXStartElement(event.asStartElement(), rev)) {
                        // StAX parser in rev metadata.
                        isFirst = false;
                        mIsRev = true;
                    } else {
                        parseStartTag(event, timestamp, page, rev, id, dateRange);
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    if (event.asEndElement().getName().equals(page.getName()) && !isFirst) {
                        // StAX parser is located at the end of an article/page.
                        mIsRev = false;
                        if (mFound) {
                            try {
                                updateShredder();
                            } catch (final IOException e) {
                                LOGWRAPPER.error(e.getMessage(), e);
                            }
                        } else {
                            // Move wtx to end of file and append page.
                            mWtx.moveToDocumentRoot();
                            final boolean hasFirstChild = mWtx.hasFirstChild();
                            if (hasFirstChild) {
                                moveToLastPage(page);
                                assert mWtx.getName().getLocalName().equals(page.getName().getLocalPart());
                            }
                            XMLShredder shredder = null;
                            if (hasFirstChild) {
                                // Shredder as child.
                                shredder = new XMLShredder.Builder(mWtx, XMLShredder.createQueueReader(mPageEvents), Insert.ASRIGHTSIBLING).build();
                            } else {
                                // Shredder as right sibling.
                                shredder = new XMLShredder.Builder(mWtx, XMLShredder.createQueueReader(mPageEvents), Insert.ASFIRSTCHILD).build();
                            }
                            shredder.call();
                            mPageEvents = new ArrayDeque<>();
                        }
                    }
                    break;
                default:
            }
        }
        mWtx.commit();
        mWtx.close();
        mResourceManager.close();
        mDatabase.close();
    } catch (final XMLStreamException | SirixException | IOException e) {
        LOGWRAPPER.error(e.getMessage(), e);
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException)

Example 43 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class SirixDeweyID method parseDivisionValues.

private final int[] parseDivisionValues(String divisionPart) throws SirixException {
    if (divisionPart.charAt(divisionPart.length() - 1) != '.')
        divisionPart += '.';
    String[] divisions = divisionPart.split("\\.");
    int[] divisionValues = new int[divisions.length];
    int i = 0;
    for (String division : divisions) {
        try {
            divisionValues[i] = Integer.parseInt(division);
            i++;
        } catch (NumberFormatException e) {
            throw new SirixException("Division " + i + " has an invalid value: " + division, e);
        }
    }
    return divisionValues;
}
Also used : SirixException(org.sirix.exception.SirixException)

Example 44 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class FMSE method emitInsert.

/**
 * Emit an insert operation.
 *
 * @param parent parent of the current {@link Node} implementation reference to insert
 * @param child the current node to insert
 * @param pos position of the insert
 * @param wtx {@link XdmNodeWriteTrx} implementation reference on old revision
 * @param rtx {@link XdmNodeReadTrx} implementation reference on new revision
 * @return inserted {@link Node} implementation reference
 * @throws SirixException if anything in sirix fails
 */
private long emitInsert(final long child, final long parent, final int pos, final XdmNodeWriteTrx wtx, final XdmNodeReadTrx rtx) {
    assert child >= 0;
    assert parent >= 0;
    assert wtx != null;
    assert rtx != null;
    // Determines if node has been already inserted (for subtrees).
    if (mAlreadyInserted.get(child) != null) {
        // actually child'
        return child;
    }
    wtx.moveTo(parent);
    rtx.moveTo(child);
    try {
        switch(rtx.getKind()) {
            case ATTRIBUTE:
                try {
                    wtx.insertAttribute(rtx.getName(), rtx.getValue());
                } catch (final SirixUsageException e) {
                    mTotalMatching.remove(wtx.getNodeKey());
                    wtx.setValue(rtx.getValue());
                }
                process(wtx.getNodeKey(), rtx.getNodeKey());
                break;
            case NAMESPACE:
                // Note that the insertion is right (localPart as prefix).
                try {
                    wtx.insertNamespace(new QNm(rtx.getName().getNamespaceURI(), rtx.getName().getLocalName(), ""));
                } catch (final SirixUsageException e) {
                    mTotalMatching.remove(wtx.getNodeKey());
                }
                process(wtx.getNodeKey(), rtx.getNodeKey());
                break;
            default:
                // In case of other node types.
                long oldKey = 0;
                if (pos == 0) {
                    switch(rtx.getKind()) {
                        case ELEMENT:
                            oldKey = wtx.copySubtreeAsFirstChild(rtx).getNodeKey();
                            break;
                        case TEXT:
                            // is inserted.
                            if (wtx.hasFirstChild()) {
                                wtx.moveToFirstChild();
                                if (wtx.getKind() == Kind.TEXT) {
                                    mTotalMatching.remove(wtx.getNodeKey());
                                    wtx.remove();
                                }
                                wtx.moveTo(parent);
                            }
                            oldKey = wtx.insertTextAsFirstChild(rtx.getValue()).getNodeKey();
                            break;
                        default:
                    }
                } else {
                    assert wtx.hasFirstChild();
                    wtx.moveToFirstChild();
                    for (int i = 0; i < pos - 1; i++) {
                        assert wtx.hasRightSibling();
                        wtx.moveToRightSibling();
                    }
                    // Remove right sibl. text node if a text node already exists.
                    removeRightSiblingTextNode(wtx);
                    switch(rtx.getKind()) {
                        case ELEMENT:
                            oldKey = wtx.copySubtreeAsRightSibling(rtx).getNodeKey();
                            break;
                        case TEXT:
                            oldKey = wtx.insertTextAsRightSibling(rtx.getValue()).getNodeKey();
                            break;
                        default:
                            // Already inserted.
                            throw new IllegalStateException("Child should be already inserted!");
                    }
                }
                // Mark all nodes in subtree as inserted.
                wtx.moveTo(oldKey);
                rtx.moveTo(child);
                for (final Axis oldAxis = new DescendantAxis(wtx, IncludeSelf.YES), newAxis = new DescendantAxis(rtx, IncludeSelf.YES); oldAxis.hasNext() && newAxis.hasNext(); ) {
                    oldAxis.next();
                    newAxis.next();
                    final XdmNodeReadTrx oldRtx = oldAxis.getTrx();
                    final XdmNodeReadTrx newRtx = newAxis.getTrx();
                    process(oldRtx.getNodeKey(), newRtx.getNodeKey());
                    final long newNodeKey = newRtx.getNodeKey();
                    final long oldNodeKey = oldRtx.getNodeKey();
                    if (newRtx.getKind() == Kind.ELEMENT) {
                        assert newRtx.getKind() == oldRtx.getKind();
                        if (newRtx.getAttributeCount() > 0) {
                            for (int i = 0, attCount = newRtx.getAttributeCount(); i < attCount; i++) {
                                rtx.moveToAttribute(i);
                                for (int j = 0, oldAttCount = oldRtx.getAttributeCount(); i < oldAttCount; j++) {
                                    wtx.moveToAttribute(j);
                                    if (wtx.getName().equals(rtx.getName())) {
                                        process(oldAxis.getTrx().getNodeKey(), newAxis.getTrx().getNodeKey());
                                        break;
                                    }
                                    oldAxis.getTrx().moveTo(oldNodeKey);
                                }
                                newAxis.getTrx().moveTo(newNodeKey);
                            }
                        }
                        if (newRtx.getNamespaceCount() > 0) {
                            for (int i = 0, nspCount = newRtx.getNamespaceCount(); i < nspCount; i++) {
                                rtx.moveToNamespace(i);
                                for (int j = 0, oldNspCount = oldRtx.getNamespaceCount(); j < oldNspCount; j++) {
                                    wtx.moveToNamespace(j);
                                    if (wtx.getName().getNamespaceURI().equals(rtx.getName().getNamespaceURI()) && wtx.getName().getPrefix().equals(wtx.getName().getPrefix())) {
                                        process(wtx.getNodeKey(), rtx.getNodeKey());
                                        break;
                                    }
                                    oldAxis.getTrx().moveTo(oldNodeKey);
                                }
                                newAxis.getTrx().moveTo(newNodeKey);
                            }
                        }
                    }
                    newAxis.getTrx().moveTo(newNodeKey);
                }
        }
    } catch (final SirixException e) {
        LOGWRAPPER.error(e.getMessage(), e);
    }
    return wtx.getNodeKey();
}
Also used : QNm(org.brackit.xquery.atomic.QNm) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) SirixException(org.sirix.exception.SirixException) SirixUsageException(org.sirix.exception.SirixUsageException) VisitorDescendantAxis(org.sirix.axis.visitor.VisitorDescendantAxis) DescendantAxis(org.sirix.axis.DescendantAxis) Axis(org.sirix.api.Axis) VisitorDescendantAxis(org.sirix.axis.visitor.VisitorDescendantAxis) AbstractAxis(org.sirix.axis.AbstractAxis) ChildAxis(org.sirix.axis.ChildAxis) PostOrderAxis(org.sirix.axis.PostOrderAxis) DescendantAxis(org.sirix.axis.DescendantAxis) LevelOrderAxis(org.sirix.axis.LevelOrderAxis)

Example 45 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class FMSEImport method dataImport.

/**
 * Import the data.
 *
 * @param resOldRev {@link File} for old revision (sirix resource)
 * @param resNewRev {@link File} for new revision (XML resource)
 */
private void dataImport(final Path resOldRev, @Nonnull final Path resNewRev) {
    try {
        final Path newRevTarget = Files.createTempDirectory(resNewRev.toAbsolutePath().toString());
        if (Files.exists(newRevTarget)) {
            SirixFiles.recursiveRemove(newRevTarget);
        }
        shredder(checkNotNull(resNewRev), newRevTarget);
        try (final Database databaseOld = Databases.openDatabase(resOldRev);
            final ResourceManager resMgrOld = databaseOld.getResourceManager(new ResourceManagerConfiguration.Builder("shredded").build());
            final XdmNodeWriteTrx wtx = resMgrOld.beginNodeWriteTrx();
            final Database databaseNew = Databases.openDatabase(newRevTarget);
            final ResourceManager resourceNew = databaseNew.getResourceManager(new ResourceManagerConfiguration.Builder("shredded").build());
            final XdmNodeReadTrx rtx = resourceNew.beginNodeReadTrx();
            final FMSE fmes = new FMSE()) {
            fmes.diff(wtx, rtx);
        }
    } catch (final SirixException | IOException | XMLStreamException e) {
        LOGWRAPPER.error(e.getMessage(), e);
    }
}
Also used : Path(java.nio.file.Path) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) XMLStreamException(javax.xml.stream.XMLStreamException) Database(org.sirix.api.Database) FMSE(org.sirix.diff.algorithm.fmse.FMSE) SirixException(org.sirix.exception.SirixException) ResourceManager(org.sirix.api.ResourceManager) IOException(java.io.IOException)

Aggregations

SirixException (org.sirix.exception.SirixException)74 DocumentException (org.brackit.xquery.xdm.DocumentException)25 IOException (java.io.IOException)18 Database (org.sirix.api.Database)17 JaxRxException (org.jaxrx.core.JaxRxException)14 NodeReadTrx (org.sirix.api.NodeReadTrx)13 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)13 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)12 Session (org.sirix.api.Session)12 ResourceManager (org.sirix.api.ResourceManager)10 WebApplicationException (javax.ws.rs.WebApplicationException)8 Path (java.nio.file.Path)7 XMLStreamException (javax.xml.stream.XMLStreamException)7 QNm (org.brackit.xquery.atomic.QNm)7 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)6 OutputStream (java.io.OutputStream)5 SubtreeListener (org.brackit.xquery.node.parser.SubtreeListener)5 AbstractTemporalNode (org.brackit.xquery.xdm.AbstractTemporalNode)5 StreamingOutput (javax.ws.rs.core.StreamingOutput)4 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)4