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].");
}
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;
}
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();
}
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);
}
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);
}
}
}
Aggregations