From fbae17bb3423daf0024ca0fc45b0e3c645a5d7de Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Sat, 6 Aug 2016 06:19:53 +0000 Subject: [PATCH] getRecentComments speeded up by reducing synchronization time at minimum git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/social-networking-library@130989 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../server/DBCassandraAstyanaxImpl.java | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 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 1ed165a..6a9d37d 100644 --- a/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java +++ b/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java @@ -1502,23 +1502,33 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .executeWithCallback(new RowCallback() { @Override public void success(Rows rows) { - synchronized (commentsByUser) { - nextRow: for (Row row : rows) { - for(Column column: row.getColumns()) - if(column.getName().equals("Userid")){ - if(column.getStringValue().equals(userid)){ - try{ - Comment c = readCommentById(row.getKey()); - Feed f = readFeed(c.getFeedid()); - if(c.getTime().getTime() >= timeInMillis && - (f.getType() == FeedType.TWEET || f.getType() == FeedType.SHARE || f.getType() == FeedType.PUBLISH)) - commentsByUser.add(c); - }catch(Exception e){ - _log.error("Unable to read comment with id" + row.getKey(), e); - } + + // use a temporary list for each thread + List partialCommentsList = new ArrayList(); + + nextRow: for (Row row : rows) { + for(Column column: row.getColumns()){ + if(column.getName().equals("Userid")){ + if(column.getStringValue().equals(userid)){ + try{ + Comment c = readCommentById(row.getKey()); + Feed f = readFeed(c.getFeedid()); + if(c.getTime().getTime() >= timeInMillis && + (f.getType() == FeedType.TWEET || f.getType() == FeedType.SHARE || f.getType() == FeedType.PUBLISH)) + partialCommentsList.add(c); + }catch(Exception e){ + _log.error("Unable to read comment with id" + row.getKey(), e); } - continue nextRow; } + continue nextRow; + } + } + } + + // if there is something to save... + if(partialCommentsList.size() > 0){ + synchronized (commentsByUser){ + commentsByUser.addAll(partialCommentsList); } } } @@ -1528,12 +1538,11 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { // decrement value int currentValue = maxRetryExecutions.decrementAndGet(); if(currentValue <= 0){ - _log.error("Error while fetching user's recent comments ... repeating operation"); - return true; - }else - { _log.error("Too many errors while fetching user's comments, exiting"); return false; + }else{ + _log.error("Error while fetching user's recent comments ... repeating operation"); + return true; } } });