Skip to content

Commit

Permalink
feat: Add support for MongoDB data storage (#250)
Browse files Browse the repository at this point in the history
* Started impl for mongo

* added docs

* refactor of the mongo code, made mongodb artifacts download at run time, tested and working

* complete all change requests

* remove mongo and bson from relocations as they arnt needed

* changed the config

* updated docs

* not null not null not null not null not null not null not null not null not null not null not null not null not null not null not null not null not null not null not null not null not null not null not null not null

---------

Co-authored-by: William <will27528@gmail.com>
  • Loading branch information
ProdPreva1l and WiIIiam278 authored Mar 2, 2024
1 parent eeb5e57 commit 67dddf0
Show file tree
Hide file tree
Showing 16 changed files with 522 additions and 20 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ext {
set 'jedis_version', jedis_version.toString()
set 'mysql_driver_version', mysql_driver_version.toString()
set 'mariadb_driver_version', mariadb_driver_version.toString()
set 'mongodb_driver_version', mongodb_driver_version.toString()
set 'snappy_version', snappy_version.toString()
}

Expand Down
2 changes: 1 addition & 1 deletion bukkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ shadowJar {
relocate 'org.jetbrains', 'net.william278.husksync.libraries'
relocate 'org.intellij', 'net.william278.husksync.libraries'
relocate 'com.zaxxer', 'net.william278.husksync.libraries'
relocate 'de.exlll', 'net.william278.huskclaims.libraries'
relocate 'de.exlll', 'net.william278.husksync.libraries'
relocate 'net.william278.desertwell', 'net.william278.husksync.libraries.desertwell'
relocate 'net.william278.paginedown', 'net.william278.husksync.libraries.paginedown'
relocate 'net.william278.mapdataapi', 'net.william278.husksync.libraries.mapdataapi'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import net.william278.husksync.data.Identifier;
import net.william278.husksync.data.Serializer;
import net.william278.husksync.database.Database;
import net.william278.husksync.database.MongoDbDatabase;
import net.william278.husksync.database.MySqlDatabase;
import net.william278.husksync.event.BukkitEventDispatcher;
import net.william278.husksync.hook.PlanHook;
Expand Down Expand Up @@ -162,7 +163,11 @@ public void onEnable() {

// Initialize the database
initialize(getSettings().getDatabase().getType().getDisplayName() + " database connection", (plugin) -> {
this.database = new MySqlDatabase(this);
this.database = switch (settings.getDatabase().getType()) {
case MYSQL, MARIADB -> new MySqlDatabase(this);
case MONGO -> new MongoDbDatabase(this);
default -> throw new IllegalStateException("Invalid database type");
};
this.database.initialize();
});

Expand Down
1 change: 1 addition & 0 deletions bukkit/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ libraries:
- 'redis.clients:jedis:${jedis_version}'
- 'com.mysql:mysql-connector-j:${mysql_driver_version}'
- 'org.mariadb.jdbc:mariadb-java-client:${mariadb_driver_version}'
- 'org.mongodb:mongodb-driver:${mongodb_driver_version}'
- 'org.xerial.snappy:snappy-java:${snappy_version}'
1 change: 1 addition & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
compileOnly "redis.clients:jedis:$jedis_version"
compileOnly "com.mysql:mysql-connector-j:$mysql_driver_version"
compileOnly "org.mariadb.jdbc:mariadb-java-client:$mariadb_driver_version"
compileOnly "org.mongodb:mongodb-driver:$mongodb_driver_version"
compileOnly "org.xerial.snappy:snappy-java:$snappy_version"

testImplementation "redis.clients:jedis:$jedis_version"
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/net/william278/husksync/HuskSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ final class FailedToLoadException extends IllegalStateException {
HuskSync has failed to load! The plugin will not be enabled and no data will be synchronized.
Please make sure the plugin has been setup correctly (https://william278.net/docs/husksync/setup):
1) Make sure you've entered your MySQL or MariaDB database details correctly in config.yml
1) Make sure you've entered your MySQL, MariaDB or MongoDB database details correctly in config.yml
2) Make sure your Redis server details are also correct in config.yml
3) Make sure your config is up-to-date (https://william278.net/docs/husksync/config-file)
4) Check the error below for more details
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public HuskSyncCommand(@NotNull HuskSync plugin) {
AboutMenu.Credit.of("William278").description("Click to visit website").url("https://william278.net"))
.credits("Contributors",
AboutMenu.Credit.of("HarvelsX").description("Code"),
AboutMenu.Credit.of("HookWoods").description("Code"))
AboutMenu.Credit.of("HookWoods").description("Code"),
AboutMenu.Credit.of("Preva1l").description("Code"))
.credits("Translators",
AboutMenu.Credit.of("Namiu").description("Japanese (ja-jp)"),
AboutMenu.Credit.of("anchelthe").description("Spanish (es-es)"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.william278.husksync.database.Database;
import net.william278.husksync.listener.EventListener;
import net.william278.husksync.sync.DataSyncer;
import org.checkerframework.checker.units.qual.C;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
Expand Down Expand Up @@ -85,10 +86,10 @@ public class Settings {
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class DatabaseSettings {

@Comment("Type of database to use (MYSQL, MARIADB)")
@Comment("Type of database to use (MYSQL, MARIADB, MONGO)")
private Database.Type type = Database.Type.MYSQL;

@Comment("Specify credentials here for your MYSQL or MARIADB database")
@Comment("Specify credentials here for your MYSQL, MARIADB OR MONGO database")
private DatabaseCredentials credentials = new DatabaseCredentials();

@Getter
Expand All @@ -100,9 +101,12 @@ public static class DatabaseCredentials {
private String database = "HuskSync";
private String username = "root";
private String password = "pa55w0rd";
@Comment("Only change this if you have select MYSQL or MARIADB")
private String parameters = String.join("&",
"?autoReconnect=true", "useSSL=false",
"useUnicode=true", "characterEncoding=UTF-8");
@Comment("Only change this if you have selected MONGO")
private String mongoAuthDb = "admin";
}

@Comment("MYSQL / MARIADB database Hikari connection pool properties. Don't modify this unless you know what you're doing!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package net.william278.husksync.database;

import lombok.AllArgsConstructor;
import lombok.Getter;
import net.william278.husksync.HuskSync;
import net.william278.husksync.config.Settings;
Expand Down Expand Up @@ -253,9 +254,11 @@ public final void pinSnapshot(@NotNull User user, @NotNull UUID versionUuid) {
/**
* Identifies types of databases
*/
@Getter
public enum Type {
MYSQL("MySQL", "mysql"),
MARIADB("MariaDB", "mariadb");
MARIADB("MariaDB", "mariadb"),
MONGO("MongoDB", "mongo");

private final String displayName;
private final String protocol;
Expand All @@ -264,16 +267,6 @@ public enum Type {
this.displayName = displayName;
this.protocol = protocol;
}

@NotNull
public String getDisplayName() {
return displayName;
}

@NotNull
public String getProtocol() {
return protocol;
}
}

/**
Expand Down
Loading

0 comments on commit 67dddf0

Please sign in to comment.