PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Aurora/Services/DataService/Connectors/Database/Avatar/LocalAvatarConnector.cs

https://bitbucket.org/VirtualReality/aurora-sim
C# | 127 lines | 86 code | 14 blank | 27 comment | 5 complexity | c8cebcef04593bb6f48c4119f64d58e8 MD5 | raw file
  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the Aurora-Sim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System.Collections.Generic;
  28. using System.Linq;
  29. using Aurora.Framework;
  30. using Nini.Config;
  31. using OpenMetaverse;
  32. using OpenSim.Services.Interfaces;
  33. namespace Aurora.Services.DataService
  34. {
  35. public class LocalAvatarConnector : IAvatarData
  36. {
  37. private IGenericData GD;
  38. private string m_realm = "avatars";
  39. //private string m_cacherealm = "avatarscache";
  40. private PreAddedDictionary<UUID, object> m_locks = new PreAddedDictionary<UUID, object>(() => new object());
  41. #region IAvatarData Members
  42. public void Initialize(IGenericData GenericData, IConfigSource source, IRegistryCore simBase,
  43. string defaultConnectionString)
  44. {
  45. if (source.Configs["AuroraConnectors"].GetString("AvatarConnector", "LocalConnector") == "LocalConnector")
  46. {
  47. GD = GenericData;
  48. string connectionString = defaultConnectionString;
  49. if (source.Configs[Name] != null)
  50. connectionString = source.Configs[Name].GetString("ConnectionString", defaultConnectionString);
  51. GD.ConnectToDatabase(connectionString, "Avatars",
  52. source.Configs["AuroraConnectors"].GetBoolean("ValidateTables", true));
  53. DataManager.DataManager.RegisterPlugin(this);
  54. }
  55. }
  56. public string Name
  57. {
  58. get { return "IAvatarData"; }
  59. }
  60. public AvatarData Get(string field, string val)
  61. {
  62. return InternalGet(m_realm, field, val);
  63. }
  64. public bool Store(UUID PrincipalID, AvatarData data)
  65. {
  66. lock (m_locks[PrincipalID])
  67. {
  68. QueryFilter filter = new QueryFilter();
  69. filter.andFilters["PrincipalID"] = PrincipalID;
  70. GD.Delete(m_realm, filter);
  71. List<object[]> insertList = new List<object[]>();
  72. foreach (KeyValuePair<string, string> kvp in data.Data)
  73. {
  74. insertList.Add(new object[3]{
  75. PrincipalID,
  76. kvp.Key,
  77. kvp.Value
  78. });
  79. }
  80. GD.InsertMultiple(m_realm, insertList);
  81. }
  82. return true;
  83. }
  84. public bool Delete(string field, string val)
  85. {
  86. QueryFilter filter = new QueryFilter();
  87. filter.andFilters[field] = val;
  88. return GD.Delete(m_realm, filter);
  89. }
  90. #endregion
  91. public void Dispose()
  92. {
  93. }
  94. private AvatarData InternalGet(string realm, string field, string val)
  95. {
  96. QueryFilter filter = new QueryFilter();
  97. filter.andFilters[field] = val;
  98. List<string> data = GD.Query(new string[]{
  99. "Name",
  100. "`Value`"
  101. }, realm, filter, null, null, null);
  102. AvatarData retVal = new AvatarData {
  103. AvatarType = 1,
  104. Data = new Dictionary<string, string>()
  105. };
  106. for (int i = 0; i < data.Count; i += 2)
  107. {
  108. retVal.Data[data[i]] = data[i + 1];
  109. }
  110. return retVal;
  111. }
  112. }
  113. }