From 80520206fcd4c8c85eb28b4800c42136365b164d Mon Sep 17 00:00:00 2001 From: Justin Clarke Casey Date: Mon, 1 Dec 2008 18:42:14 +0000 Subject: * Unforunately it turns out not to be that simple. Revert the rest of r7560 for now. --- OpenSim/Data/NHibernate/LLQuaternionUserType.cs | 6 +- OpenSim/Data/NHibernate/LLVector3UserType.cs | 7 +- OpenSim/Data/NHibernate/NHibernateAssetData.cs | 77 ++++++++-- OpenSim/Data/NHibernate/NHibernateInventoryData.cs | 107 +++++++++----- OpenSim/Data/NHibernate/NHibernateManager.cs | 142 +++---------------- OpenSim/Data/NHibernate/NHibernateRegionData.cs | 124 +++++++++++------ OpenSim/Data/NHibernate/NHibernateUserData.cs | 155 +++++++++++++-------- .../Data/NHibernate/Resources/AssetBase.hbm.xml | 2 +- .../Resources/InventoryFolderBase.hbm.xml | 2 +- .../NHibernate/Resources/InventoryItemBase.hbm.xml | 10 +- .../Resources/MySQLDialect/002_RegionStore.sql | 4 +- .../Data/NHibernate/Resources/RegionStore.hbm.xml | 85 ++++------- .../NHibernate/Resources/UserAgentData.hbm.xml | 8 +- .../NHibernate/Resources/UserProfileData.hbm.xml | 4 +- OpenSim/Data/NHibernate/Terrain.cs | 2 +- 15 files changed, 385 insertions(+), 350 deletions(-) (limited to 'OpenSim/Data/NHibernate') diff --git a/OpenSim/Data/NHibernate/LLQuaternionUserType.cs b/OpenSim/Data/NHibernate/LLQuaternionUserType.cs index 0cd5c6f..7431433 100644 --- a/OpenSim/Data/NHibernate/LLQuaternionUserType.cs +++ b/OpenSim/Data/NHibernate/LLQuaternionUserType.cs @@ -78,11 +78,7 @@ namespace OpenSim.Data.NHibernate int w = rs.GetOrdinal(names[3]); if (!rs.IsDBNull(x)) { - float X = (Single)Convert.ToDouble(rs[x].ToString()); - float Y = (Single)Convert.ToDouble(rs[y].ToString()); - float Z = (Single)Convert.ToDouble(rs[z].ToString()); - float W = (Single)Convert.ToDouble(rs[w].ToString()); - quat = new Quaternion(X, Y, Z, W); + quat = new Quaternion((Single)rs[x], (Single)rs[y], (Single)rs[z], (Single)rs[w]); } return quat; } diff --git a/OpenSim/Data/NHibernate/LLVector3UserType.cs b/OpenSim/Data/NHibernate/LLVector3UserType.cs index 25a2bc6..cdd15ec 100644 --- a/OpenSim/Data/NHibernate/LLVector3UserType.cs +++ b/OpenSim/Data/NHibernate/LLVector3UserType.cs @@ -74,12 +74,9 @@ namespace OpenSim.Data.NHibernate int x = rs.GetOrdinal(names[0]); int y = rs.GetOrdinal(names[1]); int z = rs.GetOrdinal(names[2]); - if (!rs.IsDBNull(x) && !rs.IsDBNull(y) && !rs.IsDBNull(z)) + if (!rs.IsDBNull(x)) { - float X = (Single)Convert.ToDouble(rs[x].ToString()); - float Y = (Single)Convert.ToDouble(rs[y].ToString()); - float Z = (Single)Convert.ToDouble(rs[z].ToString()); - vector = new Vector3(X, Y, Z); + vector = new Vector3((Single)rs[x], (Single)rs[y], (Single)rs[z]); } return vector; } diff --git a/OpenSim/Data/NHibernate/NHibernateAssetData.cs b/OpenSim/Data/NHibernate/NHibernateAssetData.cs index 90d41e3..6ee527e 100644 --- a/OpenSim/Data/NHibernate/NHibernateAssetData.cs +++ b/OpenSim/Data/NHibernate/NHibernateAssetData.cs @@ -33,7 +33,10 @@ 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 OpenSim.Framework; using Environment=NHibernate.Cfg.Environment; @@ -46,7 +49,9 @@ namespace OpenSim.Data.NHibernate { private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - private NHibernateManager manager; + private Configuration cfg; + private ISessionFactory factory; + private ISession session; override public void Dispose() { } @@ -57,23 +62,79 @@ namespace OpenSim.Data.NHibernate public override 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 Inventory 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"); + - m_log.InfoFormat("[NHIBERNATE] Initializing NHibernateAssetData"); - manager = new NHibernateManager(connect, "AssetStore"); + + 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, "AssetStore"); + m.Update(); } override public AssetBase FetchAsset(UUID uuid) { - return (AssetBase)manager.Load(typeof(AssetBase), uuid); + try + { + return session.Load(typeof(AssetBase), uuid) as AssetBase; + } + catch (ObjectNotFoundException) + { + m_log.ErrorFormat("[NHIBERNATE] no such asset {0}", uuid); + return null; + } + catch (Exception e) + { + m_log.Error("[NHIBERNATE] unexpected exception: ", e); + return null; + } } private void Save(AssetBase asset) { - AssetBase temp = (AssetBase)manager.Load(typeof(AssetBase), asset.FullID); - if (temp == null) + try { - manager.Save(asset); + // a is not used anywhere? + // AssetBase a = session.Load(typeof(AssetBase), asset.FullID) as AssetBase; + session.Load(typeof(AssetBase), asset.FullID); + } + catch (ObjectNotFoundException) + { + session.Save(asset); + session.Flush(); + } + catch (Exception e) + { + m_log.Error("[NHIBERNATE] issue saving asset", e); } } @@ -86,7 +147,7 @@ namespace OpenSim.Data.NHibernate override public void UpdateAsset(AssetBase asset) { m_log.InfoFormat("[NHIBERNATE] updating asset {0}", asset.FullID); - manager.Update(asset); + Save(asset); } // private void LogAssetLoad(AssetBase asset) diff --git a/OpenSim/Data/NHibernate/NHibernateInventoryData.cs b/OpenSim/Data/NHibernate/NHibernateInventoryData.cs index f7de8eb..bceb5d5 100644 --- a/OpenSim/Data/NHibernate/NHibernateInventoryData.cs +++ b/OpenSim/Data/NHibernate/NHibernateInventoryData.cs @@ -33,7 +33,10 @@ using System.Text.RegularExpressions; using OpenMetaverse; using log4net; using NHibernate; -using NHibernate.Criterion; +using NHibernate.Cfg; +using NHibernate.Expression; +using NHibernate.Mapping.Attributes; +using NHibernate.Tool.hbm2ddl; using OpenSim.Framework; using Environment=NHibernate.Cfg.Environment; @@ -43,7 +46,9 @@ namespace OpenSim.Data.NHibernate { private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - private NHibernateManager manager; + private Configuration cfg; + private ISessionFactory factory; + private ISession session; public void Initialise() { @@ -56,8 +61,35 @@ namespace OpenSim.Data.NHibernate /// public void Initialise(string connect) { - m_log.InfoFormat("[NHIBERNATE] Initializing NHibernateInventoryData"); - manager = new NHibernateManager(connect, "InventoryStore"); + // 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 Inventory connection string '" + connect + "'"); + } + string dialect = parts[0]; + + // Establish NHibernate Connection + cfg = new Configuration(); + cfg.SetProperty(Environment.ConnectionProvider, + "NHibernate.Connection.DriverConnectionProvider"); + cfg.SetProperty(Environment.Dialect, + "NHibernate.Dialect." + parts[0]); + cfg.SetProperty(Environment.ConnectionDriver, + "NHibernate.Driver." + parts[1]); + cfg.SetProperty(Environment.ConnectionString, parts[2]); + cfg.AddAssembly("OpenSim.Data.NHibernate"); + + 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, "InventoryStore"); + m.Update(); + } /***************************************************************** @@ -77,8 +109,7 @@ namespace OpenSim.Data.NHibernate { try { - m_log.InfoFormat("[NHIBERNATE] getInventoryItem {0}", item); - return (InventoryItemBase)manager.Load(typeof(InventoryItemBase), item); + return session.Load(typeof(InventoryItemBase), item) as InventoryItemBase; } catch { @@ -95,11 +126,15 @@ namespace OpenSim.Data.NHibernate { if (!ExistsItem(item.ID)) { - manager.Save(item); + using (ITransaction transaction = session.BeginTransaction()) + { + session.Save(item); + transaction.Commit(); + } } else { - m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Item {0} that already exists, updating instead", item.ID); + m_log.ErrorFormat("Attempted to add Inventory Item {0} that already exists, updating instead", item.ID); updateInventoryItem(item); } } @@ -112,11 +147,15 @@ namespace OpenSim.Data.NHibernate { if (ExistsItem(item.ID)) { - manager.Update(item); + using (ITransaction transaction = session.BeginTransaction()) + { + session.Update(item); + transaction.Commit(); + } } else { - m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Item {0} that already exists", item.ID); + m_log.ErrorFormat("Attempted to add Inventory Item {0} that already exists", item.ID); } } @@ -126,16 +165,11 @@ namespace OpenSim.Data.NHibernate /// public void deleteInventoryItem(UUID itemID) { - InventoryItemBase item = (InventoryItemBase)manager.Load(typeof(InventoryItemBase), itemID); - if (item != null) + using (ITransaction transaction = session.BeginTransaction()) { - manager.Delete(item); + session.Delete(itemID); + transaction.Commit(); } - else - { - m_log.ErrorFormat("[NHIBERNATE] Error deleting InventoryItemBase {0}", itemID); - } - } /// @@ -147,11 +181,11 @@ namespace OpenSim.Data.NHibernate { try { - return (InventoryFolderBase)manager.Load(typeof(InventoryFolderBase), folder); + return session.Load(typeof(InventoryFolderBase), folder) as InventoryFolderBase; } catch { - m_log.ErrorFormat("[NHIBERNATE] Couldn't find inventory item: {0}", folder); + m_log.ErrorFormat("Couldn't find inventory item: {0}", folder); return null; } } @@ -164,11 +198,15 @@ namespace OpenSim.Data.NHibernate { if (!ExistsFolder(folder.ID)) { - manager.Save(folder); + using (ITransaction transaction = session.BeginTransaction()) + { + session.Save(folder); + transaction.Commit(); + } } else { - m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Folder {0} that already exists, updating instead", folder.ID); + m_log.ErrorFormat("Attempted to add Inventory Folder {0} that already exists, updating instead", folder.ID); updateInventoryFolder(folder); } } @@ -181,11 +219,15 @@ namespace OpenSim.Data.NHibernate { if (ExistsFolder(folder.ID)) { - manager.Update(folder); + using (ITransaction transaction = session.BeginTransaction()) + { + session.Update(folder); + transaction.Commit(); + } } else { - m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Folder {0} that already exists", folder.ID); + m_log.ErrorFormat("Attempted to add Inventory Folder {0} that already exists", folder.ID); } } @@ -195,16 +237,11 @@ namespace OpenSim.Data.NHibernate /// public void deleteInventoryFolder(UUID folderID) { - InventoryFolderBase item = (InventoryFolderBase)manager.Load(typeof(InventoryFolderBase), folderID); - if (item != null) - { - manager.Delete(item); - } - else + using (ITransaction transaction = session.BeginTransaction()) { - m_log.ErrorFormat("[NHIBERNATE] Error deleting InventoryFolderBase {0}", folderID); + session.Delete(folderID.ToString()); + transaction.Commit(); } - manager.Delete(folderID); } // useful private methods @@ -280,7 +317,7 @@ namespace OpenSim.Data.NHibernate public List getInventoryInFolder(UUID folderID) { // try { - ICriteria criteria = manager.GetSession().CreateCriteria(typeof(InventoryItemBase)); + ICriteria criteria = session.CreateCriteria(typeof(InventoryItemBase)); criteria.Add(Expression.Eq("Folder", folderID)); List list = new List(); foreach (InventoryItemBase item in criteria.List()) @@ -303,7 +340,7 @@ namespace OpenSim.Data.NHibernate // see InventoryItemBase.getUserRootFolder public InventoryFolderBase getUserRootFolder(UUID user) { - ICriteria criteria = manager.GetSession().CreateCriteria(typeof(InventoryFolderBase)); + ICriteria criteria = session.CreateCriteria(typeof(InventoryFolderBase)); criteria.Add(Expression.Eq("ParentID", UUID.Zero)); criteria.Add(Expression.Eq("Owner", user)); foreach (InventoryFolderBase folder in criteria.List()) @@ -321,7 +358,7 @@ namespace OpenSim.Data.NHibernate /// ID of parent private void getInventoryFolders(ref List folders, UUID parentID) { - ICriteria criteria = manager.GetSession().CreateCriteria(typeof(InventoryFolderBase)); + ICriteria criteria = session.CreateCriteria(typeof(InventoryFolderBase)); criteria.Add(Expression.Eq("ParentID", parentID)); foreach (InventoryFolderBase item in criteria.List()) { diff --git a/OpenSim/Data/NHibernate/NHibernateManager.cs b/OpenSim/Data/NHibernate/NHibernateManager.cs index 36d84e2..161ec1d 100644 --- a/OpenSim/Data/NHibernate/NHibernateManager.cs +++ b/OpenSim/Data/NHibernate/NHibernateManager.cs @@ -25,141 +25,35 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -using System; using System.Reflection; -using System.IO; using log4net; using NHibernate; -using NHibernate.Cfg; -using NHibernate.Mapping.Attributes; -using NHibernate.Tool.hbm2ddl; -using OpenMetaverse; -using Environment = NHibernate.Cfg.Environment; namespace OpenSim.Data.NHibernate { - public class NHibernateManager + internal class NHibernateManager { - private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - private string dialect; - private Configuration cfg; - private ISessionFactory factory; - private ISession session; + // private ISessionFactory factory; + // private ISession session; + // private ITransaction transaction; - public NHibernateManager(string connect, string store) + public NHibernateManager() { - - // 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 Inventory connection string '" + connect + "'"); - } - - 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"); - - //To create sql file uncomment code below and write the name of the file - //SchemaExport exp = new SchemaExport(cfg); - //exp.SetOutputFile("nameofthefile.sql"); - //exp.Create(false, true); - - HbmSerializer.Default.Validate = true; - using (MemoryStream stream = - HbmSerializer.Default.Serialize(Assembly.GetExecutingAssembly())) - cfg.AddInputStream(stream); - - factory = cfg.BuildSessionFactory(); - session = factory.OpenSession(); - - Assembly assem = GetType().Assembly; - Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, store); - m.Update(); + // This is stubbing for now, it will become dynamic later and support different db backends +// NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration(); +// cfg.Properties[NHibernate.Cfg.Environment.ConnectionProvider] = +// "NHibernate.Connection.DriverConnectionProvider"; +// cfg.Properties[NHibernate.Cfg.Environment.Dialect] = +// "NHibernate.Dialect.SQLite"; +// cfg.Properties[NHibernate.Cfg.Environment.ConnectionDriver] = +// "NHibernate.Driver.SqliteClientDriver"; +// cfg.Properties[NHibernate.Cfg.Environment.ConnectionString] = +// "URI=file:opensim-nh.db,version=3"; + +// factory = cfg.BuildSessionFactory(); } - public object Load(Type type, UUID uuid) - { - object obj = null; - try - { - obj = session.Load(type, uuid); - } - catch (Exception) - { - m_log.ErrorFormat("[NHIBERNATE] {0} not found with ID {1} ", type.Name, uuid); - } - return obj; - - } - - public bool Save(object obj) - { - try - { - session.BeginTransaction(); - session.Save(obj); - session.Transaction.Commit(); - session.Flush(); - return true; - } - catch (Exception e) - { - m_log.Error("[NHIBERNATE] issue saving object ", e); - } - return false; - } - - public bool Update(object obj) - { - try - { - session.BeginTransaction(); - session.Update(obj); - session.Transaction.Commit(); - session.Flush(); - return true; - } - catch (Exception e) - { - m_log.Error("[NHIBERNATE] issue updating object ", e); - } - return false; - } - - public bool Delete(object obj) - { - try - { - session.BeginTransaction(); - session.Delete(obj); - session.Transaction.Commit(); - session.Flush(); - return true; - } - catch (Exception e) - { - - m_log.Error("[NHIBERNATE] issue deleting object ", e); - } - return false; - } - - public ISession GetSession() - { - return session; - } } } diff --git a/OpenSim/Data/NHibernate/NHibernateRegionData.cs b/OpenSim/Data/NHibernate/NHibernateRegionData.cs index 0aba58c..475cd71 100644 --- a/OpenSim/Data/NHibernate/NHibernateRegionData.cs +++ b/OpenSim/Data/NHibernate/NHibernateRegionData.cs @@ -34,7 +34,10 @@ using System.Text.RegularExpressions; using OpenMetaverse; using log4net; using NHibernate; -using NHibernate.Criterion; +using NHibernate.Cfg; +using NHibernate.Expression; +using NHibernate.Mapping.Attributes; +using NHibernate.Tool.hbm2ddl; using OpenSim.Framework; using OpenSim.Region.Environment.Interfaces; using OpenSim.Region.Environment.Scenes; @@ -49,12 +52,51 @@ namespace OpenSim.Data.NHibernate { private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - private NHibernateManager manager; + private Configuration cfg; + private ISessionFactory factory; + private ISession session; + + // public void Initialise() + // { + // Initialise("SQLiteDialect;SqliteClientDriver;URI=file:OpenSim.db,version=3", true); + // } public void Initialise(string connect) { - m_log.InfoFormat("[NHIBERNATE] Initializing NHibernateRegionData"); - manager = new NHibernateManager(connect, "RegionStore"); + // 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(); } /*********************************************************************** @@ -80,18 +122,15 @@ namespace OpenSim.Data.NHibernate { try { - 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); - } - + 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); } catch (Exception e) { @@ -103,19 +142,15 @@ namespace OpenSim.Data.NHibernate { try { - - 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); - } - + 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(); } catch (Exception e) { @@ -138,6 +173,7 @@ namespace OpenSim.Data.NHibernate m_log.InfoFormat("Storing part {0}", part.UUID); SaveOrUpdate(part); } + session.Flush(); } catch (Exception e) { @@ -149,7 +185,7 @@ namespace OpenSim.Data.NHibernate { SceneObjectGroup group = new SceneObjectGroup(); - ICriteria criteria = manager.GetSession().CreateCriteria(typeof(SceneObjectPart)); + ICriteria criteria = session.CreateCriteria(typeof(SceneObjectPart)); criteria.Add(Expression.Eq("RegionID", region)); criteria.Add(Expression.Eq("ParentUUID", uuid)); criteria.AddOrder( Order.Asc("ParentID") ); @@ -180,8 +216,9 @@ namespace OpenSim.Data.NHibernate SceneObjectGroup g = LoadObject(obj, regionUUID); foreach (SceneObjectPart p in g.Children.Values) { - manager.Delete(p); + session.Delete(p); } + session.Flush(); m_log.InfoFormat("[REGION DB]: Removing obj: {0} from region: {1}", obj.Guid, regionUUID); @@ -197,7 +234,7 @@ namespace OpenSim.Data.NHibernate Dictionary SOG = new Dictionary(); List ret = new List(); - ICriteria criteria = manager.GetSession().CreateCriteria(typeof(SceneObjectPart)); + ICriteria criteria = session.CreateCriteria(typeof(SceneObjectPart)); criteria.Add(Expression.Eq("RegionID", regionUUID)); criteria.AddOrder( Order.Asc("ParentID") ); foreach (SceneObjectPart p in criteria.List()) @@ -215,7 +252,7 @@ namespace OpenSim.Data.NHibernate } // get the inventory - ICriteria InvCriteria = manager.GetSession().CreateCriteria(typeof(TaskInventoryItem)); + ICriteria InvCriteria = session.CreateCriteria(typeof(TaskInventoryItem)); InvCriteria.Add(Expression.Eq("ParentPartID", p.UUID)); IList inventory = new List(); foreach (TaskInventoryItem i in InvCriteria.List()) @@ -254,14 +291,16 @@ namespace OpenSim.Data.NHibernate /// Heightfield data public double[,] LoadTerrain(UUID regionID) { - Terrain t = (Terrain)manager.Load(typeof(Terrain), regionID); - if (t != null) + try { + Terrain t = session.Load(typeof(Terrain), regionID) as Terrain; return t.Doubles; } - - m_log.Info("No terrain yet"); - return null; + catch (ObjectNotFoundException) + { + m_log.Info("No terrain yet"); + return null; + } } /// @@ -300,7 +339,7 @@ namespace OpenSim.Data.NHibernate /// public void Shutdown() { - //session.Flush(); + session.Flush(); } /// @@ -358,20 +397,21 @@ namespace OpenSim.Data.NHibernate /// public void StorePrimInventory(UUID primID, ICollection items) { - ICriteria criteria = manager.GetSession().CreateCriteria(typeof(TaskInventoryItem)); + ICriteria criteria = session.CreateCriteria(typeof(TaskInventoryItem)); criteria.Add(Expression.Eq("ParentPartID", primID)); try { foreach (TaskInventoryItem i in criteria.List()) { - manager.Delete(i); + session.Delete(i); } foreach (TaskInventoryItem i in items) { - manager.Save(i); + session.Save(i); } + session.Flush(); } catch (Exception e) { diff --git a/OpenSim/Data/NHibernate/NHibernateUserData.cs b/OpenSim/Data/NHibernate/NHibernateUserData.cs index ee0d2b8..3a22dc3 100644 --- a/OpenSim/Data/NHibernate/NHibernateUserData.cs +++ b/OpenSim/Data/NHibernate/NHibernateUserData.cs @@ -33,7 +33,10 @@ using System.Text.RegularExpressions; using OpenMetaverse; using log4net; using NHibernate; -using NHibernate.Criterion; +using NHibernate.Cfg; +using NHibernate.Expression; +using NHibernate.Mapping.Attributes; +using NHibernate.Tool.hbm2ddl; using OpenSim.Framework; using Environment=NHibernate.Cfg.Environment; @@ -46,7 +49,9 @@ namespace OpenSim.Data.NHibernate { private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - private NHibernateManager manager; + private Configuration cfg; + private ISessionFactory factory; + private ISession session; public override void Initialise() { @@ -56,40 +61,62 @@ namespace OpenSim.Data.NHibernate public override void Initialise(string connect) { - m_log.InfoFormat("[NHIBERNATE] Initializing NHibernateUserData"); - manager = new NHibernateManager(connect, "UserStore"); + char[] split = {';'}; + string[] parts = connect.Split(split, 3); + if (parts.Length != 3) + { + // TODO: make this a real exception type + throw new Exception("Malformed Inventory connection string '" + connect + "'"); + } + string dialect = parts[0]; + + // This is stubbing for now, it will become dynamic later and support different db backends + cfg = new Configuration(); + cfg.SetProperty(Environment.ConnectionProvider, + "NHibernate.Connection.DriverConnectionProvider"); + cfg.SetProperty(Environment.Dialect, + "NHibernate.Dialect." + parts[0]); + cfg.SetProperty(Environment.ConnectionDriver, + "NHibernate.Driver." + parts[1]); + cfg.SetProperty(Environment.ConnectionString, parts[2]); + cfg.AddAssembly("OpenSim.Data.NHibernate"); + + 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, "UserStore"); + m.Update(); } private bool ExistsUser(UUID uuid) { UserProfileData user = null; - - m_log.InfoFormat("[NHIBERNATE] ExistsUser; {0}", uuid); - user = (UserProfileData)manager.Load(typeof(UserProfileData), uuid); - - if (user == null) + try { - m_log.InfoFormat("[NHIBERNATE] User with given UUID does not exist {0} ", uuid); - return false; + user = session.Load(typeof(UserProfileData), uuid) as UserProfileData; + } + catch (ObjectNotFoundException) + { + user = null; } - return true; - + return (user != null); } override public UserProfileData GetUserByUUID(UUID uuid) { UserProfileData user; - m_log.InfoFormat("[NHIBERNATE] GetUserByUUID: {0} ", uuid); - - user = (UserProfileData)manager.Load(typeof(UserProfileData), uuid); - if (user != null) + // TODO: I'm sure I'll have to do something silly here + try { - UserAgentData agent = GetAgentByUUID(uuid); - if (agent != null) - { - user.CurrentAgent = agent; - } + user = session.Load(typeof(UserProfileData), uuid) as UserProfileData; + user.CurrentAgent = GetAgentByUUID(uuid); + } + catch (ObjectNotFoundException) + { + user = null; } return user; @@ -99,30 +126,33 @@ namespace OpenSim.Data.NHibernate { if (!ExistsUser(profile.ID)) { - m_log.InfoFormat("[NHIBERNATE] AddNewUserProfile {0}", profile.ID); - manager.Save(profile); + session.Save(profile); SetAgentData(profile.ID, profile.CurrentAgent); - } else { - m_log.ErrorFormat("[NHIBERNATE] Attempted to add User {0} {1} that already exists, updating instead", profile.FirstName, profile.SurName); + m_log.ErrorFormat("Attempted to add User {0} {1} that already exists, updating instead", profile.FirstName, profile.SurName); UpdateUserProfile(profile); } } private void SetAgentData(UUID uuid, UserAgentData agent) { - UserAgentData old = (UserAgentData)manager.Load(typeof(UserAgentData), uuid); - if (old != null) + if (agent == null) { - m_log.InfoFormat("[NHIBERNATE] SetAgentData deleting old: {0} ",uuid); - manager.Delete(old); + // TODO: got to figure out how to do a delete right } - if (agent != null) + else { - m_log.InfoFormat("[NHIBERNATE] SetAgentData: {0} ", agent.ProfileID); - manager.Save(agent); + try + { + UserAgentData old = session.Load(typeof(UserAgentData), uuid) as UserAgentData; + session.Delete(old); + } + catch (ObjectNotFoundException) + { + } + session.Save(agent); } } @@ -130,13 +160,13 @@ namespace OpenSim.Data.NHibernate { if (ExistsUser(profile.ID)) { - manager.Update(profile); + session.Update(profile); SetAgentData(profile.ID, profile.CurrentAgent); return true; } else { - m_log.ErrorFormat("[NHIBERNATE] Attempted to update User {0} {1} that doesn't exist, updating instead", profile.FirstName, profile.SurName); + m_log.ErrorFormat("Attempted to update User {0} {1} that doesn't exist, updating instead", profile.FirstName, profile.SurName); AddNewUserProfile(profile); return true; } @@ -144,32 +174,37 @@ namespace OpenSim.Data.NHibernate override public void AddNewUserAgent(UserAgentData agent) { - UserAgentData old = (UserAgentData)manager.Load(typeof(UserAgentData), agent.ProfileID); - if (old != null) + try { - manager.Delete(old); + UserAgentData old = session.Load(typeof(UserAgentData), agent.ProfileID) as UserAgentData; + session.Delete(old); } - - manager.Save(agent); - + catch (ObjectNotFoundException) + { + } + session.Save(agent); } public void UpdateUserAgent(UserAgentData agent) { - m_log.InfoFormat("[NHIBERNATE] UpdateUserAgent: {0} ", agent.ProfileID); - manager.Update(agent); + session.Update(agent); } override public UserAgentData GetAgentByUUID(UUID uuid) { - m_log.InfoFormat("[NHIBERNATE] GetAgentByUUID: {0} ", uuid); - return (UserAgentData)manager.Load(typeof(UserAgentData), uuid); + try + { + return session.Load(typeof(UserAgentData), uuid) as UserAgentData; + } + catch + { + return null; + } } override public UserProfileData GetUserByName(string fname, string lname) { - m_log.InfoFormat("[NHIBERNATE] GetUserByName: {0} {1} ", fname, lname); - ICriteria criteria = manager.GetSession().CreateCriteria(typeof(UserProfileData)); + ICriteria criteria = session.CreateCriteria(typeof(UserProfileData)); criteria.Add(Expression.Eq("FirstName", fname)); criteria.Add(Expression.Eq("SurName", lname)); foreach (UserProfileData profile in criteria.List()) @@ -198,7 +233,7 @@ namespace OpenSim.Data.NHibernate if (querysplit.Length == 2) { - ICriteria criteria = manager.GetSession().CreateCriteria(typeof(UserProfileData)); + ICriteria criteria = session.CreateCriteria(typeof(UserProfileData)); criteria.Add(Expression.Like("FirstName", querysplit[0])); criteria.Add(Expression.Like("SurName", querysplit[1])); foreach (UserProfileData profile in criteria.List()) @@ -227,18 +262,26 @@ namespace OpenSim.Data.NHibernate /// TODO: stubs for now to get us to a compiling state gently public override AvatarAppearance GetUserAppearance(UUID user) { - return (AvatarAppearance)manager.Load(typeof(AvatarAppearance), user); + AvatarAppearance appearance; + // TODO: I'm sure I'll have to do something silly here + try { + appearance = session.Load(typeof(AvatarAppearance), user) as AvatarAppearance; + } catch (ObjectNotFoundException) { + appearance = null; + } + return appearance; } private bool ExistsAppearance(UUID uuid) { - AvatarAppearance appearance = (AvatarAppearance)manager.Load(typeof(AvatarAppearance), uuid); - if (appearance == null) - { - return false; + AvatarAppearance appearance; + try { + appearance = session.Load(typeof(AvatarAppearance), uuid) as AvatarAppearance; + } catch (ObjectNotFoundException) { + appearance = null; } - return true; + return (appearance == null) ? false : true; } @@ -252,11 +295,11 @@ namespace OpenSim.Data.NHibernate bool exists = ExistsAppearance(user); if (exists) { - manager.Update(appearance); + session.Update(appearance); } else { - manager.Save(appearance); + session.Save(appearance); } } diff --git a/OpenSim/Data/NHibernate/Resources/AssetBase.hbm.xml b/OpenSim/Data/NHibernate/Resources/AssetBase.hbm.xml index 7c42609..7c3d8cf 100644 --- a/OpenSim/Data/NHibernate/Resources/AssetBase.hbm.xml +++ b/OpenSim/Data/NHibernate/Resources/AssetBase.hbm.xml @@ -4,7 +4,7 @@ - + diff --git a/OpenSim/Data/NHibernate/Resources/InventoryFolderBase.hbm.xml b/OpenSim/Data/NHibernate/Resources/InventoryFolderBase.hbm.xml index c5f0115..6afe9fa 100644 --- a/OpenSim/Data/NHibernate/Resources/InventoryFolderBase.hbm.xml +++ b/OpenSim/Data/NHibernate/Resources/InventoryFolderBase.hbm.xml @@ -5,7 +5,7 @@ - + diff --git a/OpenSim/Data/NHibernate/Resources/InventoryItemBase.hbm.xml b/OpenSim/Data/NHibernate/Resources/InventoryItemBase.hbm.xml index f5e365b..9a4d7ca 100644 --- a/OpenSim/Data/NHibernate/Resources/InventoryItemBase.hbm.xml +++ b/OpenSim/Data/NHibernate/Resources/InventoryItemBase.hbm.xml @@ -12,15 +12,15 @@ - - - - + + + + - + diff --git a/OpenSim/Data/NHibernate/Resources/MySQLDialect/002_RegionStore.sql b/OpenSim/Data/NHibernate/Resources/MySQLDialect/002_RegionStore.sql index d34d44e..fc11e95 100644 --- a/OpenSim/Data/NHibernate/Resources/MySQLDialect/002_RegionStore.sql +++ b/OpenSim/Data/NHibernate/Resources/MySQLDialect/002_RegionStore.sql @@ -2,8 +2,8 @@ BEGIN; CREATE TABLE `Terrain` ( `RegionID` char(36) not null, - `MapData` longblob, + `Map` longblob, PRIMARY KEY (`RegionID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -COMMIT; +COMMIT; \ No newline at end of file diff --git a/OpenSim/Data/NHibernate/Resources/RegionStore.hbm.xml b/OpenSim/Data/NHibernate/Resources/RegionStore.hbm.xml index 1851904..fe055bb 100644 --- a/OpenSim/Data/NHibernate/Resources/RegionStore.hbm.xml +++ b/OpenSim/Data/NHibernate/Resources/RegionStore.hbm.xml @@ -4,7 +4,7 @@ - + @@ -14,17 +14,17 @@ - + - - - - - + + + + + @@ -83,24 +83,24 @@ - - + + - + - + - - - - + + + + - - - + + + @@ -108,8 +108,7 @@ - - + @@ -122,47 +121,15 @@ - - - - - + + + + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/OpenSim/Data/NHibernate/Resources/UserAgentData.hbm.xml b/OpenSim/Data/NHibernate/Resources/UserAgentData.hbm.xml index e10fb8f..0beddaf 100644 --- a/OpenSim/Data/NHibernate/Resources/UserAgentData.hbm.xml +++ b/OpenSim/Data/NHibernate/Resources/UserAgentData.hbm.xml @@ -5,7 +5,7 @@ - + @@ -13,9 +13,9 @@ - - + diff --git a/OpenSim/Data/NHibernate/Resources/UserProfileData.hbm.xml b/OpenSim/Data/NHibernate/Resources/UserProfileData.hbm.xml index 740a9f1..873930b 100644 --- a/OpenSim/Data/NHibernate/Resources/UserProfileData.hbm.xml +++ b/OpenSim/Data/NHibernate/Resources/UserProfileData.hbm.xml @@ -9,8 +9,8 @@ - - + + diff --git a/OpenSim/Data/NHibernate/Terrain.cs b/OpenSim/Data/NHibernate/Terrain.cs index 88be17f..dd0da44 100644 --- a/OpenSim/Data/NHibernate/Terrain.cs +++ b/OpenSim/Data/NHibernate/Terrain.cs @@ -60,7 +60,7 @@ namespace OpenSim.Data.NHibernate set { regionID = value; } } - public byte[] MapData + public byte[] Map { get { return serializeTerrain(map); } set { map = parseTerrain(value); } -- cgit v1.1