Skip to content

Commit

Permalink
Added Manager for DataUnits, it also close DataUnits and Repositories…
Browse files Browse the repository at this point in the history
… after certain time.

WARN: Time limits may broke some operations - download
  • Loading branch information
skodapetr committed Jan 4, 2015
1 parent 23ddf01 commit 2be7174
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ public class RDFQuery implements Query {

private ArrayList<Item> cachedItems;

private final ManageableWritableRDFDataUnit repository;

/**
* Constructor.
*
Expand All @@ -66,7 +64,6 @@ public RDFQuery(RDFQueryDefinition queryDefinition) {
this.baseQuery = queryDefinition.getBaseQuery();
this.batchSize = queryDefinition.getBatchSize();
this.queryDefinition = queryDefinition;
this.repository = RepositoryFrontendHelper.getRepository(queryDefinition.getInfo(), queryDefinition.getDpu(), queryDefinition.getDataUnit());
}

/**
Expand All @@ -76,7 +73,7 @@ public RDFQuery(RDFQueryDefinition queryDefinition) {
*/
@Override
public int size() {

ManageableWritableRDFDataUnit repository = RepositoryFrontendHelper.getRepository(queryDefinition.getInfo(), queryDefinition.getDpu(), queryDefinition.getDataUnit());
if (repository == null) {
throw new RuntimeException("Unable to load RDFDataUnit.");
}
Expand Down Expand Up @@ -127,6 +124,7 @@ public List<Item> loadItems(int startIndex, int count) {
return cachedItems.subList(startIndex, startIndex + count);
}

ManageableWritableRDFDataUnit repository = RepositoryFrontendHelper.getRepository(queryDefinition.getInfo(), queryDefinition.getDpu(), queryDefinition.getDataUnit());
if (repository == null) {
LOG.debug("Unable to load RDFDataUnit.");
throw new RuntimeException("Unable to load RDFDataUnit.");
Expand All @@ -143,12 +141,11 @@ public List<Item> loadItems(int startIndex, int count) {
restriction.setOffset(offset * batchSize);
}
String query = restriction.getRestrictedQuery();
SPARQLQueryType type;
Object data = null;
RepositoryConnection connection = null;
try {
type = getQueryType(query);
Graph graph = null;
SPARQLQueryType type = getQueryType(query);
Object data;
Graph graph;
connection = repository.getConnection();
switch (type) {
case SELECT:
Expand Down Expand Up @@ -223,18 +220,9 @@ public List<Item> loadItems(int startIndex, int count) {
try {
connection.close();
} catch (RepositoryException ex) {
Notification.show("Connection problem",
"Could close connection to frontend repository: "
+ ex.getCause().getMessage(),
Notification.Type.ERROR_MESSAGE);
LOG.warn("Can't close connection.", ex);
}
}
// close reporistory
try {
repository.release();
} catch (DataUnitException ex) {
LOG.error("Faild to release repository.", ex);
}
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,11 @@
import com.vaadin.data.Container;
import com.vaadin.ui.UI;

import cz.cuni.mff.xrg.odcs.commons.app.dataunit.DataUnitFactory;
import cz.cuni.mff.xrg.odcs.commons.app.dpu.DPUInstanceRecord;
import cz.cuni.mff.xrg.odcs.commons.app.execution.context.DataUnitInfo;
import cz.cuni.mff.xrg.odcs.commons.app.execution.context.DpuContextInfo;
import cz.cuni.mff.xrg.odcs.commons.app.execution.context.ExecutionInfo;
import cz.cuni.mff.xrg.odcs.commons.app.resource.MissingResourceException;
import cz.cuni.mff.xrg.odcs.commons.app.resource.ResourceManager;
import cz.cuni.mff.xrg.odcs.frontend.AppEntry;
import cz.cuni.mff.xrg.odcs.frontend.dataunit.FrontendDataUnitManager;
import cz.cuni.mff.xrg.odcs.rdf.enums.RDFFormatType;
import cz.cuni.mff.xrg.odcs.rdf.enums.SelectFormatType;
import cz.cuni.mff.xrg.odcs.rdf.exceptions.InvalidQueryException;
Expand All @@ -64,8 +61,6 @@
import cz.cuni.mff.xrg.odcs.rdf.query.utils.RegexFilter;
import cz.cuni.mff.xrg.odcs.rdf.repositories.GraphUrl;
import cz.cuni.mff.xrg.odcs.rdf.repositories.MyRDFHandler;
import eu.unifiedviews.commons.dataunit.ManagableDataUnit;
import eu.unifiedviews.dataunit.DataUnitException;
import eu.unifiedviews.dataunit.rdf.impl.ManageableWritableRDFDataUnit;
import eu.unifiedviews.helpers.dataunit.dataset.DatasetBuilder;

Expand All @@ -85,51 +80,22 @@ public class RepositoryFrontendHelper {
*/
public static ManageableWritableRDFDataUnit getRepository(ExecutionInfo executionInfo,
DPUInstanceRecord dpuInstance, DataUnitInfo dataUnitInfo) {
final FrontendDataUnitManager dataUnitManager = ((AppEntry) UI.getCurrent()).getBean(FrontendDataUnitManager.class);
return dataUnitManager.getRDFDataUnit(executionInfo, dpuInstance, dataUnitInfo, FrontendDataUnitManager.CLOSE_TIME_SHORT);
}

// TODO Petr: We need to close the repository somewhere!

// get type and directory
if (dataUnitInfo == null) {
// the context doesn't exist
return null;
}

//
if (executionInfo == null) {
log.error("executionInfo is null!");
return null;
}

DpuContextInfo dpuInfo = executionInfo.dpu(dpuInstance);
if (dpuInfo == null) {
log.error("DPU info is null!");
return null;
}

// Get DataUnit factory.
final DataUnitFactory dataUnitFactory = ((AppEntry) UI.getCurrent()).getBean(DataUnitFactory.class);
final ResourceManager resourceManager = ((AppEntry) UI.getCurrent()).getBean(ResourceManager.class);
switch (dataUnitInfo.getType()) {
case RDF:
try {
final String dataUnitId = executionInfo.getExecutionContext().generateDataUnitId(dpuInstance, dataUnitInfo.getIndex());
final File directory = resourceManager.getDataUnitWorkingDir(executionInfo.getExecutionContext().getExecution(), dpuInstance, dataUnitInfo.getIndex());
// Get ManagableDataUnit ~ ManagableRdfDataUnit.
final ManagableDataUnit dataUnit = dataUnitFactory.create(ManagableDataUnit.Type.RDF,
executionInfo.getExecutionContext().getExecutionId(),
GraphUrl.translateDataUnitId(dataUnitId),
dataUnitInfo.getName(),
directory);
// Load data.
dataUnit.load();
// Return ready DataUnit.
return (ManageableWritableRDFDataUnit)dataUnit;
} catch (DataUnitException | eu.unifiedviews.commons.rdf.repository.RDFException | MissingResourceException ex) {
throw new RuntimeException("Failed to load data unit.", ex);
}
default: // No support for other DataUnits.
return null;
}
/**
* Longer time before close then getRepository.
*
* @param executionInfo
* @param dpuInstance
* @param dataUnitInfo
* @return
*/
public static ManageableWritableRDFDataUnit getRepositoryForDownload(ExecutionInfo executionInfo,
DPUInstanceRecord dpuInstance, DataUnitInfo dataUnitInfo) {
final FrontendDataUnitManager dataUnitManager = ((AppEntry) UI.getCurrent()).getBean(FrontendDataUnitManager.class);
return dataUnitManager.getRDFDataUnit(executionInfo, dpuInstance, dataUnitInfo, FrontendDataUnitManager.CLOSE_TIME_LONG);
}

/**
Expand Down
Loading

0 comments on commit 2be7174

Please sign in to comment.