From 022c0c57d173762aa9ff8fdb4c056261c744edfa Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Mon, 25 Jul 2016 18:55:32 +0000 Subject: [PATCH] fix for topics git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/user/top-topics@130798 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../user/topics/server/TopicServiceImpl.java | 202 ++++++++---------- .../topics/server/cache/CacheInterface.java | 26 --- .../topics/server/cache/CacheUtilities.java | 26 --- .../topics/server/cache/CacheValueBean.java | 40 ---- .../topics/server/cache/TopicsCacheImpl.java | 77 ------- 5 files changed, 93 insertions(+), 278 deletions(-) delete mode 100644 src/main/java/org/gcube/portlets/user/topics/server/cache/CacheInterface.java delete mode 100644 src/main/java/org/gcube/portlets/user/topics/server/cache/CacheUtilities.java delete mode 100644 src/main/java/org/gcube/portlets/user/topics/server/cache/CacheValueBean.java delete mode 100644 src/main/java/org/gcube/portlets/user/topics/server/cache/TopicsCacheImpl.java diff --git a/src/main/java/org/gcube/portlets/user/topics/server/TopicServiceImpl.java b/src/main/java/org/gcube/portlets/user/topics/server/TopicServiceImpl.java index 1d22087..851ce39 100644 --- a/src/main/java/org/gcube/portlets/user/topics/server/TopicServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/topics/server/TopicServiceImpl.java @@ -21,7 +21,6 @@ import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl; import org.gcube.portal.databook.server.DatabookStore; import org.gcube.portal.databook.shared.Feed; import org.gcube.portlets.user.topics.client.TopicService; -import org.gcube.portlets.user.topics.server.cache.TopicsCacheImpl; import org.gcube.portlets.user.topics.shared.HashTagAndOccurrence; import org.gcube.portlets.user.topics.shared.HashtagsWrapper; import org.gcube.vomanagement.usermanagement.GroupManager; @@ -49,6 +48,7 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi * The Cassandra store interface */ private DatabookStore store; + /** * connect to cassandra at startup */ @@ -56,6 +56,9 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi store = new DBCassandraAstyanaxImpl(); } + /** + * close connection to cassandra at shutdown + */ public void destroy() { store.closeConnection(); } @@ -88,7 +91,7 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi */ @Override public HashtagsWrapper getHashtags() { - ArrayList hashtagsChart; + ArrayList hashtagsChart = new ArrayList(); ASLSession session = getASLSession(); String userName = session.getUsername(); @@ -97,138 +100,119 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi long timestampStart = System.currentTimeMillis(); - // get cache reference - TopicsCacheImpl topicsCache = TopicsCacheImpl.getCacheInstance(); + // get the reference time + Calendar referenceTime = Calendar.getInstance(); + int currentMonth = referenceTime.get(Calendar.MONTH); // jan = 0, ..... dec = 11 + referenceTime.set(Calendar.MONTH, currentMonth - WINDOW_SIZE_IN_MONTHS); // the year is automatically decreased if needed - hashtagsChart = topicsCache.get(currentScope); + // print it + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + _log.debug("Reference time for trending topics is " + format.format(referenceTime.getTime())); - if(hashtagsChart != null){ - - _log.debug("TopTopics for scope " + currentScope + " retrieved from the cache"); - - } - else{ - - _log.debug("TopTopics for scope " + currentScope + " are not in the cache, evaluating them..."); - - // create array - hashtagsChart = new ArrayList(); - - // get the reference time - Calendar referenceTime = Calendar.getInstance(); - int currentMonth = referenceTime.get(Calendar.MONTH); // jan = 0, ..... dec = 11 - referenceTime.set(Calendar.MONTH, currentMonth - WINDOW_SIZE_IN_MONTHS); // the year is automatically decreased if needed + try { - // print it - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - _log.debug("Reference time for trending topics is " + format.format(referenceTime.getTime())); + //in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info) + //this check just return nothing if that happens + if (userName.compareTo("test.user") == 0) { + _log.debug("Found " + userName + " returning nothing"); + return null; + } - try { - //in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info) - //this check just return nothing if that happens - if (userName.compareTo("test.user") == 0) { - _log.debug("Found " + userName + " returning nothing"); - return null; - } - ArrayList toSort = new ArrayList(); - if (isInfrastructure) { - _log.debug("****** retrieving hashtags for user VREs"); + ArrayList toSort = new ArrayList(); + if (isInfrastructure) { + _log.debug("****** retrieving hashtags for user VREs"); - // different vres could have a same hashtag, we need to merge them - Map hashtags = new HashMap(); + // different vres could have a same hashtag, we need to merge them + Map hashtags = new HashMap(); - // we need a map for the couple - // it is needed because later we need to retrieve the most recent feed among the ones - // containing the hashtag itself - Map> hashtagsInVres = new HashMap>(); + // we need a map for the couple + // it is needed because later we need to retrieve the most recent feed among the ones + // containing the hashtag itself + Map> hashtagsInVres = new HashMap>(); - GroupManager gm = new LiferayGroupManager(); - UserManager um = new LiferayUserManager(); - GCubeUser user = um.getUserByUsername(userName); + GroupManager gm = new LiferayGroupManager(); + UserManager um = new LiferayUserManager(); + GCubeUser user = um.getUserByUsername(userName); - List groups = gm.listGroupsByUser(user.getUserId()); - for (GCubeGroup group : groups) { - if (gm.isVRE(group.getGroupId())) { - String vreid = gm.getInfrastructureScope(group.getGroupId()); //get the scope - Map map = store.getVREHashtagsWithOccurrenceFilteredByTime(vreid, referenceTime.getTimeInMillis()); + List groups = gm.listGroupsByUser(user.getUserId()); + for (GCubeGroup group : groups) { + if (gm.isVRE(group.getGroupId())) { + String vreid = gm.getInfrastructureScope(group.getGroupId()); //get the scope + Map map = store.getVREHashtagsWithOccurrenceFilteredByTime(vreid, referenceTime.getTimeInMillis()); - // merge the values if needed - for (String hashtag : map.keySet()) { + // merge the values if needed + for (String hashtag : map.keySet()) { - if(hashtags.containsKey(hashtag)){ + if(hashtags.containsKey(hashtag)){ - int currentValue = hashtags.get(hashtag); - int newValue = currentValue + map.get(hashtag); + int currentValue = hashtags.get(hashtag); + int newValue = currentValue + map.get(hashtag); - // remove and re-add - hashtags.remove(hashtag); - hashtags.put(hashtag, newValue); + // remove and re-add + hashtags.remove(hashtag); + hashtags.put(hashtag, newValue); - // get the current list of vres in which the hashtag is present and add this new one - List vres = hashtagsInVres.get(hashtag); - vres.add(vreid); - hashtagsInVres.remove(hashtag); - hashtagsInVres.put(hashtag, vres); + // get the current list of vres in which the hashtag is present and add this new one + List vres = hashtagsInVres.get(hashtag); + vres.add(vreid); + hashtagsInVres.remove(hashtag); + hashtagsInVres.put(hashtag, vres); - }else{ + }else{ - hashtags.put(hashtag, map.get(hashtag)); + hashtags.put(hashtag, map.get(hashtag)); - // put in the hashmap hashtagsInVres too - List vres = new ArrayList(); - vres.add(vreid); - hashtagsInVres.put(hashtag, vres); - } + // put in the hashmap hashtagsInVres too + List vres = new ArrayList(); + vres.add(vreid); + hashtagsInVres.put(hashtag, vres); } } } - - // now we need to evaluate score for each element - Map weights = evaluateWeight(hashtags, WINDOW_SIZE_IN_MONTHS, currentMonth, referenceTime, null, hashtagsInVres); - - // at the end build the list - for (String hashtag : hashtags.keySet()) { - toSort.add(new HashTagAndOccurrence(hashtag, hashtags.get(hashtag), weights.get(hashtag))); - } - } + + // now we need to evaluate score for each element + Map weights = evaluateWeight(hashtags, WINDOW_SIZE_IN_MONTHS, currentMonth, referenceTime, null, hashtagsInVres); + + // at the end build the list + for (String hashtag : hashtags.keySet()) { + toSort.add(new HashTagAndOccurrence(hashtag, hashtags.get(hashtag), weights.get(hashtag))); + } + + } + else { //else must be in a VRE scope - else { - String scope = session.getScope(); - _log.debug("****** retrieving hashtags for scope " + scope); - Map hashtags = store.getVREHashtagsWithOccurrenceFilteredByTime(scope, referenceTime.getTimeInMillis()); - // now we need to evaluate the wiehgt for each element - Map weights = evaluateWeight(hashtags, WINDOW_SIZE_IN_MONTHS, currentMonth, referenceTime, scope, null); - for (String hashtag : hashtags.keySet()) { - toSort.add(new HashTagAndOccurrence(hashtag, hashtags.get(hashtag), weights.get(hashtag))); - } + String scope = session.getScope(); + _log.debug("****** retrieving hashtags for scope " + scope); + Map hashtags = store.getVREHashtagsWithOccurrenceFilteredByTime(scope, referenceTime.getTimeInMillis()); + // now we need to evaluate the weight for each element + Map weights = evaluateWeight(hashtags, WINDOW_SIZE_IN_MONTHS, currentMonth, referenceTime, scope, null); + for (String hashtag : hashtags.keySet()) { + toSort.add(new HashTagAndOccurrence(hashtag, hashtags.get(hashtag), weights.get(hashtag))); } - - _log.debug("Number of topics retrieved is " + toSort.size()); - - Collections.sort(toSort); // sort for weight - - for (HashTagAndOccurrence wrapper : toSort) { - - _log.debug("Entry is " + wrapper.toString() + " with weight " + wrapper.getWeight()); - - String hashtag = wrapper.getHashtag(); - - String href="\"?"+ - new String(Base64.encodeBase64(GCubeSocialNetworking.HASHTAG_OID.getBytes()))+"="+ - new String(Base64.encodeBase64(hashtag.getBytes()))+"\""; - String hashtagLink = ""+hashtag+""; - hashtagsChart.add(hashtagLink); - } - - // update the cache - topicsCache.insert(currentScope, hashtagsChart); } - catch (Exception e) { - e.printStackTrace(); - return null; + + _log.debug("Number of topics retrieved is " + toSort.size()); + + Collections.sort(toSort); // sort for weight + + for (HashTagAndOccurrence wrapper : toSort) { + + _log.debug("Entry is " + wrapper.toString() + " with weight " + wrapper.getWeight()); + + String hashtag = wrapper.getHashtag(); + + String href="\"?"+ + new String(Base64.encodeBase64(GCubeSocialNetworking.HASHTAG_OID.getBytes()))+"="+ + new String(Base64.encodeBase64(hashtag.getBytes()))+"\""; + String hashtagLink = ""+hashtag+""; + hashtagsChart.add(hashtagLink); } } + catch (Exception e) { + e.printStackTrace(); + return null; + } long timestampEnd = System.currentTimeMillis() - timestampStart; _log.debug("Overall time to retrieve hastags is " + timestampEnd + "ms"); diff --git a/src/main/java/org/gcube/portlets/user/topics/server/cache/CacheInterface.java b/src/main/java/org/gcube/portlets/user/topics/server/cache/CacheInterface.java deleted file mode 100644 index 7e7fa82..0000000 --- a/src/main/java/org/gcube/portlets/user/topics/server/cache/CacheInterface.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.gcube.portlets.user.topics.server.cache; - -/** - * Cache interface - * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) - * @param the key type - * @param the value type - */ -public interface CacheInterface { - - /** - * Retrieve a value V from the cache - * @param key - * @return - */ - public V get(K key); - - /** - * Insert an object V with key K into the cache - * @param key - * @param value - * @return - */ - public boolean insert(K key, V value); - -} diff --git a/src/main/java/org/gcube/portlets/user/topics/server/cache/CacheUtilities.java b/src/main/java/org/gcube/portlets/user/topics/server/cache/CacheUtilities.java deleted file mode 100644 index 13364b4..0000000 --- a/src/main/java/org/gcube/portlets/user/topics/server/cache/CacheUtilities.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.gcube.portlets.user.topics.server.cache; - -/** - * Utility functions for caches - * @author Costantino Perciante at ISTI-CNR - * (costantino.perciante@isti.cnr.it) - * - */ -public class CacheUtilities { - /** - * Check if the bean expired - * @param beanTimestamp - * @param ttl - * @return if expired, otherwise - */ - public static boolean expired(long beanTimestamp, long ttl){ - - long currentTime = System.currentTimeMillis(); - - if((beanTimestamp + ttl) <= currentTime) - return true; - else - return false; - - } -} diff --git a/src/main/java/org/gcube/portlets/user/topics/server/cache/CacheValueBean.java b/src/main/java/org/gcube/portlets/user/topics/server/cache/CacheValueBean.java deleted file mode 100644 index b51e1fb..0000000 --- a/src/main/java/org/gcube/portlets/user/topics/server/cache/CacheValueBean.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.gcube.portlets.user.topics.server.cache; - -/** - * A bean object to be used as value within caches. It contains a TTL value too. - * @author Costantino Perciante at ISTI-CNR - * (costantino.perciante@isti.cnr.it) - * - * @param the value type - */ -public class CacheValueBean { - - private V value; - private long TTL; - - /** - * @param value - * @param tTL - */ - public CacheValueBean(V value, long ttl) { - super(); - this.value = value; - this.TTL = ttl; - } - public V getValue() { - return value; - } - public void setValue(V value) { - this.value = value; - } - public long getTTL() { - return TTL; - } - public void setTTL(long ttl) { - this.TTL = ttl; - } - @Override - public String toString() { - return "CacheValueBean [value=" + value + ", TTL=" + TTL + "]"; - } -} diff --git a/src/main/java/org/gcube/portlets/user/topics/server/cache/TopicsCacheImpl.java b/src/main/java/org/gcube/portlets/user/topics/server/cache/TopicsCacheImpl.java deleted file mode 100644 index 2e1ede3..0000000 --- a/src/main/java/org/gcube/portlets/user/topics/server/cache/TopicsCacheImpl.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.gcube.portlets.user.topics.server.cache; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; - -import org.slf4j.LoggerFactory; -/** - * TopTopics cache: the key of each value is the scope - * @author Costantino Perciante at ISTI-CNR - * (costantino.perciante@isti.cnr.it) - */ -public class TopicsCacheImpl implements CacheInterface> { - - private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TopicsCacheImpl.class); - - private static TopicsCacheImpl instance = new TopicsCacheImpl(); - - /** - * The hashmap - */ - private Map>> cacheMap; - - /** - * Cache entry expires after EXPIRED_AFTER ms - */ - private static final long EXPIRED_AFTER = 1000 * 60 * 2; - - /** - * private constructor - */ - private TopicsCacheImpl(){ - - this.cacheMap = new HashMap>>(); - - } - - /** - * Retrieve the current cache instance object - * @return - */ - public static TopicsCacheImpl getCacheInstance(){ - - return instance; - - } - - @Override - public ArrayList get(String key) { - if(cacheMap.containsKey(key)){ - - CacheValueBean> bean = cacheMap.get(key); - - if(CacheUtilities.expired(bean.getTTL(), EXPIRED_AFTER)){ - cacheMap.remove(key); - logger.debug("TopTopics for scope " + key + "expired, return null"); - } - else - return bean.getValue(); - } - - return null; - } - - @Override - public boolean insert(String key, ArrayList value) { - CacheValueBean> newBean = new CacheValueBean>(value, System.currentTimeMillis()); - - if(cacheMap.containsKey(key)) - cacheMap.remove(key); - - cacheMap.put(key, newBean); - - return true; - } - -}