Search in sources :

Example 46 with ResourceManager

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

the class ResourceStore method openResource.

/**
 * Open a resource, that is get an instance of a {@link ResourceManager} in order to read/write
 * from the resource.
 *
 * @param database The database.
 * @param resourceConfig The resource configuration.
 * @param resourceManagerConfig The resource manager configuration.
 * @param bufferManager The buffer manager.
 * @param resourceFile The resource to open.
 * @return A resource manager.
 */
public ResourceManager openResource(@Nonnull final DatabaseImpl database, @Nonnull final ResourceConfiguration resourceConfig, @Nonnull final ResourceManagerConfiguration resourceManagerConfig, @Nonnull final BufferManager bufferManager, @Nonnull final Path resourceFile) {
    checkNotNull(database);
    checkNotNull(resourceConfig);
    return mResourceManagers.computeIfAbsent(resourceFile, k -> {
        final Storage storage = StorageType.getStorage(resourceConfig);
        final UberPage uberPage;
        if (storage.exists()) {
            try (final Reader reader = storage.createReader()) {
                final PageReference firstRef = reader.readUberPageReference();
                if (firstRef.getPage() == null) {
                    uberPage = (UberPage) reader.read(firstRef, null);
                } else {
                    uberPage = (UberPage) firstRef.getPage();
                }
            }
        } else {
            // Bootstrap uber page and make sure there already is a root node.
            uberPage = new UberPage();
        }
        final ResourceManager resourceManager = new XdmResourceManager(database, this, resourceConfig, resourceManagerConfig, bufferManager, StorageType.getStorage(resourceConfig), uberPage, mReadSemaphore, mWriteSempahore);
        Databases.putResourceManager(resourceFile, resourceManager);
        return resourceManager;
    });
}
Also used : PageReference(org.sirix.page.PageReference) Storage(org.sirix.io.Storage) UberPage(org.sirix.page.UberPage) Reader(org.sirix.io.Reader) 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