From a63526c86e777646178b288c953144cccd70ea1f Mon Sep 17 00:00:00 2001 From: madoooaboelazaiem Date: Mon, 28 Jun 2021 20:32:10 +0200 Subject: [PATCH 1/6] feat: added user-population data --- .../src/main/java/org/sab/user/UserApp.java | 5 +-- .../org/sab/postgres/PostgresConnection.java | 35 ++++++++++++++++++- .../resources/sql/CreateUserProcedures.sql | 19 +++++++++- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/apps/user_app/src/main/java/org/sab/user/UserApp.java b/apps/user_app/src/main/java/org/sab/user/UserApp.java index fab1d037..5a8ee46e 100644 --- a/apps/user_app/src/main/java/org/sab/user/UserApp.java +++ b/apps/user_app/src/main/java/org/sab/user/UserApp.java @@ -9,17 +9,18 @@ import org.sab.validation.exceptions.EnvironmentVariableNotLoaded; import java.io.IOException; +import java.sql.SQLException; public class UserApp extends Service { public static final String ARANGO_DB_NAME = System.getenv("ARANGO_DB"); - public static void main(String[] args) throws IOException, EnvironmentVariableNotLoaded { + public static void main(String[] args) throws IOException, EnvironmentVariableNotLoaded, SQLException { new UserApp().start(); dbInit(); } - public static void dbInit() throws IOException, EnvironmentVariableNotLoaded { + public static void dbInit() throws IOException, EnvironmentVariableNotLoaded, SQLException { if (!Utilities.inContainerizationMode()) PostgresConnection.dbInit(); arangoDbInit(); diff --git a/libs/postgres/src/main/java/org/sab/postgres/PostgresConnection.java b/libs/postgres/src/main/java/org/sab/postgres/PostgresConnection.java index 9d7d6084..93d81669 100644 --- a/libs/postgres/src/main/java/org/sab/postgres/PostgresConnection.java +++ b/libs/postgres/src/main/java/org/sab/postgres/PostgresConnection.java @@ -1,6 +1,7 @@ package org.sab.postgres; +import org.sab.auth.Auth; import org.sab.validation.exceptions.EnvironmentVariableNotLoaded; import java.io.BufferedReader; @@ -9,6 +10,7 @@ import java.io.InputStreamReader; import java.sql.*; import java.util.Properties; +import java.util.UUID; public class PostgresConnection { private static PostgresConnection instance = null; @@ -102,6 +104,36 @@ private static void createUsersProcedures() throws IOException, EnvironmentVaria runScript(getScriptPath("CreateUserProcedures")); } + private static void createMockData() throws IOException, EnvironmentVariableNotLoaded { + for (int rowNumber = 0; rowNumber < 10000; rowNumber++) { + String username = "user" + rowNumber; + String userId = UUID.randomUUID().toString(); + String hashedPassword = Auth.hash("12345678"); + String email = "user" + rowNumber + "@gmail.com"; + Date birthdate = Date.valueOf("1998-1-1"); + + try { + PostgresConnection.call("create_user", userId, username, email, hashedPassword, birthdate); + System.out.println("user" + rowNumber + " Inserted"); + } catch (EnvironmentVariableNotLoaded | SQLException e) { + System.out.println("Error Occured while adding user number " + rowNumber); + } + } + } + private static void runMockDataProcedure() throws EnvironmentVariableNotLoaded, SQLException { + PostgresConnection postgresConnection = getInstance(); + Connection connection = postgresConnection.connect(); + try { + connection.prepareCall("{call mockData()}").execute(); + } catch ( SQLException e) { + System.out.println(e); + System.out.println("Error Occured while adding users mock data "); + }finally { + connection.close(); + } + } + + private static void runScript(String scriptPath) throws IOException, EnvironmentVariableNotLoaded { for (String param : propertiesParams) if (System.getenv(param) == null) @@ -126,9 +158,10 @@ private static void runScript(String scriptPath) throws IOException, Environment } - public static void dbInit() throws IOException, EnvironmentVariableNotLoaded { + public static void dbInit() throws IOException, EnvironmentVariableNotLoaded, SQLException { createUsersTable(); createUsersProcedures(); + runMockDataProcedure(); } private static String getScriptPath(String sqlScriptName) { diff --git a/libs/postgres/src/main/resources/sql/CreateUserProcedures.sql b/libs/postgres/src/main/resources/sql/CreateUserProcedures.sql index 37b03b8e..7e1d4c4b 100644 --- a/libs/postgres/src/main/resources/sql/CreateUserProcedures.sql +++ b/libs/postgres/src/main/resources/sql/CreateUserProcedures.sql @@ -15,7 +15,7 @@ CREATE OR REPLACE FUNCTION delete_user(in_username VARCHAR) RETURNS VOID AS $$ BEGIN DELETE FROM users - WHERE username = in_username ; + WHERE username = in_username; INSERT INTO deleted_users (username) VALUES (in_username); @@ -71,3 +71,20 @@ BEGIN END; $$ LANGUAGE PLPGSQL; + + +CREATE OR REPLACE FUNCTION mockData() + RETURNS VOID AS $$ +declare + counter integer := 0; +BEGIN + while counter < 10000 loop + INSERT INTO users (user_id,username, email, password, birthdate, photo_url) + VALUES (concat('',counter),concat('users',counter),concat('users',counter,'@gmail.com'),'$2y$12$XLbi8bFbqRlsuzERzeRZTehyZxM8zW9NPphiHEdyuAeL8Uihl0kfO','1998-1-1','https://live.staticflickr.com/8172/8066465258_2c7eb75964_m.jpg'); + counter := counter + 1; + end loop; +END; +$$ + LANGUAGE PLPGSQL; + + From 2773e403d45c060860de41b8221451aff4a51e9a Mon Sep 17 00:00:00 2001 From: madoooaboelazaiem Date: Mon, 28 Jun 2021 21:49:47 +0200 Subject: [PATCH 2/6] feat: added test for population data --- .../java/org/sab/user/commands/UserAppTest.java | 14 +++++++++++++- .../java/org/sab/postgres/PostgresConnection.java | 14 ++++++++++++++ .../main/resources/sql/CreateUserProcedures.sql | 6 ++---- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/apps/user_app/src/test/java/org/sab/user/commands/UserAppTest.java b/apps/user_app/src/test/java/org/sab/user/commands/UserAppTest.java index e7696227..9f8da15b 100644 --- a/apps/user_app/src/test/java/org/sab/user/commands/UserAppTest.java +++ b/apps/user_app/src/test/java/org/sab/user/commands/UserAppTest.java @@ -13,9 +13,12 @@ import org.sab.functions.Utilities; import org.sab.models.CollectionNames; import org.sab.models.user.UserAttributes; +import org.sab.postgres.PostgresConnection; import org.sab.user.UserApp; +import org.sab.validation.exceptions.EnvironmentVariableNotLoaded; import java.io.IOException; +import java.sql.SQLException; import java.util.Date; import java.util.Map; @@ -40,7 +43,16 @@ public static void connectToDbs() { } } - + @Test + public void T00_UserPopulationData() throws SQLException, EnvironmentVariableNotLoaded { + try { + int resultSet = PostgresConnection.countRows(); + assertEquals(20000,resultSet); + } catch (SQLException e) { + System.out.println("Population Test Failure"); + System.out.println(e); + } + } @Test public void T01_SignUpCreatesAnEntryInDB() { diff --git a/libs/postgres/src/main/java/org/sab/postgres/PostgresConnection.java b/libs/postgres/src/main/java/org/sab/postgres/PostgresConnection.java index 93d81669..ad95ccb5 100644 --- a/libs/postgres/src/main/java/org/sab/postgres/PostgresConnection.java +++ b/libs/postgres/src/main/java/org/sab/postgres/PostgresConnection.java @@ -120,6 +120,20 @@ private static void createMockData() throws IOException, EnvironmentVariableNotL } } } + public static int countRows() throws SQLException, EnvironmentVariableNotLoaded { + PostgresConnection postgresConnection = getInstance(); + Connection connection = postgresConnection.connect(); + int count = 0; + try { + ResultSet resultSet = connection.createStatement().executeQuery("SELECT count(*) FROM users"); + while (resultSet.next()) { + count = resultSet.getInt("count"); + } + } catch (SQLException e) { + System.out.println(e); + } + return count; + } private static void runMockDataProcedure() throws EnvironmentVariableNotLoaded, SQLException { PostgresConnection postgresConnection = getInstance(); Connection connection = postgresConnection.connect(); diff --git a/libs/postgres/src/main/resources/sql/CreateUserProcedures.sql b/libs/postgres/src/main/resources/sql/CreateUserProcedures.sql index 7e1d4c4b..295395df 100644 --- a/libs/postgres/src/main/resources/sql/CreateUserProcedures.sql +++ b/libs/postgres/src/main/resources/sql/CreateUserProcedures.sql @@ -78,13 +78,11 @@ CREATE OR REPLACE FUNCTION mockData() declare counter integer := 0; BEGIN - while counter < 10000 loop + while counter < 20000 loop INSERT INTO users (user_id,username, email, password, birthdate, photo_url) VALUES (concat('',counter),concat('users',counter),concat('users',counter,'@gmail.com'),'$2y$12$XLbi8bFbqRlsuzERzeRZTehyZxM8zW9NPphiHEdyuAeL8Uihl0kfO','1998-1-1','https://live.staticflickr.com/8172/8066465258_2c7eb75964_m.jpg'); counter := counter + 1; end loop; END; $$ - LANGUAGE PLPGSQL; - - + LANGUAGE PLPGSQL; \ No newline at end of file From 89a9b237ab021e3988121e0b8e294e9d7bc27497 Mon Sep 17 00:00:00 2001 From: madoooaboelazaiem Date: Mon, 28 Jun 2021 22:02:30 +0200 Subject: [PATCH 3/6] test: ignored population test --- .../src/test/java/org/sab/user/commands/UserAppTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/user_app/src/test/java/org/sab/user/commands/UserAppTest.java b/apps/user_app/src/test/java/org/sab/user/commands/UserAppTest.java index 9f8da15b..cf5cf16d 100644 --- a/apps/user_app/src/test/java/org/sab/user/commands/UserAppTest.java +++ b/apps/user_app/src/test/java/org/sab/user/commands/UserAppTest.java @@ -5,6 +5,7 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.FixMethodOrder; +import org.junit.Ignore; import org.junit.Test; import org.junit.runners.MethodSorters; import org.sab.arango.Arango; @@ -43,7 +44,7 @@ public static void connectToDbs() { } } - @Test + @Ignore public void T00_UserPopulationData() throws SQLException, EnvironmentVariableNotLoaded { try { int resultSet = PostgresConnection.countRows(); From 91e7ff9d7d2b9c1c3952090b8a21e18d6a6229c2 Mon Sep 17 00:00:00 2001 From: Lujine Date: Tue, 29 Jun 2021 12:41:20 +0200 Subject: [PATCH 4/6] feat: add arango population script --- .../java/org/sab/arango/PopulateArango.java | 507 ++++++++++++++++++ 1 file changed, 507 insertions(+) create mode 100644 libs/arango/src/main/java/org/sab/arango/PopulateArango.java diff --git a/libs/arango/src/main/java/org/sab/arango/PopulateArango.java b/libs/arango/src/main/java/org/sab/arango/PopulateArango.java new file mode 100644 index 00000000..88f5dffb --- /dev/null +++ b/libs/arango/src/main/java/org/sab/arango/PopulateArango.java @@ -0,0 +1,507 @@ +package org.sab.arango; + +import com.arangodb.entity.BaseDocument; +import com.arangodb.entity.BaseEdgeDocument; +import org.sab.models.CollectionNames; +import org.sab.models.CommentAttributes; +import org.sab.models.SubThreadAttributes; +import org.sab.models.ThreadAttributes; +import org.sab.models.report.SubThreadReportAttributes; +import org.sab.models.user.UserAttributes; + +import java.sql.Timestamp; +import java.time.LocalDateTime; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class PopulateArango { + + private static final String dbName = System.getenv("ARANGO_DB"); + + private static final String threadDescription = ThreadAttributes.DESCRIPTION.getDb(); + private static final String threadCreator = ThreadAttributes.CREATOR_ID.getDb(); + private static final String threadFollowers = ThreadAttributes.NUM_OF_FOLLOWERS.getDb(); + private static final String threadDate = ThreadAttributes.DATE_CREATED.getDb(); + + private static final String subThreadParentThread = SubThreadAttributes.PARENT_THREAD_ID.getDb(); + private static final String subThreadTitle = SubThreadAttributes.TITLE.getDb(); + private static final String subThreadCreator = SubThreadAttributes.CREATOR_ID.getDb(); + private static final String subThreadLikes = SubThreadAttributes.LIKES.getDb(); + private static final String subThreadDislikes = SubThreadAttributes.DISLIKES.getDb(); + private static final String subThreadContent = SubThreadAttributes.CONTENT.getDb(); + private static final String subThreadHasImage = SubThreadAttributes.HAS_IMAGE.getDb(); + private static final String subThreadDate = SubThreadAttributes.DATE_CREATED.getDb(); + + private static final String commentCreatorID = CommentAttributes.CREATOR_ID.getDb(); + private static final String commentContent = CommentAttributes.CONTENT.getDb(); + private static final String commentLikes = CommentAttributes.LIKES.getDb(); + private static final String commentDislikes = CommentAttributes.DISLIKES.getDb(); + private static final String commentDate = CommentAttributes.DISLIKES.getDb(); + private static final String commentParentId = CommentAttributes.PARENT_SUBTHREAD_ID.getDb(); + private static final String commentParentType = CommentAttributes.PARENT_CONTENT_TYPE.getDb(); + + private static final String reportSubThreadId = SubThreadReportAttributes.SUBTHREAD_Id.getDb(); + private static final String reportParentId = SubThreadReportAttributes.PARENT_THREAD_ID.getDb(); + private static final String reportUserName = SubThreadReportAttributes.REPORTER_ID.getDb(); + private static final String reportType = SubThreadReportAttributes.TYPE_OF_REPORT.getDb(); + private static final String reportMessage = SubThreadReportAttributes.REPORT_MSG.getDb(); + private static final String reportDate = SubThreadReportAttributes.DATE_CREATED.getDb(); + + private static final String userIsDeleted = UserAttributes.IS_DELETED.getArangoDb(); + private static final String userNumOfFollowers = UserAttributes.NUM_OF_FOLLOWERS.getArangoDb(); + + private static final String USERS_COLLECTION_NAME = CollectionNames.USER.get(); + + private static final String THREADS_COLLECTION_NAME = CollectionNames.THREAD.get(); + private static final String SUB_THREADS_COLLECTION_NAME = CollectionNames.SUBTHREAD.get(); + private static final String REPORTS_COLLECTION_NAME = CollectionNames.SUBTHREAD_REPORTS.get(); + private static final String COMMENT_COLLECTION_NAME = CollectionNames.COMMENT.get(); + + private static final String THREAD_CONTAIN_SUB_THREAD_COLLECTION_NAME = CollectionNames.THREAD_CONTAIN_SUBTHREAD.get(); + private static final String USER_FOLLOW_USER_COLLECTION_NAME = CollectionNames.USER_FOLLOW_USER.get(); + private static final String USER_BLOCK_USER_COLLECTION_NAME = CollectionNames.USER_BLOCK_USER.get(); + private static final String USER_FOLLOW_THREAD_COLLECTION_NAME = CollectionNames.USER_FOLLOW_THREAD.get(); + private static final String USER_MOD_THREAD_COLLECTION_NAME = CollectionNames.USER_MOD_THREAD.get(); + private static final String USER_CREATE_COMMENT_COLLECTION_NAME = CollectionNames.USER_CREATE_COMMENT.get(); + private static final String CONTENT_COMMENT_COLLECTION_NAME = CollectionNames.CONTENT_COMMENT.get(); + + // TODO from thread + private static final String USER_BOOKMARK_THREAD_COLLECTION_NAME = CollectionNames.USER_BOOKMARK_THREAD.get(); + private static final String USER_BOOKMARK_SUBTHREAD_COLLECTION_NAME = CollectionNames.USER_BOOKMARK_SUBTHREAD.get(); + private static final String USER_LIKE_SUBTHREAD_COLLECTION_NAME = CollectionNames.USER_LIKE_SUBTHREAD.get(); + private static final String USER_DISLIKE_SUBTHREAD_COLLECTION_NAME = CollectionNames.USER_DISLIKE_SUBTHREAD.get(); + private static final String USER_LIKE_COMMENT_COLLECTION_NAME = CollectionNames.USER_LIKE_COMMENT.get(); + private static final String USER_DISLIKE_COMMENT_COLLECTION_NAME = CollectionNames.USER_DISLIKE_COMMENT.get(); + + private static final String USER_BANNED_FROM_THREAD_COLLECTION_NAME = CollectionNames.USER_BANNED_FROM_THREAD.get(); + + public static void populate(Arango arango) { + arango.createDatabaseIfNotExists(dbName); + arango.createCollectionIfNotExists(dbName, THREADS_COLLECTION_NAME, false); + arango.createCollectionIfNotExists(dbName, SUB_THREADS_COLLECTION_NAME, false); + arango.createCollectionIfNotExists(dbName, USERS_COLLECTION_NAME, false); + arango.createCollectionIfNotExists(dbName, COMMENT_COLLECTION_NAME, false); + arango.createCollectionIfNotExists(dbName, REPORTS_COLLECTION_NAME, false); + + arango.createCollectionIfNotExists(dbName, THREAD_CONTAIN_SUB_THREAD_COLLECTION_NAME, true); + arango.createCollectionIfNotExists(dbName, USER_FOLLOW_USER_COLLECTION_NAME, true); + arango.createCollectionIfNotExists(dbName, USER_BLOCK_USER_COLLECTION_NAME, true); + arango.createCollectionIfNotExists(dbName, USER_FOLLOW_THREAD_COLLECTION_NAME, true); + arango.createCollectionIfNotExists(dbName, USER_MOD_THREAD_COLLECTION_NAME, true); + arango.createCollectionIfNotExists(dbName, CONTENT_COMMENT_COLLECTION_NAME, true); + arango.createCollectionIfNotExists(dbName, USER_CREATE_COMMENT_COLLECTION_NAME, true); + arango.createCollectionIfNotExists(dbName, USER_BOOKMARK_THREAD_COLLECTION_NAME, true); + arango.createCollectionIfNotExists(dbName, USER_BOOKMARK_SUBTHREAD_COLLECTION_NAME, true); + arango.createCollectionIfNotExists(dbName, USER_LIKE_SUBTHREAD_COLLECTION_NAME, true); + arango.createCollectionIfNotExists(dbName, USER_DISLIKE_SUBTHREAD_COLLECTION_NAME, true); + arango.createCollectionIfNotExists(dbName, USER_LIKE_COMMENT_COLLECTION_NAME, true); + arango.createCollectionIfNotExists(dbName, USER_DISLIKE_COMMENT_COLLECTION_NAME, true); + arango.createCollectionIfNotExists(dbName, USER_BANNED_FROM_THREAD_COLLECTION_NAME, true); + + String[] subThreads; + String[] threads; + String[] users; + String[] comments; + String[] reports; + + HashMap threadCreatorMap = new HashMap<>(); + HashMap subThreadParentMap = new HashMap<>(); + + HashMap threadNumFollowMap = new HashMap<>(); + HashMap subThreadNumLikeMap = new HashMap<>(); + HashMap subThreadNumDislikeMap = new HashMap<>(); + HashMap commentNumLikeMap = new HashMap<>(); + HashMap commentNumDislikeMap = new HashMap<>(); + + int userNum = 100, threadNum = 200, subthreadNum = 200, commentNum = 200, reportsNum = 20; + int usersToFollowNum = 5, threadsToFollowNum = 5, threadsToBookmarkNum = 5, numToBan = 50; + int usersToBlockNum = 5, subThreadsToBookmarkNum = 5, subThreadsToLikeNum = 5, subThreadsToDislikeNum = 5; + int commentsToLikeNum = 5, commentsToDislikeNum = 5; + + userNum++; + + // Dummy Data + users = new String[userNum]; + threads = new String[threadNum]; + subThreads = new String[subthreadNum]; + comments = new String[commentNum]; + reports = new String[reportsNum]; + + for (int i = 0; i < userNum - 1; i++) { + BaseDocument user = new BaseDocument(); + // TODO add the rest of the attributes + String userName = "users" + i; + user.setKey(userName); + user.addAttribute(userIsDeleted, false); + user.addAttribute(userNumOfFollowers, 0); + arango.createDocument(dbName, USERS_COLLECTION_NAME, user); + users[i] = userName; + } + BaseDocument user = new BaseDocument(); + // TODO add the rest of the attributes + String userName = "Ronic"; + user.setKey(userName); + user.addAttribute(userIsDeleted, false); + user.addAttribute(userNumOfFollowers, 0); + arango.createDocument(dbName, USERS_COLLECTION_NAME, user); + users[userNum-1] = userName; + System.out.println("Done with " + USERS_COLLECTION_NAME); + + BaseEdgeDocument edgeDocument; + for (int i = 0; i < threadNum; i++) { + String threadName = "thread" + i; + String creator = users[i % users.length]; + + BaseDocument thread = new BaseDocument(); + thread.setKey(threadName); + thread.addAttribute(threadDescription, "thread description" + i); + thread.addAttribute(threadCreator, creator); + thread.addAttribute(threadFollowers, 0); + thread.addAttribute(threadDate, Timestamp.valueOf(LocalDateTime.now())); + arango.createDocument(dbName, THREADS_COLLECTION_NAME, thread); + threads[i] = threadName; + + edgeDocument = new BaseEdgeDocument(); + edgeDocument.setFrom(USERS_COLLECTION_NAME + "/" + creator); + edgeDocument.setTo(THREADS_COLLECTION_NAME + "/" + threadName); + + threadCreatorMap.put(threadName, creator); + + arango.createEdgeDocument(dbName, USER_MOD_THREAD_COLLECTION_NAME, edgeDocument); + } + System.out.println("Done with " + THREADS_COLLECTION_NAME); + + for (int i = 0; i < subthreadNum; i++) { + BaseDocument subThread = new BaseDocument(); + String threadName = threads[i % threads.length]; + subThread.addAttribute(subThreadParentThread, threadName); + subThread.addAttribute(subThreadTitle, "title" + i); + subThread.addAttribute(subThreadCreator, users[i % users.length]); + subThread.addAttribute(subThreadLikes, 0); + subThread.addAttribute(subThreadDislikes, 0); + subThread.addAttribute(subThreadContent, "content"); + subThread.addAttribute(subThreadHasImage, false); + subThread.addAttribute(subThreadDate, Timestamp.valueOf(LocalDateTime.now())); + BaseDocument created1 = arango.createDocument(dbName, SUB_THREADS_COLLECTION_NAME, subThread); + subThreads[i] = created1.getKey(); + + edgeDocument = new BaseEdgeDocument(); + edgeDocument.setTo(SUB_THREADS_COLLECTION_NAME + "/" + subThreads[i]); + edgeDocument.setFrom(THREADS_COLLECTION_NAME + "/" + threadName); + + arango.createEdgeDocument(dbName, THREAD_CONTAIN_SUB_THREAD_COLLECTION_NAME, edgeDocument); + subThreadParentMap.put(subThreads[i], threadName); + } + System.out.println("Done with " + SUB_THREADS_COLLECTION_NAME + " and " + THREAD_CONTAIN_SUB_THREAD_COLLECTION_NAME); + + for (int i = 0; i < reportsNum; i++) { + BaseDocument report = new BaseDocument(); + String subThreadId = subThreads[i % subThreads.length]; + String parentThreadId = subThreadParentMap.get(subThreadId); + + String typeOfReport = "SUBTHREAD_REPORT"; + String message = "This is a report"; + report.addAttribute(reportSubThreadId, subThreadId); + report.addAttribute(reportParentId, parentThreadId); + report.addAttribute(reportUserName, users[(i + 100) % users.length]); + report.addAttribute(reportType, typeOfReport); + report.addAttribute(reportMessage, message); + report.addAttribute(reportDate, Timestamp.valueOf(LocalDateTime.now())); + BaseDocument created1 = arango.createDocument(dbName, REPORTS_COLLECTION_NAME, report); + reports[i] = created1.getKey(); + } + System.out.println("Done with " + REPORTS_COLLECTION_NAME); + + for (int i = 0; i < commentNum; i++) { + + BaseDocument comment = new BaseDocument(); + String subthreadName = subThreads[i]; + String creatorId = users[i % users.length]; + + comment.addAttribute(commentParentId, subthreadName); + comment.addAttribute(commentCreatorID, creatorId); + comment.addAttribute(commentContent, "This is a comment"); + comment.addAttribute(commentParentType, "subthread"); + comment.addAttribute(commentLikes, 0); + comment.addAttribute(commentDislikes, 0); + java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis()); + comment.addAttribute(commentDate, sqlDate); + + BaseDocument created1 = arango.createDocument(dbName, COMMENT_COLLECTION_NAME, comment); + comments[i] = created1.getKey(); + + edgeDocument = new BaseEdgeDocument(); + edgeDocument.setTo(USERS_COLLECTION_NAME + "/" + creatorId); + edgeDocument.setFrom(COMMENT_COLLECTION_NAME + "/" + comments[i]); + arango.createEdgeDocument(dbName, USER_CREATE_COMMENT_COLLECTION_NAME, edgeDocument); + + edgeDocument = new BaseEdgeDocument(); + edgeDocument.setTo(SUB_THREADS_COLLECTION_NAME + "/" + subthreadName); + edgeDocument.setFrom(COMMENT_COLLECTION_NAME + "/" + comments[i]); + arango.createEdgeDocument(dbName, CONTENT_COMMENT_COLLECTION_NAME, edgeDocument); + } + + System.out.println("Done with " + COMMENT_COLLECTION_NAME); + + + for (int i = 0; i < users.length; i++) { + int j = 0; + + int min = 0, max = threadNum; + List range = IntStream.range(min, max).boxed() + .collect(Collectors.toCollection(ArrayList::new)); + Collections.shuffle(range); + + while (j < threadsToFollowNum) { + edgeDocument = new BaseEdgeDocument(); + + String threadName = threads[range.get(j)]; + String from = USERS_COLLECTION_NAME + "/" + users[i]; + String to = THREADS_COLLECTION_NAME + "/" + threadName; + edgeDocument.setFrom(from); + edgeDocument.setTo(to); + + arango.createEdgeDocument(dbName, USER_FOLLOW_THREAD_COLLECTION_NAME, edgeDocument); + threadNumFollowMap.put(threadName, threadNumFollowMap.getOrDefault(threadName, 0) + 1); + j++; + } + } + System.out.println("Done with " + USER_FOLLOW_THREAD_COLLECTION_NAME); + + for (int i = 0; i < users.length; i++) { + int j = 0; + + int min = 0, max = userNum; + List range = IntStream.range(min, max).boxed() + .collect(Collectors.toCollection(ArrayList::new)); + range.remove(i); + Collections.shuffle(range); + + while (j < usersToFollowNum) { + edgeDocument = new BaseEdgeDocument(); + + + edgeDocument.setFrom(USERS_COLLECTION_NAME + "/" + users[i]); + edgeDocument.setTo(USERS_COLLECTION_NAME + "/" + users[range.get(j)]); + arango.createEdgeDocument(dbName, USER_FOLLOW_USER_COLLECTION_NAME, edgeDocument); + j++; + } + } + System.out.println("Done with " + USER_FOLLOW_USER_COLLECTION_NAME); + + for (int i = 0; i < users.length; i++) { + int j = 0; + + int min = 0, max = userNum; + List range = IntStream.range(min, max).boxed() + .collect(Collectors.toCollection(ArrayList::new)); + range.remove(i); + Collections.shuffle(range); + + while (j < usersToBlockNum) { + edgeDocument = new BaseEdgeDocument(); + + edgeDocument.setFrom(USERS_COLLECTION_NAME + "/" + users[i]); + edgeDocument.setTo(USERS_COLLECTION_NAME + "/" + users[range.get(j)]); + arango.createEdgeDocument(dbName, USER_BLOCK_USER_COLLECTION_NAME, edgeDocument); + j++; + } + } + System.out.println("Done with " + USER_BLOCK_USER_COLLECTION_NAME); + + for (int i = 0; i < users.length; i++) { + int j = 0; + + int min = 0, max = threadNum; + List range = IntStream.range(min, max).boxed() + .collect(Collectors.toCollection(ArrayList::new)); + Collections.shuffle(range); + + while (j < threadsToBookmarkNum) { + edgeDocument = new BaseEdgeDocument(); + + String from = USERS_COLLECTION_NAME + "/" + users[i]; + String to = THREADS_COLLECTION_NAME + "/" + threads[range.get(j)]; + edgeDocument.setFrom(from); + edgeDocument.setTo(to); + + arango.createEdgeDocument(dbName, USER_BOOKMARK_THREAD_COLLECTION_NAME, edgeDocument); + j++; + } + } + System.out.println("Done with " + USER_BOOKMARK_THREAD_COLLECTION_NAME); + + for (int i = 0; i < users.length; i++) { + int j = 0; + + int min = 0, max = threadNum; + List range = IntStream.range(min, max).boxed() + .collect(Collectors.toCollection(ArrayList::new)); + Collections.shuffle(range); + + while (j < subThreadsToBookmarkNum) { + edgeDocument = new BaseEdgeDocument(); + + String from = USERS_COLLECTION_NAME + "/" + users[i]; + String to = SUB_THREADS_COLLECTION_NAME + "/" + threads[range.get(j)]; + edgeDocument.setFrom(from); + edgeDocument.setTo(to); + + arango.createEdgeDocument(dbName, USER_BOOKMARK_SUBTHREAD_COLLECTION_NAME, edgeDocument); + j++; + } + } + System.out.println("Done with " + USER_BOOKMARK_SUBTHREAD_COLLECTION_NAME); + + for (int i = 0; i < users.length; i++) { + int j = 0; + + int min = 0, max = subthreadNum; + List range = IntStream.range(min, max).boxed() + .collect(Collectors.toCollection(ArrayList::new)); + Collections.shuffle(range); + + while (j < subThreadsToLikeNum) { + edgeDocument = new BaseEdgeDocument(); + + String subthreadName = subThreads[range.get(j)]; + String from = USERS_COLLECTION_NAME + "/" + users[i]; + String to = SUB_THREADS_COLLECTION_NAME + "/" + subthreadName; + edgeDocument.setFrom(from); + edgeDocument.setTo(to); + + arango.createEdgeDocument(dbName, USER_LIKE_SUBTHREAD_COLLECTION_NAME, edgeDocument); + subThreadNumLikeMap.put(subthreadName, subThreadNumLikeMap.getOrDefault(subthreadName, 0) + 1); + j++; + } + } + System.out.println("Done with " + USER_LIKE_SUBTHREAD_COLLECTION_NAME); + + for (int i = 0; i < users.length; i++) { + int j = 0; + + int min = 0, max = subthreadNum; + List range = IntStream.range(min, max).boxed() + .collect(Collectors.toCollection(ArrayList::new)); + Collections.shuffle(range); + + while (j < subThreadsToDislikeNum) { + edgeDocument = new BaseEdgeDocument(); + + String subthreadName = subThreads[range.get(j)]; + String from = USERS_COLLECTION_NAME + "/" + users[i]; + String to = SUB_THREADS_COLLECTION_NAME + "/" + subthreadName; + edgeDocument.setFrom(from); + edgeDocument.setTo(to); + + arango.createEdgeDocument(dbName, USER_DISLIKE_SUBTHREAD_COLLECTION_NAME, edgeDocument); + subThreadNumDislikeMap.put(subthreadName, subThreadNumDislikeMap.getOrDefault(subthreadName, 0) + 1); + j++; + } + } + System.out.println("Done with " + USER_DISLIKE_SUBTHREAD_COLLECTION_NAME); + + for (int i = 0; i < users.length; i++) { + int j = 0; + + int min = 0, max = commentNum; + List range = IntStream.range(min, max).boxed() + .collect(Collectors.toCollection(ArrayList::new)); + Collections.shuffle(range); + + while (j < commentsToLikeNum) { + edgeDocument = new BaseEdgeDocument(); + + String commentId = comments[range.get(j)]; + String from = USERS_COLLECTION_NAME + "/" + users[i]; + String to = COMMENT_COLLECTION_NAME + "/" + commentId; + edgeDocument.setFrom(from); + edgeDocument.setTo(to); + + arango.createEdgeDocument(dbName, USER_LIKE_COMMENT_COLLECTION_NAME, edgeDocument); + commentNumLikeMap.put(commentId, commentNumLikeMap.getOrDefault(commentId, 0) + 1); + j++; + } + } + System.out.println("Done with " + USER_LIKE_COMMENT_COLLECTION_NAME); + + for (int i = 0; i < users.length; i++) { + int j = 0; + + int min = 0, max = commentNum; + List range = IntStream.range(min, max).boxed() + .collect(Collectors.toCollection(ArrayList::new)); + Collections.shuffle(range); + + while (j < commentsToDislikeNum) { + edgeDocument = new BaseEdgeDocument(); + + String commentId = comments[range.get(j)]; + String from = USERS_COLLECTION_NAME + "/" + users[i]; + String to = COMMENT_COLLECTION_NAME + "/" + commentId; + edgeDocument.setFrom(from); + edgeDocument.setTo(to); + + arango.createEdgeDocument(dbName, USER_DISLIKE_COMMENT_COLLECTION_NAME, edgeDocument); + commentNumDislikeMap.put(commentId, commentNumDislikeMap.getOrDefault(commentId, 0) + 1); + j++; + } + } + System.out.println("Done with " + USER_DISLIKE_COMMENT_COLLECTION_NAME); + + int min = 0, max = userNum; + List range = IntStream.range(min, max).boxed() + .collect(Collectors.toCollection(ArrayList::new)); + Collections.shuffle(range); + for (int i = 0; i < numToBan; i++) { + + String toBan = users[range.get(i)]; + String thread; + String mod; + + do { + thread = threads[(int) (Math.random() * threads.length)]; + mod = threadCreatorMap.get(thread); + } while (mod.equals(toBan)); + + edgeDocument = new BaseEdgeDocument(); +// System.out.println("Ban: " + toBan + "from thread: " + thread + " by mod: " + mod); + + String from = USERS_COLLECTION_NAME + "/" + toBan; + String to = THREADS_COLLECTION_NAME + "/" + thread; + edgeDocument.setFrom(from); + edgeDocument.setTo(to); + + arango.createEdgeDocument(dbName, USER_BANNED_FROM_THREAD_COLLECTION_NAME, edgeDocument); + } + + System.out.println("Done with " + USER_BANNED_FROM_THREAD_COLLECTION_NAME); + + threadNumFollowMap.forEach((key, value) -> { + arango.updateDocument(dbName, THREADS_COLLECTION_NAME, Map.of(threadFollowers, value), key); + }); + subThreadNumLikeMap.forEach((key, value) -> { + arango.updateDocument(dbName, SUB_THREADS_COLLECTION_NAME, Map.of(subThreadLikes, value), key); + }); + subThreadNumDislikeMap.forEach((key, value) -> { + arango.updateDocument(dbName, SUB_THREADS_COLLECTION_NAME, Map.of(subThreadDislikes, value), key); + }); + commentNumLikeMap.forEach((key, value) -> { + arango.updateDocument(dbName, COMMENT_COLLECTION_NAME, Map.of(commentLikes, value), key); + }); + commentNumDislikeMap.forEach((key, value) -> { + arango.updateDocument(dbName, COMMENT_COLLECTION_NAME, Map.of(commentDislikes, value), key); + }); + + } + + public static void main(String[] args) { + Arango arango = Arango.getConnectedInstance(); + arango.dropDatabase(dbName); + populate(arango); + System.out.println("Totally Done"); + arango = null; + } + +} From 2635421fba0cd1f8c227509bfbd997c7240e1f33 Mon Sep 17 00:00:00 2001 From: Lujine Date: Tue, 29 Jun 2021 12:48:20 +0200 Subject: [PATCH 5/6] refactor: remove insertion --- .../java/org/sab/arango/PopulateArango.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/libs/arango/src/main/java/org/sab/arango/PopulateArango.java b/libs/arango/src/main/java/org/sab/arango/PopulateArango.java index 88f5dffb..6eb7fdc9 100644 --- a/libs/arango/src/main/java/org/sab/arango/PopulateArango.java +++ b/libs/arango/src/main/java/org/sab/arango/PopulateArango.java @@ -119,7 +119,7 @@ public static void populate(Arango arango) { int usersToBlockNum = 5, subThreadsToBookmarkNum = 5, subThreadsToLikeNum = 5, subThreadsToDislikeNum = 5; int commentsToLikeNum = 5, commentsToDislikeNum = 5; - userNum++; +// userNum++; // Dummy Data users = new String[userNum]; @@ -128,7 +128,7 @@ public static void populate(Arango arango) { comments = new String[commentNum]; reports = new String[reportsNum]; - for (int i = 0; i < userNum - 1; i++) { + for (int i = 0; i < userNum; i++) { BaseDocument user = new BaseDocument(); // TODO add the rest of the attributes String userName = "users" + i; @@ -138,14 +138,13 @@ public static void populate(Arango arango) { arango.createDocument(dbName, USERS_COLLECTION_NAME, user); users[i] = userName; } - BaseDocument user = new BaseDocument(); - // TODO add the rest of the attributes - String userName = "Ronic"; - user.setKey(userName); - user.addAttribute(userIsDeleted, false); - user.addAttribute(userNumOfFollowers, 0); - arango.createDocument(dbName, USERS_COLLECTION_NAME, user); - users[userNum-1] = userName; +// BaseDocument user = new BaseDocument(); +// String userName = "Ronic"; +// user.setKey(userName); +// user.addAttribute(userIsDeleted, false); +// user.addAttribute(userNumOfFollowers, 0); +// arango.createDocument(dbName, USERS_COLLECTION_NAME, user); +// users[userNum-1] = userName; System.out.println("Done with " + USERS_COLLECTION_NAME); BaseEdgeDocument edgeDocument; From 026202391213015cd95961b4089f048907730a3e Mon Sep 17 00:00:00 2001 From: Lujine Date: Tue, 29 Jun 2021 13:33:41 +0200 Subject: [PATCH 6/6] fix: change edge collections to and from --- .../src/main/java/org/sab/arango/PopulateArango.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/arango/src/main/java/org/sab/arango/PopulateArango.java b/libs/arango/src/main/java/org/sab/arango/PopulateArango.java index 6eb7fdc9..fb7cef3a 100644 --- a/libs/arango/src/main/java/org/sab/arango/PopulateArango.java +++ b/libs/arango/src/main/java/org/sab/arango/PopulateArango.java @@ -231,13 +231,13 @@ public static void populate(Arango arango) { comments[i] = created1.getKey(); edgeDocument = new BaseEdgeDocument(); - edgeDocument.setTo(USERS_COLLECTION_NAME + "/" + creatorId); - edgeDocument.setFrom(COMMENT_COLLECTION_NAME + "/" + comments[i]); + edgeDocument.setFrom(USERS_COLLECTION_NAME + "/" + creatorId); + edgeDocument.setTo(COMMENT_COLLECTION_NAME + "/" + comments[i]); arango.createEdgeDocument(dbName, USER_CREATE_COMMENT_COLLECTION_NAME, edgeDocument); edgeDocument = new BaseEdgeDocument(); - edgeDocument.setTo(SUB_THREADS_COLLECTION_NAME + "/" + subthreadName); - edgeDocument.setFrom(COMMENT_COLLECTION_NAME + "/" + comments[i]); + edgeDocument.setFrom(SUB_THREADS_COLLECTION_NAME + "/" + subthreadName); + edgeDocument.setTo(COMMENT_COLLECTION_NAME + "/" + comments[i]); arango.createEdgeDocument(dbName, CONTENT_COMMENT_COLLECTION_NAME, edgeDocument); }