Db4LocalStorage.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) OpenSim project, http://sim.opensecondlife.org/
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the <organization> nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using Db4objects.Db4o;
  30. using Db4objects.Db4o.Query;
  31. using libsecondlife;
  32. using OpenSim.Framework.Interfaces;
  33. using OpenSim.Framework.Types;
  34. using OpenSim.Framework.Terrain;
  35. namespace OpenSim.Storage.LocalStorageDb4o
  36. {
  37. /// <summary>
  38. ///
  39. /// </summary>
  40. public class Db4LocalStorage : ILocalStorage
  41. {
  42. private IObjectContainer db;
  43. private string datastore;
  44. public Db4LocalStorage()
  45. {
  46. }
  47. public void Initialise(string dfile)
  48. {
  49. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Db4LocalStorage Opening " + dfile);
  50. datastore = dfile;
  51. try
  52. {
  53. db = Db4oFactory.OpenFile(datastore);
  54. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Db4LocalStorage creation");
  55. }
  56. catch (Exception e)
  57. {
  58. db.Close();
  59. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Db4LocalStorage :Constructor - Exception occured");
  60. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(e.ToString());
  61. }
  62. }
  63. public void StorePrim(PrimData prim)
  64. {
  65. IObjectSet result = db.Query(new UUIDQuery(prim.FullID));
  66. if(result.Count>0)
  67. {
  68. //prim already in storage
  69. //so update it
  70. PrimData found = (PrimData) result.Next();
  71. found.PathBegin = prim.PathBegin;
  72. found.PathCurve= prim.PathCurve;
  73. found.PathEnd = prim.PathEnd;
  74. found.PathRadiusOffset = prim.PathRadiusOffset;
  75. found.PathRevolutions = prim.PathRevolutions;
  76. found.PathScaleX= prim.PathScaleX;
  77. found.PathScaleY = prim.PathScaleY;
  78. found.PathShearX = prim.PathShearX;
  79. found.PathShearY = prim.PathShearY;
  80. found.PathSkew = prim.PathSkew;
  81. found.PathTaperX = prim.PathTaperX;
  82. found.PathTaperY = prim.PathTaperY;
  83. found.PathTwist = prim.PathTwist;
  84. found.PathTwistBegin = prim.PathTwistBegin;
  85. found.PCode = prim.PCode;
  86. found.ProfileBegin = prim.ProfileBegin;
  87. found.ProfileCurve = prim.ProfileCurve;
  88. found.ProfileEnd = prim.ProfileEnd;
  89. found.ProfileHollow = prim.ProfileHollow;
  90. found.Position = prim.Position;
  91. found.Rotation = prim.Rotation;
  92. found.Texture = prim.Texture;
  93. db.Set(found);
  94. db.Commit();
  95. }
  96. else
  97. {
  98. //not in storage
  99. db.Set(prim);
  100. db.Commit();
  101. }
  102. }
  103. public void RemovePrim(LLUUID primID)
  104. {
  105. IObjectSet result = db.Query(new UUIDQuery(primID));
  106. if(result.Count>0)
  107. {
  108. PrimData found = (PrimData) result.Next();
  109. db.Delete(found);
  110. }
  111. }
  112. public void LoadPrimitives(ILocalStorageReceiver receiver)
  113. {
  114. IObjectSet result = db.Get(typeof(PrimData));
  115. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Db4LocalStorage.cs: LoadPrimitives() - number of prims in storages is "+result.Count);
  116. foreach (PrimData prim in result) {
  117. receiver.PrimFromStorage(prim);
  118. }
  119. }
  120. public float[] LoadWorld()
  121. {
  122. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("LoadWorld() - Loading world....");
  123. //World blank = new World();
  124. float[] heightmap = null;
  125. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("LoadWorld() - Looking for a heightmap in local DB");
  126. IObjectSet world_result = db.Get(typeof(MapStorage));
  127. if (world_result.Count > 0)
  128. {
  129. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("LoadWorld() - Found a heightmap in local database, loading");
  130. MapStorage map = (MapStorage)world_result.Next();
  131. //blank.LandMap = map.Map;
  132. heightmap = map.Map;
  133. }
  134. else
  135. {
  136. /*
  137. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("LoadWorld() - No heightmap found, generating new one");
  138. HeightmapGenHills hills = new HeightmapGenHills();
  139. // blank.LandMap = hills.GenerateHeightmap(200, 4.0f, 80.0f, false);
  140. // heightmap = hills.GenerateHeightmap(200, 4.0f, 80.0f, false);
  141. heightmap = new float[256, 256];
  142. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("LoadWorld() - Saving heightmap to local database");
  143. MapStorage map = new MapStorage();
  144. map.Map = heightmap; //blank.LandMap;
  145. db.Set(map);
  146. db.Commit();
  147. */
  148. }
  149. return heightmap;
  150. }
  151. public void SaveMap(float[] heightmap)
  152. {
  153. IObjectSet world_result = db.Get(typeof(MapStorage));
  154. if (world_result.Count > 0)
  155. {
  156. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("SaveWorld() - updating saved copy of heightmap in local database");
  157. MapStorage map = (MapStorage)world_result.Next();
  158. db.Delete(map);
  159. }
  160. MapStorage map1 = new MapStorage();
  161. map1.Map = heightmap; //OpenSim_Main.local_world.LandMap;
  162. db.Set(map1);
  163. db.Commit();
  164. }
  165. public void ShutDown()
  166. {
  167. db.Commit();
  168. db.Close();
  169. }
  170. }
  171. }