From 4ed7ca62b153817a142ded621cc5c5c1b1c055c5 Mon Sep 17 00:00:00 2001 From: Adam Frisby Date: Mon, 1 Dec 2008 07:50:38 +0000 Subject: * From Mantis#2701 - Implements NHibernate data adapter, adds support for MsSql2005 to NHibernate. * Patch courtesy of Adminotech and realXtend. Thanks! --- OpenSim/Data/NHibernate/NHibernateRegionData.cs | 124 ++++++++---------------- 1 file changed, 42 insertions(+), 82 deletions(-) (limited to 'OpenSim/Data/NHibernate/NHibernateRegionData.cs') diff --git a/OpenSim/Data/NHibernate/NHibernateRegionData.cs b/OpenSim/Data/NHibernate/NHibernateRegionData.cs index 475cd71..0aba58c 100644 --- a/OpenSim/Data/NHibernate/NHibernateRegionData.cs +++ b/OpenSim/Data/NHibernate/NHibernateRegionData.cs @@ -34,10 +34,7 @@ using System.Text.RegularExpressions; using OpenMetaverse; using log4net; using NHibernate; -using NHibernate.Cfg; -using NHibernate.Expression; -using NHibernate.Mapping.Attributes; -using NHibernate.Tool.hbm2ddl; +using NHibernate.Criterion; using OpenSim.Framework; using OpenSim.Region.Environment.Interfaces; using OpenSim.Region.Environment.Scenes; @@ -52,51 +49,12 @@ namespace OpenSim.Data.NHibernate { private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - private Configuration cfg; - private ISessionFactory factory; - private ISession session; - - // public void Initialise() - // { - // Initialise("SQLiteDialect;SqliteClientDriver;URI=file:OpenSim.db,version=3", true); - // } + private NHibernateManager manager; public void Initialise(string connect) { - // Split out the dialect, driver, and connect string - char[] split = {';'}; - string[] parts = connect.Split(split, 3); - if (parts.Length != 3) - { - // TODO: make this a real exception type - throw new Exception("Malformed Region connection string '" + connect + "'"); - } - - string dialect = parts[0]; - - // NHibernate setup - cfg = new Configuration(); - cfg.SetProperty(Environment.ConnectionProvider, - "NHibernate.Connection.DriverConnectionProvider"); - cfg.SetProperty(Environment.Dialect, - "NHibernate.Dialect." + dialect); - cfg.SetProperty(Environment.ConnectionDriver, - "NHibernate.Driver." + parts[1]); - cfg.SetProperty(Environment.ConnectionString, parts[2]); - cfg.AddAssembly("OpenSim.Data.NHibernate"); - - HbmSerializer.Default.Validate = true; - using (MemoryStream stream = - HbmSerializer.Default.Serialize(Assembly.GetExecutingAssembly())) - cfg.AddInputStream(stream); - - factory = cfg.BuildSessionFactory(); - session = factory.OpenSession(); - - // This actually does the roll forward assembly stuff - Assembly assem = GetType().Assembly; - Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, "RegionStore"); - m.Update(); + m_log.InfoFormat("[NHIBERNATE] Initializing NHibernateRegionData"); + manager = new NHibernateManager(connect, "RegionStore"); } /*********************************************************************** @@ -122,15 +80,18 @@ namespace OpenSim.Data.NHibernate { try { - SceneObjectPart old = session.Load(typeof(SceneObjectPart), p.UUID) as SceneObjectPart; - session.Evict(old); - session.Update(p); - m_log.InfoFormat("[NHIBERNATE] updating object {0}", p.UUID); - } - catch (ObjectNotFoundException) - { - m_log.InfoFormat("[NHIBERNATE] saving object {0}", p.UUID); - session.Save(p); + SceneObjectPart old = (SceneObjectPart)manager.Load(typeof(SceneObjectPart), p.UUID); + if (old != null) + { + m_log.InfoFormat("[NHIBERNATE] updating object {0}", p.UUID); + manager.Update(old); + } + else + { + m_log.InfoFormat("[NHIBERNATE] saving object {0}", p.UUID); + manager.Save(p); + } + } catch (Exception e) { @@ -142,15 +103,19 @@ namespace OpenSim.Data.NHibernate { try { - Terrain old = session.Load(typeof(Terrain), t.RegionID) as Terrain; - session.Evict(old); - session.Update(t); - session.Flush(); - } - catch (ObjectNotFoundException) - { - session.Save(t); - session.Flush(); + + Terrain old = (Terrain)manager.Load(typeof(Terrain), t.RegionID); + if (old != null) + { + m_log.InfoFormat("[NHIBERNATE] updating terrain {0}", t.RegionID); + manager.Update(old); + } + else + { + m_log.InfoFormat("[NHIBERNATE] saving terrain {0}", t.RegionID); + manager.Save(t); + } + } catch (Exception e) { @@ -173,7 +138,6 @@ namespace OpenSim.Data.NHibernate m_log.InfoFormat("Storing part {0}", part.UUID); SaveOrUpdate(part); } - session.Flush(); } catch (Exception e) { @@ -185,7 +149,7 @@ namespace OpenSim.Data.NHibernate { SceneObjectGroup group = new SceneObjectGroup(); - ICriteria criteria = session.CreateCriteria(typeof(SceneObjectPart)); + ICriteria criteria = manager.GetSession().CreateCriteria(typeof(SceneObjectPart)); criteria.Add(Expression.Eq("RegionID", region)); criteria.Add(Expression.Eq("ParentUUID", uuid)); criteria.AddOrder( Order.Asc("ParentID") ); @@ -216,9 +180,8 @@ namespace OpenSim.Data.NHibernate SceneObjectGroup g = LoadObject(obj, regionUUID); foreach (SceneObjectPart p in g.Children.Values) { - session.Delete(p); + manager.Delete(p); } - session.Flush(); m_log.InfoFormat("[REGION DB]: Removing obj: {0} from region: {1}", obj.Guid, regionUUID); @@ -234,7 +197,7 @@ namespace OpenSim.Data.NHibernate Dictionary SOG = new Dictionary(); List ret = new List(); - ICriteria criteria = session.CreateCriteria(typeof(SceneObjectPart)); + ICriteria criteria = manager.GetSession().CreateCriteria(typeof(SceneObjectPart)); criteria.Add(Expression.Eq("RegionID", regionUUID)); criteria.AddOrder( Order.Asc("ParentID") ); foreach (SceneObjectPart p in criteria.List()) @@ -252,7 +215,7 @@ namespace OpenSim.Data.NHibernate } // get the inventory - ICriteria InvCriteria = session.CreateCriteria(typeof(TaskInventoryItem)); + ICriteria InvCriteria = manager.GetSession().CreateCriteria(typeof(TaskInventoryItem)); InvCriteria.Add(Expression.Eq("ParentPartID", p.UUID)); IList inventory = new List(); foreach (TaskInventoryItem i in InvCriteria.List()) @@ -291,16 +254,14 @@ namespace OpenSim.Data.NHibernate /// Heightfield data public double[,] LoadTerrain(UUID regionID) { - try + Terrain t = (Terrain)manager.Load(typeof(Terrain), regionID); + if (t != null) { - Terrain t = session.Load(typeof(Terrain), regionID) as Terrain; return t.Doubles; } - catch (ObjectNotFoundException) - { - m_log.Info("No terrain yet"); - return null; - } + + m_log.Info("No terrain yet"); + return null; } /// @@ -339,7 +300,7 @@ namespace OpenSim.Data.NHibernate /// public void Shutdown() { - session.Flush(); + //session.Flush(); } /// @@ -397,21 +358,20 @@ namespace OpenSim.Data.NHibernate /// public void StorePrimInventory(UUID primID, ICollection items) { - ICriteria criteria = session.CreateCriteria(typeof(TaskInventoryItem)); + ICriteria criteria = manager.GetSession().CreateCriteria(typeof(TaskInventoryItem)); criteria.Add(Expression.Eq("ParentPartID", primID)); try { foreach (TaskInventoryItem i in criteria.List()) { - session.Delete(i); + manager.Delete(i); } foreach (TaskInventoryItem i in items) { - session.Save(i); + manager.Save(i); } - session.Flush(); } catch (Exception e) { -- cgit v1.1