NHibernateAssetData.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.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 OpenSim 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;
  28. using System.IO;
  29. using System.Reflection;
  30. using System.Text.RegularExpressions;
  31. using libsecondlife;
  32. using log4net;
  33. using NHibernate;
  34. using NHibernate.Cfg;
  35. using NHibernate.Mapping.Attributes;
  36. using NHibernate.Tool.hbm2ddl;
  37. using OpenSim.Framework;
  38. using Environment=NHibernate.Cfg.Environment;
  39. namespace OpenSim.Data.NHibernate
  40. {
  41. /// <summary>
  42. /// A User storage interface for the DB4o database system
  43. /// </summary>
  44. public class NHibernateAssetData : AssetDataBase
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private Configuration cfg;
  48. private ISessionFactory factory;
  49. private ISession session;
  50. override public void Dispose() { }
  51. public override void Initialise()
  52. {
  53. Initialise("SQLiteDialect;SqliteClientDriver;URI=file:Asset.db,version=3");
  54. }
  55. public override void Initialise(string connect)
  56. {
  57. // Split out the dialect, driver, and connect string
  58. char[] split = {';'};
  59. string[] parts = connect.Split(split, 3);
  60. if (parts.Length != 3)
  61. {
  62. // TODO: make this a real exception type
  63. throw new Exception("Malformed Inventory connection string '" + connect + "'");
  64. }
  65. string dialect = parts[0];
  66. // NHibernate setup
  67. cfg = new Configuration();
  68. cfg.SetProperty(Environment.ConnectionProvider,
  69. "NHibernate.Connection.DriverConnectionProvider");
  70. cfg.SetProperty(Environment.Dialect,
  71. "NHibernate.Dialect." + dialect);
  72. cfg.SetProperty(Environment.ConnectionDriver,
  73. "NHibernate.Driver." + parts[1]);
  74. cfg.SetProperty(Environment.ConnectionString, parts[2]);
  75. cfg.AddAssembly("OpenSim.Data.NHibernate");
  76. HbmSerializer.Default.Validate = true;
  77. using (MemoryStream stream =
  78. HbmSerializer.Default.Serialize(Assembly.GetExecutingAssembly()))
  79. cfg.AddInputStream(stream);
  80. factory = cfg.BuildSessionFactory();
  81. session = factory.OpenSession();
  82. // This actually does the roll forward assembly stuff
  83. Assembly assem = GetType().Assembly;
  84. Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, "AssetStore");
  85. m.Update();
  86. }
  87. override public AssetBase FetchAsset(LLUUID uuid)
  88. {
  89. try
  90. {
  91. return session.Load(typeof(AssetBase), uuid) as AssetBase;
  92. }
  93. catch (Exception e)
  94. {
  95. m_log.Error("[NHIBERNATE] issue loading asset", e);
  96. return null;
  97. }
  98. }
  99. override public void CreateAsset(AssetBase asset)
  100. {
  101. if (!ExistsAsset(asset.FullID))
  102. {
  103. m_log.InfoFormat("[NHIBERNATE] inserting asset {0}", asset.FullID);
  104. using (ITransaction transaction = session.BeginTransaction())
  105. {
  106. session.Save(asset);
  107. transaction.Commit();
  108. }
  109. }
  110. }
  111. override public void UpdateAsset(AssetBase asset)
  112. {
  113. if (ExistsAsset(asset.FullID))
  114. {
  115. using (ITransaction transaction = session.BeginTransaction())
  116. {
  117. session.Update(asset);
  118. transaction.Commit();
  119. }
  120. }
  121. }
  122. // private void LogAssetLoad(AssetBase asset)
  123. // {
  124. // string temporary = asset.Temporary ? "Temporary" : "Stored";
  125. // string local = asset.Local ? "Local" : "Remote";
  126. // int assetLength = (asset.Data != null) ? asset.Data.Length : 0;
  127. // m_log.Info("[SQLITE]: " +
  128. // string.Format("Loaded {6} {5} Asset: [{0}][{3}/{4}] \"{1}\":{2} ({7} bytes)",
  129. // asset.FullID, asset.Name, asset.Description, asset.Type,
  130. // asset.InvType, temporary, local, assetLength));
  131. // }
  132. override public bool ExistsAsset(LLUUID uuid)
  133. {
  134. m_log.InfoFormat("[NHIBERNATE] ExistsAsset: {0}", uuid);
  135. return (FetchAsset(uuid) != null);
  136. }
  137. public void DeleteAsset(LLUUID uuid)
  138. {
  139. }
  140. public override string Name {
  141. get { return "NHibernate"; }
  142. }
  143. public override string Version {
  144. get { return "0.1"; }
  145. }
  146. }
  147. }