NHibernateAssetData.cs 7.0 KB

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