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;
}
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;
}
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;
}
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;
}
Aggregations