add mysql code

master
Christopher 2020-07-08 23:52:52 +02:00
parent e0ff4ffbdf
commit f60094505b
3 changed files with 119 additions and 36 deletions

View File

@ -12,6 +12,7 @@
<ReferencePath>../../../bin</ReferencePath> <ReferencePath>../../../bin</ReferencePath>
<Reference name="System" localCopy="false"/> <Reference name="System" localCopy="false"/>
<Reference name="System.Data"/>
<Reference name="System.Xml"/> <Reference name="System.Xml"/>
<Reference name="log4net.dll" path="../../../bin"/> <Reference name="log4net.dll" path="../../../bin"/>
<Reference name="Nini.dll" path="../../../bin"/> <Reference name="Nini.dll" path="../../../bin"/>
@ -25,6 +26,7 @@
<Reference name="OpenSim.Services.Interfaces" path="../../../bin"/> <Reference name="OpenSim.Services.Interfaces" path="../../../bin"/>
<Reference name="OpenSim.Server.Base" path="../../../bin"/> <Reference name="OpenSim.Server.Base" path="../../../bin"/>
<Reference name="OpenSim.Data" path="../../../bin"/> <Reference name="OpenSim.Data" path="../../../bin"/>
<Reference name="MySQL.Data" path="../../../bin"/>
<Files> <Files>
<Match pattern="*.cs" recurse="true"/> <Match pattern="*.cs" recurse="true"/>
</Files> </Files>

View File

