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
This commit is contained in:
Costantino Perciante 2016-08-03 08:08:51 +00:00
parent 0ee2a0426b
commit 6d7d952e99
2 changed files with 53 additions and 7 deletions

View File

@ -588,11 +588,18 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
List<Feed> toReturn = new ArrayList<Feed>(); List<Feed> toReturn = new ArrayList<Feed>();
// get the last comments by the user Date now = new Date();
List<Comment> lastComments = getRecentCommentsByUserAndDate(userid, timeInMillis); 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<Comment> lastComments = getRecentCommentsByUserAndDateBody(userid, timeInMillis, false);
// evaluate unique feeds' ids // evaluate unique feeds' ids
List<String> feedsIds = new ArrayList<String>(); HashSet<String> feedsIds = new HashSet<String>();
for (Comment comment : lastComments) { for (Comment comment : lastComments) {
String feedId = comment.getFeedid(); String feedId = comment.getFeedid();
@ -606,6 +613,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
} }
} }
Collections.sort(toReturn, Collections.reverseOrder());
return toReturn; return toReturn;
} }
/** /**
@ -1455,7 +1463,31 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
public List<Comment> getRecentCommentsByUserAndDate(final String userid, public List<Comment> getRecentCommentsByUserAndDate(final String userid,
final long timeInMillis) throws Exception { final long timeInMillis) throws Exception {
long start = System.currentTimeMillis(); final List<Comment> 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<Comment> getRecentCommentsByUserAndDateBody(final String userid,
final long timeInMillis, boolean sort) throws ConnectionException{
final List<Comment> commentsByUser = new ArrayList<Comment>(); final List<Comment> commentsByUser = new ArrayList<Comment>();
final AtomicInteger maxRetryExecutions = new AtomicInteger(10); final AtomicInteger maxRetryExecutions = new AtomicInteger(10);
@ -1499,16 +1531,17 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
return true; return true;
}else }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; return false;
} }
} }
}); });
Collections.sort(commentsByUser, Collections.reverseOrder()); if(sort)
_log.debug("Time taken to retrieve most recent user's comments is " + (System.currentTimeMillis() - start)); Collections.sort(commentsByUser, Collections.reverseOrder());
return commentsByUser; return commentsByUser;
} }
/** /**

View File

@ -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<Feed> res = store.getRecentCommentedFeedsByUserAndDate(userid, oneYearAgo.getTimeInMillis());
// long end = System.currentTimeMillis();
// System.out.println("Result is " + (end - init));
//
//
// }
// @Test // @Test
// public void getRecentCommentsByUserAndDate() throws Exception{ // public void getRecentCommentsByUserAndDate() throws Exception{
// //