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.
0.6.0-stable
Sean Dague 2008-06-20 20:23:55 +00:00
parent 0c84c27265
commit 17fd6cf661
4 changed files with 136 additions and 212 deletions

View File

@ -49,6 +49,7 @@ namespace OpenSim.Data.NHibernate
private Configuration cfg; private Configuration cfg;
private ISessionFactory factory; private ISessionFactory factory;
private ISession session;
public override void Initialise() public override void Initialise()
{ {
@ -87,27 +88,25 @@ namespace OpenSim.Data.NHibernate
cfg.AddInputStream(stream); cfg.AddInputStream(stream);
factory = cfg.BuildSessionFactory(); factory = cfg.BuildSessionFactory();
session = factory.OpenSession();
// This actually does the roll forward assembly stuff // This actually does the roll forward assembly stuff
Assembly assem = GetType().Assembly; Assembly assem = GetType().Assembly;
Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, "AssetStore"); Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, "AssetStore");
m.Update(); m.Update();
} }
override public AssetBase FetchAsset(LLUUID uuid) override public AssetBase FetchAsset(LLUUID uuid)
{ {
using (ISession session = factory.OpenSession()) try
{ {
try return session.Load(typeof(AssetBase), uuid) as AssetBase;
{ }
return session.Load(typeof(AssetBase), uuid) as AssetBase; catch (Exception e)
} {
catch (Exception e) m_log.Error("[NHIBERNATE] issue loading asset", e);
{ return null;
m_log.Error("[NHIBERNATE] issue loading asset", e);
return null;
}
} }
} }
@ -116,13 +115,10 @@ namespace OpenSim.Data.NHibernate
if (!ExistsAsset(asset.FullID)) if (!ExistsAsset(asset.FullID))
{ {
m_log.InfoFormat("[NHIBERNATE] inserting asset {0}", 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)) 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();
}
} }
} }
} }

View File

@ -48,6 +48,7 @@ namespace OpenSim.Data.NHibernate
private Configuration cfg; private Configuration cfg;
private ISessionFactory factory; private ISessionFactory factory;
private ISession session;
/// <summary> /// <summary>
/// Initialises the interface /// Initialises the interface
@ -76,6 +77,7 @@ namespace OpenSim.Data.NHibernate
cfg.AddAssembly("OpenSim.Data.NHibernate"); cfg.AddAssembly("OpenSim.Data.NHibernate");
factory = cfg.BuildSessionFactory(); factory = cfg.BuildSessionFactory();
session = factory.OpenSession();
// This actually does the roll forward assembly stuff // This actually does the roll forward assembly stuff
Assembly assem = GetType().Assembly; Assembly assem = GetType().Assembly;
@ -99,17 +101,14 @@ namespace OpenSim.Data.NHibernate
/// <returns>A class containing item information</returns> /// <returns>A class containing item information</returns>
public InventoryItemBase getInventoryItem(LLUUID item) public InventoryItemBase getInventoryItem(LLUUID item)
{ {
using (ISession session = factory.OpenSession()) try
{ {
try return session.Load(typeof(InventoryItemBase), item) as InventoryItemBase;
{ }
return session.Load(typeof(InventoryItemBase), item) as InventoryItemBase; catch
} {
catch m_log.ErrorFormat("Couldn't find inventory item: {0}", item);
{ return null;
m_log.ErrorFormat("Couldn't find inventory item: {0}", item);
return null;
}
} }
} }
@ -121,13 +120,10 @@ namespace OpenSim.Data.NHibernate
{ {
if (!ExistsItem(item.ID)) 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 else
@ -145,13 +141,10 @@ namespace OpenSim.Data.NHibernate
{ {
if (ExistsItem(item.ID)) 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 else
@ -166,13 +159,10 @@ namespace OpenSim.Data.NHibernate
/// <param name="item"></param> /// <param name="item"></param>
public void deleteInventoryItem(LLUUID itemID) 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
/// <returns>A class containing folder information</returns> /// <returns>A class containing folder information</returns>
public InventoryFolderBase getInventoryFolder(LLUUID folder) public InventoryFolderBase getInventoryFolder(LLUUID folder)
{ {
using (ISession session = factory.OpenSession()) try
{ {
try return session.Load(typeof(InventoryFolderBase), folder) as InventoryFolderBase;
{ }
return session.Load(typeof(InventoryFolderBase), folder) as InventoryFolderBase; catch
} {
catch m_log.ErrorFormat("Couldn't find inventory item: {0}", folder);
{ return null;
m_log.ErrorFormat("Couldn't find inventory item: {0}", folder);
return null;
}
} }
} }
@ -205,13 +192,10 @@ namespace OpenSim.Data.NHibernate
{ {
if (!ExistsFolder(folder.ID)) 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 else
@ -229,13 +213,10 @@ namespace OpenSim.Data.NHibernate
{ {
if (ExistsFolder(folder.ID)) 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 else
@ -250,13 +231,10 @@ namespace OpenSim.Data.NHibernate
/// <param name="folder"></param> /// <param name="folder"></param>
public void deleteInventoryFolder(LLUUID folderID) 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
/// <returns>A List of InventoryItemBase items</returns> /// <returns>A List of InventoryItemBase items</returns>
public List<InventoryItemBase> getInventoryInFolder(LLUUID folderID) public List<InventoryItemBase> getInventoryInFolder(LLUUID folderID)
{ {
using (ISession session = factory.OpenSession()) // try {
ICriteria criteria = session.CreateCriteria(typeof(InventoryItemBase));
criteria.Add(Expression.Eq("Folder", folderID));
List<InventoryItemBase> list = new List<InventoryItemBase>();
foreach (InventoryItemBase item in criteria.List())
{ {
// try { list.Add(item);
ICriteria criteria = session.CreateCriteria(typeof(InventoryItemBase));
criteria.Add(Expression.Eq("Folder", folderID));
List<InventoryItemBase> list = new List<InventoryItemBase>();
foreach (InventoryItemBase item in criteria.List())
{
list.Add(item);
}
return list;
// }
// catch
// {
// return new List<InventoryItemBase>();
// }
} }
return list;
// }
// catch
// {
// return new List<InventoryItemBase>();
// }
} }
public List<InventoryFolderBase> getUserRootFolders(LLUUID user) public List<InventoryFolderBase> getUserRootFolders(LLUUID user)
@ -356,18 +331,15 @@ namespace OpenSim.Data.NHibernate
// see InventoryItemBase.getUserRootFolder // see InventoryItemBase.getUserRootFolder
public InventoryFolderBase getUserRootFolder(LLUUID user) 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)); return folder;
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;
} }
m_log.ErrorFormat("No Inventory Root Folder Found for: {0}", user);
return null;
} }
/// <summary> /// <summary>
@ -377,15 +349,11 @@ namespace OpenSim.Data.NHibernate
/// <param name="parentID">ID of parent</param> /// <param name="parentID">ID of parent</param>
private void getInventoryFolders(ref List<InventoryFolderBase> folders, LLUUID parentID) private void getInventoryFolders(ref List<InventoryFolderBase> 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())
{ {
folders.Add(item);
ICriteria criteria = session.CreateCriteria(typeof(InventoryFolderBase));
criteria.Add(Expression.Eq("ParentID", parentID));
foreach (InventoryFolderBase item in criteria.List())
{
folders.Add(item);
}
} }
} }

