Marauroa Database Structure: Difference between revisions

Jump to navigation Jump to search
Content deleted Content added
imported>MartinFuchs
imported>Hendrik Brummermann
No edit summary
Line 147: Line 147:
| players_online | int(11) | YES | | NULL | |
| players_online | int(11) | YES | | NULL | |
+-----------------+-----------+------+-----+-------------------+-------+
+-----------------+-----------+------+-----+-------------------+-------+
</pre>


Important tables which store original data, use the InnoDB engine. Tables with redundant data for easy access and logging use the MyISAM table type.
Important tables which store original data, use the InnoDB engine. Tables with redundant data for easy access and logging use the MyISAM table type.
Line 152: Line 153:


== JDBC Database HOWTO==
== JDBC Database HOWTO==
JDBC technology is an API that lets you access virtually any tabular data source from the Java programming language. It provides cross-DBMS connectivity to a wide range of SQL databases, and now, with the new JDBC API, it also provides access to other tabular data sources, such as spreadsheets or flat files.
JDBC technology is an API that lets you access virtually any tabular data source from the Java programming language. It provides cross-DBMS connectivity to a wide range of relational databases. Unfortunatally it does not hide vendor specific stuff. Marauroa and Stendhal currently only work with MySQL because of that. Adding support for other database software would be very easy as the database specific code is concentrated in the classes JDBCDatabase (Marauroa) and StendhalPlayerDatabase (Stendhal). We have no need for that, so it was not done, yet. We will, however, accept patches for multi database system support, so if you need it, go ahead.


JDBCPlayerDatabase is anyway not database independent; on the Player table we are using AUTOINCREMENT that is a unique keyword of MySQL that is not part of the SQL standard.


You need to download MySQL Connector/J in order to get it to run. <br>
You need to download MySQL Connector/J in order to get it to run: http://www.mysql.com/downloads/api-jdbc-stable.html
http://www.mysql.com/downloads/api-jdbc-stable.html


To configure Marauroa to work with a JDBC source, we need to modify the configuration of the JDBC Connection.
To configure Marauroa to work with a JDBC source, run the appropriate GenerateINI program. For Stendhal this is games.stendhal.server.core.engine.GenerateINI.

So open the configuration file '''server.ini''' ''(or whatever you choose to name it)'' and edit the next fields
<pre>
marauroa_DATABASE=JDBCPlayerDatabase

jdbc_class=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost/marauroa
jdbc_user=marauroa_dbuser
jdbc_pwd=marauroa_dbpwd
</pre>

* ''jdbc_class'': This field tells the engine what Driver to use for database access. Please refer to your software manual to see the multiple options.
* ''jdbc_url'': This points to the type and source of the information, for MySQL the string must be as follows:
* '''jdbc:mysql://ip:database_name/'''
* ''jdbc_user'': This is the username for the database
* ''jdbc_pwd'': This is the password for that username in the database.

Now, simply save the changes and your configuration file is ready.


Before using the application with the database, you need to create the database itself. So, with MySQL just run MySQL and enter:
Before using the application with the database, you need to create the database itself. So, with MySQL just run MySQL and enter:
<pre>
<pre>
create database stendhal;
create database stendhal;
grant all on stendhal.* to stendhal_dbuser@localhost identified by 'stendhal_dbpwd';
grant all on stendhal.* to stendhal_user@localhost identified by 'stendhal_passwd';
</pre>
</pre>


The rest of code is handled by the server itself, and it will create the tables if they don't exist.
The rest of code is handled by the server itself, and it will create the tables if they don't exist.


= Storing objects in the database =
== Storing objects in the database ==
Objects are stored in the database to save their state. This is a heavy (intensive) operation, so we should cache objects and store them only from time to time.
Objects are stored in the database to save their state. This is an expansive operation, so it is only done every 10 minutes or on special events (like logout).


This decision is made in RPManager. It calculates how often an object has to be stored.
This decision is made in RPManager. It calculates how often an object has to be stored.


Marauroa knows two types of objects which can be store two databases: players and zones. Both objects can contain other objects.
You have to write the code to load all the database objects into RPWorld. This loading must be coded in the RPWorld class inside the onInit and onFinish methods.

== How objects are identified ==
It is important to notice that RPObject.ID is only valid while the object is online. So there is no guarantee that any new object won't have that same RPObject.ID once the previous object has been removed.

To solve this problem we store a hidden attribute in each object called '''#db_id''' which gives a unique id to each object regardless of whether the object is online at a particular point in time or not.

When an object is loaded in to the game again, it will be assigned a new valid RPObject.ID. However, the object will still have a valid and unique persistent #db_id which can be used when we need to store the object again. We can simply update the existing copy already on the database.

== How objects are stored ==
There is a problem right now in Marauroa: storing an object involves two steps, remove the previous stored object and insert the new one.
This is a very complex operation and it could be ''simplified'' to update just the fields that has changed. May you want to do it?