diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Data/NHibernate/NHibernateRegionData.cs | 124 |
1 files changed, 82 insertions, 42 deletions
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; | |||
34 | using OpenMetaverse; | 34 | using OpenMetaverse; |
35 | using log4net; | 35 | using log4net; |
36 | using NHibernate; | 36 | using NHibernate; |
37 | using NHibernate.Criterion; | 37 | using NHibernate.Cfg; |
38 | using NHibernate.Expression; | ||
39 | using NHibernate.Mapping.Attributes; | ||
40 | using NHibernate.Tool.hbm2ddl; | ||
38 | using OpenSim.Framework; | 41 | using OpenSim.Framework; |
39 | using OpenSim.Region.Environment.Interfaces; | 42 | using OpenSim.Region.Environment.Interfaces; |
40 | using OpenSim.Region.Environment.Scenes; | 43 | using OpenSim.Region.Environment.Scenes; |
@@ -49,12 +52,51 @@ namespace OpenSim.Data.NHibernate | |||
49 | { | 52 | { |
50 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 53 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
51 | 54 | ||
52 | private NHibernateManager manager; | 55 | private Configuration cfg; |
56 | private ISessionFactory factory; | ||
57 | private ISession session; | ||
58 | |||
59 | // public void Initialise() | ||
60 | // { | ||
61 | // Initialise("SQLiteDialect;SqliteClientDriver;URI=file:OpenSim.db,version=3", true); | ||
62 | // } | ||
53 | 63 | ||
54 | public void Initialise(string connect) | 64 | public void Initialise(string connect) |
55 | { | 65 | { |
56 | m_log.InfoFormat("[NHIBERNATE] Initializing NHibernateRegionData"); | 66 | // Split out the dialect, driver, and connect string |
57 | manager = new NHibernateManager(connect, "RegionStore"); | 67 | char[] split = {';'}; |
68 | string[] parts = connect.Split(split, 3); | ||
69 | if (parts.Length != 3) | ||
70 | { | ||
71 | // TODO: make this a real exception type | ||
72 | throw new Exception("Malformed Region connection string '" + connect + "'"); | ||
73 | } | ||
74 | |||
75 | string dialect = parts[0]; | ||
76 | |||
77 | // NHibernate setup | ||
78 | cfg = new Configuration(); | ||
79 | cfg.SetProperty(Environment.ConnectionProvider, | ||
80 | "NHibernate.Connection.DriverConnectionProvider"); | ||
81 | cfg.SetProperty(Environment.Dialect, | ||
82 | "NHibernate.Dialect." + dialect); | ||
83 | cfg.SetProperty(Environment.ConnectionDriver, | ||
84 | "NHibernate.Driver." + parts[1]); | ||
85 | cfg.SetProperty(Environment.ConnectionString, parts[2]); | ||
86 | cfg.AddAssembly("OpenSim.Data.NHibernate"); | ||
87 | |||
88 | HbmSerializer.Default.Validate = true; | ||
89 | using (MemoryStream stream = | ||
90 | HbmSerializer.Default.Serialize(Assembly.GetExecutingAssembly())) | ||
91 | cfg.AddInputStream(stream); | ||
92 | |||
93 | factory = cfg.BuildSessionFactory(); | ||
94 | session = factory.OpenSession(); | ||
95 | |||
96 | // This actually does the roll forward assembly stuff | ||
97 | Assembly assem = GetType().Assembly; | ||
98 | Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, "RegionStore"); | ||
99 | m.Update(); | ||
58 | } | 100 | } |
59 | 101 | ||
60 | /*********************************************************************** | 102 | /*********************************************************************** |
@@ -80,18 +122,15 @@ namespace OpenSim.Data.NHibernate | |||
80 | { | 122 | { |
81 | try | 123 | try |
82 | { | 124 | { |
83 | SceneObjectPart old = (SceneObjectPart)manager.Load(typeof(SceneObjectPart), p.UUID); | 125 | SceneObjectPart old = session.Load(typeof(SceneObjectPart), p.UUID) as SceneObjectPart; |
84 | if (old != null) | 126 | session.Evict(old); |
85 | { | 127 | session.Update(p); |
86 | m_log.InfoFormat("[NHIBERNATE] updating object {0}", p.UUID); | 128 | m_log.InfoFormat("[NHIBERNATE] updating object {0}", p.UUID); |
87 | manager.Update(old); | 129 | } |
88 | } | 130 | catch (ObjectNotFoundException) |
89 | else | 131 | { |
90 | { | 132 | m_log.InfoFormat("[NHIBERNATE] saving object {0}", p.UUID); |
91 | m_log.InfoFormat("[NHIBERNATE] saving object {0}", p.UUID); | 133 | session.Save(p); |
92 | manager.Save(p); | ||
93 | } | ||
94 | |||
95 | } | 134 | } |
96 | catch (Exception e) | 135 | catch (Exception e) |
97 | { | 136 | { |
@@ -103,19 +142,15 @@ namespace OpenSim.Data.NHibernate | |||
103 | { | 142 | { |
104 | try | 143 | try |
105 | { | 144 | { |
106 | 145 | Terrain old = session.Load(typeof(Terrain), t.RegionID) as Terrain; | |
107 | Terrain old = (Terrain)manager.Load(typeof(Terrain), t.RegionID); | 146 | session.Evict(old); |
108 | if (old != null) | 147 | session.Update(t); |
109 | { | 148 | session.Flush(); |
110 | m_log.InfoFormat("[NHIBERNATE] updating terrain {0}", t.RegionID); | 149 | } |
111 | manager.Update(old); | 150 | catch (ObjectNotFoundException) |
112 | } | 151 | { |
113 | else | 152 | session.Save(t); |
114 | { | 153 | session.Flush(); |
115 | m_log.InfoFormat("[NHIBERNATE] saving terrain {0}", t.RegionID); | ||
116 | manager.Save(t); | ||
117 | } | ||
118 | |||
119 | } | 154 | } |
120 | catch (Exception e) | 155 | catch (Exception e) |
121 | { | 156 | { |
@@ -138,6 +173,7 @@ namespace OpenSim.Data.NHibernate | |||
138 | m_log.InfoFormat("Storing part {0}", part.UUID); | 173 | m_log.InfoFormat("Storing part {0}", part.UUID); |
139 | SaveOrUpdate(part); | 174 | SaveOrUpdate(part); |
140 | } | 175 | } |
176 | session.Flush(); | ||
141 | } | 177 | } |
142 | catch (Exception e) | 178 | catch (Exception e) |
143 | { | 179 | { |
@@ -149,7 +185,7 @@ namespace OpenSim.Data.NHibernate | |||
149 | { | 185 | { |
150 | SceneObjectGroup group = new SceneObjectGroup(); | 186 | SceneObjectGroup group = new SceneObjectGroup(); |
151 | 187 | ||
152 | ICriteria criteria = manager.GetSession().CreateCriteria(typeof(SceneObjectPart)); | 188 | ICriteria criteria = session.CreateCriteria(typeof(SceneObjectPart)); |
153 | criteria.Add(Expression.Eq("RegionID", region)); | 189 | criteria.Add(Expression.Eq("RegionID", region)); |
154 | criteria.Add(Expression.Eq("ParentUUID", uuid)); | 190 | criteria.Add(Expression.Eq("ParentUUID", uuid)); |
155 | criteria.AddOrder( Order.Asc("ParentID") ); | 191 | criteria.AddOrder( Order.Asc("ParentID") ); |
@@ -180,8 +216,9 @@ namespace OpenSim.Data.NHibernate | |||
180 | SceneObjectGroup g = LoadObject(obj, regionUUID); | 216 | SceneObjectGroup g = LoadObject(obj, regionUUID); |
181 | foreach (SceneObjectPart p in g.Children.Values) | 217 | foreach (SceneObjectPart p in g.Children.Values) |
182 | { | 218 | { |
183 | manager.Delete(p); | 219 | session.Delete(p); |
184 | } | 220 | } |
221 | session.Flush(); | ||
185 | 222 | ||
186 | m_log.InfoFormat("[REGION DB]: Removing obj: {0} from region: {1}", obj.Guid, regionUUID); | 223 | m_log.InfoFormat("[REGION DB]: Removing obj: {0} from region: {1}", obj.Guid, regionUUID); |
187 | 224 | ||
@@ -197,7 +234,7 @@ namespace OpenSim.Data.NHibernate | |||
197 | Dictionary<UUID, SceneObjectGroup> SOG = new Dictionary<UUID, SceneObjectGroup>(); | 234 | Dictionary<UUID, SceneObjectGroup> SOG = new Dictionary<UUID, SceneObjectGroup>(); |
198 | List<SceneObjectGroup> ret = new List<SceneObjectGroup>(); | 235 | List<SceneObjectGroup> ret = new List<SceneObjectGroup>(); |
199 | 236 | ||
200 | ICriteria criteria = manager.GetSession().CreateCriteria(typeof(SceneObjectPart)); | 237 | ICriteria criteria = session.CreateCriteria(typeof(SceneObjectPart)); |
201 | criteria.Add(Expression.Eq("RegionID", regionUUID)); | 238 | criteria.Add(Expression.Eq("RegionID", regionUUID)); |
202 | criteria.AddOrder( Order.Asc("ParentID") ); | 239 | criteria.AddOrder( Order.Asc("ParentID") ); |
203 | foreach (SceneObjectPart p in criteria.List()) | 240 | foreach (SceneObjectPart p in criteria.List()) |
@@ -215,7 +252,7 @@ namespace OpenSim.Data.NHibernate | |||
215 | } | 252 | } |
216 | // get the inventory | 253 | // get the inventory |
217 | 254 | ||
218 | ICriteria InvCriteria = manager.GetSession().CreateCriteria(typeof(TaskInventoryItem)); | 255 | ICriteria InvCriteria = session.CreateCriteria(typeof(TaskInventoryItem)); |
219 | InvCriteria.Add(Expression.Eq("ParentPartID", p.UUID)); | 256 | InvCriteria.Add(Expression.Eq("ParentPartID", p.UUID)); |
220 | IList<TaskInventoryItem> inventory = new List<TaskInventoryItem>(); | 257 | IList<TaskInventoryItem> inventory = new List<TaskInventoryItem>(); |
221 | foreach (TaskInventoryItem i in InvCriteria.List()) | 258 | foreach (TaskInventoryItem i in InvCriteria.List()) |
@@ -254,14 +291,16 @@ namespace OpenSim.Data.NHibernate | |||
254 | /// <returns>Heightfield data</returns> | 291 | /// <returns>Heightfield data</returns> |
255 | public double[,] LoadTerrain(UUID regionID) | 292 | public double[,] LoadTerrain(UUID regionID) |
256 | { | 293 | { |
257 | Terrain t = (Terrain)manager.Load(typeof(Terrain), regionID); | 294 | try |
258 | if (t != null) | ||
259 | { | 295 | { |
296 | Terrain t = session.Load(typeof(Terrain), regionID) as Terrain; | ||
260 | return t.Doubles; | 297 | return t.Doubles; |
261 | } | 298 | } |
262 | 299 | catch (ObjectNotFoundException) | |
263 | m_log.Info("No terrain yet"); | 300 | { |
264 | return null; | 301 | m_log.Info("No terrain yet"); |
302 | return null; | ||
303 | } | ||
265 | } | 304 | } |
266 | 305 | ||
267 | /// <summary> | 306 | /// <summary> |
@@ -300,7 +339,7 @@ namespace OpenSim.Data.NHibernate | |||
300 | /// </summary> | 339 | /// </summary> |
301 | public void Shutdown() | 340 | public void Shutdown() |
302 | { | 341 | { |
303 | //session.Flush(); | 342 | session.Flush(); |
304 | } | 343 | } |
305 | 344 | ||
306 | /// <summary> | 345 | /// <summary> |
@@ -358,20 +397,21 @@ namespace OpenSim.Data.NHibernate | |||
358 | /// <param name="items"></param> | 397 | /// <param name="items"></param> |
359 | public void StorePrimInventory(UUID primID, ICollection<TaskInventoryItem> items) | 398 | public void StorePrimInventory(UUID primID, ICollection<TaskInventoryItem> items) |
360 | { | 399 | { |
361 | ICriteria criteria = manager.GetSession().CreateCriteria(typeof(TaskInventoryItem)); | 400 | ICriteria criteria = session.CreateCriteria(typeof(TaskInventoryItem)); |
362 | criteria.Add(Expression.Eq("ParentPartID", primID)); | 401 | criteria.Add(Expression.Eq("ParentPartID", primID)); |
363 | try | 402 | try |
364 | { | 403 | { |
365 | foreach (TaskInventoryItem i in criteria.List()) | 404 | foreach (TaskInventoryItem i in criteria.List()) |
366 | { | 405 | { |
367 | manager.Delete(i); | 406 | session.Delete(i); |
368 | } | 407 | } |
369 | 408 | ||
370 | foreach (TaskInventoryItem i in items) | 409 | foreach (TaskInventoryItem i in items) |
371 | { | 410 | { |
372 | manager.Save(i); | 411 | session.Save(i); |
373 | 412 | ||
374 | } | 413 | } |
414 | session.Flush(); | ||
375 | } | 415 | } |
376 | catch (Exception e) | 416 | catch (Exception e) |
377 | { | 417 | { |