From 17fd6cf66113b4c6482d72ac54a79382408eec9c Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Fri, 20 Jun 2008 20:23:55 +0000 Subject: lots of futzing with nhibernate to make it more efficient. I don't think this works, but I don't want to loose the work before the weekend, and it doesn't break the build. --- OpenSim/Data/NHibernate/NHibernateAssetData.cs | 39 +++-- OpenSim/Data/NHibernate/NHibernateInventoryData.cs | 146 ++++++++----------- OpenSim/Data/NHibernate/NHibernateUserData.cs | 157 ++++++++------------- .../NHibernate/Resources/InventoryItemBase.hbm.xml | 6 +- 4 files changed, 136 insertions(+), 212 deletions(-) (limited to 'OpenSim/Data/NHibernate') diff --git a/OpenSim/Data/NHibernate/NHibernateAssetData.cs b/OpenSim/Data/NHibernate/NHibernateAssetData.cs index db77942..89f907f 100644 --- a/OpenSim/Data/NHibernate/NHibernateAssetData.cs +++ b/OpenSim/Data/NHibernate/NHibernateAssetData.cs @@ -49,6 +49,7 @@ namespace OpenSim.Data.NHibernate private Configuration cfg; private ISessionFactory factory; + private ISession session; public override void Initialise() { @@ -87,27 +88,25 @@ namespace OpenSim.Data.NHibernate 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(LLUUID uuid) { - using (ISession session = factory.OpenSession()) + try { - try - { - return session.Load(typeof(AssetBase), uuid) as AssetBase; - } - catch (Exception e) - { - m_log.Error("[NHIBERNATE] issue loading asset", e); - return null; - } + return session.Load(typeof(AssetBase), uuid) as AssetBase; + } + catch (Exception e) + { + m_log.Error("[NHIBERNATE] issue loading asset", e); + return null; } } @@ -116,13 +115,10 @@ namespace OpenSim.Data.NHibernate if (!ExistsAsset(asset.FullID)) { m_log.InfoFormat("[NHIBERNATE] inserting asset {0}", asset.FullID); - using (ISession session = factory.OpenSession()) + using (ITransaction transaction = session.BeginTransaction()) { - using (ITransaction transaction = session.BeginTransaction()) - { - session.Save(asset); - transaction.Commit(); - } + session.Save(asset); + transaction.Commit(); } } } @@ -131,13 +127,10 @@ namespace OpenSim.Data.NHibernate { if (ExistsAsset(asset.FullID)) { - using (ISession session = factory.OpenSession()) + using (ITransaction transaction = session.BeginTransaction()) { - using (ITransaction transaction = session.BeginTransaction()) - { - session.Update(asset); - transaction.Commit(); - } + session.Update(asset); + transaction.Commit(); } } } diff --git a/OpenSim/Data/NHibernate/NHibernateInventoryData.cs b/OpenSim/Data/NHibernate/NHibernateInventoryData.cs index 017759e..46dcbad 100644 --- a/OpenSim/Data/NHibernate/NHibernateInventoryData.cs +++ b/OpenSim/Data/NHibernate/NHibernateInventoryData.cs @@ -48,6 +48,7 @@ namespace OpenSim.Data.NHibernate private Configuration cfg; private ISessionFactory factory; + private ISession session; /// /// Initialises the interface @@ -76,6 +77,7 @@ namespace OpenSim.Data.NHibernate cfg.AddAssembly("OpenSim.Data.NHibernate"); factory = cfg.BuildSessionFactory(); + session = factory.OpenSession(); // This actually does the roll forward assembly stuff Assembly assem = GetType().Assembly; @@ -99,17 +101,14 @@ namespace OpenSim.Data.NHibernate /// A class containing item information public InventoryItemBase getInventoryItem(LLUUID item) { - using (ISession session = factory.OpenSession()) + try { - try - { - return session.Load(typeof(InventoryItemBase), item) as InventoryItemBase; - } - catch - { - m_log.ErrorFormat("Couldn't find inventory item: {0}", item); - return null; - } + return session.Load(typeof(InventoryItemBase), item) as InventoryItemBase; + } + catch + { + m_log.ErrorFormat("Couldn't find inventory item: {0}", item); + return null; } } @@ -121,13 +120,10 @@ namespace OpenSim.Data.NHibernate { if (!ExistsItem(item.ID)) { - using (ISession session = factory.OpenSession()) + using (ITransaction transaction = session.BeginTransaction()) { - using (ITransaction transaction = session.BeginTransaction()) - { - session.Save(item); - transaction.Commit(); - } + session.Save(item); + transaction.Commit(); } } else @@ -145,13 +141,10 @@ namespace OpenSim.Data.NHibernate { if (ExistsItem(item.ID)) { - using (ISession session = factory.OpenSession()) + using (ITransaction transaction = session.BeginTransaction()) { - using (ITransaction transaction = session.BeginTransaction()) - { - session.Update(item); - transaction.Commit(); - } + session.Update(item); + transaction.Commit(); } } else @@ -166,13 +159,10 @@ namespace OpenSim.Data.NHibernate /// public void deleteInventoryItem(LLUUID itemID) { - using (ISession session = factory.OpenSession()) + using (ITransaction transaction = session.BeginTransaction()) { - using (ITransaction transaction = session.BeginTransaction()) - { - session.Delete(itemID); - transaction.Commit(); - } + session.Delete(itemID); + transaction.Commit(); } } @@ -183,17 +173,14 @@ namespace OpenSim.Data.NHibernate /// A class containing folder information public InventoryFolderBase getInventoryFolder(LLUUID folder) { - using (ISession session = factory.OpenSession()) + try { - try - { - return session.Load(typeof(InventoryFolderBase), folder) as InventoryFolderBase; - } - catch - { - m_log.ErrorFormat("Couldn't find inventory item: {0}", folder); - return null; - } + return session.Load(typeof(InventoryFolderBase), folder) as InventoryFolderBase; + } + catch + { + m_log.ErrorFormat("Couldn't find inventory item: {0}", folder); + return null; } } @@ -205,13 +192,10 @@ namespace OpenSim.Data.NHibernate { if (!ExistsFolder(folder.ID)) { - using (ISession session = factory.OpenSession()) + using (ITransaction transaction = session.BeginTransaction()) { - using (ITransaction transaction = session.BeginTransaction()) - { - session.Save(folder); - transaction.Commit(); - } + session.Save(folder); + transaction.Commit(); } } else @@ -229,13 +213,10 @@ namespace OpenSim.Data.NHibernate { if (ExistsFolder(folder.ID)) { - using (ISession session = factory.OpenSession()) + using (ITransaction transaction = session.BeginTransaction()) { - using (ITransaction transaction = session.BeginTransaction()) - { - session.Update(folder); - transaction.Commit(); - } + session.Update(folder); + transaction.Commit(); } } else @@ -250,13 +231,10 @@ namespace OpenSim.Data.NHibernate /// public void deleteInventoryFolder(LLUUID folderID) { - using (ISession session = factory.OpenSession()) + using (ITransaction transaction = session.BeginTransaction()) { - using (ITransaction transaction = session.BeginTransaction()) - { - session.Delete(folderID.ToString()); - transaction.Commit(); - } + session.Delete(folderID.ToString()); + transaction.Commit(); } } @@ -329,23 +307,20 @@ namespace OpenSim.Data.NHibernate /// A List of InventoryItemBase items public List getInventoryInFolder(LLUUID folderID) { - using (ISession session = factory.OpenSession()) + // try { + ICriteria criteria = session.CreateCriteria(typeof(InventoryItemBase)); + criteria.Add(Expression.Eq("Folder", folderID)); + List list = new List(); + foreach (InventoryItemBase item in criteria.List()) { - // try { - ICriteria criteria = session.CreateCriteria(typeof(InventoryItemBase)); - criteria.Add(Expression.Eq("Folder", folderID)); - List list = new List(); - foreach (InventoryItemBase item in criteria.List()) - { - list.Add(item); - } - return list; -// } -// catch -// { -// return new List(); -// } + list.Add(item); } + return list; + // } + // catch + // { + // return new List(); + // } } public List getUserRootFolders(LLUUID user) @@ -356,18 +331,15 @@ namespace OpenSim.Data.NHibernate // see InventoryItemBase.getUserRootFolder public InventoryFolderBase getUserRootFolder(LLUUID user) { - using (ISession session = factory.OpenSession()) + ICriteria criteria = session.CreateCriteria(typeof(InventoryFolderBase)); + criteria.Add(Expression.Eq("ParentID", LLUUID.Zero)); + criteria.Add(Expression.Eq("Owner", user)); + foreach (InventoryFolderBase folder in criteria.List()) { - ICriteria criteria = session.CreateCriteria(typeof(InventoryFolderBase)); - criteria.Add(Expression.Eq("ParentID", LLUUID.Zero)); - criteria.Add(Expression.Eq("Owner", user)); - foreach (InventoryFolderBase folder in criteria.List()) - { - return folder; - } - m_log.ErrorFormat("No Inventory Root Folder Found for: {0}", user); - return null; + return folder; } + m_log.ErrorFormat("No Inventory Root Folder Found for: {0}", user); + return null; } /// @@ -377,15 +349,11 @@ namespace OpenSim.Data.NHibernate /// ID of parent private void getInventoryFolders(ref List folders, LLUUID parentID) { - using (ISession session = factory.OpenSession()) + ICriteria criteria = session.CreateCriteria(typeof(InventoryFolderBase)); + criteria.Add(Expression.Eq("ParentID", parentID)); + foreach (InventoryFolderBase item in criteria.List()) { - - ICriteria criteria = session.CreateCriteria(typeof(InventoryFolderBase)); - criteria.Add(Expression.Eq("ParentID", parentID)); - foreach (InventoryFolderBase item in criteria.List()) - { - folders.Add(item); - } + folders.Add(item); } } diff --git a/OpenSim/Data/NHibernate/NHibernateUserData.cs b/OpenSim/Data/NHibernate/NHibernateUserData.cs index 2bec02c..0fb297f 100644 --- a/OpenSim/Data/NHibernate/NHibernateUserData.cs +++ b/OpenSim/Data/NHibernate/NHibernateUserData.cs @@ -51,6 +51,7 @@ namespace OpenSim.Data.NHibernate private Configuration cfg; private ISessionFactory factory; + private ISession session; public override void Initialise(string connect) { @@ -75,7 +76,8 @@ namespace OpenSim.Data.NHibernate 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"); @@ -87,10 +89,7 @@ namespace OpenSim.Data.NHibernate UserProfileData user = null; try { - using (ISession session = factory.OpenSession()) - { - user = session.Load(typeof(UserProfileData), uuid) as UserProfileData; - } + user = session.Load(typeof(UserProfileData), uuid) as UserProfileData; } catch (Exception) {} @@ -101,11 +100,9 @@ namespace OpenSim.Data.NHibernate { UserProfileData user; // TODO: I'm sure I'll have to do something silly here - using (ISession session = factory.OpenSession()) - { - user = session.Load(typeof(UserProfileData), uuid) as UserProfileData; - user.CurrentAgent = GetAgentByUUID(uuid); - } + user = session.Load(typeof(UserProfileData), uuid) as UserProfileData; + user.CurrentAgent = GetAgentByUUID(uuid); + return user; } @@ -113,16 +110,10 @@ namespace OpenSim.Data.NHibernate { if (!ExistsUser(profile.ID)) { - using (ISession session = factory.OpenSession()) - { - using (ITransaction transaction = session.BeginTransaction()) - { - session.Save(profile); - SetAgentData(profile.ID, profile.CurrentAgent, session); - // TODO: save agent - transaction.Commit(); - } - } + session.Save(profile); + SetAgentData(profile.ID, profile.CurrentAgent); + // TODO: save agent + session.Transaction.Commit(); } else { @@ -131,7 +122,7 @@ namespace OpenSim.Data.NHibernate } } - private static void SetAgentData(LLUUID uuid, UserAgentData agent, ISession session) + private void SetAgentData(LLUUID uuid, UserAgentData agent) { if (agent == null) { @@ -140,14 +131,12 @@ namespace OpenSim.Data.NHibernate else { UserAgentData old = session.Load(typeof(UserAgentData), uuid) as UserAgentData; - if (old == null) + if (old != null) { - session.Save(agent); - } - else - { - session.Update(agent); + session.Delete(old); } + + session.Save(agent); } } @@ -155,16 +144,10 @@ namespace OpenSim.Data.NHibernate { if (ExistsUser(profile.ID)) { - using (ISession session = factory.OpenSession()) - { - using (ITransaction transaction = session.BeginTransaction()) - { - session.Update(profile); - SetAgentData(profile.ID, profile.CurrentAgent, session); - transaction.Commit(); - return true; - } - } + session.Update(profile); + SetAgentData(profile.ID, profile.CurrentAgent); + session.Transaction.Commit(); + return true; } else { @@ -176,36 +159,27 @@ namespace OpenSim.Data.NHibernate override public void AddNewUserAgent(UserAgentData agent) { - using (ISession session = factory.OpenSession()) - { - using (ITransaction transaction = session.BeginTransaction()) - { - session.Save(agent); - transaction.Commit(); - } + UserAgentData old = session.Load(typeof(UserAgentData), agent.ProfileID) as UserAgentData; + + if (old == null) { + session.Save(agent); + session.Transaction.Commit(); + } else { + UpdateUserAgent(agent); } } public void UpdateUserAgent(UserAgentData agent) { - using (ISession session = factory.OpenSession()) - { - using (ITransaction transaction = session.BeginTransaction()) - { - session.Update(agent); - transaction.Commit(); - } - } + session.Update(agent); + session.Transaction.Commit(); } override public UserAgentData GetAgentByUUID(LLUUID uuid) { try { - using (ISession session = factory.OpenSession()) - { - return session.Load(typeof(UserAgentData), uuid) as UserAgentData; - } + return session.Load(typeof(UserAgentData), uuid) as UserAgentData; } catch { @@ -215,18 +189,15 @@ namespace OpenSim.Data.NHibernate override public UserProfileData GetUserByName(string fname, string lname) { - using (ISession session = factory.OpenSession()) + ICriteria criteria = session.CreateCriteria(typeof(UserProfileData)); + criteria.Add(Expression.Eq("FirstName", fname)); + criteria.Add(Expression.Eq("SurName", lname)); + foreach (UserProfileData profile in criteria.List()) { - ICriteria criteria = session.CreateCriteria(typeof(UserProfileData)); - criteria.Add(Expression.Eq("FirstName", fname)); - criteria.Add(Expression.Eq("SurName", lname)); - foreach (UserProfileData profile in criteria.List()) - { - profile.CurrentAgent = GetAgentByUUID(profile.ID); - return profile; - } - return null; + profile.CurrentAgent = GetAgentByUUID(profile.ID); + return profile; } + return null; } override public UserAgentData GetAgentByName(string fname, string lname) @@ -247,19 +218,16 @@ namespace OpenSim.Data.NHibernate if (querysplit.Length == 2) { - using (ISession session = factory.OpenSession()) + 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()) { - 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()) - { - AvatarPickerAvatar user = new AvatarPickerAvatar(); - user.AvatarID = profile.ID; - user.firstName = profile.FirstName; - user.lastName = profile.SurName; - results.Add(user); - } + AvatarPickerAvatar user = new AvatarPickerAvatar(); + user.AvatarID = profile.ID; + user.firstName = profile.FirstName; + user.lastName = profile.SurName; + results.Add(user); } } return results; @@ -281,20 +249,17 @@ namespace OpenSim.Data.NHibernate { AvatarAppearance appearance; // TODO: I'm sure I'll have to do something silly here - using (ISession session = factory.OpenSession()) - { - appearance = session.Load(typeof(AvatarAppearance), user) as AvatarAppearance; - } + appearance = session.Load(typeof(AvatarAppearance), user) as AvatarAppearance; + return appearance; } private bool ExistsAppearance(LLUUID uuid) { AvatarAppearance appearance; - using (ISession session = factory.OpenSession()) - { - appearance = session.Load(typeof(AvatarAppearance), uuid) as AvatarAppearance; - } + + appearance = session.Load(typeof(AvatarAppearance), uuid) as AvatarAppearance; + return (appearance == null) ? false : true; } @@ -302,20 +267,18 @@ namespace OpenSim.Data.NHibernate public override void UpdateUserAppearance(LLUUID user, AvatarAppearance appearance) { bool exists = ExistsAppearance(user); - using (ISession session = factory.OpenSession()) + + using (ITransaction transaction = session.BeginTransaction()) { - using (ITransaction transaction = session.BeginTransaction()) + if (exists) + { + session.Update(appearance); + } + else { - if (exists) - { - session.Update(appearance); - } - else - { - session.Save(appearance); - } - transaction.Commit(); + session.Save(appearance); } + transaction.Commit(); } } diff --git a/OpenSim/Data/NHibernate/Resources/InventoryItemBase.hbm.xml b/OpenSim/Data/NHibernate/Resources/InventoryItemBase.hbm.xml index 82a810e..167dd5b 100644 --- a/OpenSim/Data/NHibernate/Resources/InventoryItemBase.hbm.xml +++ b/OpenSim/Data/NHibernate/Resources/InventoryItemBase.hbm.xml @@ -18,9 +18,9 @@ - - - + + + \ No newline at end of file -- cgit v1.1