From 6d7d952e99270febf6ddcf69b53edc12ce67b8f8 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Wed, 3 Aug 2016 08:08:51 +0000 Subject: [PATCH] minor fix to getRecentComments and getRecentCommentedFeeds methods git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/social-networking-library@130954 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../server/DBCassandraAstyanaxImpl.java | 47 ++++++++++++++++--- .../server/DatabookCassandraTest.java | 13 +++++ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java b/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java index 2c9eb94..5922005 100644 --- a/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java +++ b/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java @@ -588,11 +588,18 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { List toReturn = new ArrayList(); - // get the last comments by the user - List lastComments = getRecentCommentsByUserAndDate(userid, timeInMillis); + Date now = new Date(); + if (timeInMillis > now.getTime()) + throw new IllegalArgumentException("the timeInMillis must be before today"); + + if(userid == null || userid.isEmpty()) + throw new IllegalArgumentException("the userId parameter cannot be null/empty"); + + // get the last comments by the user (it is not needed to get them sorted) + List lastComments = getRecentCommentsByUserAndDateBody(userid, timeInMillis, false); // evaluate unique feeds' ids - List feedsIds = new ArrayList(); + HashSet feedsIds = new HashSet(); for (Comment comment : lastComments) { String feedId = comment.getFeedid(); @@ -606,6 +613,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { } } + Collections.sort(toReturn, Collections.reverseOrder()); return toReturn; } /** @@ -1455,7 +1463,31 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { public List getRecentCommentsByUserAndDate(final String userid, final long timeInMillis) throws Exception { - long start = System.currentTimeMillis(); + final List commentsByUser; + + Date now = new Date(); + if (timeInMillis > now.getTime()) + throw new IllegalArgumentException("the timeInMillis must be before today"); + + if(userid == null || userid.isEmpty()) + throw new IllegalArgumentException("the userId parameter cannot be null/empty"); + + + commentsByUser = getRecentCommentsByUserAndDateBody(userid, timeInMillis, true); + + return commentsByUser; + } + + /** + * Private method that allows also to specify if the returned list must be sorted or not + * @param userid the user id + * @param timeInMillis the initial time to consider + * @param sort a boolean value to specify if the returned list must be sorted (from the most recent to the oldest comment) + * @return a list of comments recently made by the user + * @throws ConnectionException + */ + private List getRecentCommentsByUserAndDateBody(final String userid, + final long timeInMillis, boolean sort) throws ConnectionException{ final List commentsByUser = new ArrayList(); final AtomicInteger maxRetryExecutions = new AtomicInteger(10); @@ -1499,16 +1531,17 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { return true; }else { - _log.error("Too many errors while fetching user's comments, returning"); + _log.error("Too many errors while fetching user's comments, exiting"); return false; } } }); - Collections.sort(commentsByUser, Collections.reverseOrder()); - _log.debug("Time taken to retrieve most recent user's comments is " + (System.currentTimeMillis() - start)); + if(sort) + Collections.sort(commentsByUser, Collections.reverseOrder()); return commentsByUser; + } /** diff --git a/src/main/java/org/gcube/portal/databook/server/DatabookCassandraTest.java b/src/main/java/org/gcube/portal/databook/server/DatabookCassandraTest.java index ad481d8..5f6a402 100644 --- a/src/main/java/org/gcube/portal/databook/server/DatabookCassandraTest.java +++ b/src/main/java/org/gcube/portal/databook/server/DatabookCassandraTest.java @@ -17,6 +17,19 @@ public class DatabookCassandraTest { } + // @Test + // public void getRecentCommentedFeedsByUserAndDate() throws Exception{ + // String userid = "massimiliano.assante"; + // Calendar oneYearAgo = Calendar.getInstance(); + // oneYearAgo.set(Calendar.YEAR, oneYearAgo.get(Calendar.YEAR) - 1); + // + // long init = System.currentTimeMillis(); + // List res = store.getRecentCommentedFeedsByUserAndDate(userid, oneYearAgo.getTimeInMillis()); + // long end = System.currentTimeMillis(); + // System.out.println("Result is " + (end - init)); + // + // + // } // @Test // public void getRecentCommentsByUserAndDate() throws Exception{ //