From 3c5f168628d67b51887a88ec5e52f6d5becaf7a6 Mon Sep 17 00:00:00 2001 From: Matthew Cech Date: Sat, 30 Mar 2019 14:26:21 -0700 Subject: [PATCH] = Updated core documentation quickly --- src/core/CommandThread.java | 3 +-- src/core/DatabaseDriver.java | 24 +++++++++++------------- src/core/DatabaseManager.java | 6 ++---- src/core/DatabaseTrackedObject.java | 8 ++++---- src/core/ObjectBuilderFactory.java | 10 +++------- src/core/Stats.java | 3 ++- src/dataStructures/GenericImage.java | 3 ++- src/network/NetworkDerpi.java | 1 + src/network/NetworkE621.java | 1 + 9 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/core/CommandThread.java b/src/core/CommandThread.java index eb2d848..2fc5707 100644 --- a/src/core/CommandThread.java +++ b/src/core/CommandThread.java @@ -8,8 +8,7 @@ import dataStructures.UserInput; public class CommandThread extends Thread { - // Pile of variables - // TODO(wisp): Consider packaging this up into a thread arguments object potentially...? + // Pile of variables. This may be packaged later as thread arguments. CommandManager manager; UserInput input; KittyGuild guild; diff --git a/src/core/DatabaseDriver.java b/src/core/DatabaseDriver.java index 3d98d33..a97d1bb 100644 --- a/src/core/DatabaseDriver.java +++ b/src/core/DatabaseDriver.java @@ -12,13 +12,13 @@ import utils.LogFilter; // This is where we put everything and swap out the moving parts, // ie MySQL, PostgreSQL, SQLite, etc... // -// TODO(wisp): Right now this can be messed up with SQL injection stuff if users -// are allowed to directly touch data. Leaving it like this temporarily. +// Right now this can be messed up with SQL injection stuff if users +// are allowed to directly touch data. Leaving it like this temporarily, +// and is for prototyping only! public class DatabaseDriver { + // Config and local varaibles private JDBCDriver driver; - - // Note: Changing these values can mess up the database... private final String globalTableName = "kitty_globals"; private final String globalKeyName = "GlobalKey"; private final String globalValueName = "GlobalValue"; @@ -28,6 +28,7 @@ public class DatabaseDriver driver = null; } + // Set up and create a table in the database public boolean Connect() { driver = new JDBCDriverSQLite(); @@ -54,7 +55,7 @@ public class DatabaseDriver } } - // the key will be created if it doesn't exist, and the default value returned. + // Creates a key. The key will be created if it doesn't exist, and the default value returned. public String CreateGetKey(String key) { GlobalLog.Log(LogFilter.Database, "CreateGeyKey: key-" + key); @@ -71,41 +72,38 @@ public class DatabaseDriver } } + // Prototype formatting for key updating private void UpdateKey(String key, String value) { - //GlobalLog.Log(LogFilter.Database, "Update key " + key); String command = "UPDATE " + globalTableName + " SET " + globalValueName + " = '" + value + "' WHERE " + globalKeyName + "= '" + key + "';"; - //GlobalLog.Log(LogFilter.Database, "Composed command: " + command); driver.ExecuteStatement(command); } + // Protoype updating for seeing if a key exists private boolean HasKey(String key) { - //GlobalLog.Log(LogFilter.Database, "Has key " + key); String command = "SELECT COUNT(1) as count FROM " + globalTableName + " WHERE " + globalKeyName + " = '" + key + "';"; - //GlobalLog.Log(LogFilter.Database, "Composed command: " + command); ResultSet set = driver.ExecuteReturningStatement(command); String out = ResultAsString(set, "count"); return out.charAt(0) == '1'; } + // Prototype for getting a key private String GetKey(String key) { - //GlobalLog.Log(LogFilter.Database, "Getting key " + key); String command = "SELECT " + globalValueName + " as searchedKey FROM " + globalTableName +" WHERE " + globalKeyName + "= \'" + key + "\';"; - //GlobalLog.Log(LogFilter.Database, "Composed command: " + command); ResultSet set = driver.ExecuteReturningStatement(command); return ResultAsString(set, "searchedKey"); } + // Prototype for creating a key private void CreateKey(String key, String value) { - //GlobalLog.Log(LogFilter.Database, "Creating Key " + key); String command = "INSERT INTO " + globalTableName + " (GlobalKey, GlobalValue) VALUES ('" + key + "', '" + value + "');"; - //GlobalLog.Log(LogFilter.Database, "Composed command: " + command); driver.ExecuteStatement(command); } + // Transforms a result into a string if possible. private String ResultAsString(ResultSet rs, String key) { if(rs == null) diff --git a/src/core/DatabaseManager.java b/src/core/DatabaseManager.java index c3649d9..b1f72ec 100644 --- a/src/core/DatabaseManager.java +++ b/src/core/DatabaseManager.java @@ -31,10 +31,8 @@ public class DatabaseManager driver.Connect(); } - // Thumbs through registered objects and syncs them with the database. - // TODO(wisp) Right now this just syncs on the main thread, but we will - // want to have upkeep commands queue up for a dedicated database thread - // in the future to offload the wait times. + // Thumbs through registered objects and syncs them with the database. + // Consider moving this operation to a separate thread. public void Upkeep() { for(int i = 0 ; i < trackedObjects.size(); ++i) diff --git a/src/core/DatabaseTrackedObject.java b/src/core/DatabaseTrackedObject.java index 9d5dfa7..c149df5 100644 --- a/src/core/DatabaseTrackedObject.java +++ b/src/core/DatabaseTrackedObject.java @@ -1,5 +1,7 @@ package core; +// Objects inheriting from this are capable of being stored in and +// have their data populated from database entries. public abstract class DatabaseTrackedObject { private boolean isDirty; @@ -26,10 +28,8 @@ public abstract class DatabaseTrackedObject isDirty = false; } - // TODO(wisp): Not the best way to handle this, potentially consider - // using an object factory that looks up how to serialize and - // deserialize based on the type of the thing being tracked. - // For now, this is fine. + // Consider an object factory instead of dedicated serialization methods + // if this starts to become impractical. For now, it works. public abstract String Serialize(); public abstract void DeSerialzie(String string); } diff --git a/src/core/ObjectBuilderFactory.java b/src/core/ObjectBuilderFactory.java index 503a2b8..c709df4 100644 --- a/src/core/ObjectBuilderFactory.java +++ b/src/core/ObjectBuilderFactory.java @@ -31,13 +31,10 @@ public class ObjectBuilderFactory // Stats tracking and whatnot... for setting stats too internally potentially private static Stats stats; - //RPManger for tracking RP system + // RPManger for tracking RP system private static RPManager rpManager; - // NOTE(wisp): This is designed to initialize at the last possible second. - // The idea behind this is that there may be other things that may need - // time to initialize before the factory can use them, and this guarantees - // they get the time they need. + // Lazy initialization style for private static boolean hasInitialized; private static Semaphore initMutex = new Semaphore(1); private static void LazyInit() @@ -50,8 +47,7 @@ public class ObjectBuilderFactory initMutex.acquire(); try { - // NOTE(wisp): Actually put all the init code here. - // In the future, this is where we would read from something external. + // Initialization here. This is where we could read from something external. guildCache = new HashMap(); userCache = new HashMap(); channelCache = new HashMap(); diff --git a/src/core/Stats.java b/src/core/Stats.java index 82f5f8e..43ca17b 100644 --- a/src/core/Stats.java +++ b/src/core/Stats.java @@ -8,7 +8,8 @@ import core.CommandManager.ThreadData; import utils.GlobalLog; import utils.LogFilter; -// NOTE(wisp): This is a class designed to be asked about various kittybot stats +// This is a class designed to be asked about various kittybot stats. +// It follows a singleton pattern, and is accessable from anywhere internally. public class Stats { public static String botName = "KittyBot"; diff --git a/src/dataStructures/GenericImage.java b/src/dataStructures/GenericImage.java index ecfaa58..ffe7212 100644 --- a/src/dataStructures/GenericImage.java +++ b/src/dataStructures/GenericImage.java @@ -1,5 +1,6 @@ -package core; +package dataStructures; +// This is a structure representing an image reponse for handing around. public class GenericImage { private String artist; diff --git a/src/network/NetworkDerpi.java b/src/network/NetworkDerpi.java index c67fa4d..2719b4d 100644 --- a/src/network/NetworkDerpi.java +++ b/src/network/NetworkDerpi.java @@ -2,6 +2,7 @@ package network; import com.google.gson.Gson; import core.*; +import dataStructures.GenericImage; import offline.*; import utils.*; diff --git a/src/network/NetworkE621.java b/src/network/NetworkE621.java index 0ce696f..5017964 100644 --- a/src/network/NetworkE621.java +++ b/src/network/NetworkE621.java @@ -2,6 +2,7 @@ package network; import com.google.gson.Gson; import core.*; +import dataStructures.GenericImage; import utils.*; /**