Atavism Version 2018.1AGIS API

atavism.server.engine
Class Database

java.lang.Object
  extended by atavism.server.engine.Database

public class Database
extends java.lang.Object

Access to Entities in database - default is MySQL All Entity access requires both an object oid and a Namespace.


Nested Class Summary
static class Database.OidChunk
          A class to represent a range of allocated oids.
static class Database.StringEscaper
           
static class Database.XMLExceptionListener
          Logs the error encountered during de-serialization.
 
Constructor Summary
Database()
          Constructor starts the keepalive thread
Database(java.lang.String sDriver)
          General database connection.
 
Method Summary
 boolean characterNameTaken(java.lang.String name)
          Get user name.
 void close()
          Close the database connection.
 void connect(java.lang.String url, java.lang.String username, java.lang.String password)
          Connect to the database at the host given by the url, using the given username and password
 void createCharacter(java.lang.String worldName, OID aoid, AOObject user, Namespace namespace)
          Creates this character as an entry in the database.
 Namespace createNamespace(java.lang.String nsString)
          Store the mapping between the given namespace string and integer in the namespaces table.
 boolean databaseContainsTable(java.lang.String dbName, java.lang.String tableName)
          Tests to see if the table of the given name is present in a database.
 boolean databaseTableContainsColumn(java.lang.String dbName, java.lang.String tableName, java.lang.String columnName)
          Tests to see if the table of the given name contain the given row in the current database.
 void deleteObjectData(OID oid)
          Delete object and all its sub-objects from the objstore table
 void deleteObjectData(OID oid, Namespace namespace)
          Delete a sub-object from the objstore table
 void deletePlayerCharacter(OID oid)
          Delete the user with the given oid from the player_character table.
 void encacheNamespaceMapping()
          Encache the mapping between namespace strings and namespace integers.
protected  void encodeEntity(java.io.ByteArrayOutputStream ba, Entity entity)
          Write the representation of the entity into the ByteArrayOutputStream.
 void executeBatch(java.util.List<java.lang.String> statements)
          Run the series of SQL statements in the list argument.
 int executeUpdate(java.lang.String update)
          Run the update statement in the string arg.
 Namespace findExistingNamespace(java.lang.Integer nsInt)
          Find an existing namespace int in the database.
 Namespace findExistingNamespace(java.lang.String nsString)
          Find an existing namespace string in the database.
 int getAccountCount(java.lang.String worldName)
           
 java.sql.Connection getConnection()
          returns the connection object -- you must lock around this.
 java.util.List<OID> getGameIDs(java.lang.String worldName, OID atavismID)
          Each Atavism account may have more than 1 character for a given world.
 java.util.List<OID> getInstanceContent(OID instanceOid, ObjectType exclusion)
           
 OID getLocation(OID oid, Namespace ns, Point location)
          Get object location.
 java.util.concurrent.locks.Lock getLock()
           
 java.lang.String getObjectName(OID oid, Namespace namespace)
          Get object name.
 java.util.List<java.lang.String> getObjectNames(java.util.List<OID> inputOids, Namespace namespace, java.lang.String unknownName)
          Get object names for the given oids.
 java.util.List<Namespace> getObjectNamespaces(OID oid)
           
 OID getOidByName(java.lang.String name, Namespace namespace)
           
 OID getOidByName(java.lang.String name, Namespace namespace, OID instanceOid)
           
 Database.OidChunk getOidChunk(int chunkSize)
          Returns a block of oids, and record the fact that they have been allocated in the database.
 java.util.List<java.lang.Object> getOidsAndNamesMatchingName(java.lang.String playerName, boolean exactMatch)
          Get the oids and names matching the name supplied, base on the name string to match, and whether to return based on a starting match of an exact match.
 java.util.Set<OID> getPersistedObjects(Namespace namespace, Geometry g)
          Returns the set of oid's for objects managed by the namespace that are persisted in the geometry passed in, and all objects that have no geometry.
 java.util.List<PluginStatus> getPluginStatus(java.lang.String pluginType)
          Get the contents of the database row into a PluginStatus instance.
 java.lang.String getUserName(OID uid)
          Get user name.
 Entity loadEntity(OID oid, Namespace namespace)
          Retrieves the user - returns null if user with OID does not exist.
 Entity loadEntity(java.lang.String persistenceKey)
          Load the entity with the given key.
static java.lang.String makeNameCollectionString(java.util.Collection<java.lang.String> names)
           
