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 c9ed545..6f21ce5 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 @@ -22,6 +22,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; +import net.sf.ehcache.Status; import net.sf.ehcache.config.CacheConfiguration; import net.sf.ehcache.config.MemoryUnit; import net.sf.ehcache.config.SizeOfPolicyConfiguration; @@ -108,7 +109,9 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements String cachePath = System.getenv("CATALINA_TMPDIR") + "/DBManager"; logger.info("dbmanager-> Creating cache in folder: " + cachePath); - CacheManager cacheManager = CacheManager.create(); +// CacheManager cacheManager = CacheManager.create(); + + cacheManager = CacheManager.create(); if (cacheManager == null) { logger.error("dbmanager-> Error while starting the servlet. Failed to get the cacheManager. cacheManager null"); @@ -2068,18 +2071,44 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements } private synchronized void insertDataIntoCache(net.sf.ehcache.Element data) { - if (DBCache != null) { - DBCache.put(data); + logger.info("dbmanager-> disk store path for the cache: " + cacheManager.getDiskStorePath()); + + if(cacheManager.cacheExists("DBCache")){ + if (DBCache.getStatus().toString().equals(Status.STATUS_ALIVE.toString())){ + logger.info("dbmanager-> Cache Status:"+ "STATUS_ALIVE"); + + DBCache.put(data); +// logger.info("dbmanager-> element added in cache with key: " + data.getKey().toString()); + + }else{ + logger.info("dbmanager-> Cache Status:"+ DBCache.getStatus().toString()); + } } +// if (DBCache != null) { +// DBCache.put(data); +// } } private synchronized net.sf.ehcache.Element getDataFromCache(String key) { net.sf.ehcache.Element data = null; - if (DBCache != null) { - - data = DBCache.get(key); - + logger.info("dbmanager-> disk store path for the cache: " + cacheManager.getDiskStorePath()); + + if(cacheManager.cacheExists("DBCache")){ + if (DBCache.getStatus().toString().equals(Status.STATUS_ALIVE.toString())){ + logger.info("dbmanager-> Cache Status:"+ "STATUS_ALIVE"); + + data = DBCache.get(key); +// System.out.println("element get from cache with key " + key); + + }else{ + logger.info("dbmanager-> Cache Status:"+ DBCache.getStatus().toString()); + } } + +// if (DBCache != null) { +// data = DBCache.get(key); +// } + return data; } @@ -2468,9 +2497,23 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements } private synchronized void refreshCache(String key) throws Exception { + + logger.info("dbmanager-> disk store path for the cache: " + cacheManager.getDiskStorePath()); + + if(cacheManager.cacheExists("DBCache")){ + if (DBCache.getStatus().toString().equals(Status.STATUS_ALIVE.toString())){ + logger.info("dbmanager-> Cache Status:"+ Status.STATUS_ALIVE.toString()); + + DBCache.remove(key); +// logger.info("dbmanager-> Element with key: " +key+ " removed."); + }else{ + logger.info("dbmanager-> Cache Status:"+ DBCache.getStatus().toString()); + } + } + // try { // System.out.println("SIZE CACHE PRE REMOVING: " + DBCache.getSize()); - DBCache.remove(key); +// DBCache.remove(key); // System.out.println("SIZE CACHE POST REMOVING: " + DBCache.getSize()); // } catch (Exception e) { // TODO: handle exception @@ -2486,22 +2529,49 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements logger.info("dbmanager-> refreshing submitted queries in cache with key:" + key); + + logger.info("dbmanager-> disk store path for the cache: " + cacheManager.getDiskStorePath()); + + if(cacheManager.cacheExists("DBCache")){ + if (DBCache.getStatus().toString().equals(Status.STATUS_ALIVE.toString())){ + logger.info("dbmanager-> Cache Status:"+ "STATUS_ALIVE"); + + List keysInCache = DBCache.getKeys(); + int listSize = keysInCache.size(); + List keysToBeRemoved = new ArrayList<>(); - List keysInCache = DBCache.getKeys(); - int listSize = keysInCache.size(); - List keysToBeRemoved = new ArrayList<>(); - - // recover keys list that match the key - for (int i = 0; i < listSize; i++) { - if (keysInCache.get(i).startsWith(key)) { - // System.out.println("data removed with key: " + - // keysInCache.get(i)); - keysToBeRemoved.add(keysInCache.get(i)); + // recover keys list that match the key + for (int i = 0; i < listSize; i++) { + if (keysInCache.get(i).startsWith(key)) { + // System.out.println("data removed with key: " + + // keysInCache.get(i)); + keysToBeRemoved.add(keysInCache.get(i)); + } + } + // remove keys + DBCache.removeAll(keysToBeRemoved); + // System.out.println("SubmittedQuery refreshed with key: " + key); + + }else{ + logger.info("dbmanager-> Cache Status:"+ DBCache.getStatus().toString()); } } - // remove keys - DBCache.removeAll(keysToBeRemoved); - // System.out.println("SubmittedQuery refreshed with key: " + key); + +// List keysInCache = DBCache.getKeys(); +// int listSize = keysInCache.size(); +// List keysToBeRemoved = new ArrayList<>(); +// +// // recover keys list that match the key +// for (int i = 0; i < listSize; i++) { +// if (keysInCache.get(i).startsWith(key)) { +// // System.out.println("data removed with key: " + +// // keysInCache.get(i)); +// keysToBeRemoved.add(keysInCache.get(i)); +// } +// } +// // remove keys +// DBCache.removeAll(keysToBeRemoved); +// // System.out.println("SubmittedQuery refreshed with key: " + key); } private synchronized void refreshSamplingsInCache( @@ -2512,24 +2582,53 @@ public class GWTdbManagerServiceImpl extends RemoteServiceServlet implements + keyUsedForSamplingsRefresh + " " + keyUsedForSmartSamplingRefresh); + + logger.info("dbmanager-> disk store path for the cache: " + cacheManager.getDiskStorePath()); + - List keysInCache = DBCache.getKeys(); - int listSize = keysInCache.size(); - List keysToBeRemoved = new ArrayList<>(); + if(cacheManager.cacheExists("DBCache")){ + if (DBCache.getStatus().toString().equals(Status.STATUS_ALIVE.toString())){ + logger.info("dbmanager-> Cache Status:"+ "STATUS_ALIVE"); + + List keysInCache = DBCache.getKeys(); + int listSize = keysInCache.size(); + List keysToBeRemoved = new ArrayList<>(); - // recover keys list that match the key - for (int i = 0; i < listSize; i++) { - if ((keysInCache.get(i).startsWith(keyUsedForSamplingsRefresh)) - || (keysInCache.get(i) - .startsWith(keyUsedForSmartSamplingRefresh))) { - keysToBeRemoved.add(keysInCache.get(i)); + // recover keys list that match the key + for (int i = 0; i < listSize; i++) { + if ((keysInCache.get(i).startsWith(keyUsedForSamplingsRefresh)) + || (keysInCache.get(i) + .startsWith(keyUsedForSmartSamplingRefresh))) { + keysToBeRemoved.add(keysInCache.get(i)); + } + } + // remove keys + DBCache.removeAll(keysToBeRemoved); + // System.out.println("Samplings refreshed with keys: " + // + keyUsedForSamplingsRefresh + " " + // + keyUsedForSmartSamplingRefresh); + }else{ + logger.info("dbmanager-> Cache Status:"+ DBCache.getStatus().toString()); } - } - // remove keys - DBCache.removeAll(keysToBeRemoved); - // System.out.println("Samplings refreshed with keys: " - // + keyUsedForSamplingsRefresh + " " - // + keyUsedForSmartSamplingRefresh); + } + +// List keysInCache = DBCache.getKeys(); +// int listSize = keysInCache.size(); +// List keysToBeRemoved = new ArrayList<>(); +// +// // recover keys list that match the key +// for (int i = 0; i < listSize; i++) { +// if ((keysInCache.get(i).startsWith(keyUsedForSamplingsRefresh)) +// || (keysInCache.get(i) +// .startsWith(keyUsedForSmartSamplingRefresh))) { +// keysToBeRemoved.add(keysInCache.get(i)); +// } +// } +// // remove keys +// DBCache.removeAll(keysToBeRemoved); +// // System.out.println("Samplings refreshed with keys: " +// // + keyUsedForSamplingsRefresh + " " +// // + keyUsedForSmartSamplingRefresh); } private String storeResultIntoCSVFile(List result, String n)