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);
}
}
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);
}
}
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);
}
}
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);
}
}
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;
}
Aggregations