use of org.sirix.api.Session in project sirix by sirixdb.
the class BookShredding method shredder.
private static void shredder(final File pBooks) throws Exception {
final DatabaseConfiguration config = new DatabaseConfiguration(TestHelper.PATHS.PATH1.getFile());
Databases.truncateDatabase(config);
Databases.createDatabase(config);
final Database database = Databases.openDatabase(config.getFile());
database.createResource(new ResourceConfiguration.Builder(TestHelper.RESOURCE, PATHS.PATH1.getConfig()).build());
final Session session = database.getSession(new SessionConfiguration.Builder(TestHelper.RESOURCE).build());
final NodeWriteTrx wtx = session.beginNodeWriteTrx();
final XMLEventReader reader = XMLShredder.createFileReader(pBooks);
final XMLShredder shredder = new XMLShredder.Builder(wtx, reader, Insert.ASFIRSTCHILD).commitAfterwards().build();
shredder.call();
wtx.close();
session.close();
}
use of org.sirix.api.Session in project sirix by sirixdb.
the class TestNodeWrapper method testCompareOrder.
@Test
public void testCompareOrder() throws XPathException, SirixException {
final Processor proc = new Processor(false);
final Configuration config = proc.getUnderlyingConfiguration();
final Session session = generateSession();
final NodeWriteTrx trx = session.beginNodeWriteTrx();
trx.commit();
trx.close();
// Not the same document.
NodeInfo node = new DocumentWrapper(session, config);
NodeInfo other = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 3);
try {
node.compareOrder(other);
fail();
} catch (final IllegalStateException e) {
}
// Before.
node = new DocumentWrapper(mHolder.getSession(), config);
other = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 3);
assertEquals(-1, node.compareOrder(other));
// After.
node = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 3);
other = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 0);
assertEquals(1, node.compareOrder(other));
// Same.
node = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 3);
other = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 3);
assertEquals(0, node.compareOrder(other));
session.close();
mDatabase.close();
}
use of org.sirix.api.Session in project sirix by sirixdb.
the class RestXPathProcessor method doXPathRes.
/**
* This method performs an XPath evaluation and writes it to a given output
* stream.
*
* @param resource
* The existing resource.
* @param revision
* The revision of the requested document.
* @param output
* The output stream where the results are written.
* @param nodeid
* <code>true</code> if node id's have to be delivered.
* <code>false</code> otherwise.
* @param xpath
* The XPath expression.
* @throws SirixException
*/
private void doXPathRes(final String resource, final Integer revision, final OutputStream output, final boolean nodeid, final String xpath) throws SirixException {
// Database connection to sirix
Database database = null;
Session session = null;
NodeReadTrx rtx = null;
try {
database = Databases.openDatabase(mStoragePath);
session = database.getSession(new SessionConfiguration.Builder(resource).build());
// Creating a transaction
if (revision == null) {
rtx = session.beginNodeReadTrx();
} else {
rtx = session.beginNodeReadTrx(revision);
}
final Axis axis = new XPathAxis(rtx, xpath);
for (final long key : axis) {
WorkerHelper.serializeXML(session, output, false, nodeid, key, revision).call();
}
} catch (final Exception globExcep) {
throw new WebApplicationException(globExcep, Response.Status.INTERNAL_SERVER_ERROR);
} finally {
if (rtx != null) {
rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
WorkerHelper.closeRTX(rtx, session, database);
}
}
}
use of org.sirix.api.Session in project sirix by sirixdb.
the class RestXPathProcessor method getXpathResource.
/**
* Getting part of the XML based on a XPath query
*
* @param dbFile
* where the content should be extracted
*
* @param query
* contains XPath query
* @param rId
* To response the resource with a restid for each node (
* <code>true</code>) or without ( <code>false</code>).
* @param doRevision
* The revision of the requested resource. If <code>null</code>, than
* response the latest revision.
* @param output
* The OutputStream reference which have to be modified and returned
* @param doNodeId
* specifies whether node id should be shown
* @param doWrap
* output of result elements
* @throws SirixException
*/
public void getXpathResource(final File dbFile, final long rId, final String query, final boolean doNodeId, final Integer doRevision, final OutputStream output, final boolean doWrap) throws SirixException {
// work around because of query root char '/'
String qQuery = query;
if (query.charAt(0) == '/')
qQuery = ".".concat(query);
Database database = null;
Session session = null;
NodeReadTrx rtx = null;
try {
database = Databases.openDatabase(dbFile.getParentFile());
session = database.getSession(new SessionConfiguration.Builder(dbFile.getName()).build());
if (doRevision == null) {
rtx = session.beginNodeReadTrx();
} else {
rtx = session.beginNodeReadTrx(doRevision);
}
final boolean exist = rtx.moveTo(rId).hasMoved();
if (exist) {
final Axis axis = new XPathAxis(rtx, qQuery);
if (doWrap) {
output.write(beginResult.getBytes());
for (final long key : axis) {
WorkerHelper.serializeXML(session, output, false, doNodeId, key, doRevision).call();
}
output.write(endResult.getBytes());
} else {
for (final long key : axis) {
WorkerHelper.serializeXML(session, output, false, doNodeId, key, doRevision).call();
}
}
} else {
throw new WebApplicationException(404);
}
} catch (final Exception globExcep) {
throw new WebApplicationException(globExcep, Response.Status.INTERNAL_SERVER_ERROR);
} finally {
WorkerHelper.closeRTX(rtx, session, database);
}
}
use of org.sirix.api.Session in project sirix by sirixdb.
the class DatabaseRepresentation method revertToRevision.
/**
* This method reverts the latest revision data to the requested.
*
* @param resourceName
* The name of the XML resource.
* @param backToRevision
* The revision value, which has to be set as the latest.
* @throws WebApplicationException
* @throws SirixException
*/
public void revertToRevision(final String resourceName, final int backToRevision) throws JaxRxException, SirixException {
Database database = null;
Session session = null;
NodeWriteTrx wtx = null;
boolean abort = false;
try {
database = Databases.openDatabase(mStoragePath);
session = database.getSession(new SessionConfiguration.Builder(resourceName).build());
wtx = session.beginNodeWriteTrx();
wtx.revertTo(backToRevision);
wtx.commit();
} catch (final SirixException exce) {
abort = true;
throw new JaxRxException(exce);
} finally {
WorkerHelper.closeWTX(abort, wtx, session, database);
}
}
Aggregations