Search in sources :

Example 1 with ResourceConfiguration

use of org.sirix.access.conf.ResourceConfiguration in project sirix by sirixdb.

the class DatabaseRepresentation method shred.

/**
 * This method is responsible to save the XML file, which is in an
 * {@link InputStream}, as a sirix object.
 *
 * @param xmlInput
 *          XML file wrapped in an {@link InputStream}
 * @param resource
 *          name of the resource
 * @return {@code true} if shredding process has been successful,
 *         {@code false} otherwise.
 * @throws SirixException
 *           if any sirix related exception occurs
 */
public final boolean shred(final InputStream xmlInput, final String resource) throws SirixException {
    boolean allOk = false;
    NodeWriteTrx wtx = null;
    Database database = null;
    Session session = null;
    boolean abort = false;
    try {
        final DatabaseConfiguration dbConf = new DatabaseConfiguration(mStoragePath);
        Databases.createDatabase(dbConf);
        database = Databases.openDatabase(dbConf.getFile());
        // Shredding the database to the file as XML
        final ResourceConfiguration resConf = new ResourceConfiguration.Builder(resource, dbConf).revisionsToRestore(1).build();
        if (database.createResource(resConf)) {
            session = database.getSession(new SessionConfiguration.Builder(resource).build());
            wtx = session.beginNodeWriteTrx();
            final XMLShredder shredder = new XMLShredder.Builder(wtx, RESTXMLShredder.createReader(xmlInput), Insert.ASFIRSTCHILD).commitAfterwards().build();
            shredder.call();
            allOk = true;
        }
    } catch (final Exception exce) {
        abort = true;
        throw new JaxRxException(exce);
    } finally {
        WorkerHelper.closeWTX(abort, wtx, session, database);
    }
    return allOk;
}
Also used : DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) NodeWriteTrx(org.sirix.api.NodeWriteTrx) Database(org.sirix.api.Database) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) RESTXMLShredder(org.sirix.service.jaxrx.util.RESTXMLShredder) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) JaxRxException(org.jaxrx.core.JaxRxException) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Session(org.sirix.api.Session) ResourceConfiguration(org.sirix.access.conf.ResourceConfiguration) JaxRxException(org.jaxrx.core.JaxRxException)

Example 2 with ResourceConfiguration

use of org.sirix.access.conf.ResourceConfiguration in project sirix by sirixdb.

the class DatabaseImpl method createResource.

// //////////////////////////////////////////////////////////
// START Creation/Deletion of Resources /////////////////////
// //////////////////////////////////////////////////////////
@Override
public synchronized boolean createResource(final ResourceConfiguration resConfig) {
    boolean returnVal = true;
    final Path path = mDBConfig.getFile().resolve(DatabaseConfiguration.DatabasePaths.DATA.getFile()).resolve(resConfig.mPath);
    // If file is existing, skip.
    if (Files.exists(path)) {
        return false;
    } else {
        try {
            Files.createDirectory(path);
        } catch (UnsupportedOperationException | IOException | SecurityException e) {
            returnVal = false;
        }
        if (returnVal) {
            // Creation of the folder structure.
            try {
                for (final ResourceConfiguration.ResourcePaths resourcePath : ResourceConfiguration.ResourcePaths.values()) {
                    final Path toCreate = path.resolve(resourcePath.getFile());
                    if (resourcePath.isFolder()) {
                        Files.createDirectory(toCreate);
                    } else {
                        returnVal = ResourceConfiguration.ResourcePaths.INDEXES.getFile().equals(resourcePath.getFile()) ? true : Files.createFile(toCreate) != null;
                    }
                    if (!returnVal)
                        break;
                }
            } catch (UnsupportedOperationException | IOException | SecurityException e) {
                returnVal = false;
            }
        }
    }
    if (returnVal) {
        // If everything was correct so far, initialize storage.
        // Serialization of the config.
        mResourceID.set(mDBConfig.getMaxResourceID());
        ResourceConfiguration.serialize(resConfig.setID(mResourceID.getAndIncrement()));
        mDBConfig.setMaximumResourceID(mResourceID.get());
        mResources.forcePut(mResourceID.get(), resConfig.getResource().getFileName().toString());
        try {
            try (final ResourceManager resourceTrxManager = this.getResourceManager(new ResourceManagerConfiguration.Builder(resConfig.getResource().getFileName().toString()).build());
                final XdmNodeWriteTrx wtx = resourceTrxManager.beginNodeWriteTrx()) {
                wtx.commit();
            }
        } catch (final SirixException e) {
            LOGWRAPPER.error(e.getMessage(), e);
            returnVal = false;
        }
    }
    if (!returnVal) {
        // If something was not correct, delete the partly created substructure.
        SirixFiles.recursiveRemove(resConfig.mPath);
    }
    return returnVal;
}
Also used : Path(java.nio.file.Path) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) SirixException(org.sirix.exception.SirixException) SirixIOException(org.sirix.exception.SirixIOException) IOException(java.io.IOException) ResourceManager(org.sirix.api.ResourceManager) ResourceConfiguration(org.sirix.access.conf.ResourceConfiguration)