View File

@ -51,6 +51,7 @@ namespace OpenSim.Data.NHibernate
private Configuration cfg; private Configuration cfg;
private ISessionFactory factory; private ISessionFactory factory;
private ISession session;
public override void Initialise(string connect) public override void Initialise(string connect)
{ {
@ -75,7 +76,8 @@ namespace OpenSim.Data.NHibernate
cfg.AddAssembly("OpenSim.Data.NHibernate"); cfg.AddAssembly("OpenSim.Data.NHibernate");
factory = cfg.BuildSessionFactory(); factory = cfg.BuildSessionFactory();
session = factory.OpenSession();
// This actually does the roll forward assembly stuff // This actually does the roll forward assembly stuff
Assembly assem = GetType().Assembly; Assembly assem = GetType().Assembly;
Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, "UserStore"); 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; UserProfileData user = null;
try try
{ {
using (ISession session = factory.OpenSession()) user = session.Load(typeof(UserProfileData), uuid) as UserProfileData;
{
user = session.Load(typeof(UserProfileData), uuid) as UserProfileData;
}
} }
catch (Exception) {} catch (Exception) {}
@ -101,11 +100,9 @@ namespace OpenSim.Data.NHibernate
{ {
UserProfileData user; UserProfileData user;
// TODO: I'm sure I'll have to do something silly here // 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; return user;
} }
@ -113,16 +110,10 @@ namespace OpenSim.Data.NHibernate
{ {
if (!ExistsUser(profile.ID)) if (!ExistsUser(profile.ID))
{ {
using (ISession session = factory.OpenSession()) session.Save(profile);
{ SetAgentData(profile.ID, profile.CurrentAgent);
using (ITransaction transaction = session.BeginTransaction()) // TODO: save agent
{ session.Transaction.Commit();
session.Save(profile);
SetAgentData(profile.ID, profile.CurrentAgent, session);
// TODO: save agent
transaction.Commit();
}
}
} }
else 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) if (agent == null)
{ {
@ -140,14 +131,12 @@ namespace OpenSim.Data.NHibernate
else else
{ {
UserAgentData old = session.Load(typeof(UserAgentData), uuid) as UserAgentData; UserAgentData old = session.Load(typeof(UserAgentData), uuid) as UserAgentData;
if (old == null) if (old != null)
{ {
session.Save(agent); session.Delete(old);
}
else
{
session.Update(agent);
} }
session.Save(agent);
} }
} }
@ -155,16 +144,10 @@ namespace OpenSim.Data.NHibernate
{ {
if (ExistsUser(profile.ID)) if (ExistsUser(profile.ID))
{ {
using (ISession session = factory.OpenSession()) session.Update(profile);
{ SetAgentData(profile.ID, profile.CurrentAgent);
using (ITransaction transaction = session.BeginTransaction()) session.Transaction.Commit();
{ return true;
session.Update(profile);
SetAgentData(profile.ID, profile.CurrentAgent, session);
transaction.Commit();
return true;
}
}
} }
else else
{ {
@ -176,36 +159,27 @@ namespace OpenSim.Data.NHibernate
override public void AddNewUserAgent(UserAgentData agent) override public void AddNewUserAgent(UserAgentData agent)
{ {
using (ISession session = factory.OpenSession()) UserAgentData old = session.Load(typeof(UserAgentData), agent.ProfileID) as UserAgentData;
{
using (ITransaction transaction = session.BeginTransaction()) if (old == null) {
{ session.Save(agent);
session.Save(agent); session.Transaction.Commit();
transaction.Commit(); } else {
} UpdateUserAgent(agent);
} }
} }
public void UpdateUserAgent(UserAgentData agent) public void UpdateUserAgent(UserAgentData agent)
{ {
using (ISession session = factory.OpenSession()) session.Update(agent);
{ session.Transaction.Commit();
using (ITransaction transaction = session.BeginTransaction())
{
session.Update(agent);
transaction.Commit();
}
}
} }
override public UserAgentData GetAgentByUUID(LLUUID uuid) override public UserAgentData GetAgentByUUID(LLUUID uuid)
{ {
try try
{ {
using (ISession session = factory.OpenSession()) return session.Load(typeof(UserAgentData), uuid) as UserAgentData;
{
return session.Load(typeof(UserAgentData), uuid) as UserAgentData;
}
} }
catch catch
{ {
@ -215,18 +189,15 @@ namespace OpenSim.Data.NHibernate
override public UserProfileData GetUserByName(string fname, string lname) 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)); profile.CurrentAgent = GetAgentByUUID(profile.ID);
criteria.Add(Expression.Eq("FirstName", fname)); return profile;
criteria.Add(Expression.Eq("SurName", lname));
foreach (UserProfileData profile in criteria.List())
{
profile.CurrentAgent = GetAgentByUUID(profile.ID);
return profile;
}
return null;
} }
return null;
} }
override public UserAgentData GetAgentByName(string fname, string lname) override public UserAgentData GetAgentByName(string fname, string lname)
@ -247,19 +218,16 @@ namespace OpenSim.Data.NHibernate
if (querysplit.Length == 2) 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)); AvatarPickerAvatar user = new AvatarPickerAvatar();
criteria.Add(Expression.Like("FirstName", querysplit[0])); user.AvatarID = profile.ID;
criteria.Add(Expression.Like("SurName", querysplit[1])); user.firstName = profile.FirstName;
foreach (UserProfileData profile in criteria.List()) 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; return results;
@ -281,20 +249,17 @@ namespace OpenSim.Data.NHibernate
{ {
AvatarAppearance appearance; AvatarAppearance appearance;
// TODO: I'm sure I'll have to do something silly here // 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; return appearance;
} }
private bool ExistsAppearance(LLUUID uuid) private bool ExistsAppearance(LLUUID uuid)
{ {
AvatarAppearance appearance; 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; return (appearance == null) ? false : true;
} }
@ -302,20 +267,18 @@ namespace OpenSim.Data.NHibernate
public override void UpdateUserAppearance(LLUUID user, AvatarAppearance appearance) public override void UpdateUserAppearance(LLUUID user, AvatarAppearance appearance)
{ {
bool exists = ExistsAppearance(user); bool exists = ExistsAppearance(user);
using (ISession session = factory.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{ {
using (ITransaction transaction = session.BeginTransaction()) if (exists)
{ {
if (exists) session.Update(appearance);
{
session.Update(appearance);
}
else
{
session.Save(appearance);
}
transaction.Commit();
} }
else
{
session.Save(appearance);
}
transaction.Commit();
} }
} }

View File

@ -18,9 +18,9 @@
<property name="EveryOnePermissions" type="System.UInt32" /> <property name="EveryOnePermissions" type="System.UInt32" />
<property name="GroupID" index="item_group_id" type="OpenSim.Data.NHibernate.LLUUIDUserType, OpenSim.Data.NHibernate" /> <property name="GroupID" index="item_group_id" type="OpenSim.Data.NHibernate.LLUUIDUserType, OpenSim.Data.NHibernate" />
<property name="GroupOwned" type="boolean" /> <property name="GroupOwned" type="boolean" />
<property name="SalePrice" type="Int32" /> <property name="SalePrice" type="System.Int32" />
<property name="SaleType" type="Int16" /> <property name="SaleType" type="System.Byte" />
<property name="Flags" type="Int32" /> <property name="Flags" type="Syste.UInt32" />
<property name="CreationDate" type="Int32" /> <property name="CreationDate" type="Int32" />
</class> </class>
</hibernate-mapping> </hibernate-mapping>