Starting with MongoDB

This commit is contained in:
samo_lego 2020-10-07 22:27:44 +02:00
parent 7def6aea23
commit 2098897104
6 changed files with 208 additions and 17 deletions

View File

@ -45,12 +45,17 @@ dependencies {
include "at.favre.lib:bytes:${bytes_version}"
// Storage
// leveldb database
// LevelDB database
implementation group: 'org.iq80.leveldb', name: 'leveldb', version: '0.12'
implementation group: 'org.iq80.leveldb', name: 'leveldb-api', version: '0.12'
include group: 'org.iq80.leveldb', name: 'leveldb', version: '0.12'
include group: 'org.iq80.leveldb', name: 'leveldb-api', version: '0.12'
// MongoDB driver
implementation 'org.mongodb:mongodb-driver:3.12.7'
include 'org.mongodb:mongodb-driver:3.12.7'
// JNA lib
include 'net.java.dev.jna:jna:5.5.0'

View File

@ -24,7 +24,7 @@ import org.samo_lego.simpleauth.event.item.DropItemCallback;
import org.samo_lego.simpleauth.event.item.TakeItemCallback;
import org.samo_lego.simpleauth.storage.AuthConfig;
import org.samo_lego.simpleauth.storage.PlayerCache;
import org.samo_lego.simpleauth.storage.SimpleAuthDatabase;
import org.samo_lego.simpleauth.storage.DBHelper;
import java.io.File;
import java.io.FileReader;
@ -46,7 +46,7 @@ import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid;
public class SimpleAuth implements DedicatedServerModInitializer {
public static SimpleAuthDatabase DB = new SimpleAuthDatabase();
public static DBHelper DB = new DBHelper();
public static final ExecutorService THREADPOOL = Executors.newCachedThreadPool();

View File

@ -86,9 +86,9 @@ public class AuthConfig {
public boolean allowFalling = false;
/**
* Whether to tp player to spawn when joining (to hide coordinates)
* Whether to tp player to spawn when joining (to hide coordinates).
*/
public boolean spawnOnJoin = false;
public boolean spawnOnJoin = false;
/**
* Data for spawn (where deauthenticated players are teleported).
@ -103,6 +103,27 @@ public class AuthConfig {
public double y;
public double z;
}
/**
* Whether to use MongoDB instead of LevelDB.
* Note: you need to install MongoDB yourself.
*/
public boolean useMongoDB = false;
public static class MongoDBCredentials {
/**
* Credentials for MongoDB database.
* Leave this as-is if you are using LevelDB or don't need
* special credentials.
*/
public String host = "localhost";
public int port = 27017;
public boolean useSsl = true;
public String databaseName = "SimpleAuthPlayerData";
public String password;
public String username;
}
}
public static class LangConfig {
public String enterPassword = "§6You need to enter your password!";
@ -193,6 +214,7 @@ public class AuthConfig {
public MainConfig main = new MainConfig();
public MainConfig.WorldSpawn worldSpawn = new MainConfig.WorldSpawn();
public MainConfig.MongoDBCredentials mongoDBCredentials = new MainConfig.MongoDBCredentials();
public LangConfig lang = new LangConfig();
public ExperimentalConfig experimental = new ExperimentalConfig();

View File

@ -2,18 +2,17 @@ package org.samo_lego.simpleauth.storage;
import org.iq80.leveldb.DB;
import org.iq80.leveldb.DBException;
import org.iq80.leveldb.Options;
import org.samo_lego.simpleauth.SimpleAuth;
import org.samo_lego.simpleauth.storage.database.LevelDB;
import org.samo_lego.simpleauth.storage.database.MongoDB;
import java.io.File;
import java.io.IOException;
import static org.iq80.leveldb.impl.Iq80DBFactory.bytes;
import static org.iq80.leveldb.impl.Iq80DBFactory.factory;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logError;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo;
import static org.samo_lego.simpleauth.SimpleAuth.config;
public class SimpleAuthDatabase {
public class DBHelper {
private DB levelDBStore;
public DB getLevelDBStore() {
@ -24,13 +23,10 @@ public class SimpleAuthDatabase {
* Connects to the DB.
*/
public void openConnection() {
try {
Options options = new Options();
levelDBStore = factory.open(new File(SimpleAuth.gameDirectory + "/mods/SimpleAuth/levelDBStore"), options);
} catch (Error | IOException e) {
logError(e.getMessage());
}
if(config.main.useMongoDB)
MongoDB.initialize();
else
LevelDB.initialize();
}
/**

View File

@ -0,0 +1,136 @@
package org.samo_lego.simpleauth.storage.database;
import org.iq80.leveldb.DB;
import org.iq80.leveldb.DBException;
import org.iq80.leveldb.Options;
import org.samo_lego.simpleauth.SimpleAuth;
import java.io.File;
import java.io.IOException;
import static org.iq80.leveldb.impl.Iq80DBFactory.bytes;
import static org.iq80.leveldb.impl.Iq80DBFactory.factory;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logError;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo;
public class LevelDB {
private static DB levelDBStore;
public static DB getLevelDBStore() {
return levelDBStore;
}
/**
* Connects to the LevelDB.
*/
public static void initialize() {
Options options = new Options();
try {
levelDBStore = factory.open(new File(SimpleAuth.gameDirectory + "/mods/SimpleAuth/levelDBStore"), options);
} catch (IOException e) {
logError(e.getMessage());
}
}
/**
* Closes database connection.
*/
public static void close() {
if (levelDBStore != null) {
try {
levelDBStore.close();
logInfo("Database connection closed successfully.");
} catch (Error | IOException e) {
logError(e.getMessage());
}
}
}
/**
* Tells whether DB connection is closed.
*
* @return false if connection is open, otherwise false
*/
public static boolean isClosed() {
return levelDBStore == null;
}
/**
* Inserts the data for the player.
*
* @param uuid uuid of the player to insert data for
* @param data data to put inside database
* @return true if operation was successful, otherwise false
*/
public static boolean registerUser(String uuid, String data) {
try {
if(!isUserRegistered(uuid)) {
levelDBStore.put(bytes("UUID:" + uuid), bytes("data:" + data));
return true;
}
return false;
} catch (Error e) {
logError("Register error: " + e.getMessage());
return false;
}
}
/**
* Checks if player is registered.
*
* @param uuid player's uuid
* @return true if registered, otherwise false
*/
public static boolean isUserRegistered(String uuid) {
try {
return levelDBStore.get(bytes("UUID:" + uuid)) != null;
} catch (DBException e) {
logError(e.getMessage());
}
return false;
}
/**
* Deletes data for the provided uuid.
*
* @param uuid uuid of player to delete data for
*/
public static void deleteUserData(String uuid) {
try {
levelDBStore.delete(bytes("UUID:" + uuid));
} catch (Error e) {
logError(e.getMessage());
}
}
/**
* Updates player's data.
*
* @param uuid uuid of the player to update data for
* @param data data to put inside database
*/
public static void updateUserData(String uuid, String data) {
try {
levelDBStore.put(bytes("UUID:" + uuid), bytes("data:" + data));
} catch (Error e) {
logError(e.getMessage());
}
}
/**
* Gets the hashed password from DB.
*
* @param uuid uuid of the player to get data for.
* @return data as string if player has it, otherwise empty string.
*/
public static String getData(String uuid){
try {
if(isUserRegistered(uuid)) // Gets password from db and removes "data:" prefix from it
return new String(levelDBStore.get(bytes("UUID:" + uuid))).substring(5);
} catch (Error e) {
logError("Error getting data: " + e.getMessage());
}
return "";
}
}

View File

@ -0,0 +1,32 @@
package org.samo_lego.simpleauth.storage.database;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import static org.samo_lego.simpleauth.SimpleAuth.config;
public class MongoDB {
private static MongoCollection<Document> collection;
public static void initialize() {
/*mongoClient = MongoClients.create(
String.format(
"mongodb://%s:%s@%s:%s/?authSource=db1&ssl=%s",
config.mongoDBCredentials.username,
config.mongoDBCredentials.password,
config.mongoDBCredentials.host,
config.mongoDBCredentials.port,
config.mongoDBCredentials.useSsl
)
);*/
MongoClient mongoClient = MongoClients.create(String.format("mongodb://%s:%s", config.mongoDBCredentials.host, config.mongoDBCredentials.port));
MongoDatabase database = mongoClient.getDatabase(config.mongoDBCredentials.databaseName);
collection = database.getCollection("players");
}
}