static java.lang.String makeOidCollectionString(java.util.List<OID> oids)
           
 void mapAtavismID(java.lang.String worldName, OID atavismID, OID objID)
          Adds a atavism ID -> player OID mapping to the player_character table.
 void ping()
          Runs a select statement to make sure we can still talk to the database.
 boolean registerStatusReportingPlugin(EnginePlugin plugin, long runId)
          Register a plugin
 java.io.InputStream retrieveEntityDataByOidAndNamespace(OID oid, Namespace namespace)
          Loads an object's serialization data from the database.
 java.io.InputStream retrieveEntityDataByPersistenceKey(java.lang.String persistenceKey)
          Loads the object with the given persistence key from the database.
 void saveObject(Entity entity, Namespace namespace)
          Save the entity in the given namespace
 void saveObject(java.lang.String persistenceKey, byte[] data, Namespace namespace)
          Helper method.
 void saveObject(java.lang.String persistenceKey, Entity entity, Namespace namespace)
          FIXME: This happens on the object manager- we dont need to lock the quad tree anymore saves object and all objects owned by this object into the database. if the object already exists, it will overwrite with a newer version. if the object is new, will create a new row and also set the dbid into the obj itself (obj.getDBid() will return the new id).
 void saveObjectHelper(java.lang.String persistenceKey, Entity entity, Namespace namespace, byte[] data)
          A helper method that saves the byte array in the namespace with the oid supplied by the entity.
 boolean unregisterStatusReportingPlugin(EnginePlugin plugin)
          Unregister the plugin for status reporting
 boolean updatePluginStatus(EnginePlugin plugin, long nextUpdateTime)
          Update the status of a plugin
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Database

public Database()
Constructor starts the keepalive thread


Database

public Database(java.lang.String sDriver)
General database connection. Create JDBC database for with the supplied string representing the driver class, e.g. "com.mysql.jdbc.Driver". Also starts the keepalive thread.

Method Detail

getConnection

public java.sql.Connection getConnection()
returns the connection object -- you must lock around this. be careful when using this.


getLock

public java.util.concurrent.locks.Lock getLock()

connect

public void connect(java.lang.String url,
                    java.lang.String username,
                    java.lang.String password)
Connect to the database at the host given by the url, using the given username and password

Parameters:
url - Specifies the host to connect to.
username - The account name used to log into the database.
password - The password to use to log in.

executeUpdate

public int executeUpdate(java.lang.String update)
Run the update statement in the string arg.

Parameters:
update - An SQL update statement, or any other statement that does not return values.

executeBatch

public void executeBatch(java.util.List<java.lang.String> statements)
Run the series of SQL statements in the list argument.

Parameters:
statements - A list of SQL statements to be executed.

databaseTableContainsColumn

public boolean databaseTableContainsColumn(java.lang.String dbName,
                                           java.lang.String tableName,
                                           java.lang.String columnName)
Tests to see if the table of the given name contain the given row in the current database. Used by Engine as an indication the version of database schema, since normally we add tables with new schema versions.

Parameters:
tableName - The name of the table.
columnName - The name of the column.
Returns:
True if the column is contained in the table; false otherwise.

databaseContainsTable

public boolean databaseContainsTable(java.lang.String dbName,
                                     java.lang.String tableName)
Tests to see if the table of the given name is present in a database. Used by Engine as an indication the version of database schema, since normally we add tables with new schema versions. However, this method requires MySQL version 5, and since some customers have MySQL 4, we can't use it.

Parameters:
dbName - The name of the database.
tableName - The name of the table.
Returns:
True if the table is contained in the database; false otherwise.

close

public void close()
Close the database connection.


encacheNamespaceMapping

public void encacheNamespaceMapping()
Encache the mapping between namespace strings and namespace integers.


findExistingNamespace

public Namespace findExistingNamespace(java.lang.String nsString)
Find an existing namespace string in the database. If it exists, return the namespace object; else return null. This is called only if we haven't already encached the namespace.

Parameters:
nsString - The string name of the namespace.
Returns:
The namespace gotten from the database, or null.

findExistingNamespace

public Namespace findExistingNamespace(java.lang.Integer nsInt)
Find an existing namespace int in the database. If it exists, return the namespace object; else return null. This is called only if we haven't already encached the namespace.

Parameters:
nsInt - The int associated with the namespace.
Returns:
The namespace gotten from the database, or null.

createNamespace

public Namespace createNamespace(java.lang.String nsString)
Store the mapping between the given namespace string and integer in the namespaces table.

Parameters:
nsString - The namespace string for the new namespace.
Returns:
A newly-created Namespace, either from a row already in the namespaces table, or from a new row.

createCharacter

public void createCharacter(java.lang.String worldName,
                            OID aoid,
                            AOObject user,
                            Namespace namespace)
Creates this character as an entry in the database. Will also populate the aoid -> gameid table. will set the aoid for the user object if it hasn't been set already

