Search in sources :

Example 1 with XdmNodeWriteTrx

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

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

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

the class PCRCollectorImpl method getPCRsForPaths.

@Override
public PCRValue getPCRsForPaths(Set<Path<QNm>> paths) {
    try (final PathSummaryReader reader = mRtx instanceof XdmNodeWriteTrx ? ((XdmNodeWriteTrx) mRtx).getPathSummary() : mRtx.getResourceManager().openPathSummary(mRtx.getRevisionNumber())) {
        final long maxPCR = reader.getMaxNodeKey();
        final Set<Long> pathClassRecords = reader.getPCRsForPaths(paths);
        return PCRValue.getInstance(maxPCR, pathClassRecords);
    } catch (final PathException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return PCRValue.getEmptyInstance();
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) PathException(org.brackit.xquery.util.path.PathException) PathSummaryReader(org.sirix.index.path.summary.PathSummaryReader)

Example 4 with XdmNodeWriteTrx

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

the class DBCollection method getDocumentInternal.

private DBNode getDocumentInternal(final ResourceManagerConfiguration resourceManagerConfig, final int revision, final boolean updatable) throws SirixException {
    final ResourceManager resource = mDatabase.getResourceManager(resourceManagerConfig);
    final int version = revision == -1 ? resource.getMostRecentRevisionNumber() : revision;
    final XdmNodeReadTrx trx;
    if (updatable) {
        if (resource.getAvailableNodeWriteTrx() == 0) {
            final Optional<XdmNodeWriteTrx> optionalWriteTrx;
            optionalWriteTrx = resource.getXdmNodeWriteTrx();
            if (optionalWriteTrx.isPresent()) {
                trx = optionalWriteTrx.get();
            } else {
                trx = resource.beginNodeWriteTrx();
            }
        } else {
            trx = resource.beginNodeWriteTrx();
        }
        if (version < resource.getMostRecentRevisionNumber())
            ((XdmNodeWriteTrx) trx).revertTo(version);
    } else {
        trx = resource.beginNodeReadTrx(version);
    }
    return new DBNode(trx, this);
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) ResourceManager(org.sirix.api.ResourceManager)

Example 5 with XdmNodeWriteTrx

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

the class DBNode method append.

@Override
public DBNode append(Kind kind, QNm name, Atomic value) throws DocumentException {
    if (mIsWtx) {
        moveRtx();
        final XdmNodeWriteTrx wtx = (XdmNodeWriteTrx) mRtx;
        try {
            return append(wtx, kind, name, value);
        } catch (final SirixException e) {
            wtx.close();
            throw new DocumentException(e);
        }
    } else {
        final XdmNodeWriteTrx wtx = getWtx();
        try {
            return append(wtx, kind, name, value);
        } catch (final SirixException e) {
            wtx.close();
            throw new DocumentException(e);
        }
    }
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) DocumentException(org.brackit.xquery.xdm.DocumentException) SirixException(org.sirix.exception.SirixException)

Aggregations

XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)101 Test (org.junit.Test)61 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)48 ResourceManager (org.sirix.api.ResourceManager)33 Database (org.sirix.api.Database)22 QNm (org.brackit.xquery.atomic.QNm)15 SirixException (org.sirix.exception.SirixException)15 Path (java.nio.file.Path)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 DocumentException (org.brackit.xquery.xdm.DocumentException)7 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)7 DescendantAxis (org.sirix.axis.DescendantAxis)7 XMLSerializerBuilder (org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder)7 IOException (java.io.IOException)5 NonStructuralWrapperAxis (org.sirix.axis.NonStructuralWrapperAxis)5 SirixIOException (org.sirix.exception.SirixIOException)5 SirixDeweyID (org.sirix.node.SirixDeweyID)5 DBNode (org.sirix.xquery.node.DBNode)5 XMLEventReader (javax.xml.stream.XMLEventReader)4 QueryException (org.brackit.xquery.QueryException)4