NHibernateRegionData.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Text.RegularExpressions;
  32. using libsecondlife;
  33. using log4net;
  34. using NHibernate;
  35. using NHibernate.Cfg;
  36. using NHibernate.Mapping.Attributes;
  37. using NHibernate.Tool.hbm2ddl;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.Environment.Interfaces;
  40. using OpenSim.Region.Environment.Scenes;
  41. using Environment=NHibernate.Cfg.Environment;
  42. namespace OpenSim.Data.NHibernate
  43. {
  44. /// <summary>
  45. /// A RegionData Interface to the NHibernate database
  46. /// </summary>
  47. public class NHibernateRegionData : IRegionDataStore
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. private Configuration cfg;
  51. private ISessionFactory factory;
  52. private ISession session;
  53. public void Initialise()
  54. {
  55. Initialise("SQLiteDialect;SqliteClientDriver;URI=file:OpenSim.db,version=3", true);
  56. }
  57. public void Initialise(string connect, bool persistpriminventories)
  58. {
  59. // Split out the dialect, driver, and connect string
  60. char[] split = {';'};
  61. string[] parts = connect.Split(split, 3);
  62. if (parts.Length != 3)
  63. {
  64. // TODO: make this a real exception type
  65. throw new Exception("Malformed Inventory connection string '" + connect + "'");
  66. }
  67. string dialect = parts[0];
  68. // NHibernate setup
  69. cfg = new Configuration();
  70. cfg.SetProperty(Environment.ConnectionProvider,
  71. "NHibernate.Connection.DriverConnectionProvider");
  72. cfg.SetProperty(Environment.Dialect,
  73. "NHibernate.Dialect." + dialect);
  74. cfg.SetProperty(Environment.ConnectionDriver,
  75. "NHibernate.Driver." + parts[1]);
  76. cfg.SetProperty(Environment.ConnectionString, parts[2]);
  77. cfg.AddAssembly("OpenSim.Data.NHibernate");
  78. HbmSerializer.Default.Validate = true;
  79. using (MemoryStream stream =
  80. HbmSerializer.Default.Serialize(Assembly.GetExecutingAssembly()))
  81. cfg.AddInputStream(stream);
  82. factory = cfg.BuildSessionFactory();
  83. session = factory.OpenSession();
  84. // This actually does the roll forward assembly stuff
  85. Assembly assem = GetType().Assembly;
  86. Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, "AssetStore");
  87. m.Update();
  88. }
  89. /***********************************************************************
  90. *
  91. * Public Interface Functions
  92. *
  93. **********************************************************************/
  94. public void StoreRegionSettings(RegionSettings rs)
  95. {
  96. }
  97. public RegionSettings LoadRegionSettings(LLUUID regionUUID)
  98. {
  99. return null;
  100. }
  101. /// <summary>
  102. /// Adds an object into region storage
  103. /// </summary>
  104. /// <param name="obj">the object</param>
  105. /// <param name="regionUUID">the region UUID</param>
  106. public void StoreObject(SceneObjectGroup obj, LLUUID regionUUID)
  107. {
  108. NHPrim[] prims = (NHPrim[]) obj.GetParts();
  109. }
  110. /// <summary>
  111. /// Removes an object from region storage
  112. /// </summary>
  113. /// <param name="obj">the object</param>
  114. /// <param name="regionUUID">the region UUID</param>
  115. public void RemoveObject(LLUUID obj, LLUUID regionUUID)
  116. {
  117. m_log.InfoFormat("[REGION DB]: Removing obj: {0} from region: {1}", obj.UUID, regionUUID);
  118. }
  119. /// <summary>
  120. /// Load persisted objects from region storage.
  121. /// </summary>
  122. /// <param name="regionUUID">The region UUID</param>
  123. /// <returns>List of loaded groups</returns>
  124. public List<SceneObjectGroup> LoadObjects(LLUUID regionUUID)
  125. {
  126. List<SceneObjectGroup> prims = new List<SceneObjectGroup>();
  127. return prims;
  128. }
  129. /// <summary>
  130. /// Store a terrain revision in region storage
  131. /// </summary>
  132. /// <param name="ter">terrain heightfield</param>
  133. /// <param name="regionID">region UUID</param>
  134. public void StoreTerrain(double[,] ter, LLUUID regionID)
  135. {
  136. }
  137. /// <summary>
  138. /// Load the latest terrain revision from region storage
  139. /// </summary>
  140. /// <param name="regionID">the region UUID</param>
  141. /// <returns>Heightfield data</returns>
  142. public double[,] LoadTerrain(LLUUID regionID)
  143. {
  144. double[,] terret = new double[256,256];
  145. terret.Initialize();
  146. return terret;
  147. }
  148. /// <summary>
  149. ///
  150. /// </summary>
  151. /// <param name="globalID"></param>
  152. public void RemoveLandObject(LLUUID globalID)
  153. {
  154. }
  155. /// <summary>
  156. ///
  157. /// </summary>
  158. /// <param name="parcel"></param>
  159. public void StoreLandObject(ILandObject parcel)
  160. {
  161. }
  162. /// <summary>
  163. ///
  164. /// </summary>
  165. /// <param name="regionUUID"></param>
  166. /// <returns></returns>
  167. public List<LandData> LoadLandObjects(LLUUID regionUUID)
  168. {
  169. List<LandData> landDataForRegion = new List<LandData>();
  170. return landDataForRegion;
  171. }
  172. /// <summary>
  173. /// See <see cref="Commit"/>
  174. /// </summary>
  175. public void Shutdown()
  176. {
  177. }
  178. /// <summary>
  179. /// Load a region banlist
  180. /// </summary>
  181. /// <param name="regionUUID">the region UUID</param>
  182. /// <returns>The banlist</returns>
  183. public List<RegionBanListItem> LoadRegionBanList(LLUUID regionUUID)
  184. {
  185. List<RegionBanListItem> regionbanlist = new List<RegionBanListItem>();
  186. return regionbanlist;
  187. }
  188. /// <summary>
  189. /// Add en entry into region banlist
  190. /// </summary>
  191. /// <param name="item"></param>
  192. public void AddToRegionBanlist(RegionBanListItem item)
  193. {
  194. }
  195. /// <summary>
  196. /// remove an entry from the region banlist
  197. /// </summary>
  198. /// <param name="item"></param>
  199. public void RemoveFromRegionBanlist(RegionBanListItem item)
  200. {
  201. }
  202. /// <summary>
  203. ///
  204. /// </summary>
  205. /// <param name="val"></param>
  206. /// <returns></returns>
  207. private static Array serializeTerrain(double[,] val)
  208. {
  209. MemoryStream str = new MemoryStream(65536*sizeof (double));
  210. BinaryWriter bw = new BinaryWriter(str);
  211. // TODO: COMPATIBILITY - Add byte-order conversions
  212. for (int x = 0; x < 256; x++)
  213. for (int y = 0; y < 256; y++)
  214. bw.Write(val[x, y]);
  215. return str.ToArray();
  216. }
  217. /// <summary>
  218. /// see IRegionDatastore
  219. /// </summary>
  220. /// <param name="primID"></param>
  221. /// <param name="items"></param>
  222. public void StorePrimInventory(LLUUID primID, ICollection<TaskInventoryItem> items)
  223. {
  224. }
  225. }
  226. }