add first version of other data storage typs

master
Christopher 2020-07-08 01:26:12 +02:00
parent ab7039177e
commit bd9124da1f
6 changed files with 189 additions and 50 deletions

View File

@ -32,6 +32,11 @@ namespace OpenSim.Modules.DataValue
private String m_storageTyp = null; private String m_storageTyp = null;
private iStorage m_storage = null; private iStorage m_storage = null;
private List<StorageElement> m_cache = new List<StorageElement>();
private Timer m_timer = null;
private int m_rateLimit = 0;
public string Name public string Name
{ {
get { return "DataValueModule"; } get { return "DataValueModule"; }
@ -58,13 +63,18 @@ namespace OpenSim.Modules.DataValue
{ {
m_config = source.Configs["XEngine"]; m_config = source.Configs["XEngine"];
m_storageTyp = m_config.GetString("DataStorageTyp", "RegionExtras").ToUpper().Trim(); m_storageTyp = m_config.GetString("DataStorageTyp", "FileSystem").ToUpper().Trim();
} }
catch (Exception e) catch (Exception e)
{ {
m_log.ErrorFormat("[" + Name + "]: initialization error: {0}", e.Message); m_log.ErrorFormat("[" + Name + "]: initialization error: {0}", e.Message);
return; return;
} }
m_timer = new Timer();
m_timer.Interval = 1000;
m_timer.Elapsed += cleanUp;
m_timer.Start();
} }
public void RegionLoaded(Scene scene) public void RegionLoaded(Scene scene)
@ -83,26 +93,16 @@ namespace OpenSim.Modules.DataValue
m_storage = new MySQL(m_scene, m_config); m_storage = new MySQL(m_scene, m_config);
if(m_storage == null) if(m_storage == null)
m_storage = new RegionExtras(m_scene, m_config); m_storage = new FileSystem(m_scene, m_config);
m_scriptModule = m_scene.RequestModuleInterface<IScriptModuleComms>(); m_scriptModule = m_scene.RequestModuleInterface<IScriptModuleComms>();
if (m_scriptModule == null) if (m_scriptModule != null)
{
m_log.ErrorFormat("[" + Name + "]: Failed to load IScriptModuleComms!");
return;
}
try
{ {
m_scriptModule.RegisterScriptInvocation(this, "osGetDataValue"); m_scriptModule.RegisterScriptInvocation(this, "osGetDataValue");
m_scriptModule.RegisterScriptInvocation(this, "osSetDataValue"); m_scriptModule.RegisterScriptInvocation(this, "osSetDataValue");
m_scriptModule.RegisterScriptInvocation(this, "osDeleteDataValue"); m_scriptModule.RegisterScriptInvocation(this, "osDeleteDataValue");
m_scriptModule.RegisterScriptInvocation(this, "osCheckDataValue"); m_scriptModule.RegisterScriptInvocation(this, "osCheckDataValue");
} }
catch (Exception e)
{
m_log.WarnFormat("[" + Name + "]: script method registration failed; {0}", e.Message);
}
} }
public void RemoveRegion(Scene scene) public void RemoveRegion(Scene scene)
@ -117,24 +117,109 @@ namespace OpenSim.Modules.DataValue
[ScriptInvocation] [ScriptInvocation]
public string osGetDataValue(UUID hostID, UUID scriptID, string key) public string osGetDataValue(UUID hostID, UUID scriptID, string key)
{ {
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();
String _data = m_storage.get(_host.GroupID.ToString(), key);
if (_data == null)
return "";
m_cache.Add(new StorageElement(_host.GroupID.ToString(), key, _data, m_storage));
return _data;
}
throw new Exception("No data Storage aviable.");
} }
[ScriptInvocation] [ScriptInvocation]
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)
{
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);
}
throw new Exception("No data Storage aviable.");
}
[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)
{
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);
m_storage.remove(_host.GroupID.ToString(), key);
}
throw new Exception("No data Storage aviable.");
} }
[ScriptInvocation] [ScriptInvocation]
public int osCheckDataValue(UUID hostID, UUID scriptID, string key) public int osCheckDataValue(UUID hostID, UUID scriptID, string key)
{ {
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();
if (m_storage.check(_host.GroupID.ToString(), key))
return 1;
return 0;
}
throw new Exception("No data Storage aviable.");
}
private void cleanUp(object sender, ElapsedEventArgs e)
{
if(m_rateLimit >= 0)
m_rateLimit = m_rateLimit - 100;
List<StorageElement> _allStorageElements = m_cache.FindAll(X => X.LastUse + 10 <= (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
foreach(StorageElement _element in _allStorageElements)
m_cache.Remove(_element);
}
private void checkRateLimit()
{
m_rateLimit++;
if (m_rateLimit >= 1000)
throw new Exception("Data storage rate limit reached!");
} }
#endregion #endregion

View File

@ -26,45 +26,34 @@ namespace OpenSim.Modules.DataValue.Storage
{ {
m_scene = scene; m_scene = scene;
m_dataValueDirectory = config.Configs["XEngine"].GetString("DataValueStorageDirectory", m_dataValueDirectory); m_dataValueDirectory = config.GetString("DataValueStorageDirectory", m_dataValueDirectory);
m_enabledCompress = config.Configs["XEngine"].GetBoolean("EnabledDataStorageCompressing", m_enabledCompress);
} }
public bool check(String storageID, string key) public bool check(String storageID, string key)
{ {
string _filePath = getFilePath(hostID, key); string _filePath = getFilePath(storageID, key);
FileInfo file = new FileInfo(_filePath); FileInfo file = new FileInfo(_filePath);
if (file.Exists) if (file.Exists)
return 1; return true;
return 0; return false;
} }
public string get(String storageID, string key) public String get(String storageID, string key)
{ {
string _filePath = getFilePath(hostID, key); FileInfo file = new FileInfo(getFilePath(storageID, key));
FileInfo file = new FileInfo(_filePath);
if (file.Exists) if (file.Exists)
{
if (m_enabledCompress)
return Compress.Unzip(File.ReadAllBytes(file.FullName));
return File.ReadAllText(file.FullName); return File.ReadAllText(file.FullName);
}
return null;
return "";
} }
public string remove(string storageID, string key) public void remove(string storageID, string key)
{ {
string _filePath = getFilePath(hostID, key); FileInfo file = new FileInfo(getFilePath(storageID, key));
FileInfo file = new FileInfo(_filePath);
if (file.Exists) if (file.Exists)
file.Delete(); file.Delete();
@ -72,21 +61,11 @@ namespace OpenSim.Modules.DataValue.Storage
public void save(String storageID, string key, string data) public void save(String storageID, string key, string data)
{ {
string _filePath = getFilePath(hostID, key); FileInfo file = new FileInfo(getFilePath(storageID, key));
File.WriteAllText(file.FullName, data);
FileInfo file = new FileInfo(_filePath);
if (m_enabledCompress)
{
File.WriteAllBytes(file.FullName, Compress.Zip(value));
return;
}
File.WriteAllText(file.FullName, value);
} }
}
private string getFilePath(UUID host, string index) private string getFilePath(String host, String index)
{ {
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(host); SceneObjectGroup _host = m_scene.GetSceneObjectGroup(host);

View File

@ -31,7 +31,7 @@ namespace OpenSim.Modules.DataValue.Storage
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string remove(string storageID, string key) public void remove(string storageID, string key)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -34,7 +34,7 @@ namespace OpenSim.Modules.DataValue.Storage
return m_scene.GetExtraSetting("V:" + storageID + "." + key); return m_scene.GetExtraSetting("V:" + storageID + "." + key);
} }
public string remove(string storageID, string key) public void remove(string storageID, string key)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -9,8 +9,8 @@ namespace OpenSim.Modules.DataValue.Storage
interface iStorage interface iStorage
{ {
void save(String storageID, String key, String data); void save(String storageID, String key, String data);
string get(String storageID, String key); String get(String storageID, String key);
string remove(String storageID, String key); void remove(String storageID, String key);
bool check(String storageID, String key); bool check(String storageID, String key);
} }
} }

75
src/StorageElement.cs Normal file
View File

@ -0,0 +1,75 @@
using OpenSim.Modules.DataValue.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenSim.Modules.DataValue
{
class StorageElement
{
private String m_group = null;
private String m_index = null;
private String m_data = null;
private int m_lastUseTime = 0;
private iStorage m_storage = null;
public StorageElement(String group, String index, String data, iStorage storage)
{
m_group = group;
m_index = index;
m_data = data;
m_storage = storage;
m_lastUseTime = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
}
public string get()
{
m_lastUseTime = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
return m_data;
}
public void remove()
{
m_lastUseTime = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
m_storage.remove(m_group, m_index);
}
public void save(string data)
{
m_lastUseTime = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
m_data = data;
m_storage.save(m_group, m_index, data);
}
public int LastUse
{
get
{
return m_lastUseTime;
}
}
public String Group
{
get
{
return m_group;
}
}
public String Index
{
get
{
return m_index;
}
}
}
}