[LIB] MCDatabase - A new extensible database

Discussion in 'Resources' started by McDjuady, May 23, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    McDjuady

    Hi Guys!

    So today i wanted to show you a small library i made called MCDatabase. Go check it out here:
    https://github.com/McDjuady/MCDatabase

    The idea behind the Database model is, that you have groups with groups and variables in them.
    So a database could look like this:
    Code:
    database:
      value2 = 2
      group:
        value = 1
        group2:
          value3 = 3
      
    This makes it very easy to understand and used.
    I decided to not force the user to use a certain format for ther database files, but rather choose it on their own, by implementing their own database.
    MCDatabase is only the Core, which includes some Interfaces, a DatabaseManager and "DatabaseBridges". But i'll get to that later.
    The interfaces are designed to give you easy access to the Database. There is one for the Database itself, one for Groups, a DatabaseProvider and a Serializable-Interface.
    The Database Interface contains functions to save/load, create or destroy groups, to flush it or to copy it to an other DB.
    The DatabaseGroup Interface contains functions to set all kinds of values (Integer,Boolean,String,etc.). You can also create subgroups in it or copy it to an other group.
    The DatabaseProvider is used to create new Instances of the Database or delete one.
    This is nescessary, to work with the Bukkit Services, which i use to get the available Implementations for the Database.
    Finaly the Serializable interface is used to save and load objects which implement this interface to the database

    Now to the DatabaseBridges.
    DatabaseBridges are designed to transform Objects to the Database and extract them later.
    This is used to save Objects which i can't modify to use the Serializable, like Collections, Maps or Locations from bukkit.
    They are realy easy to use, and if you miss some, you can add your own ones.

    Examples:
    I provided an example implementation using xml and jdom. It's realy not the best and gets very slow whit large files.
    https://github.com/McDjuady/MCJDOMDatabase

    How do i use it?
    All you have to do is to add those lines to the onEnable() and onDisable() method of your plugin
    Code:
    public class Plugin extends JavaPlugin {
     
      DatabaseManager mgr;
     
      [...]
     
      public void onEnable() {
        [...]
        mgr = new DatabaseManager(this);
        mgr.startup() //Searches for an implementation and evantualy loads already created Databases
        [...]
      }
      [...]
      public void onDisable(){
        [...]
        mgr.shutdown() //Saves the loaded Databases
        [...]
      }
      [...]
    }
    Now you can create your own Databases via mgr.createDatabase(name). For example:
    Code:
    Database test = mgr.createDatabase("test")
    Creating groups is also very easy
    Code:
    DatabaseGroup testGroup = test.createGroup("testGroup")
    Adding a value
    Code:
    testGroup.addInt("testInt",1)
    Creating a subgroup
    Code:
    DatabaseGroup subGroup = testGroup.createSubgroup("subGroup")
    Now let's save a location to this subgroup
    Code:
    mgr.getDatabaseBridgeFactory().translateAndSave(new Location(Bukkit.getWorld(world),0D,0D,0D), subGroup)
    And at last, let's load it
    Code:
    Location loc = mgr.getDatabaseBridgeFactory().loadAndTranslate(subGroup)
    That's all i have to say :)

    Feedback is apreciated

    Greetings
    McDjuady
     
Thread Status:
Not open for further replies.

Share This Page