Search in sources :

Example 46 with Database

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

the class DBStore method create.

@Override
public TemporalCollection<?> create(final String collName, @Nullable final Stream<SubtreeParser> parsers) throws DocumentException {
    if (parsers != null) {
        final DatabaseConfiguration dbConf = new DatabaseConfiguration(mLocation.resolve(collName));
        try {
            Databases.truncateDatabase(dbConf);
            Databases.createDatabase(dbConf);
            final Database database = Databases.openDatabase(dbConf.getFile());
            mDatabases.add(database);
            final ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
            int i = database.listResources().size() + 1;
            try {
                SubtreeParser parser = null;
                while ((parser = parsers.next()) != null) {
                    final SubtreeParser nextParser = parser;
                    final String resourceName = new StringBuilder("resource").append(String.valueOf(i)).toString();
                    pool.submit(() -> {
                        database.createResource(ResourceConfiguration.newBuilder(resourceName, dbConf).storageType(mStorageType).useDeweyIDs(true).useTextCompression(true).buildPathSummary(true).build());
                        try (final ResourceManager resource = database.getResourceManager(new ResourceManagerConfiguration.Builder(resourceName).build());
                            final XdmNodeWriteTrx wtx = resource.beginNodeWriteTrx()) {
                            final DBCollection collection = new DBCollection(collName, database);
                            mCollections.put(database, collection);
                            nextParser.parse(new SubtreeBuilder(collection, wtx, Insert.ASFIRSTCHILD, Collections.<SubtreeListener<? super AbstractTemporalNode<DBNode>>>emptyList()));
                            wtx.commit();
                        }
                        return null;
                    });
                    i++;
                }
            } finally {
                parsers.close();
            }
            pool.shutdown();
            pool.awaitTermination(5, TimeUnit.MINUTES);
            return new DBCollection(collName, database);
        } catch (final SirixRuntimeException | InterruptedException e) {
            throw new DocumentException(e.getCause());
        }
    }
    return null;
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) SubtreeListener(org.brackit.xquery.node.parser.SubtreeListener) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) SubtreeParser(org.brackit.xquery.node.parser.SubtreeParser) AbstractTemporalNode(org.brackit.xquery.xdm.AbstractTemporalNode) ResourceManager(org.sirix.api.ResourceManager) SirixRuntimeException(org.sirix.exception.SirixRuntimeException) DocumentException(org.brackit.xquery.xdm.DocumentException) Database(org.sirix.api.Database) ExecutorService(java.util.concurrent.ExecutorService)

Example 47 with Database

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

the class DBStore method drop.

@Override
public void drop(final String name) throws DocumentException {
    final DatabaseConfiguration dbConfig = new DatabaseConfiguration(mLocation.resolve(name));
    if (Databases.existsDatabase(dbConfig)) {
        try {
            Databases.truncateDatabase(dbConfig);
            final Database database = Databases.openDatabase(dbConfig.getFile());
            mDatabases.remove(database);
            mCollections.remove(database);
        } catch (final SirixRuntimeException e) {
            throw new DocumentException(e);
        }
    }
    throw new DocumentException("No collection with the specified name found!");
}
Also used : SirixRuntimeException(org.sirix.exception.SirixRuntimeException) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) DocumentException(org.brackit.xquery.xdm.DocumentException) Database(org.sirix.api.Database)

Example 48 with Database

use of org.sirix.api.Database 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();
}
Also used : DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) Database(org.sirix.api.Database) NodeWriteTrx(org.sirix.api.NodeWriteTrx) XMLEventReader(javax.xml.stream.XMLEventReader) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) Session(org.sirix.api.Session)

Example 49 with Database

use of org.sirix.api.Database 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);
        }
    }
}
Also used : NodeReadTrx(org.sirix.api.NodeReadTrx) WebApplicationException(javax.ws.rs.WebApplicationException) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) Database(org.sirix.api.Database) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) Axis(org.sirix.api.Axis) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Session(org.sirix.api.Session)

Example 50 with Database

use of org.sirix.api.Database 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);
    }
}
Also used : NodeReadTrx(org.sirix.api.NodeReadTrx) WebApplicationException(javax.ws.rs.WebApplicationException) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) Database(org.sirix.api.Database) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) Axis(org.sirix.api.Axis) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Session(org.sirix.api.Session)

Aggregations

Database (org.sirix.api.Database)55 ResourceManager (org.sirix.api.ResourceManager)30 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)22 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)19 Test (org.junit.Test)18 SirixException (org.sirix.exception.SirixException)18 Session (org.sirix.api.Session)16 IOException (java.io.IOException)13 Path (java.nio.file.Path)13 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)13 XMLSerializerBuilder (org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder)12 JaxRxException (org.jaxrx.core.JaxRxException)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 NodeWriteTrx (org.sirix.api.NodeWriteTrx)8 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)7 WebApplicationException (javax.ws.rs.WebApplicationException)6 NodeReadTrx (org.sirix.api.NodeReadTrx)6 XMLSerializer (org.sirix.service.xml.serialize.XMLSerializer)6 XMLEventReader (javax.xml.stream.XMLEventReader)5 DocumentException (org.brackit.xquery.xdm.DocumentException)5