Db4LocalStorage.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. using System;
  29. using Db4objects.Db4o;
  30. using libsecondlife;
  31. using OpenSim.Framework.Console;
  32. using OpenSim.Framework.Interfaces;
  33. using OpenSim.Framework.Types;
  34. namespace OpenSim.Region.Storage.LocalStorageDb4o
  35. {
  36. /// <summary>
  37. ///
  38. /// </summary>
  39. public class Db4LocalStorage : ILocalStorage
  40. {
  41. private IObjectContainer db;
  42. private string datastore;
  43. public Db4LocalStorage()
  44. {
  45. }
  46. public void Initialise(string dfile)
  47. {
  48. MainLog.Instance.Warn("Db4LocalStorage Opening " + dfile);
  49. datastore = dfile;
  50. try
  51. {
  52. db = Db4oFactory.OpenFile(datastore);
  53. MainLog.Instance.Verbose("Db4LocalStorage creation");
  54. }
  55. catch (Exception e)
  56. {
  57. db.Close();
  58. MainLog.Instance.Warn("Db4LocalStorage :Constructor - Exception occured");
  59. MainLog.Instance.Warn(e.ToString());
  60. }
  61. }
  62. public void StorePrim(PrimData prim)
  63. {
  64. IObjectSet result = db.Query(new UUIDPrimQuery(prim.FullID));
  65. if (result.Count > 0)
  66. {
  67. //prim already in storage
  68. //so update it
  69. PrimData found = (PrimData)result.Next();
  70. found.PathBegin = prim.PathBegin;
  71. found.PathCurve = prim.PathCurve;
  72. found.PathEnd = prim.PathEnd;
  73. found.PathRadiusOffset = prim.PathRadiusOffset;
  74. found.PathRevolutions = prim.PathRevolutions;
  75. found.PathScaleX = prim.PathScaleX;
  76. found.PathScaleY = prim.PathScaleY;
  77. found.PathShearX = prim.PathShearX;
  78. found.PathShearY = prim.PathShearY;
  79. found.PathSkew = prim.PathSkew;
  80. found.PathTaperX = prim.PathTaperX;
  81. found.PathTaperY = prim.PathTaperY;
  82. found.PathTwist = prim.PathTwist;
  83. found.PathTwistBegin = prim.PathTwistBegin;
  84. found.PCode = prim.PCode;
  85. found.ProfileBegin = prim.ProfileBegin;
  86. found.ProfileCurve = prim.ProfileCurve;
  87. found.ProfileEnd = prim.ProfileEnd;
  88. found.ProfileHollow = prim.ProfileHollow;
  89. found.Position = prim.Position;
  90. found.Rotation = prim.Rotation;
  91. found.TextureEntry = prim.TextureEntry;
  92. db.Set(found);
  93. db.Commit();
  94. }
  95. else
  96. {
  97. //not in storage
  98. db.Set(prim);
  99. db.Commit();
  100. }
  101. }
  102. public void RemovePrim(LLUUID primID)
  103. {
  104. IObjectSet result = db.Query(new UUIDPrimQuery(primID));
  105. if (result.Count > 0)
  106. {
  107. PrimData found = (PrimData)result.Next();
  108. db.Delete(found);
  109. }
  110. }
  111. public void LoadPrimitives(ILocalStorageReceiver receiver)
  112. {
  113. IObjectSet result = db.Get(typeof(PrimData));
  114. MainLog.Instance.Verbose("Db4LocalStorage.cs: LoadPrimitives() - number of prims in storages is " + result.Count);
  115. foreach (PrimData prim in result)
  116. {
  117. receiver.PrimFromStorage(prim);
  118. }
  119. }
  120. public float[] LoadWorld()
  121. {
  122. MainLog.Instance.Verbose("LoadWorld() - Loading world....");
  123. float[] heightmap = null;
  124. MainLog.Instance.Verbose("LoadWorld() - Looking for a heightmap in local DB");
  125. IObjectSet world_result = db.Get(typeof(MapStorage));
  126. if (world_result.Count > 0)
  127. {
  128. MainLog.Instance.Verbose("LoadWorld() - Found a heightmap in local database, loading");
  129. MapStorage map = (MapStorage)world_result.Next();
  130. //blank.LandMap = map.Map;
  131. heightmap = map.Map;
  132. }
  133. return heightmap;
  134. }
  135. public void SaveMap(float[] heightmap)
  136. {
  137. IObjectSet world_result = db.Get(typeof(MapStorage));
  138. if (world_result.Count > 0)
  139. {
  140. MainLog.Instance.Verbose("SaveWorld() - updating saved copy of heightmap in local database");
  141. MapStorage map = (MapStorage)world_result.Next();
  142. db.Delete(map);
  143. }
  144. MapStorage map1 = new MapStorage();
  145. map1.Map = heightmap; //OpenSim_Main.local_world.LandMap;
  146. db.Set(map1);
  147. db.Commit();
  148. }
  149. public void SaveParcel(ParcelData parcel)
  150. {
  151. IObjectSet result = db.Query(new UUIDParcelQuery(parcel.globalID));
  152. if (result.Count > 0)
  153. {
  154. //Old Parcel
  155. ParcelData updateParcel = (ParcelData)result.Next();
  156. updateParcel.AABBMax = parcel.AABBMax;
  157. updateParcel.AABBMin = parcel.AABBMin;
  158. updateParcel.area = parcel.area;
  159. updateParcel.auctionID = parcel.auctionID;
  160. updateParcel.authBuyerID = parcel.authBuyerID;
  161. updateParcel.category = parcel.category;
  162. updateParcel.claimDate = parcel.claimDate;
  163. updateParcel.claimPrice = parcel.claimPrice;
  164. updateParcel.groupID = parcel.groupID;
  165. updateParcel.groupPrims = parcel.groupPrims;
  166. updateParcel.isGroupOwned = parcel.isGroupOwned;
  167. updateParcel.landingType = parcel.landingType;
  168. updateParcel.mediaAutoScale = parcel.mediaAutoScale;
  169. updateParcel.mediaID = parcel.mediaID;
  170. updateParcel.mediaURL = parcel.mediaURL;
  171. updateParcel.musicURL = parcel.musicURL;
  172. updateParcel.localID = parcel.localID;
  173. updateParcel.ownerID = parcel.ownerID;
  174. updateParcel.passHours = parcel.passHours;
  175. updateParcel.passPrice = parcel.passPrice;
  176. updateParcel.parcelBitmapByteArray = (byte[])parcel.parcelBitmapByteArray.Clone();
  177. updateParcel.parcelDesc = parcel.parcelDesc;
  178. updateParcel.parcelFlags = parcel.parcelFlags;
  179. updateParcel.parcelName = parcel.parcelName;
  180. updateParcel.parcelStatus = parcel.parcelStatus;
  181. updateParcel.salePrice = parcel.salePrice;
  182. updateParcel.snapshotID = parcel.snapshotID;
  183. updateParcel.userLocation = parcel.userLocation;
  184. updateParcel.userLookAt = parcel.userLookAt;
  185. db.Set(updateParcel);
  186. }
  187. else
  188. {
  189. db.Set(parcel);
  190. }
  191. db.Commit();
  192. }
  193. public void SaveParcels(ParcelData[] parcel_data)
  194. {
  195. MainLog.Instance.Notice("Parcel Backup: Saving Parcels...");
  196. int i;
  197. for (i = 0; i < parcel_data.GetLength(0); i++)
  198. {
  199. SaveParcel(parcel_data[i]);
  200. }
  201. MainLog.Instance.Notice("Parcel Backup: Parcel Save Complete");
  202. }
  203. public void RemoveParcel(ParcelData parcel)
  204. {
  205. IObjectSet result = db.Query(new UUIDParcelQuery(parcel.globalID));
  206. if (result.Count > 0)
  207. {
  208. db.Delete(result[0]);
  209. }
  210. db.Commit();
  211. }
  212. public void RemoveAllParcels()
  213. {
  214. MainLog.Instance.Notice("Parcel Backup: Removing all parcels...");
  215. IObjectSet result = db.Get(typeof(ParcelData));
  216. if (result.Count > 0)
  217. {
  218. foreach (ParcelData parcelData in result)
  219. {
  220. RemoveParcel(parcelData);
  221. }
  222. }
  223. }
  224. public void LoadParcels(ILocalStorageParcelReceiver recv)
  225. {
  226. MainLog.Instance.Notice("Parcel Backup: Loading Parcels...");
  227. IObjectSet result = db.Get(typeof(ParcelData));
  228. if (result.Count > 0)
  229. {
  230. MainLog.Instance.Notice("Parcel Backup: Parcels exist in database.");
  231. foreach (ParcelData parcelData in result)
  232. {
  233. recv.ParcelFromStorage(parcelData);
  234. }
  235. }
  236. else
  237. {
  238. MainLog.Instance.Notice("Parcel Backup: No parcels exist. Creating basic parcel.");
  239. recv.NoParcelDataFromStorage();
  240. }
  241. MainLog.Instance.Notice("Parcel Backup: Parcels Restored");
  242. }
  243. public void ShutDown()
  244. {
  245. db.Commit();
  246. db.Close();
  247. }
  248. }
  249. }