Example 3 with ResourceConfiguration

use of org.sirix.access.conf.ResourceConfiguration in project sirix by sirixdb.

the class DatabaseImpl method getResourceManager.

// //////////////////////////////////////////////////////////
// END resource name <=> ID handling ////////////////////////
// //////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////
// START DB-Operations //////////////////////////////////////
// //////////////////////////////////////////////////////////
@Override
public synchronized ResourceManager getResourceManager(final ResourceManagerConfiguration resourceManagerConfig) throws SirixException {
    final Path resourceFile = mDBConfig.getFile().resolve(DatabaseConfiguration.DatabasePaths.DATA.getFile()).resolve(resourceManagerConfig.getResource());
    if (!Files.exists(resourceFile)) {
        throw new SirixUsageException("Resource could not be opened (since it was not created?) at location", resourceFile.toString());
    }
    if (mResourceStore.hasOpenResourceManager(resourceFile))
        return mResourceStore.getOpenResourceManager(resourceFile);
    final ResourceConfiguration resourceConfig = ResourceConfiguration.deserialize(resourceFile);
    // Resource of must be associated to this database.
    assert resourceConfig.mPath.getParent().getParent().equals(mDBConfig.getFile());
    if (!mBufferManagers.containsKey(resourceFile))
        mBufferManagers.put(resourceFile, new BufferManagerImpl());
    final ResourceManager resourceManager = mResourceStore.openResource(this, resourceConfig, resourceManagerConfig, mBufferManagers.get(resourceFile), resourceFile);
    return resourceManager;
}
Also used : Path(java.nio.file.Path) BufferManagerImpl(org.sirix.cache.BufferManagerImpl) ResourceManager(org.sirix.api.ResourceManager) SirixUsageException(org.sirix.exception.SirixUsageException) ResourceConfiguration(org.sirix.access.conf.ResourceConfiguration)

Example 4 with ResourceConfiguration

use of org.sirix.access.conf.ResourceConfiguration in project sirix by sirixdb.

the class PageReadTrxImpl method getSnapshotPages.

/**
 * Dereference key/value page reference and get all leaves, the {@link KeyValuePage}s from the
 * revision-trees.
 *
 * @param recordPageKey key of node page
 * @param pageKind kind of page, that is the type of tree to dereference
 * @param index index number or {@code -1}, if it's a regular record page
 * @param pageReference optional page reference pointing to the first page
 * @return dereferenced pages
 *
 * @throws SirixIOException if an I/O-error occurs within the creation process
 */
final <K extends Comparable<? super K>, V extends Record, T extends KeyValuePage<K, V>> List<T> getSnapshotPages(final PageReference pageReference) {
    assert pageReference != null;
    final ResourceConfiguration config = mResourceManager.getResourceConfig();
    final int revsToRestore = config.mRevisionsToRestore;
    final int[] revisionsToRead = config.mRevisionKind.getRevisionRoots(mRootPage.getRevision(), revsToRestore);
    final List<T> pages = new ArrayList<>(revisionsToRead.length);
    boolean first = true;
    for (int i = 0; i < revisionsToRead.length; i++) {
        long refKeyToRecordPage = Constants.NULL_ID_LONG;
        if (first) {
            first = false;
            refKeyToRecordPage = pageReference.getKey();
        } else {
            refKeyToRecordPage = pages.get(pages.size() - 1).getPreviousReferenceKey();
        }
        if (refKeyToRecordPage != Constants.NULL_ID_LONG) {
            final PageReference reference = new PageReference().setKey(refKeyToRecordPage);
            if (reference.getKey() != Constants.NULL_ID_LONG) {
                @SuppressWarnings("unchecked") final T page = (T) mPageReader.read(reference, this);
                pages.add(page);
                if (page.size() == Constants.NDP_NODE_COUNT) {
                    // versions.
                    break;
                }
            }
        } else {
            break;
        }
    }
    return pages;
}
Also used : PageReference(org.sirix.page.PageReference) ArrayList(java.util.ArrayList) ResourceConfiguration(org.sirix.access.conf.ResourceConfiguration)

Aggregations

ResourceConfiguration (org.sirix.access.conf.ResourceConfiguration)4 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 ResourceManager (org.sirix.api.ResourceManager)2 SirixException (org.sirix.exception.SirixException)2 ArrayList (java.util.ArrayList)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 JaxRxException (org.jaxrx.core.JaxRxException)1 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)1 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)1 Database (org.sirix.api.Database)1 NodeWriteTrx (org.sirix.api.NodeWriteTrx)1 Session (org.sirix.api.Session)1 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)1 BufferManagerImpl (org.sirix.cache.BufferManagerImpl)1 SirixIOException (org.sirix.exception.SirixIOException)1 SirixUsageException (org.sirix.exception.SirixUsageException)1 PageReference (org.sirix.page.PageReference)1 RESTXMLShredder (org.sirix.service.jaxrx.util.RESTXMLShredder)1 XMLSerializerBuilder (org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder)1