From 53ac9390b78ca6ef7ef19bb25912e1feadd63665 Mon Sep 17 00:00:00 2001 From: Loredana Liccardo Date: Tue, 30 Sep 2014 12:32:27 +0000 Subject: [PATCH] sampling and smartSampling cached git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/databases-manager-portlet@100288 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../client/panels/GxtBorderLayoutPanel.java | 48 +++- .../server/GWTdbManagerServiceImpl.java | 250 ++++++++++++------ 2 files changed, 212 insertions(+), 86 deletions(-) diff --git a/src/main/java/org/gcube/portlets/user/databasesmanager/client/panels/GxtBorderLayoutPanel.java b/src/main/java/org/gcube/portlets/user/databasesmanager/client/panels/GxtBorderLayoutPanel.java index c627576..493785e 100644 --- a/src/main/java/org/gcube/portlets/user/databasesmanager/client/panels/GxtBorderLayoutPanel.java +++ b/src/main/java/org/gcube/portlets/user/databasesmanager/client/panels/GxtBorderLayoutPanel.java @@ -1324,12 +1324,12 @@ public class GxtBorderLayoutPanel extends ContentPanel { } @Override - public void onSuccess(SamplingResultWithFileFromServlet obj) { + public void onSuccess(SamplingResultWithFileFromServlet samplingResult) { rootLogger.log(Level.SEVERE, "SUCCESS RPC sample"); - if (obj != null) { + if (samplingResult != null) { // get data - List result = obj.getListOutput(); + List result = samplingResult.getListOutput(); // get the attributes list for the result // table List listAttributes = new ArrayList(); @@ -1342,11 +1342,19 @@ public class GxtBorderLayoutPanel extends ContentPanel { + result.size()); // get path - String fileName = obj.getFileName(); + String fileName = samplingResult.getFileName(); // parse the result in order to obtain a // table parseResult(result, listAttributes, fileName); + } else{ + MessageBox.alert("Error ", + "
Message: " + "no data available", null); + + if (obj.isMasked()) { + obj.unmask(); + } + } } @@ -1468,12 +1476,12 @@ public class GxtBorderLayoutPanel extends ContentPanel { } @Override - public void onSuccess(SamplingResultWithFileFromServlet obj) { + public void onSuccess(SamplingResultWithFileFromServlet samplingResult) { rootLogger.log(Level.SEVERE, "SUCCESS RPC smartSample"); - if (obj != null) { + if (samplingResult != null) { // get data - List result = obj.getListOutput(); + List result = samplingResult.getListOutput(); // get the attributes list for the result // table List listAttributes = new ArrayList(); @@ -1485,10 +1493,18 @@ public class GxtBorderLayoutPanel extends ContentPanel { rootLogger.log(Level.SEVERE, "output size: " + result.size()); // get path - String fileName = obj.getFileName(); + String fileName = samplingResult.getFileName(); // parse the result in order to obtain a // table parseResult(result, listAttributes, fileName); + }else{ + MessageBox.alert("Error ", + "
Message: " + "no data available", null); + + if (obj.isMasked()) { + obj.unmask(); + } + } } }); @@ -1578,13 +1594,13 @@ public class GxtBorderLayoutPanel extends ContentPanel { } @Override - public void onSuccess(SamplingResultWithFileFromServlet obj) { + public void onSuccess(SamplingResultWithFileFromServlet samplingResult) { rootLogger .log(Level.SEVERE, "SUCCESS RPC randomSample"); - if (obj != null) { + if (samplingResult != null) { // get data - List result = obj.getListOutput(); + List result = samplingResult.getListOutput(); // get the attributes list for the result // table List listAttributes = new ArrayList(); @@ -1596,10 +1612,18 @@ public class GxtBorderLayoutPanel extends ContentPanel { rootLogger.log(Level.SEVERE, "output size: " + result.size()); // get path - String fileName = obj.getFileName(); + String fileName = samplingResult.getFileName(); // parse the result in order to obtain a // table parseResult(result, listAttributes, fileName); + } else{ + MessageBox.alert("Error ", + "
Message: " + "no data available", null); + + if (obj.isMasked()) { + obj.unmask(); + } + } } }); diff --git a/src/main/java/org/gcube/portlets/user/databasesmanager/server/GWTdbManagerServiceImpl.java b/src/main/java/org/gcube/portlets/user/databasesmanager/server/GWTdbManagerServiceImpl.java index 4d72306..94c4f9b 100644 --- a/src/main/java/org/gcube/portlets/user/databasesmanager/server/GWTdbManagerServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/databasesmanager/server/GWTdbManagerServiceImpl.java @@ -189,8 +189,11 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements } catch (Exception e) { // TODO: handle exception -// logger.error("dbmanager-> Error while starting the servlet. Failed to create the cache", e); - throw new Exception("Error while starting the servlet. Failed to create the cache. Exception: " +e); + // logger.error("dbmanager-> Error while starting the servlet. Failed to create the cache", + // e); + throw new Exception( + "Error while starting the servlet. Failed to create the cache. Exception: " + + e); } } @@ -721,7 +724,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements } catch (Exception e) { // e.printStackTrace(); // throw new Exception("Failed to load data. " + e); -// logger.error("dbmanager-> ", e); + // logger.error("dbmanager-> ", e); if (!(e instanceof StatisticalManagerException)) { // GWT can't serialize all exceptions @@ -904,6 +907,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements // data output // List output = new ArrayList(); List output = null; + SubmitQueryResultWithFileFromServlet result; // list that contains table attributes List listAttributes = null; @@ -960,6 +964,31 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements inputParameters.get(4).setValue(language); inputParameters.get(5).setValue(query); + // get data from cache + // check if data exist considering as key the input parameters + + // parse the query in order to remove spaces + String queryParsed = parseQuery(inputParameters.get(5).getValue()); + + String key = inputParameters.get(0).getValue() + + inputParameters.get(1).getValue() + + inputParameters.get(2).getValue() + + inputParameters.get(3).getValue() + + inputParameters.get(4).getValue() + + queryParsed; + + net.sf.ehcache.Element dataFromCache = getDataFromCache(key); + + Object value = null; + if (dataFromCache != null) { + value = dataFromCache.getObjectValue(); + // System.out.println("***GETTING DATA FROM CACHE"); + } + if (value != null) { + result = (SubmitQueryResultWithFileFromServlet) value; + + } else { + // create data structure ComputationOutput outputData = new ComputationOutput(); @@ -1027,10 +1056,17 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements String partialPathFile = applicationPath + "/computationResult/" + fileName; - SubmitQueryResultWithFileFromServlet obj = new SubmitQueryResultWithFileFromServlet( + result = new SubmitQueryResultWithFileFromServlet( listAttributes, convertedQuery, partialPathFile); + + // put data in cache + net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( + key, result); + insertDataIntoCache(dataToCache); + + } - return obj; + return result; } catch (Exception e) { // e.printStackTrace(); @@ -1068,6 +1104,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements List inputParameters = new ArrayList(); // output sample result List output = new ArrayList(); + SamplingResultWithFileFromServlet result; String algorithmId = "SAMPLEONTABLE"; @@ -1126,41 +1163,70 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements inputParameters.get(2).setValue(scm); inputParameters.get(3).setValue(tab); - // create data structure - ComputationOutput outputData = new ComputationOutput(); - // computation id - String computationId = startComputation(algorithmId, - inputParameters, outputData); + // get data from cache + // check if data exist considering as key the input parameters + String key = algorithmId + inputParameters.get(0).getValue() + + inputParameters.get(1).getValue() + + inputParameters.get(2).getValue() + + inputParameters.get(3).getValue(); - // print check on retrieving data - // logger.info("output data retrieved"); + net.sf.ehcache.Element dataFromCache = getDataFromCache(key); - // data output values - LinkedHashMap mapValues = new LinkedHashMap(); - // data output keys - LinkedHashMap mapKeys = new LinkedHashMap(); + Object value = null; + if (dataFromCache != null) { + value = dataFromCache.getObjectValue(); + // System.out.println("***GETTING DATA FROM CACHE"); + } + if (value != null) { + result = (SamplingResultWithFileFromServlet) value; - mapValues = outputData.getMapValues(); - mapKeys = outputData.getmapKeys(); + } else { + + // start computation + + // create data structure + ComputationOutput outputData = new ComputationOutput(); + // computation id + String computationId = startComputation(algorithmId, + inputParameters, outputData); + + // print check on retrieving data + // logger.info("output data retrieved"); + + // data output values + LinkedHashMap mapValues = new LinkedHashMap(); + // data output keys + LinkedHashMap mapKeys = new LinkedHashMap(); + + mapValues = outputData.getMapValues(); + mapKeys = outputData.getmapKeys(); + + for (int i = 0; i < mapValues.size(); i++) { + Result row = new Result(mapKeys.get(String.valueOf(i)), + mapValues.get(String.valueOf(i))); + output.add(row); + } + + String name = "Sampling" + "_" + tab; + String fileName = storeResultIntoCSVFile(output, name); + + HttpServletRequest request = this.getThreadLocalRequest(); + String applicationPath = request.getContextPath(); + // logger.info("dbmanager-> Application Path: " + + // applicationPath); + String partialPathFile = applicationPath + + "/computationResult/" + fileName; + result = new SamplingResultWithFileFromServlet(output, + partialPathFile); + + // put data in cache + net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( + key, result); + insertDataIntoCache(dataToCache); - for (int i = 0; i < mapValues.size(); i++) { - Result row = new Result(mapKeys.get(String.valueOf(i)), - mapValues.get(String.valueOf(i))); - output.add(row); } - String name = "Sampling" + "_" + tab; - String fileName = storeResultIntoCSVFile(output, name); - - HttpServletRequest request = this.getThreadLocalRequest(); - String applicationPath = request.getContextPath(); - // logger.info("dbmanager-> Application Path: " + applicationPath); - String partialPathFile = applicationPath + "/computationResult/" - + fileName; - SamplingResultWithFileFromServlet obj = new SamplingResultWithFileFromServlet( - output, partialPathFile); - - return obj; + return result; } catch (Exception e) { logger.error("dbmanager-> ", e); @@ -1187,6 +1253,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements List inputParameters = new ArrayList(); // output sample result List output = new ArrayList(); + SamplingResultWithFileFromServlet result; String algorithmId = "SMARTSAMPLEONTABLE"; @@ -1245,41 +1312,68 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements inputParameters.get(2).setValue(scm); inputParameters.get(3).setValue(tab); - // create data structure - ComputationOutput outputData = new ComputationOutput(); - // computation id - String computationId = startComputation(algorithmId, - inputParameters, outputData); + // get data from cache + // check if data exist considering as key the input parameters + String key = algorithmId + inputParameters.get(0).getValue() + + inputParameters.get(1).getValue() + + inputParameters.get(2).getValue() + + inputParameters.get(3).getValue(); - // print check on retrieving data - // logger.info("dbmanager-> output data retrieved"); + net.sf.ehcache.Element dataFromCache = getDataFromCache(key); - // data output values - LinkedHashMap mapValues = new LinkedHashMap(); - // data output keys - LinkedHashMap mapKeys = new LinkedHashMap(); + Object value = null; + if (dataFromCache != null) { + value = dataFromCache.getObjectValue(); + // System.out.println("***GETTING DATA FROM CACHE"); + } + if (value != null) { + result = (SamplingResultWithFileFromServlet) value; - mapValues = outputData.getMapValues(); - mapKeys = outputData.getmapKeys(); + } else { + + // create data structure + ComputationOutput outputData = new ComputationOutput(); + // computation id + String computationId = startComputation(algorithmId, + inputParameters, outputData); + + // print check on retrieving data + // logger.info("dbmanager-> output data retrieved"); + + // data output values + LinkedHashMap mapValues = new LinkedHashMap(); + // data output keys + LinkedHashMap mapKeys = new LinkedHashMap(); + + mapValues = outputData.getMapValues(); + mapKeys = outputData.getmapKeys(); + + for (int i = 0; i < mapValues.size(); i++) { + Result row = new Result(mapKeys.get(String.valueOf(i)), + mapValues.get(String.valueOf(i))); + output.add(row); + } + + String name = "SmartSampling" + "_" + tab; + String fileName = storeResultIntoCSVFile(output, name); + + HttpServletRequest request = this.getThreadLocalRequest(); + String applicationPath = request.getContextPath(); + // logger.info("dbmanager-> Application Path: " + + // applicationPath); + String partialPathFile = applicationPath + + "/computationResult/" + fileName; + result = new SamplingResultWithFileFromServlet(output, + partialPathFile); + + // put data in cache + net.sf.ehcache.Element dataToCache = new net.sf.ehcache.Element( + key, result); + insertDataIntoCache(dataToCache); - for (int i = 0; i < mapValues.size(); i++) { - Result row = new Result(mapKeys.get(String.valueOf(i)), - mapValues.get(String.valueOf(i))); - output.add(row); } - String name = "SmartSampling" + "_" + tab; - String fileName = storeResultIntoCSVFile(output, name); - - HttpServletRequest request = this.getThreadLocalRequest(); - String applicationPath = request.getContextPath(); - // logger.info("dbmanager-> Application Path: " + applicationPath); - String partialPathFile = applicationPath + "/computationResult/" - + fileName; - SamplingResultWithFileFromServlet obj = new SamplingResultWithFileFromServlet( - output, partialPathFile); - - return obj; + return result; } catch (Exception e) { logger.error("dbmanager-> ", e); @@ -1915,7 +2009,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements } catch (Exception e) { logger.info("dbmanager-> startComputation: the job submit has failed!"); // e.printStackTrace(); -// logger.error("dbmanager-> ", e); + // logger.error("dbmanager-> ", e); throw e; } } @@ -1994,7 +2088,7 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements StatisticalManagerException e = new StatisticalManagerException( smResource.description()); // e.printStackTrace(); -// logger.error("dbmanager-> ", e); + // logger.error("dbmanager-> ", e); throw e; } } @@ -2289,15 +2383,15 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements } private synchronized void refreshCache(String key) throws Exception { -// try { - employeeCache.remove(key); -// } catch (Exception e) { - // TODO: handle exception -// logger.error("dbmanager-> ", e); - // throw new Exception("Failure to clear the cache. " + e); -// throw new Exception( -// "Error in server while loading data. Exception: " + e); -// } + // try { + employeeCache.remove(key); + // } catch (Exception e) { + // TODO: handle exception + // logger.error("dbmanager-> ", e); + // throw new Exception("Failure to clear the cache. " + e); + // throw new Exception( + // "Error in server while loading data. Exception: " + e); + // } } private String storeResultIntoCSVFile(List result, String n) @@ -2348,4 +2442,12 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements return SessionUtil.isSessionExpired(this.getThreadLocalRequest() .getSession()); } + + private String parseQuery(String query) { + String queryParsed; + + queryParsed = query.trim(); + queryParsed = queryParsed.replaceAll(" +", " "); + return queryParsed; + } }