From bb4027fe4cd05f43b4bc6dde3fa2cd65bb4065af Mon Sep 17 00:00:00 2001 From: Trip-kun Date: Sun, 14 Jan 2024 02:02:50 -0500 Subject: [PATCH 1/3] clean(managers): Split run method of DataManager into saveAndRemoveAllXXXDatas() methods for clarity. In the future, may create a generic DataMap object that allows templating a whole class to replace many of the repetitive methods found in DataManager and it's implementations --- .../oatmeal/managers/DataManager.java | 210 +++++++++--------- 1 file changed, 105 insertions(+), 105 deletions(-) diff --git a/src/main/java/wtf/triplapeeck/oatmeal/managers/DataManager.java b/src/main/java/wtf/triplapeeck/oatmeal/managers/DataManager.java index 3f83467..2c4200b 100644 --- a/src/main/java/wtf/triplapeeck/oatmeal/managers/DataManager.java +++ b/src/main/java/wtf/triplapeeck/oatmeal/managers/DataManager.java @@ -85,23 +85,118 @@ public synchronized MemberData getMemberData(String id) { memberData.request(); return memberData; } - public synchronized void saveAll() { + public synchronized void saveAll(boolean remove =false) { temp.addAll(guildCache.keySet()); for (String key: temp) { - saveGuildData(key, false); + saveGuildData(key, remove); } + temp.clear(); temp.addAll(userCache.keySet()); for (String key: temp) { - saveUserData(key, false); + saveUserData(key, remove); } + temp.clear(); temp.addAll(channelCache.keySet()); for (String key: temp) { - saveChannelData(key, false); + saveChannelData(key, remove); } + temp.clear(); temp.addAll(memberCache.keySet()); for (String key: temp) { - saveMemberData(key, false); + saveMemberData(key, remove); + } + temp.clear(); + } + public synchronized void saveAll() { + saveAll(false); + } + public synchronized void saveAndRemoveAllGuildDatas() { + for (String key: guildCache.keySet()) { + GuildData guildData = guildCache.get(key); + if (guildData.getEpoch()!=0 && guildData.getAccessCount()==0) { + if (!guildData.getSaved() ) { + saveGuildData(key, false); + guildData.saved(); + } + if (Instant.now().getEpochSecond() > 30 + guildData.getEpoch()) { + temp.add(key); + } + } else { + guildData.resetEpoch(); + } + } + for (String key: temp) { + GuildData guildData = guildCache.get(key); + if (guildData.getAccessCount()==0) { + saveGuildData(key, true); + } + } + temp.clear(); + } + public synchronized void saveAndRemoveAllUserDatas() { + for (String key: userCache.keySet()) { + UserData userData = userCache.get(key); + if (userData.getEpoch()!=0 && userData.getAccessCount()==0) { + if (!userData.getSaved()) { + saveUserData(key, false); + userData.saved(); + } + if (Instant.now().getEpochSecond()>30 + userData.getEpoch()) { + temp.add(key); + } + } else { + userData.resetEpoch(); + } + } + for (String key: temp) { + UserData userData = userCache.get(key); + if (userData.getAccessCount()==0) { + saveUserData(key, true); + } + } + temp.clear(); + } + public synchronized void saveAndRemoveAllChannelDatas() { + for (String key: channelCache.keySet()) { + ChannelData channelData = channelCache.get(key); + if (channelData.getEpoch()!=0 && channelData.getAccessCount()==0) { + if (!channelData.getSaved()) { + saveChannelData(key, false); + channelData.saved(); + } + if (Instant.now().getEpochSecond()>30 + channelData.getEpoch()) { + temp.add(key); + } + } + } + for (String key: temp) { + ChannelData channelData = channelCache.get(key); + if (channelData.getAccessCount()==0) { + saveChannelData(key, true); + } } + temp.clear(); + } + public synchronized void saveAndRemoveAllMemberDatas() { + for (String key: memberCache.keySet()) { + MemberData memberData = memberCache.get(key); + if (memberData.getEpoch()!=0 && memberData.getAccessCount()==0) { + if (!memberData.getSaved()) { + saveMemberData(key, false); + memberData.saved(); + } + if (Instant.now().getEpochSecond()>30 + memberData.getEpoch()) { + temp.add(key); + } + } + } + for (String key: temp) { + MemberData memberData = memberCache.get(key); + if (memberData.getAccessCount()==0) { + saveMemberData(key, true); + } + } + temp.clear(); } @Override public void run() { @@ -114,26 +209,7 @@ public void run() { throw new RuntimeException(e); } } - temp.addAll(guildCache.keySet()); - for (String key: temp) { - saveGuildData(key, true); - } - temp.clear(); - temp.addAll(userCache.keySet()); - for (String key: temp) { - saveUserData(key, true); - } - temp.clear(); - temp.addAll(channelCache.keySet()); - for (String key: temp) { - saveChannelData(key, true); - } - temp.clear(); - temp.addAll(memberCache.keySet()); - for (String key: temp) { - saveMemberData(key, true); - } - temp.clear(); + saveAll(true); break; } @@ -142,86 +218,10 @@ public void run() { } catch (InterruptedException e) { throw new RuntimeException(e); } - for (String key: guildCache.keySet()) { - GuildData guildData = guildCache.get(key); - if (guildData.getEpoch()!=0 && guildData.getAccessCount()==0) { - if (!guildData.getSaved() ) { - saveGuildData(key, false); - guildData.saved(); - } - if (Instant.now().getEpochSecond() > 30 + guildData.getEpoch()) { - temp.add(key); - } - } else { - guildData.resetEpoch(); - } - } - for (String key: temp) { - GuildData guildData = guildCache.get(key); - if (guildData.getAccessCount()==0) { - saveGuildData(key, true); - } - } - temp.clear(); - for (String key: userCache.keySet()) { - UserData userData = userCache.get(key); - if (userData.getEpoch()!=0 && userData.getAccessCount()==0) { - if (!userData.getSaved()) { - saveUserData(key, false); - userData.saved(); - } - if (Instant.now().getEpochSecond()>30 + userData.getEpoch()) { - temp.add(key); - } - } else { - userData.resetEpoch(); - } - } - for (String key: temp) { - UserData userData = userCache.get(key); - if (userData.getAccessCount()==0) { - saveUserData(key, true); - } - } - temp.clear(); - for (String key: channelCache.keySet()) { - ChannelData channelData = channelCache.get(key); - if (channelData.getEpoch()!=0 && channelData.getAccessCount()==0) { - if (!channelData.getSaved()) { - saveChannelData(key, false); - channelData.saved(); - } - if (Instant.now().getEpochSecond()>30 + channelData.getEpoch()) { - temp.add(key); - } - } - } - for (String key: temp) { - ChannelData channelData = channelCache.get(key); - if (channelData.getAccessCount()==0) { - saveChannelData(key, true); - } - } - temp.clear(); - for (String key: memberCache.keySet()) { - MemberData memberData = memberCache.get(key); - if (memberData.getEpoch()!=0 && memberData.getAccessCount()==0) { - if (!memberData.getSaved()) { - saveMemberData(key, false); - memberData.saved(); - } - if (Instant.now().getEpochSecond()>30 + memberData.getEpoch()) { - temp.add(key); - } - } - } - for (String key: temp) { - MemberData memberData = memberCache.get(key); - if (memberData.getAccessCount()==0) { - saveMemberData(key, true); - } - } - temp.clear(); + saveAndRemoveAllGuildDatas(); + saveAndRemoveAllUserDatas(); + saveAndRemoveAllChannelDatas(); + saveAndRemoveAllMemberDatas(); //TODO: REPEAT FOR ALL ENTITY TYPES } } From 2b14ce688145414341957e56f48fc6c8edd120db Mon Sep 17 00:00:00 2001 From: Trip-kun Date: Sun, 14 Jan 2024 02:05:32 -0500 Subject: [PATCH 2/3] fix(managers): Java doesn't have default args here --- src/main/java/wtf/triplapeeck/oatmeal/managers/DataManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/wtf/triplapeeck/oatmeal/managers/DataManager.java b/src/main/java/wtf/triplapeeck/oatmeal/managers/DataManager.java index 2c4200b..1b17467 100644 --- a/src/main/java/wtf/triplapeeck/oatmeal/managers/DataManager.java +++ b/src/main/java/wtf/triplapeeck/oatmeal/managers/DataManager.java @@ -85,7 +85,7 @@ public synchronized MemberData getMemberData(String id) { memberData.request(); return memberData; } - public synchronized void saveAll(boolean remove =false) { + public synchronized void saveAll(boolean remove) { temp.addAll(guildCache.keySet()); for (String key: temp) { saveGuildData(key, remove); From 3de250b20a7e3fda235d307276c95623ccb6c9cb Mon Sep 17 00:00:00 2001 From: Trip-kun Date: Sun, 14 Jan 2024 02:27:09 -0500 Subject: [PATCH 3/3] fix(managers): Remove race condition with explicit synchronized call --- .../oatmeal/managers/DataManager.java | 72 ++++++++++--------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/src/main/java/wtf/triplapeeck/oatmeal/managers/DataManager.java b/src/main/java/wtf/triplapeeck/oatmeal/managers/DataManager.java index 1b17467..c4ba80f 100644 --- a/src/main/java/wtf/triplapeeck/oatmeal/managers/DataManager.java +++ b/src/main/java/wtf/triplapeeck/oatmeal/managers/DataManager.java @@ -113,16 +113,18 @@ public synchronized void saveAll() { public synchronized void saveAndRemoveAllGuildDatas() { for (String key: guildCache.keySet()) { GuildData guildData = guildCache.get(key); - if (guildData.getEpoch()!=0 && guildData.getAccessCount()==0) { - if (!guildData.getSaved() ) { - saveGuildData(key, false); - guildData.saved(); - } - if (Instant.now().getEpochSecond() > 30 + guildData.getEpoch()) { - temp.add(key); + synchronized (guildData) { + if (guildData.getEpoch() != 0 && guildData.getAccessCount() == 0) { + if (!guildData.getSaved()) { + saveGuildData(key, false); + guildData.saved(); + } + if (Instant.now().getEpochSecond() > 30 + guildData.getEpoch()) { + temp.add(key); + } + } else { + guildData.resetEpoch(); } - } else { - guildData.resetEpoch(); } } for (String key: temp) { @@ -136,16 +138,18 @@ public synchronized void saveAndRemoveAllGuildDatas() { public synchronized void saveAndRemoveAllUserDatas() { for (String key: userCache.keySet()) { UserData userData = userCache.get(key); - if (userData.getEpoch()!=0 && userData.getAccessCount()==0) { - if (!userData.getSaved()) { - saveUserData(key, false); - userData.saved(); - } - if (Instant.now().getEpochSecond()>30 + userData.getEpoch()) { - temp.add(key); + synchronized(userData) { + if (userData.getEpoch() != 0 && userData.getAccessCount() == 0) { + if (!userData.getSaved()) { + saveUserData(key, false); + userData.saved(); + } + if (Instant.now().getEpochSecond() > 30 + userData.getEpoch()) { + temp.add(key); + } + } else { + userData.resetEpoch(); } - } else { - userData.resetEpoch(); } } for (String key: temp) { @@ -159,13 +163,15 @@ public synchronized void saveAndRemoveAllUserDatas() { public synchronized void saveAndRemoveAllChannelDatas() { for (String key: channelCache.keySet()) { ChannelData channelData = channelCache.get(key); - if (channelData.getEpoch()!=0 && channelData.getAccessCount()==0) { - if (!channelData.getSaved()) { - saveChannelData(key, false); - channelData.saved(); - } - if (Instant.now().getEpochSecond()>30 + channelData.getEpoch()) { - temp.add(key); + synchronized (channelData) { + if (channelData.getEpoch() != 0 && channelData.getAccessCount() == 0) { + if (!channelData.getSaved()) { + saveChannelData(key, false); + channelData.saved(); + } + if (Instant.now().getEpochSecond() > 30 + channelData.getEpoch()) { + temp.add(key); + } } } } @@ -180,13 +186,15 @@ public synchronized void saveAndRemoveAllChannelDatas() { public synchronized void saveAndRemoveAllMemberDatas() { for (String key: memberCache.keySet()) { MemberData memberData = memberCache.get(key); - if (memberData.getEpoch()!=0 && memberData.getAccessCount()==0) { - if (!memberData.getSaved()) { - saveMemberData(key, false); - memberData.saved(); - } - if (Instant.now().getEpochSecond()>30 + memberData.getEpoch()) { - temp.add(key); + synchronized (memberData) { + if (memberData.getEpoch() != 0 && memberData.getAccessCount() == 0) { + if (!memberData.getSaved()) { + saveMemberData(key, false); + memberData.saved(); + } + if (Instant.now().getEpochSecond() > 30 + memberData.getEpoch()) { + temp.add(key); + } } } }