Search in sources :

Example 1 with ResourceManager

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

the class XMLUpdateShredder method main.

/**
 * Main method.
 *
 * @param args input and output files
 */
public static void main(final String[] args) {
    if (args.length != 2) {
        throw new IllegalArgumentException("Usage: XMLShredder input.xml output.tnk");
    }
    LOGWRAPPER.info("Shredding '" + args[0] + "' to '" + args[1] + "' ... ");
    final long time = System.currentTimeMillis();
    final Path target = Paths.get(args[1]);
    try {
        final DatabaseConfiguration config = new DatabaseConfiguration(target);
        Databases.createDatabase(config);
        final Database db = Databases.openDatabase(target);
        db.createResource(new ResourceConfiguration.Builder("shredded", config).build());
        final ResourceManager resMgr = db.getResourceManager(new ResourceManagerConfiguration.Builder("shredded").build());
        final XdmNodeWriteTrx wtx = resMgr.beginNodeWriteTrx();
        final XMLEventReader reader = XMLShredder.createFileReader(Paths.get(args[0]));
        final XMLUpdateShredder shredder = new XMLUpdateShredder(wtx, reader, Insert.ASFIRSTCHILD, new File(args[0]), ShredderCommit.COMMIT);
        shredder.call();
        wtx.close();
        resMgr.close();
    } catch (final SirixException | XMLStreamException | IOException e) {
        LOGWRAPPER.error(e.getMessage(), e);
    }
    LOGWRAPPER.info(" done [" + (System.currentTimeMillis() - time) + "ms].");
}
Also used : Path(java.nio.file.Path) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) ResourceManager(org.sirix.api.ResourceManager) SirixIOException(org.sirix.exception.SirixIOException) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) Database(org.sirix.api.Database) XMLEventReader(javax.xml.stream.XMLEventReader) SirixException(org.sirix.exception.SirixException) File(java.io.File)

Example 2 with ResourceManager

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

the class SAXSerializer method main.

/**
 * Main method.
 *
 * @param args args[0] specifies the path to the sirix storage from which to generate SAX events.
 * @throws SirixException if any Sirix exception occurs
 */
public static void main(final String... args) {
    final Path path = Paths.get(args[0]);
    final DatabaseConfiguration config = new DatabaseConfiguration(path);
    Databases.createDatabase(config);
    final Database database = Databases.openDatabase(path);
    database.createResource(new ResourceConfiguration.Builder("shredded", config).build());
    try (final ResourceManager session = database.getResourceManager(new ResourceManagerConfiguration.Builder("shredded").build())) {
        final DefaultHandler defHandler = new DefaultHandler();
        final SAXSerializer serializer = new SAXSerializer(session, defHandler, session.getMostRecentRevisionNumber());
        serializer.call();
    }
}
Also used : Path(java.nio.file.Path) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) Database(org.sirix.api.Database) ResourceManager(org.sirix.api.ResourceManager) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 3 with ResourceManager

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

the class XMLSerializer method main.

/**
 * Main method.
 *
 * @param args args[0] specifies the input-TT file/folder; args[1] specifies the output XML file.
 * @throws Exception any exception
 */
public static void main(final String... args) throws Exception {
    if (args.length < 2 || args.length > 3) {
        throw new IllegalArgumentException("Usage: XMLSerializer input-TT output.xml");
    }
    LOGWRAPPER.info("Serializing '" + args[0] + "' to '" + args[1] + "' ... ");
    final long time = System.nanoTime();
    final Path target = Paths.get(args[1]);
    SirixFiles.recursiveRemove(target);
    Files.createDirectories(target.getParent());
    Files.createFile(target);
    final Path databaseFile = Paths.get(args[0]);
    final DatabaseConfiguration config = new DatabaseConfiguration(databaseFile);
    Databases.createDatabase(config);
    try (final Database db = Databases.openDatabase(databaseFile)) {
        db.createResource(new ResourceConfiguration.Builder("shredded", config).build());
        final ResourceManager resMgr = db.getResourceManager(new ResourceManagerConfiguration.Builder("shredded").build());
        try (final FileOutputStream outputStream = new FileOutputStream(target.toFile())) {
            final XMLSerializer serializer = XMLSerializer.newBuilder(resMgr, outputStream).emitXMLDeclaration().build();
            serializer.call();
        }
    }
    LOGWRAPPER.info(" done [" + (System.nanoTime() - time) / 1_000_000 + "ms].");
}
Also used : Path(java.nio.file.Path) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) FileOutputStream(java.io.FileOutputStream) Database(org.sirix.api.Database) ResourceManager(org.sirix.api.ResourceManager)

Example 4 with ResourceManager

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

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

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