NHibernateRegionData.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Text.RegularExpressions;
  33. using libsecondlife;
  34. using log4net;
  35. using NHibernate;
  36. using NHibernate.Cfg;
  37. using NHibernate.Expression;
  38. using NHibernate.Mapping.Attributes;
  39. using NHibernate.Tool.hbm2ddl;
  40. using OpenSim.Framework;
  41. using OpenSim.Region.Environment.Interfaces;
  42. using OpenSim.Region.Environment.Scenes;
  43. using Environment=NHibernate.Cfg.Environment;
  44. namespace OpenSim.Data.NHibernate
  45. {
  46. /// <summary>
  47. /// A RegionData Interface to the NHibernate database
  48. /// </summary>
  49. public class NHibernateRegionData : IRegionDataStore
  50. {
  51. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. private Configuration cfg;
  53. private ISessionFactory factory;
  54. private ISession session;
  55. // public void Initialise()
  56. // {
  57. // Initialise("SQLiteDialect;SqliteClientDriver;URI=file:OpenSim.db,version=3", true);
  58. // }
  59. public void Initialise(string connect, bool persistpriminventories)
  60. {
  61. // Split out the dialect, driver, and connect string
  62. char[] split = {';'};
  63. string[] parts = connect.Split(split, 3);
  64. if (parts.Length != 3)
  65. {
  66. // TODO: make this a real exception type
  67. throw new Exception("Malformed Region connection string '" + connect + "'");
  68. }
  69. string dialect = parts[0];
  70. // NHibernate setup
  71. cfg = new Configuration();
  72. cfg.SetProperty(Environment.ConnectionProvider,
  73. "NHibernate.Connection.DriverConnectionProvider");
  74. cfg.SetProperty(Environment.Dialect,
  75. "NHibernate.Dialect." + dialect);
  76. cfg.SetProperty(Environment.ConnectionDriver,
  77. "NHibernate.Driver." + parts[1]);
  78. cfg.SetProperty(Environment.ConnectionString, parts[2]);
  79. cfg.AddAssembly("OpenSim.Data.NHibernate");
  80. HbmSerializer.Default.Validate = true;
  81. using (MemoryStream stream =
  82. HbmSerializer.Default.Serialize(Assembly.GetExecutingAssembly()))
  83. cfg.AddInputStream(stream);
  84. factory = cfg.BuildSessionFactory();
  85. session = factory.OpenSession();
  86. // This actually does the roll forward assembly stuff
  87. Assembly assem = GetType().Assembly;
  88. Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, "RegionStore");
  89. m.Update();
  90. }
  91. /***********************************************************************
  92. *
  93. * Public Interface Functions
  94. *
  95. **********************************************************************/
  96. public void StoreRegionSettings(RegionSettings rs)
  97. {
  98. }
  99. public RegionSettings LoadRegionSettings(LLUUID regionUUID)
  100. {
  101. return null;
  102. }
  103. private void SaveOrUpdate(SceneObjectPart p)
  104. {
  105. try
  106. {
  107. ICriteria criteria = session.CreateCriteria(typeof(SceneObjectPart));
  108. criteria.Add(Expression.Eq("UUID", p.UUID));
  109. ArrayList l = (ArrayList)criteria.List();
  110. if (l.Count < 1)
  111. {
  112. session.Save(p);
  113. }
  114. else if (l.Count == 1)
  115. {
  116. SceneObjectPart old = (SceneObjectPart)l[0];
  117. session.Evict(old);
  118. session.Update(p);
  119. }
  120. else
  121. {
  122. m_log.Error("Not unique");
  123. }
  124. }
  125. catch (Exception e)
  126. {
  127. m_log.Error("[NHIBERNATE] issue saving prim", e);
  128. }
  129. }
  130. private void SaveOrUpdate(Terrain t)
  131. {
  132. try
  133. {
  134. ICriteria criteria = session.CreateCriteria(typeof(Terrain));
  135. criteria.Add(Expression.Eq("RegionID", t.RegionID));
  136. ArrayList l = (ArrayList)criteria.List();
  137. if (l.Count < 1)
  138. {
  139. session.Save(t);
  140. }
  141. else if (l.Count == 1)
  142. {
  143. Terrain old = (Terrain)l[0];
  144. session.Evict(old);
  145. session.Update(t);
  146. }
  147. else
  148. {
  149. m_log.Error("Not unique");
  150. }
  151. }
  152. catch (Exception e)
  153. {
  154. m_log.Error("[NHIBERNATE] issue saving terrain", e);
  155. }
  156. }
  157. /// <summary>
  158. /// Adds an object into region storage
  159. /// </summary>
  160. /// <param name="obj">the object</param>
  161. /// <param name="regionUUID">the region UUID</param>
  162. public void StoreObject(SceneObjectGroup obj, LLUUID regionUUID)
  163. {
  164. try
  165. {
  166. foreach (SceneObjectPart part in obj.Children.Values)
  167. {
  168. m_log.InfoFormat("Storing part {0}", part.UUID);
  169. SaveOrUpdate(part);
  170. }
  171. session.Flush();
  172. }
  173. catch (Exception e)
  174. {
  175. m_log.Error("Can't save: ", e);
  176. }
  177. }
  178. private SceneObjectGroup LoadObject(LLUUID uuid, LLUUID region)
  179. {
  180. SceneObjectGroup group = new SceneObjectGroup();
  181. ICriteria criteria = session.CreateCriteria(typeof(SceneObjectPart));
  182. criteria.Add(Expression.Eq("RegionID", region));
  183. criteria.Add(Expression.Eq("ParentUUID", uuid));
  184. criteria.AddOrder( Order.Asc("ParentID") );
  185. foreach (SceneObjectPart p in criteria.List())
  186. {
  187. // root part
  188. if (p.UUID == uuid)
  189. {
  190. group.AddPart(p);
  191. group.RootPart = p;
  192. }
  193. else
  194. {
  195. group.AddPart(p);
  196. }
  197. }
  198. return group;
  199. }
  200. /// <summary>
  201. /// Removes an object from region storage
  202. /// </summary>
  203. /// <param name="obj">the object</param>
  204. /// <param name="regionUUID">the region UUID</param>
  205. public void RemoveObject(LLUUID obj, LLUUID regionUUID)
  206. {
  207. SceneObjectGroup g = LoadObject(obj, regionUUID);
  208. foreach (SceneObjectPart p in g.Children.Values)
  209. {
  210. session.Delete(p);
  211. }
  212. session.Flush();
  213. m_log.InfoFormat("[REGION DB]: Removing obj: {0} from region: {1}", obj.UUID, regionUUID);
  214. }
  215. /// <summary>
  216. /// Load persisted objects from region storage.
  217. /// </summary>
  218. /// <param name="regionUUID">The region UUID</param>
  219. /// <returns>List of loaded groups</returns>
  220. public List<SceneObjectGroup> LoadObjects(LLUUID regionUUID)
  221. {
  222. Dictionary<LLUUID, SceneObjectGroup> SOG = new Dictionary<LLUUID, SceneObjectGroup>();
  223. List<SceneObjectGroup> ret = new List<SceneObjectGroup>();
  224. ICriteria criteria = session.CreateCriteria(typeof(SceneObjectPart));
  225. criteria.Add(Expression.Eq("RegionID", regionUUID));
  226. criteria.AddOrder( Order.Asc("ParentID") );
  227. foreach (SceneObjectPart p in criteria.List())
  228. {
  229. // root part
  230. if (p.UUID == p.ParentUUID)
  231. {
  232. SceneObjectGroup group = new SceneObjectGroup();
  233. group.AddPart(p);
  234. group.RootPart = p;
  235. SOG.Add(p.ParentUUID, group);
  236. }
  237. else
  238. {
  239. SOG[p.ParentUUID].AddPart(p);
  240. }
  241. }
  242. foreach (SceneObjectGroup g in SOG.Values)
  243. {
  244. ret.Add(g);
  245. }
  246. return ret;
  247. }
  248. /// <summary>
  249. /// Store a terrain revision in region storage
  250. /// </summary>
  251. /// <param name="ter">terrain heightfield</param>
  252. /// <param name="regionID">region UUID</param>
  253. public void StoreTerrain(double[,] ter, LLUUID regionID)
  254. {
  255. lock (this) {
  256. Terrain t = new Terrain(regionID, ter);
  257. SaveOrUpdate(t);
  258. }
  259. }
  260. /// <summary>
  261. /// Load the latest terrain revision from region storage
  262. /// </summary>
  263. /// <param name="regionID">the region UUID</param>
  264. /// <returns>Heightfield data</returns>
  265. public double[,] LoadTerrain(LLUUID regionID)
  266. {
  267. try
  268. {
  269. Terrain t = session.Load(typeof(Terrain), regionID) as Terrain;
  270. return t.Doubles;
  271. }
  272. catch (ObjectNotFoundException)
  273. {
  274. m_log.Info("No terrain yet");
  275. return null;
  276. }
  277. }
  278. /// <summary>
  279. ///
  280. /// </summary>
  281. /// <param name="globalID"></param>
  282. public void RemoveLandObject(LLUUID globalID)
  283. {
  284. }
  285. /// <summary>
  286. ///
  287. /// </summary>
  288. /// <param name="parcel"></param>
  289. public void StoreLandObject(ILandObject parcel)
  290. {
  291. }
  292. /// <summary>
  293. ///
  294. /// </summary>
  295. /// <param name="regionUUID"></param>
  296. /// <returns></returns>
  297. public List<LandData> LoadLandObjects(LLUUID regionUUID)
  298. {
  299. List<LandData> landDataForRegion = new List<LandData>();
  300. return landDataForRegion;
  301. }
  302. /// <summary>
  303. /// See <see cref="Commit"/>
  304. /// </summary>
  305. public void Shutdown()
  306. {
  307. session.Flush();
  308. }
  309. /// <summary>
  310. /// Load a region banlist
  311. /// </summary>
  312. /// <param name="regionUUID">the region UUID</param>
  313. /// <returns>The banlist</returns>
  314. public List<EstateBan> LoadRegionBanList(LLUUID regionUUID)
  315. {
  316. List<EstateBan> regionbanlist = new List<EstateBan>();
  317. return regionbanlist;
  318. }
  319. /// <summary>
  320. /// Add en entry into region banlist
  321. /// </summary>
  322. /// <param name="item"></param>
  323. public void AddToRegionBanlist(EstateBan item)
  324. {
  325. }
  326. /// <summary>
  327. /// remove an entry from the region banlist
  328. /// </summary>
  329. /// <param name="item"></param>
  330. public void RemoveFromRegionBanlist(EstateBan item)
  331. {
  332. }
  333. /// <summary>
  334. ///
  335. /// </summary>
  336. /// <param name="val"></param>
  337. /// <returns></returns>
  338. // private static Array serializeTerrain(double[,] val)
  339. // {
  340. // MemoryStream str = new MemoryStream(65536*sizeof (double));
  341. // BinaryWriter bw = new BinaryWriter(str);
  342. //
  343. // // TODO: COMPATIBILITY - Add byte-order conversions
  344. // for (int x = 0; x < 256; x++)
  345. // for (int y = 0; y < 256; y++)
  346. // bw.Write(val[x, y]);
  347. //
  348. // return str.ToArray();
  349. // }
  350. /// <summary>
  351. /// see IRegionDatastore
  352. /// </summary>
  353. /// <param name="primID"></param>
  354. /// <param name="items"></param>
  355. public void StorePrimInventory(LLUUID primID, ICollection<TaskInventoryItem> items)
  356. {
  357. }
  358. }
  359. }