Search in sources :

Example 41 with ResourceManager

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

the class DBCollection method add.

public DBNode add(final String resName, SubtreeParser parser) throws OperationNotSupportedException, DocumentException {
    try {
        final String resource = new StringBuilder(2).append("resource").append(mDatabase.listResources().size() + 1).toString();
        mDatabase.createResource(ResourceConfiguration.newBuilder(resource, mDatabase.getDatabaseConfig()).useDeweyIDs(true).useTextCompression(true).buildPathSummary(true).build());
        final ResourceManager manager = mDatabase.getResourceManager(ResourceManagerConfiguration.newBuilder(resource).build());
        final XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx();
        final SubtreeHandler handler = new SubtreeBuilder(this, wtx, Insert.ASFIRSTCHILD, Collections.<SubtreeListener<? super AbstractTemporalNode<DBNode>>>emptyList());
        // Make sure the CollectionParser is used.
        if (!(parser instanceof CollectionParser)) {
            parser = new CollectionParser(parser);
        }
        parser.parse(handler);
        return new DBNode(wtx, this);
    } catch (final SirixException e) {
        LOGGER.error(e.getMessage(), e);
        return null;
    }
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) CollectionParser(org.brackit.xquery.node.parser.CollectionParser) SubtreeHandler(org.brackit.xquery.node.parser.SubtreeHandler) SirixException(org.sirix.exception.SirixException) ResourceManager(org.sirix.api.ResourceManager)

Example 42 with ResourceManager

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

the class DBCollection method add.

@Override
public DBNode add(SubtreeParser parser) throws OperationNotSupportedException, DocumentException {
    try {
        final String resourceName = new StringBuilder(2).append("resource").append(mDatabase.listResources().size() + 1).toString();
        mDatabase.createResource(ResourceConfiguration.newBuilder(resourceName, mDatabase.getDatabaseConfig()).useDeweyIDs(true).useTextCompression(true).buildPathSummary(true).build());
        final ResourceManager resource = mDatabase.getResourceManager(ResourceManagerConfiguration.newBuilder(resourceName).build());
        final XdmNodeWriteTrx wtx = resource.beginNodeWriteTrx();
        final SubtreeHandler handler = new SubtreeBuilder(this, wtx, Insert.ASFIRSTCHILD, Collections.<SubtreeListener<? super AbstractTemporalNode<DBNode>>>emptyList());
        // Make sure the CollectionParser is used.
        if (!(parser instanceof CollectionParser)) {
            parser = new CollectionParser(parser);
        }
        parser.parse(handler);
        return new DBNode(wtx, this);
    } catch (final SirixException e) {
        LOGGER.error(e.getMessage(), e);
        return null;
    }
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) CollectionParser(org.brackit.xquery.node.parser.CollectionParser) SubtreeHandler(org.brackit.xquery.node.parser.SubtreeHandler) SirixException(org.sirix.exception.SirixException) ResourceManager(org.sirix.api.ResourceManager)

Example 43 with ResourceManager

use of org.sirix.api.ResourceManager 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 44 with ResourceManager

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

the class VersioningTest method test2.

/**
 * Test revisioning.
 *
 * @throws SirixException if anything in Sirix fails
 */
public void test2() throws SirixException {
    try (final ResourceManager manager = mDatabase.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build())) {
        XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx();
        wtx.insertElementAsFirstChild(new QNm("foo"));
        wtx.commit();
        wtx.insertElementAsFirstChild(new QNm("foo"));
        wtx.commit();
        wtx.insertElementAsFirstChild(new QNm("foo"));
        wtx.commit();
        wtx.insertElementAsFirstChild(new QNm("foo"));
        wtx.commit();
        wtx.insertElementAsFirstChild(new QNm("foo"));
        wtx.commit();
        wtx.close();
        try (final XdmNodeReadTrx rtx = manager.beginNodeReadTrx()) {
            assertTrue(rtx.moveToFirstChild().hasMoved());
            assertTrue(rtx.moveToFirstChild().hasMoved());
            assertTrue(rtx.moveToFirstChild().hasMoved());
            assertTrue(rtx.moveToFirstChild().hasMoved());
            assertTrue(rtx.moveToFirstChild().hasMoved());
        }
    }
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) QNm(org.brackit.xquery.atomic.QNm) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) ResourceManager(org.sirix.api.ResourceManager)

Example 45 with ResourceManager

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

the class ResourceStore method closeResource.

public boolean closeResource(final Path resourceFile) {
    final ResourceManager manager = mResourceManagers.remove(resourceFile);
    Databases.removeResourceManager(resourceFile, manager);
    return manager != null;
}
Also used : ResourceManager(org.sirix.api.ResourceManager)

Aggregations

ResourceManager (org.sirix.api.ResourceManager)46 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)33 Database (org.sirix.api.Database)30 Path (java.nio.file.Path)16 Test (org.junit.Test)14 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)14 SirixException (org.sirix.exception.SirixException)12 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 XMLSerializerBuilder (org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder)8 IOException (java.io.IOException)5 ResourceConfiguration (org.sirix.access.conf.ResourceConfiguration)5 XMLEventReader (javax.xml.stream.XMLEventReader)4 DocumentException (org.brackit.xquery.xdm.DocumentException)4 OutputStream (java.io.OutputStream)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 QNm (org.brackit.xquery.atomic.QNm)3 DescendantAxis (org.sirix.axis.DescendantAxis)3 File (java.io.File)2 Files (java.nio.file.Files)2