From f60094505be485be3c8405b3b9bbe1af5b677ff2 Mon Sep 17 00:00:00 2001 From: Christopher Date: Wed, 8 Jul 2020 23:52:52 +0200 Subject: [PATCH] add mysql code --- prebuild.xml | 2 + src/DataValue.cs | 66 +++++++++++++++++---------------- src/Storage/MySQL.cs | 87 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 119 insertions(+), 36 deletions(-) diff --git a/prebuild.xml b/prebuild.xml index 3420fc7..e4706e7 100644 --- a/prebuild.xml +++ b/prebuild.xml @@ -12,6 +12,7 @@ ../../../bin + @@ -25,6 +26,7 @@ + diff --git a/src/DataValue.cs b/src/DataValue.cs index 50a82bb..b9df10a 100644 --- a/src/DataValue.cs +++ b/src/DataValue.cs @@ -98,6 +98,8 @@ namespace OpenSim.Modules.DataValue if (m_storage == null) m_storage = new Memory(); + m_log.Info("[" + Name + "] Using '" + m_storageTyp + "' as Storage."); + m_scriptModule = m_scene.RequestModuleInterface(); if (m_scriptModule != null) { @@ -122,16 +124,16 @@ namespace OpenSim.Modules.DataValue { if(m_storage != null) { + SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); + StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); + + if (_element != null) + return _element.get(); + + checkRateLimit(); + try { - SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); - StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); - - if (_element != null) - return _element.get(); - - checkRateLimit(); - String _data = m_storage.get(_host.GroupID.ToString(), key); if (_data == null) @@ -155,19 +157,19 @@ namespace OpenSim.Modules.DataValue { if (m_storage != null) { + SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); + StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); + + if (_element != null) + { + _element.save(value); + return; + } + + checkRateLimit(); + try { - SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); - StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); - - if (_element != null) - { - _element.save(value); - return; - } - - checkRateLimit(); - m_cache.Add(new StorageElement(_host.GroupID.ToString(), key, value, m_storage)); m_storage.save(_host.GroupID.ToString(), key, value); return; @@ -183,15 +185,15 @@ namespace OpenSim.Modules.DataValue [ScriptInvocation] public void osDeleteDataValue(UUID hostID, UUID scriptID, string key, string value) { + SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); + StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); + + checkRateLimit(); + if (m_storage != null) { try { - SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); - StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); - - checkRateLimit(); - if (_element != null) m_cache.Remove(_element); @@ -212,16 +214,16 @@ namespace OpenSim.Modules.DataValue { if (m_storage != null) { + SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); + StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); + + if (_element != null) + return 1; + + checkRateLimit(); + try { - SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); - StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); - - if (_element != null) - return 1; - - checkRateLimit(); - if (m_storage.check(_host.GroupID.ToString(), key)) return 1; diff --git a/src/Storage/MySQL.cs b/src/Storage/MySQL.cs index 9878f77..9f8aa48 100644 --- a/src/Storage/MySQL.cs +++ b/src/Storage/MySQL.cs @@ -1,12 +1,15 @@ using log4net; +using MySql.Data.MySqlClient; using Nini.Config; using OpenSim.Region.Framework.Scenes; using System; using System.Collections.Generic; +using System.Data; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; +using System.Timers; 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 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) { 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) { - 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) { - 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) { - 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) { - 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(); + } } } }