From 6cf162bb91e400c667e9cee0cc59f0bfbc25ed59 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Wed, 15 Nov 2023 18:14:57 +0100 Subject: [PATCH] Fixed performance bug --- .../server/DBCassandraAstyanaxImpl.java | 212 +++++++++--------- 1 file changed, 106 insertions(+), 106 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 df0b326..4a55ba9 100644 --- a/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java +++ b/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java @@ -594,7 +594,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { if (result){ _log.info("Wrote user post with id " + post.getKey()); } - closeSession(session); + return result; } @@ -642,7 +642,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { if (result){ _log.info("Wrote user post with id " + post.getKey()); } - closeSession(session); + return result; } @@ -732,7 +732,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { boolean result = session.execute(writeBatch).wasApplied(); if (result) _log.info("Wrote app post with id " + post.getKey()); - closeSession(session); + return result; } /** @@ -780,7 +780,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { boolean result = session.execute(writeBatch).wasApplied(); if (result) _log.info("Wrote app post with id " + post.getKey()); - closeSession(session); + return result; } /** @@ -842,10 +842,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { PreparedStatement stmt3 = createVreTimelineEntry(session); writeBatch.add(stmt3.bind(vreid, toCheck.getTime().toInstant(), UUID.fromString(toCheck.getKey()))); - closeSession(session); + try{ boolean res = session.execute(writeBatch).wasApplied(); - closeSession(session); + return res; }catch (Exception e) { e.printStackTrace(); @@ -871,10 +871,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { PreparedStatement stmt3 = createVreTimelineEntry(session); writeBatch.add(stmt3.bind(vreid, toCheck.getTime().toInstant(), UUID.fromString(toCheck.getKey()))); - closeSession(session); + try{ boolean res = session.execute(writeBatch).wasApplied(); - closeSession(session); + return res; }catch (Exception e) { e.printStackTrace(); @@ -893,10 +893,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { Feed post; try{ post = findFeedById(feedid, session).get(); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + return null; } return post; @@ -912,10 +912,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { Post post; try{ post = findPostById(postid, session).get(); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + return null; } return post; @@ -939,7 +939,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .isEqualTo(QueryBuilder.bindMarker()) .build()); result = session.execute(stmtFind.bind(userid)); - closeSession(session); + } catch (Exception e) { e.printStackTrace(); } @@ -986,7 +986,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .isEqualTo(QueryBuilder.bindMarker()) .build()); result = session.execute(stmtFind.bind(userid)); - closeSession(session); + } catch (Exception e) { e.printStackTrace(); } @@ -1030,10 +1030,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { try { session.execute(writeBatch); - closeSession(session); + } catch (Exception e) { _log.error("Delete Post ERROR for postid " + feedId); - closeSession(session); + return false; } _log.info("Delete Post OK"); @@ -1053,10 +1053,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { try { session.execute(writeBatch); - closeSession(session); + } catch (Exception e) { _log.error("Delete Post ERROR for postid " + postid); - closeSession(session); + return false; } _log.info("Delete Post OK"); @@ -1221,10 +1221,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind(userid)); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } List toReturn = result.all().stream().map(row-> { @@ -1253,10 +1253,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind(userid)); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } List toReturn = result.all().stream().map(row-> { @@ -1289,10 +1289,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { try { result = session.execute(stmtFind.bind(userid)); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } for (Row row: result.all()){ if (row.getUuid(POST_ID).toString().equals(postid)){ @@ -1320,10 +1320,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind(appid)); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } List toReturn = result.all().stream().map(row-> { @@ -1352,10 +1352,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind(appid)); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } List toReturn = result.all().stream().map(row-> { @@ -1387,10 +1387,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind(PrivacyLevel.PORTAL.toString())); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } for (Row row: result.all()) { Feed toAdd = readFeedFromRow(row); @@ -1412,10 +1412,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind(PrivacyLevel.PORTAL.toString())); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } for (Row row: result.all()) { Post toAdd = readPostFromRow(row); @@ -1637,10 +1637,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind(vreid)); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } List toReturn = result.all().stream().map(row-> { @@ -1669,10 +1669,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind(vreid)); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } List toReturn = result.all().stream().map(row-> { @@ -1723,7 +1723,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { writeBatch.add(stmt3.bind(n.getUserid(), n.getTime().toInstant(), UUID.fromString(n.getKey()))); try{ boolean res = session.execute(writeBatch).wasApplied(); - closeSession(session); + return res; }catch (Exception e){ e.printStackTrace(); @@ -1740,11 +1740,11 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { CqlSession session = conn.getKeyspaceSession(); try { toReturn = findNotById(notificationid, session).get(); - closeSession(session); + } catch (Exception e) { e.printStackTrace(); - closeSession(session); + } return toReturn; } @@ -1769,7 +1769,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { // execute the operations try { boolean res = session.execute(writeBatch).wasApplied(); - closeSession(session); + return res; } catch (Exception e) { _log.error("ERROR while setting Notification " + notificationidToSet + " to read."); @@ -1791,10 +1791,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind(userid)); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } ArrayList toReturn = new ArrayList(); @@ -1820,10 +1820,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind(userid)); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } ArrayList toReturn = new ArrayList(); @@ -2020,10 +2020,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { boolean res = false; try{ res = session.execute(writeBatch).wasApplied(); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } if (res) { @@ -2081,10 +2081,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind(userid)); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } //if there are no settings for this user create an entry and put all of them at true @@ -2166,7 +2166,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { try { session.execute(writeBatch); - closeSession(session); + } catch (Exception e) { e.printStackTrace(); return false; @@ -2190,15 +2190,13 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind(UUID.fromString(commentId))); - closeSession(session); + if (result.all().isEmpty()) { throw new CommentIDNotFoundException("The requested commentId: " + commentId + " is not existing"); } toReturn = readCommentFromRow(result.one()); } catch (Exception e){ e.printStackTrace(); - closeSession(session); - } return toReturn; @@ -2220,20 +2218,22 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { List toReturn = new ArrayList(); ResultSet result = null; CqlSession session = conn.getKeyspaceSession(); - PreparedStatement stmtFind = session.prepare(QueryBuilder + PreparedStatement stmtFind = session.prepare("SELECT * FROM comments WHERE postid=?"); + /*PreparedStatement stmtFind = session.prepare(QueryBuilder .selectFrom(COMMENTS).all() + .where .whereColumn(POST_ID).isEqualTo(QueryBuilder.bindMarker()) - .build()); + .build());*/ try { result = session.execute(stmtFind.bind(UUID.fromString(postid))); - closeSession(session); + for (Row row : result.all()) { Comment toAdd = readCommentFromRow(row); toReturn.add(toAdd); } } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } return toReturn; @@ -2283,7 +2283,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind()); - closeSession(session); + if (!result.all().isEmpty()){ result.all().parallelStream().forEach( row->{ @@ -2303,7 +2303,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { } } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } if(sort) @@ -2325,11 +2325,11 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { try { boolean res = session.execute(writeBatch).wasApplied(); - closeSession(session); + _log.info("Comments update OK to: " + comment2Edit.getText()); return res; } catch (Exception e) { - closeSession(session); + _log.error("Comments update NOT OK "); return false; } @@ -2350,9 +2350,9 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { try { session.execute(writeBatch); - closeSession(session); + } catch (Exception e) { - closeSession(session); + _log.error("Comment Delete FAILED for " + commentid + " from Feed " + feedid); e.printStackTrace(); } @@ -2403,7 +2403,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { UUID.fromString(like.getFeedid()))); try { session.execute(writeBatch); - closeSession(session); + } catch (Exception e) { e.printStackTrace(); return false; @@ -2426,9 +2426,9 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .add(deleteUserLikeEntry(session).bind(userid, UUID.fromString(likeid))); try { session.execute(writeBatch); - closeSession(session); + } catch (Exception e) { - closeSession(session); + _log.error("Like Delete FAILED for " + likeid + " from Feed " + feedid); e.printStackTrace(); } @@ -2459,10 +2459,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind(userid)); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } ArrayList toReturn = new ArrayList(); @@ -2652,10 +2652,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { Like toAdd = readLikeFromRow(row); toReturn.add(toAdd); } - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } return toReturn; } @@ -2681,11 +2681,11 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { boolean secondInsert = updateVREHashtagCount(vreid, lowerCaseHashtag, true); if (! (firstInsert && secondInsert)) { _log.error("saveHashTags: Could not save the hashtag(s)"); - closeSession(session); + return false; } } - closeSession(session); + return true; } /** @@ -2705,12 +2705,12 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { boolean secondInsert = updateVREHashtagCount(vreid, lowerCaseHashtag, false); if (! (firstDelete && secondInsert)) { _log.error("deleteHashTags: Could not delete the hashtag(s)"); - closeSession(session); + return false; } } - closeSession(session); + return true; } /** @@ -2732,11 +2732,11 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { secondInsert = updateVREHashtagCount(vreid, lowerCaseHashtag, true); if (! (firstInsert && secondInsert)) { _log.error("saveHashTags: Could not save the hashtag(s)"); - closeSession(session); + return false; } } - closeSession(session); + return true; } /** @@ -2757,16 +2757,16 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { boolean secondInsert = updateVREHashtagCount(vreid, lowerCaseHashtag, false); if (!(firstDelete && secondInsert)) { _log.error("deleteHashTags: Could not delete the hashtag(s)"); - closeSession(session); + return false; } }else{ _log.error("deleteHashTags: Could not delete the hashtag(s)"); - closeSession(session); + return false; } } - closeSession(session); + return true; } /** @@ -2784,10 +2784,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind(vreid)); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } @@ -2816,10 +2816,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind(vreid)); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } HashMap toReturn = new HashMap (); @@ -2876,7 +2876,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { resultPost = session.execute(stmtFind1.bind(hashtag)); } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } PreparedStatement stmtFind2 = session.prepare(QueryBuilder @@ -2889,7 +2889,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { resultComment = session.execute(stmtFind2.bind(hashtag)); } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } Set postIds = new HashSet<>(); @@ -2930,7 +2930,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { resultPost = session.execute(stmtFind1.bind(hashtag)); } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } PreparedStatement stmtFind2 = session.prepare(QueryBuilder @@ -2943,7 +2943,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { resultComment = session.execute(stmtFind2.bind(hashtag)); } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } Set postIds = new HashSet<>(); @@ -3013,7 +3013,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { result = session.execute(stmtFind1.bind(email)); } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } // Iterate rows and their columns @@ -3047,7 +3047,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { //an entry in the EMAIL Invites m.add(createNewEmailInviteEntry(session).bind(email, invite.getVreid(), UUID.fromString(invite.getKey()))); boolean result = session.execute(m).wasApplied(); - closeSession(session); + return result ? InviteOperationResult.SUCCESS : InviteOperationResult.FAILED; } /** @@ -3069,10 +3069,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { throw new InviteStatusNotFoundException("The requested inviteid: " + inviteid + " is not existing"); } toReturn = readAInviteFromRow(result.one()); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } return toReturn; } @@ -3109,10 +3109,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .add(updateVreInviteEntry(session, STATUS).bind(status.toString(), vreid, UUID.fromString(inviteid))); try { session.execute(writeBatch); - closeSession(session); + } catch (Exception e) { _log.error("ERROR while setting Invite " + inviteid + " to " + status.toString()); - closeSession(session); + return false; } _log.trace("Invite Status Set to " + status.toString() + " OK"); @@ -3132,10 +3132,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.bind(vreid)); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } ArrayList invitesIds = new ArrayList(); @@ -3183,10 +3183,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { Attachment toAdd = readAttachmentFromRow(row); toReturn.add(toAdd); } - closeSession(session); + } catch (Exception e) { e.printStackTrace(); - closeSession(session); + return null; } return toReturn; @@ -3222,9 +3222,9 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { toSave.getThumbnailURL(), toSave.getMimeType() )); - closeSession(session); + } catch (Exception e) { - closeSession(session); + e.printStackTrace(); return false; } @@ -3517,10 +3517,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { //an entry in the feed CF try { session.execute(updatePostEntry(session, COMMENTS_NO).bind((long) newCount, UUID.fromString(toUpdate.getKey()))); - closeSession(session); + } catch (Exception e) { _log.error("CommentsNo update NOT OK "); - closeSession(session); + return false; } _log.info("CommentsNo update OK to: " + newCount); @@ -3546,10 +3546,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { //an entry in the feed CF try { session.execute(updatePostEntry(session, LIKES_NO).bind((long)newCount, UUID.fromString(toUpdate.getKey()))); - closeSession(session); + } catch (Exception e) { _log.error("LikesNo update NOT OK "); - closeSession(session); + return false; } _log.info("LikesNo update OK to: " + newCount); @@ -3599,10 +3599,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { try { session.execute(writeBatch); - closeSession(session); + } catch (Exception e) { _log.error("Hashtag Count update NOT OK "); - closeSession(session); + return false; } _log.debug("Hashtag Count update OK to: " + newCount); @@ -3638,10 +3638,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .build()); try { result = session.execute(stmtFind.getQuery()); - closeSession(session); + } catch (Exception e){ e.printStackTrace(); - closeSession(session); + } for (Row row : result.all()) {