Search in sources :

Example 1 with SirixIOException

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

the class XMLUpdateShredder method updateOnly.

/**
 * Update a shreddered file.
 *
 * @throws SirixException if sirix encounters something went wrong
 */
private void updateOnly() throws SirixException {
    try {
        // Initialize variables.
        mLevelInToShredder = 0;
        // mIsLastNode = false;
        mMovedToRightSibling = false;
        boolean firstEvent = true;
        // If structure already exists, make a sync against the current structure.
        if (mMaxNodeKey == 0) {
            // If no content is in the XML, a normal insertNewContent is executed.
            new XMLShredder.Builder(mWtx, mReader, mInsert).build().call();
        } else {
            if (mWtx.getKind() == Kind.DOCUMENT) {
                // Find the start key for the update operation.
                long startkey = Fixed.DOCUMENT_NODE_KEY.getStandardProperty() + 1;
                while (!mWtx.moveTo(startkey).hasMoved()) {
                    startkey++;
                }
            }
            XMLEvent event = null;
            StringBuilder sBuilder = new StringBuilder();
            final XMLEventFactory fac = XMLEventFactory.newInstance();
            // Iterate over all nodes.
            while (mReader.hasNext()) {
                // Parsing the next event.
                if (mDelete == EDelete.ATSTARTMIDDLE) {
                    /*
             * Do not move StAX parser forward if nodes have been deleted at the start or in the
             * middle of a subtree.
             */
                    mDelete = EDelete.NODELETE;
                } else {
                    // After an insert or after nodes were equal.
                    event = mReader.nextEvent();
                    if (event.isCharacters() && event.asCharacters().isWhiteSpace()) {
                        continue;
                    }
                    assert event != null;
                    if (firstEvent) {
                        // Setup start element from StAX parser.
                        firstEvent = false;
                        if (event.getEventType() == XMLStreamConstants.START_DOCUMENT) {
                            while (mReader.hasNext() && event.getEventType() != XMLStreamConstants.START_ELEMENT) {
                                event = mReader.nextEvent();
                            }
                            assert event.getEventType() == XMLStreamConstants.START_ELEMENT;
                        }
                        if (event.getEventType() != XMLStreamConstants.START_ELEMENT) {
                            throw new IllegalStateException("StAX parser has to be on START_DOCUMENT or START_ELEMENT event!");
                        }
                        // Get root element of subtree or whole XML document to shredder.
                        mRootElem = event.asStartElement().getName();
                    } else if (event != null && event.isEndElement() && mRootElem.equals(event.asEndElement().getName()) && mLevelInToShredder == 1) {
                        // End with shredding if end_elem equals root-elem.
                        break;
                    }
                }
                assert event != null;
                switch(event.getEventType()) {
                    case XMLStreamConstants.START_ELEMENT:
                        processStartTag(event.asStartElement());
                        break;
                    case XMLStreamConstants.CHARACTERS:
                        sBuilder.append(event.asCharacters().getData());
                        while (mReader.peek().getEventType() == XMLStreamConstants.CHARACTERS) {
                            sBuilder.append(mReader.nextEvent().asCharacters().getData());
                        }
                        final Characters text = fac.createCharacters(sBuilder.toString().trim());
                        processCharacters(text);
                        sBuilder = new StringBuilder();
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                        processEndTag();
                        break;
                    default:
                }
            }
            // if (!mIsLastNode) {
            // if (mInserted) {
            // // Remove next node after node, which was inserted, because it must
            // have been deleted.
            // if (mWtx.moveToRightSibling()) {
            // mWtx.remove();
            // }
            // } else {
            // // Remove current node (cursor has been moved to the next node
            // already).
            // mWtx.remove();
            // }
            // 
            // // Also remove any siblings.
            // boolean hasRightSibling = false;
            // while (mWtx.getStructuralNode().hasRightSibling()) {
            // hasRightSibling = true;
            // mWtx.remove();
            // }
            // if (hasRightSibling) {
            // mWtx.remove();
            // }
            // }
            mReader.close();
        }
    // TODO: use Java7 multi-catch feature.
    } catch (final XMLStreamException e) {
        throw new SirixIOException(e);
    } catch (final IOException e) {
        throw new SirixIOException(e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLEventFactory(javax.xml.stream.XMLEventFactory) Characters(javax.xml.stream.events.Characters) XMLEvent(javax.xml.stream.events.XMLEvent) SirixIOException(org.sirix.exception.SirixIOException) IOException(java.io.IOException) SirixIOException(org.sirix.exception.SirixIOException)

Example 2 with SirixIOException

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

the class BerkeleyReader method read.

@Override
public Page read(final PageReference key, final PageReadTrx pageReadTrx) throws SirixIOException {
    mPageBinding = new PageBinding(mByteHandler, pageReadTrx);
    final DatabaseEntry valueEntry = new DatabaseEntry();
    final DatabaseEntry keyEntry = new DatabaseEntry();
    TupleBinding.getPrimitiveBinding(Long.class).objectToEntry(key.getKey(), keyEntry);
    Page page = null;
    try {
        final OperationStatus status = mDatabase.get(mTxn, keyEntry, valueEntry, LockMode.DEFAULT);
        if (status == OperationStatus.SUCCESS) {
            page = mPageBinding.entryToObject(valueEntry);
        }
        return page;
    } catch (final DatabaseException exc) {
        throw new SirixIOException(exc);
    }
}
Also used : PageBinding(org.sirix.io.berkeley.binding.PageBinding) OperationStatus(com.sleepycat.je.OperationStatus) UberPage(org.sirix.page.UberPage) Page(org.sirix.page.interfaces.Page) DatabaseEntry(com.sleepycat.je.DatabaseEntry) DatabaseException(com.sleepycat.je.DatabaseException) SirixIOException(org.sirix.exception.SirixIOException)

Example 3 with SirixIOException

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

the class BerkeleyReader method readUberPageReference.

@Override
public PageReference readUberPageReference() throws SirixIOException {
    final DatabaseEntry valueEntry = new DatabaseEntry();
    final DatabaseEntry keyEntry = new DatabaseEntry();
    TupleBinding.getPrimitiveBinding(Long.class).objectToEntry(-1l, keyEntry);
    try {
        final OperationStatus status = mDatabase.get(mTxn, keyEntry, valueEntry, LockMode.DEFAULT);
        PageReference uberPageReference = new PageReference();
        if (status == OperationStatus.SUCCESS) {
            uberPageReference.setKey(TupleBinding.getPrimitiveBinding(Long.class).entryToObject(valueEntry));
        }
        final UberPage page = (UberPage) read(uberPageReference, null);
        if (uberPageReference != null) {
            uberPageReference.setPage(page);
        }
        return uberPageReference;
    } catch (final DatabaseException e) {
        throw new SirixIOException(e);
    }
}
Also used : PageReference(org.sirix.page.PageReference) UberPage(org.sirix.page.UberPage) OperationStatus(com.sleepycat.je.OperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry) DatabaseException(com.sleepycat.je.DatabaseException) SirixIOException(org.sirix.exception.SirixIOException)

Example 4 with SirixIOException

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

the class BerkeleyWriter method setLastNodePage.

/**
 * Setting the last {@link UnorderedKeyValuePage} to the persistent storage.
 *
 * @param data key to be stored
 * @throws SirixIOException if can't set last {@link UnorderedKeyValuePage}
 */
private void setLastNodePage(@Nonnegative final long data) throws SirixIOException {
    final DatabaseEntry keyEntry = new DatabaseEntry();
    final DatabaseEntry valueEntry = new DatabaseEntry();
    TupleBinding.getPrimitiveBinding(Long.class).objectToEntry(-2l, keyEntry);
    TupleBinding.getPrimitiveBinding(Long.class).objectToEntry(data, valueEntry);
    try {
        mDatabase.put(mTxn, keyEntry, valueEntry);
    } catch (final DatabaseException exc) {
        throw new SirixIOException(exc);
    }
}
Also used : DatabaseEntry(com.sleepycat.je.DatabaseEntry) DatabaseException(com.sleepycat.je.DatabaseException) SirixIOException(org.sirix.exception.SirixIOException)

Example 5 with SirixIOException

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

the class BerkeleyWriter method writeUberPageReference.

@Override
public Writer writeUberPageReference(final PageReference pageReference) throws SirixIOException {
    write(pageReference);
    final DatabaseEntry keyEntry = new DatabaseEntry();
    TupleBinding.getPrimitiveBinding(Long.class).objectToEntry(-1l, keyEntry);
    final DatabaseEntry valueEntry = new DatabaseEntry();
    TupleBinding.getPrimitiveBinding(Long.class).objectToEntry(pageReference.getKey(), valueEntry);
    try {
        mDatabase.put(mTxn, keyEntry, valueEntry);
    } catch (final DatabaseException exc) {
        throw new SirixIOException(exc);
    }
    return this;
}
Also used : DatabaseEntry(com.sleepycat.je.DatabaseEntry) DatabaseException(com.sleepycat.je.DatabaseException) SirixIOException(org.sirix.exception.SirixIOException)

Aggregations

SirixIOException (org.sirix.exception.SirixIOException)42 IOException (java.io.IOException)14 DatabaseException (com.sleepycat.je.DatabaseException)9 DatabaseEntry (com.sleepycat.je.DatabaseEntry)7 UberPage (org.sirix.page.UberPage)6 OperationStatus (com.sleepycat.je.OperationStatus)5 Path (java.nio.file.Path)5 QNm (org.brackit.xquery.atomic.QNm)5 PageReference (org.sirix.page.PageReference)5 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)5 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)4 ExecutionException (java.util.concurrent.ExecutionException)4 Str (org.brackit.xquery.atomic.Str)4 Page (org.sirix.page.interfaces.Page)4 UncheckedIOException (java.io.UncheckedIOException)3 HashSet (java.util.HashSet)3 QueryException (org.brackit.xquery.QueryException)3 Item (org.brackit.xquery.xdm.Item)3 Iter (org.brackit.xquery.xdm.Iter)3 IndexController (org.sirix.access.IndexController)3