Parameters:
worldName - The string name of the world in which to create the character.
aoid - The Atavism account id for the user.
user - The Entity representing the user character
namespace - The namespace into which to save the created character object.

mapAtavismID

public void mapAtavismID(java.lang.String worldName,
                         OID atavismID,
                         OID objID)
Adds a atavism ID -> player OID mapping to the player_character table.

Parameters:
worldName - The string name of the world in which to create the character.
atavismID - The Atavism account id for the user.
objID - The oid of the Entity representing the user character

getPersistedObjects

public java.util.Set<OID> getPersistedObjects(Namespace namespace,
                                              Geometry g)
Returns the set of oid's for objects managed by the namespace that are persisted in the geometry passed in, and all objects that have no geometry. Since for now the only objects that have location are world manager objects, this will return the null set for any namespace other than Nammespace.WORLD_MANAGER.

Parameters:
namespace - The namespace containing the object to be returned.
g - The geometry containing the locations of objects to be returned.

loadEntity

public Entity loadEntity(OID oid,
                         Namespace namespace)
Retrieves the user - returns null if user with OID does not exist. Deserializes the data, using the retrieveObjectData() method. the dbid is the same as a gameid. This method creates a world node for the user object, but not spawned.

Parameters:
oid - The oid of the of user character.
namespace - The namespace containing entity to be returned.
Returns:
The entity representing the user with the oid, or null if it can't be found.

loadEntity

public Entity loadEntity(java.lang.String persistenceKey)
Load the entity with the given key. Only one subobject in the objstore table has the key.

Parameters:
persistenceKey - The string key, which provides a "handle" by which the object to be loaded is named.
Returns:
The Entity associated with that key, or null if none exists.

retrieveEntityDataByOidAndNamespace

public java.io.InputStream retrieveEntityDataByOidAndNamespace(OID oid,
                                                               Namespace namespace)
Loads an object's serialization data from the database.

Parameters:
oid - The object oid of the object to be loaded.
namespace - The namespace containing the object.
Returns:
an InputStream that is the object data as stored by serialization. This can really be any type, such as Mob, Npc, User, Structure, or Item. If object with oid is not found, an AORuntimeException is thrown.

retrieveEntityDataByPersistenceKey

public java.io.InputStream retrieveEntityDataByPersistenceKey(java.lang.String persistenceKey)
Loads the object with the given persistence key from the database. Persistence keys provide a way to name an object in the database so as a game developer you don't need to hardwire knowledge of particular oids.

Parameters:
persistenceKey - The string key used to locate the object in the objstore table.
Returns:
an InputStream that is the object data as stored by serialization. This can really be any type, such as Mob, Npc, User, Structure, or Item. If object with oid is not found, an AORuntimeException is thrown.

getOidByName

public OID getOidByName(java.lang.String name,
                        Namespace namespace)

getOidByName

public OID getOidByName(java.lang.String name,
                        Namespace namespace,
                        OID instanceOid)

getObjectName

public java.lang.String getObjectName(OID oid,
                                      Namespace namespace)
Get object name. Name is taken from the database 'name' column.

Returns:
Object name or null if oid or namespace is not found.

getOidsAndNamesMatchingName

public java.util.List<java.lang.Object> getOidsAndNamesMatchingName(java.lang.String playerName,
                                                                    boolean exactMatch)
Get the oids and names matching the name supplied, base on the name string to match, and whether to return based on a starting match of an exact match. In any case, it returns a list of two lists, the first being the oids and the second being the names.

Parameters:
playerName - The name string to match.
exactMatch - If true, the names returned must be an exact match. If false, the names returned must start with the playerName.
Returns:
A List whose first element is the List of oids, and whose second element is the List of names matching.

getObjectNames

public java.util.List<java.lang.String> getObjectNames(java.util.List<OID> inputOids,
                                                       Namespace namespace,
                                                       java.lang.String unknownName)
Get object names for the given oids. One object name is returned for each oid in inputOids. The value of unknownName is returned for non-existent objects.

Parameters:
inputOids - The oids for which to get names.
namespace - The sub-object namespace.
unknownName - Value returned for non-existent objects.
Returns:
Object names ordered to matching the inputOids.

characterNameTaken

public boolean characterNameTaken(java.lang.String name)
Get user name. Checks ID, returns user name if there is a match, otherwise returns null.

Parameters:
uid - The user oid.
Returns:
The string username corresponding to the oid, or null if not found.

makeOidCollectionString

public static java.lang.String makeOidCollectionString(java.util.List<OID> oids)

makeNameCollectionString

public static java.lang.String makeNameCollectionString(java.util.Collection<java.lang.String> names)

getObjectNamespaces

public java.util.List<Namespace> getObjectNamespaces(OID oid)

