Skip to content

Database rework and Startup refactor#45

Closed
LoadingBG wants to merge 3 commits intoJava-Discord:mainfrom
LoadingBG:loadingbg
Closed

Database rework and Startup refactor#45
LoadingBG wants to merge 3 commits intoJava-Discord:mainfrom
LoadingBG:loadingbg

Conversation

@LoadingBG
Copy link
Contributor

Replace for loops with streams for readability (events/Startup.java) For loops can get messy sometimes so I replaced them with streams for a bit more readability in exchange for speed. However, Startup runs only once so the speed doesn't really matter.
Refactor the code a bit (other/Database.java) There were some questionable decisions on how to write the code, for example the pattern:
Object o = null;
try {
    o = /* something */;
} catch (Exception e) {
    o = /* default */;
}
return o;
instead of
try {
    return /* something */;
} catch (Exception e) {
    return /* default */;
}
or this inconsistency:
void someMethod() {
    Document setValue = new Document(varName, newValue);
    Document update = new Document("$set", setValue);
    collection.updateOne(query, update);
}
void someOtherMethod() {
    collection.updateOne(query, new BasicDBObject("$set", new BasicDBObject(varName, newValue)));
}
Also the methods #getConfigString, #getConfigInt and #getConfigBoolean had this weird line:
if (guildDocExists(guild)) insertGuildDoc(guild);
which I thought was nonsensical because why insert the guild doc if it already exists. I changed it to:
if (!guildDocExists(guild)) insertGuildDoc(guild);
Make other/Database static All the code used random Database instances for one time and then destroyed them. Since Database doesn't hold any internal data, I made all the methods static and made the constructor private.

Copy link
Contributor

@andrewlalis andrewlalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the effort, but we should not change the database methods to be static. This makes testing individual parts of the application logic very difficult when we can no longer inject a mock of the database somewhere.

If you want to really refactor things, I would appreciate if you could figure out a way to remove the database configuration stuff entirely. There is no point in storing the config in a database when a normal JSON or YAML file will work just as well, especially when the config is read-only for 99.99% of the time.

Comment on lines +54 to +59
try (BufferedReader startupMsgReader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("textfiles/startup.txt")))) {
String startup = startupMsgReader
.lines()
.collect(Collectors.joining("\n"))
.replace("{!version}", new Version().getVersion());
System.out.println("\n" + startup);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to refactor this, you might as well do Files.readString(Path) instead of all that buffered reader stuff.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the format of the config?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may have changed, but Dynxsty pasted this a while back. The config is all stored in JSON somewhere in a database, but there is not really a reason to store some simple JSON in a database when we can use GSON or Jackson to read from a file.

Also, I would suggest maybe being a bit patient with this change, because I've also been pushing him to upgrade to java 16 which adds records, which would be really handy for config.

{
    "name": "Java",
    "guild_id": "648956210850299986",
    "welcome_system": {
        "image": {
            "avatar": {
                "avX": 75,
                "avY": 100,
                "avH": 400,
                "avW": 400
            },
            "imgW": 1920,
            "imgH": 600,
            "overlayURL": "https://cdn.discordapp.com/attachments/744899463591624815/827303132098461696/WelcomeOverlay_NoShadow.png",
            "bgURL": "https://cdn.discordapp.com/attachments/744899463591624815/840928322661122048/WelcomeBG.png",
            "primCol": "16777215",
            "secCol": "16720173"
        },
        "join_msg": "Hey {!member}, welcome to **{!server}**!",
        "leave_msg": "**{!membertag}**{!boticon} left the server.",
        "welcome_cid": "650632933165760512",
        "welcome_status": true
    },
    "channels": {
        "report_cid": "760172343971807244",
        "log_cid": "793486198584115270",
        "suggestion_cid": "752535909228085348",
        "submission_cid": "811277208223875112",
        "jam_announcement_cid": "796057592405032990",
        "jam_vote_cid": "796057702635274270"
    },
    "roles": {
        "mute_rid": "736591398849085460",
        "jam_ping_rid": "761320255082463263",
        "jam_admin_rid": "735510411377508411"
    },
    "other": {
        "stats_category": {
            "stats_cid": "773622646876274728",
            "stats_text": "Server · {!membercount} Members"
        },
        "qotw": {
            "dm-qotw": true
        },
        "server_lock": {
            "lock_status": false,
            "lock_count": 0
        },
        "starboard": {
            "starboard_cid": "866099127821402112",
            "starboard_emote": "",
            "starboard_emote2": "🌟",
            "starboard_emote3": "🌠"
        }
    }
}

@LoadingBG LoadingBG closed this Aug 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants