NHibernateAssetData.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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, IDisposable
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private Configuration cfg;
  48. private ISessionFactory factory;
  49. public override void Initialise()
  50. {
  51. Initialise("SQLiteDialect;SqliteClientDriver;URI=file:Asset.db,version=3");
  52. }
  53. public override void Initialise(string connect)
  54. {
  55. // Split out the dialect, driver, and connect string
  56. char[] split = {';'};
  57. string[] parts = connect.Split(split, 3);
  58. if (parts.Length != 3)
  59. {
  60. // TODO: make this a real exception type
  61. throw new Exception("Malformed Inventory connection string '" + connect + "'");
  62. }
  63. // NHibernate setup
  64. cfg = new Configuration();
  65. cfg.SetProperty(Environment.ConnectionProvider,
  66. "NHibernate.Connection.DriverConnectionProvider");
  67. cfg.SetProperty(Environment.Dialect,
  68. "NHibernate.Dialect." + parts[0]);
  69. cfg.SetProperty(Environment.ConnectionDriver,
  70. "NHibernate.Driver." + parts[1]);
  71. cfg.SetProperty(Environment.ConnectionString, parts[2]);
  72. cfg.AddAssembly("OpenSim.Data.NHibernate");
  73. HbmSerializer.Default.Validate = true;
  74. using (MemoryStream stream =
  75. HbmSerializer.Default.Serialize(Assembly.GetExecutingAssembly()))
  76. cfg.AddInputStream(stream);
  77. factory = cfg.BuildSessionFactory();
  78. // If uncommented this will auto create tables, but it
  79. // does drops of the old tables, so we need a smarter way
  80. // to acturally manage this.
  81. // new SchemaExport(cfg).Create(true, true);
  82. InitDB();
  83. }
  84. private void InitDB()
  85. {
  86. string regex = @"no such table: Assets";
  87. Regex RE = new Regex(regex, RegexOptions.Multiline);
  88. try
  89. {
  90. using (ISession session = factory.OpenSession())
  91. {
  92. session.Load(typeof(AssetBase), LLUUID.Zero);
  93. }
  94. }
  95. catch (ObjectNotFoundException)
  96. {
  97. // yes, we know it's not there, but that's ok
  98. }
  99. catch (ADOException e)
  100. {
  101. Match m = RE.Match(e.ToString());
  102. if (m.Success)
  103. {
  104. // We don't have this table, so create it.
  105. new SchemaExport(cfg).Create(true, true);
  106. }
  107. }
  108. }
  109. override public AssetBase FetchAsset(LLUUID uuid)
  110. {
  111. using (ISession session = factory.OpenSession())
  112. {
  113. try
  114. {
  115. return session.Load(typeof(AssetBase), uuid) as AssetBase;
  116. }
  117. catch
  118. {
  119. return null;
  120. }
  121. }
  122. }
  123. override public void CreateAsset(AssetBase asset)
  124. {
  125. if (!ExistsAsset(asset.FullID))
  126. {
  127. using (ISession session = factory.OpenSession())
  128. {
  129. using (ITransaction transaction = session.BeginTransaction())
  130. {
  131. session.Save(asset);
  132. transaction.Commit();
  133. }
  134. }
  135. }
  136. }
  137. override public void UpdateAsset(AssetBase asset)
  138. {
  139. if (ExistsAsset(asset.FullID))
  140. {
  141. using (ISession session = factory.OpenSession())
  142. {
  143. using (ITransaction transaction = session.BeginTransaction())
  144. {
  145. session.Update(asset);
  146. transaction.Commit();
  147. }
  148. }
  149. }
  150. }
  151. private void LogAssetLoad(AssetBase asset)
  152. {
  153. string temporary = asset.Temporary ? "Temporary" : "Stored";
  154. string local = asset.Local ? "Local" : "Remote";
  155. int assetLength = (asset.Data != null) ? asset.Data.Length : 0;
  156. m_log.Info("[SQLITE]: " +
  157. string.Format("Loaded {6} {5} Asset: [{0}][{3}/{4}] \"{1}\":{2} ({7} bytes)",
  158. asset.FullID, asset.Name, asset.Description, asset.Type,
  159. asset.InvType, temporary, local, assetLength));
  160. }
  161. override public bool ExistsAsset(LLUUID uuid)
  162. {
  163. return (FetchAsset(uuid) != null) ? true : false;
  164. }
  165. public void DeleteAsset(LLUUID uuid)
  166. {
  167. }
  168. override public void CommitAssets() // force a sync to the database
  169. {
  170. m_log.Info("[SQLITE]: Attempting commit");
  171. }
  172. public override string Name {
  173. get { return "NHibernate"; }
  174. }
  175. public override string Version {
  176. get { return "0.1"; }
  177. }
  178. public void Dispose()
  179. {
  180. }
  181. }
  182. }