@ -98,6 +98,8 @@ namespace OpenSim.Modules.DataValue
if (m_storage == null) if (m_storage == null)
m_storage = new Memory(); m_storage = new Memory();
m_log.Info("[" + Name + "] Using '" + m_storageTyp + "' as Storage.");
m_scriptModule = m_scene.RequestModuleInterface<IScriptModuleComms>(); m_scriptModule = m_scene.RequestModuleInterface<IScriptModuleComms>();
if (m_scriptModule != null) if (m_scriptModule != null)
{ {
@ -121,8 +123,6 @@ namespace OpenSim.Modules.DataValue
public string osGetDataValue(UUID hostID, UUID scriptID, string key) public string osGetDataValue(UUID hostID, UUID scriptID, string key)
{ {
if(m_storage != null) if(m_storage != null)
{
try
{ {
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key);
@ -132,6 +132,8 @@ namespace OpenSim.Modules.DataValue
checkRateLimit(); checkRateLimit();
try
{
String _data = m_storage.get(_host.GroupID.ToString(), key); String _data = m_storage.get(_host.GroupID.ToString(), key);
if (_data == null) if (_data == null)
@ -154,8 +156,6 @@ namespace OpenSim.Modules.DataValue
public void osSetDataValue(UUID hostID, UUID scriptID, string key, string value) public void osSetDataValue(UUID hostID, UUID scriptID, string key, string value)
{ {
if (m_storage != null) if (m_storage != null)
{
try
{ {
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key);
@ -168,6 +168,8 @@ namespace OpenSim.Modules.DataValue
checkRateLimit(); checkRateLimit();
try
{
m_cache.Add(new StorageElement(_host.GroupID.ToString(), key, value, m_storage)); m_cache.Add(new StorageElement(_host.GroupID.ToString(), key, value, m_storage));
m_storage.save(_host.GroupID.ToString(), key, value); m_storage.save(_host.GroupID.ToString(), key, value);
return; return;
@ -182,16 +184,16 @@ namespace OpenSim.Modules.DataValue
} }
[ScriptInvocation] [ScriptInvocation]
public void osDeleteDataValue(UUID hostID, UUID scriptID, string key, string value) public void osDeleteDataValue(UUID hostID, UUID scriptID, string key, string value)
{
if (m_storage != null)
{
try
{ {
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key);
checkRateLimit(); checkRateLimit();
if (m_storage != null)
{
try
{
if (_element != null) if (_element != null)
m_cache.Remove(_element); m_cache.Remove(_element);
@ -211,8 +213,6 @@ namespace OpenSim.Modules.DataValue
public int osCheckDataValue(UUID hostID, UUID scriptID, string key) public int osCheckDataValue(UUID hostID, UUID scriptID, string key)
{ {
if (m_storage != null) if (m_storage != null)
{
try
{ {
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key);
@ -222,6 +222,8 @@ namespace OpenSim.Modules.DataValue
checkRateLimit(); checkRateLimit();
try
{
if (m_storage.check(_host.GroupID.ToString(), key)) if (m_storage.check(_host.GroupID.ToString(), key))
return 1; return 1;

View File

@ -1,12 +1,15 @@
using log4net; using log4net;
using MySql.Data.MySqlClient;
using Nini.Config; using Nini.Config;
using OpenSim.Region.Framework.Scenes; using OpenSim.Region.Framework.Scenes;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Timers;
namespace OpenSim.Modules.DataValue.Storage namespace OpenSim.Modules.DataValue.Storage
{ {
@ -15,30 +18,106 @@ namespace OpenSim.Modules.DataValue.Storage
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private Scene m_scene = null; private Scene m_scene = null;
private Timer m_timer = null;
private String m_connectionString = null;
private MySqlConnection m_mySQLClient = null;
public MySQL(Scene scene, IConfig config) public MySQL(Scene scene, IConfig config)
{ {
m_scene = scene; m_scene = scene;
m_connectionString = config.GetString("DataValueConnectionString", String.Empty).ToUpper().Trim();
if (m_connectionString == String.Empty)
return;
m_timer = new Timer();
m_timer.Interval = 10000;
m_timer.Elapsed += mysqlping;
m_timer.Start();
m_mySQLClient = new MySqlConnection(m_connectionString);
m_mySQLClient.Open();
createEmptyTable();
}
private void mysqlping(object sender, ElapsedEventArgs e)
{
if (!m_mySQLClient.Ping())
m_mySQLClient.Open();
} }
public bool check(String storageID, string key) public bool check(String storageID, string key)
{ {
throw new NotImplementedException(); using (MySqlCommand _mysqlCommand = m_mySQLClient.CreateCommand())
{
_mysqlCommand.CommandText = "Select StorageID, StorageKey FROM ?mysqlTable WHERE StorageID = ?mysqlStorage AND StorageKey = ?mysqlStorageKey";
_mysqlCommand.Parameters.AddWithValue("?mysqlTable", "StorageData");
_mysqlCommand.Parameters.AddWithValue("?mysqlStorage", storageID);
_mysqlCommand.Parameters.AddWithValue("?mysqlStorageKey", key);
using (IDataReader _mysqlReader = _mysqlCommand.ExecuteReader())
{
if (_mysqlReader["StorageKey"] != null)
return true;
return false;
}
}
} }
public string get(String storageID, string key) public string get(String storageID, string key)
{ {
throw new NotImplementedException(); using (MySqlCommand _mysqlCommand = m_mySQLClient.CreateCommand())
{
_mysqlCommand.CommandText = "Select StorageID, StorageKey, StorageData FROM ?mysqlTable WHERE StorageID = ?mysqlStorage AND StorageKey = ?mysqlStorageKey";
_mysqlCommand.Parameters.AddWithValue("?mysqlTable", "StorageData");
_mysqlCommand.Parameters.AddWithValue("?mysqlStorage", storageID);
_mysqlCommand.Parameters.AddWithValue("?mysqlStorageKey", key);
using (IDataReader _mysqlReader = _mysqlCommand.ExecuteReader())
{
if (_mysqlReader["StorageData"] != null)
return _mysqlReader["StorageData"].ToString();
return null;
}
}
} }
public void remove(string storageID, string key) public void remove(string storageID, string key)
{ {
throw new NotImplementedException(); using (MySqlCommand _mysqlCommand = m_mySQLClient.CreateCommand())
{
_mysqlCommand.CommandText = "DELETE FROM ?mysqlTable WHERE StorageID = ?mysqlStorage AND StorageKey = ?mysqlStorageKey";
_mysqlCommand.Parameters.AddWithValue("?mysqlTable", "StorageData");
_mysqlCommand.Parameters.AddWithValue("?mysqlStorage", storageID);
_mysqlCommand.Parameters.AddWithValue("?mysqlStorageKey", key);
_mysqlCommand.ExecuteNonQuery();
}
} }
public void save(String storageID, string key, string data) public void save(String storageID, string key, string data)
{ {
throw new NotImplementedException(); using (MySqlCommand _mysqlCommand = m_mySQLClient.CreateCommand())
{
_mysqlCommand.CommandText = "REPLACE INTO ?mysqlTable (StorageID, StorageKey, StorageData) VALUES (?mysqlStorage, ?mysqlStorageKey, ?mysqlStorageData)";
_mysqlCommand.Parameters.AddWithValue("?mysqlTable", "StorageData");
_mysqlCommand.Parameters.AddWithValue("?mysqlStorage", storageID);
_mysqlCommand.Parameters.AddWithValue("?mysqlStorageKey", key);
_mysqlCommand.Parameters.AddWithValue("?mysqlStorageData", data);
_mysqlCommand.ExecuteNonQuery();
}
}
private void createEmptyTable()
{
using (MySqlCommand _mysqlCommand = m_mySQLClient.CreateCommand())
{
_mysqlCommand.CommandText = "CREATE TABLE IF NOT EXISTS `StorageData` (`StorageID` VARCHAR(36) NOT NULL, `StorageKey` VARCHAR(256) NOT NULL, `StorageData` TEXT NOT NULL DEFAULT '', PRIMARY KEY(`StorageID`, `StorageKey`)) COLLATE = 'utf8_general_ci'; ";
_mysqlCommand.ExecuteNonQuery();
}
} }
} }
} }