From 3feb1d19ff436fea8c12375596d62af76d575531 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Wed, 7 Oct 2020 22:51:11 +0200 Subject: [PATCH] Adding methods --- .../simpleauth/storage/DBHelper.java | 72 +++++-------------- .../simpleauth/storage/PlayerCache.java | 2 +- .../simpleauth/storage/database/LevelDB.java | 2 +- .../simpleauth/storage/database/MongoDB.java | 41 +++++++++++ 4 files changed, 60 insertions(+), 57 deletions(-) diff --git a/src/main/java/org/samo_lego/simpleauth/storage/DBHelper.java b/src/main/java/org/samo_lego/simpleauth/storage/DBHelper.java index 3c5c1ca..d8c4919 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/DBHelper.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/DBHelper.java @@ -1,23 +1,11 @@ package org.samo_lego.simpleauth.storage; -import org.iq80.leveldb.DB; -import org.iq80.leveldb.DBException; import org.samo_lego.simpleauth.storage.database.LevelDB; import org.samo_lego.simpleauth.storage.database.MongoDB; -import java.io.IOException; - -import static org.iq80.leveldb.impl.Iq80DBFactory.bytes; -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 DBHelper { - private DB levelDBStore; - - public DB getLevelDBStore() { - return this.levelDBStore; - } /** * Connects to the DB. @@ -33,14 +21,8 @@ public class DBHelper { * Closes database connection. */ public void close() { - if (levelDBStore != null) { - try { - levelDBStore.close(); - logInfo("Database connection closed successfully."); - } catch (Error | IOException e) { - logError(e.getMessage()); - } - } + if(!config.main.useMongoDB) + LevelDB.close(); } /** @@ -49,7 +31,7 @@ public class DBHelper { * @return false if connection is open, otherwise false */ public boolean isClosed() { - return levelDBStore == null; + return config.main.useMongoDB || LevelDB.isClosed(); } @@ -61,16 +43,9 @@ public class DBHelper { * @return true if operation was successful, otherwise false */ public boolean registerUser(String uuid, String data) { - try { - if(!this.isUserRegistered(uuid)) { - levelDBStore.put(bytes("UUID:" + uuid), bytes("data:" + data)); - return true; - } - return false; - } catch (Error e) { - logError("Register error: " + e.getMessage()); - return false; - } + if(config.main.useMongoDB) + return MongoDB.registerUser(uuid, data); + return LevelDB.registerUser(uuid, data); } /** @@ -80,12 +55,7 @@ public class DBHelper { * @return true if registered, otherwise false */ public boolean isUserRegistered(String uuid) { - try { - return levelDBStore.get(bytes("UUID:" + uuid)) != null; - } catch (DBException e) { - logError(e.getMessage()); - } - return false; + return config.main.useMongoDB ? MongoDB.isUserRegistered(uuid) : LevelDB.isUserRegistered(uuid); } /** @@ -94,11 +64,10 @@ public class DBHelper { * @param uuid uuid of player to delete data for */ public void deleteUserData(String uuid) { - try { - levelDBStore.delete(bytes("UUID:" + uuid)); - } catch (Error e) { - logError(e.getMessage()); - } + if(config.main.useMongoDB) + MongoDB.isUserRegistered(uuid); + else + LevelDB.isUserRegistered(uuid); } /** @@ -108,11 +77,10 @@ public class DBHelper { * @param data data to put inside database */ public void updateUserData(String uuid, String data) { - try { - levelDBStore.put(bytes("UUID:" + uuid), bytes("data:" + data)); - } catch (Error e) { - logError(e.getMessage()); - } + if(config.main.useMongoDB) + MongoDB.updateUserData(uuid, data); + else + LevelDB.updateUserData(uuid, data); } /** @@ -121,13 +89,7 @@ public class DBHelper { * @param uuid uuid of the player to get data for. * @return data as string if player has it, otherwise empty string. */ - public String getData(String uuid){ - try { - if(this.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 ""; + public String getUserData(String uuid){ + return config.main.useMongoDB ? MongoDB.getUserData(uuid) : LevelDB.getUserData(uuid); } } \ No newline at end of file diff --git a/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java index 426bbf2..b7469c0 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java @@ -82,7 +82,7 @@ public class PlayerCache { this.lastAir = 300; } - String data = DB.getData(uuid); + String data = DB.getUserData(uuid); if(!data.isEmpty()) { // Getting (hashed) password JsonObject json = gson.fromJson(data, JsonObject.class); diff --git a/src/main/java/org/samo_lego/simpleauth/storage/database/LevelDB.java b/src/main/java/org/samo_lego/simpleauth/storage/database/LevelDB.java index 2f2a4d6..a9ee8ce 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/database/LevelDB.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/database/LevelDB.java @@ -124,7 +124,7 @@ public class LevelDB { * @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){ + public static String getUserData(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); diff --git a/src/main/java/org/samo_lego/simpleauth/storage/database/MongoDB.java b/src/main/java/org/samo_lego/simpleauth/storage/database/MongoDB.java index 6ee9fcb..d051d2a 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/database/MongoDB.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/database/MongoDB.java @@ -6,9 +6,17 @@ import com.mongodb.client.MongoClients; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; +import org.iq80.leveldb.DBException; +import javax.print.Doc; +import java.io.IOException; + +import static com.mongodb.client.model.Filters.eq; +import static org.iq80.leveldb.impl.Iq80DBFactory.bytes; import static org.samo_lego.simpleauth.SimpleAuth.config; +import static org.samo_lego.simpleauth.utils.SimpleLogger.logError; +import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo; public class MongoDB { private static MongoCollection collection; @@ -29,4 +37,37 @@ public class MongoDB { MongoDatabase database = mongoClient.getDatabase(config.mongoDBCredentials.databaseName); collection = database.getCollection("players"); } + + public static boolean isUserRegistered(String uuid) { + try { + return collection.find(eq("UUID", uuid)).iterator().hasNext(); + } catch (DBException e) { + logError(e.getMessage()); + } + return false; + } + + public static boolean registerUser(String uuid, String data) { + if(!isUserRegistered(uuid)) { + collection.insertOne( + new Document(uuid, data) + ); + return true; + } + return false; + } + + public static void deleteUserData(String uuid) { + collection.deleteOne(eq("UUID", uuid)); + } + + public static void updateUserData(String uuid, String data) { + collection.updateOne(eq("UUID", uuid), new Document(uuid, data)); + } + + public static String getUserData(String uuid){ + if(isUserRegistered(uuid)) + return collection.find(eq("UUID", uuid)).iterator().next().getString(uuid); + return ""; + } }