getInstanceContent

public java.util.List<OID> getInstanceContent(OID instanceOid,
                                              ObjectType exclusion)

deleteObjectData

public void deleteObjectData(OID oid)
Delete object and all its sub-objects from the objstore table

Parameters:
oid - The object oid to be deleted.

deleteObjectData

public void deleteObjectData(OID oid,
                             Namespace namespace)
Delete a sub-object from the objstore table

Parameters:
oid - The object oid to delete.
namespace - The sub-object namespace to delete.

deletePlayerCharacter

public void deletePlayerCharacter(OID oid)
Delete the user with the given oid from the player_character table. Note: This can only be done _after_ the oid/namespace pair has been deleted from the objstore table.

Parameters:
oid - The oid of the player to be deleted.

saveObject

public void saveObject(Entity entity,
                       Namespace namespace)
Save the entity in the given namespace

Parameters:
entity - The Entity object to be saved.
namespace - The namespace in which to save the entity.

saveObject

public void saveObject(java.lang.String persistenceKey,
                       byte[] data,
                       Namespace namespace)
Helper method. Deserializes the object into an entity before turning it into XML this may seem backwards, but we need to get some metadata out of the object before we save it.

Parameters:
persistenceKey - The key under which to save the object.
data - The bytes of data to be saved. They represent an Entity, and the Entity contains its own oid.
namespace - The namespace into which to save the reconstructed entity.

saveObject

public void saveObject(java.lang.String persistenceKey,
                       Entity entity,
                       Namespace namespace)
FIXME: This happens on the object manager- we dont need to lock the quad tree anymore saves object and all objects owned by this object into the database. if the object already exists, it will overwrite with a newer version. if the object is new, will create a new row and also set the dbid into the obj itself (obj.getDBid() will return the new id). DOES NOT MARK OBJECT AS PERSISTENT. DOES NOT CHECK FLAG.


encodeEntity

protected void encodeEntity(java.io.ByteArrayOutputStream ba,
                            Entity entity)
Write the representation of the entity into the ByteArrayOutputStream.

Parameters:
ba - The ByteArrayOutputStream to which the entity will be serialized.
entity - The entity to be serialized.

saveObjectHelper

public void saveObjectHelper(java.lang.String persistenceKey,
                             Entity entity,
                             Namespace namespace,
                             byte[] data)
A helper method that saves the byte array in the namespace with the oid supplied by the entity.

Parameters:
persistenceKey - The string key to associate with the object. For most objects, this is null.
entity - The entity whose state is represented by the data.
namespace - The namespace into which to save the object state.
data - The serialized state of the entity.

getGameIDs

public java.util.List<OID> getGameIDs(java.lang.String worldName,
                                      OID atavismID)
Each Atavism account may have more than 1 character for a given world. This function returns a list of "game_id"s each of which represent an individual character.

Parameters:
worldName - The name of the world containing the characters.
atavismID - The Atavism account id of the user.
Returns:
a list of Integer. the game_id is the dbid in the database record.

getAccountCount

public int getAccountCount(java.lang.String worldName)

getUserName

public java.lang.String getUserName(OID uid)
Get user name. Checks ID, returns user name if there is a match, otherwise returns null.

Parameters:
uid - The user oid.
Returns:
The string username corresponding to the oid, or null if not found.

getLocation

public OID getLocation(OID oid,
                       Namespace ns,
                       Point location)
Get object location. The namespace is usually WorldManagerClient.NAMESPACE.

Parameters:
oid - Object oid.
ns - Namespace from which to get location.
location - Returns the object location.
Returns:
Object instance oid, or null if object does not exist.

getOidChunk

public Database.OidChunk getOidChunk(int chunkSize)
Returns a block of oids, and record the fact that they have been allocated in the database.

Parameters:
chunkSize - The number oids to return.
Returns:
An OidChunk instance giving the first and last oid allocated.

registerStatusReportingPlugin

public boolean registerStatusReportingPlugin(EnginePlugin plugin,
                                             long runId)
Register a plugin


unregisterStatusReportingPlugin

public boolean unregisterStatusReportingPlugin(EnginePlugin plugin)
Unregister the plugin for status reporting


updatePluginStatus

public boolean updatePluginStatus(EnginePlugin plugin,
                                  long nextUpdateTime)
Update the status of a plugin


getPluginStatus

public java.util.List<PluginStatus> getPluginStatus(java.lang.String pluginType)
Get the contents of the database row into a PluginStatus instance. If the plugin is non-null, get for that plugin, else get for all plugins


ping

public void ping()
Runs a select statement to make sure we can still talk to the database.



Copyright © 2018 Dragonsan Studios Sp. z o.o.