FileSystemDatabase.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.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. #region Header
  28. // FileSystemDatabase.cs
  29. // User: bongiojp
  30. #endregion Header
  31. using System;
  32. using System.Collections.Generic;
  33. using System.Diagnostics;
  34. using System.IO;
  35. using Slash = System.IO.Path;
  36. using System.Reflection;
  37. using System.Xml;
  38. using OpenMetaverse;
  39. using Nini.Config;
  40. using OpenSim.Framework;
  41. using OpenSim.Region.Environment.Interfaces;
  42. using OpenSim.Region.Environment.Modules.World.Serialiser;
  43. using OpenSim.Region.Environment.Modules.World.Terrain;
  44. using OpenSim.Region.Environment.Scenes;
  45. using OpenSim.Region.Physics.Manager;
  46. using log4net;
  47. namespace OpenSim.Region.Environment.Modules.ContentManagement
  48. {
  49. public class FileSystemDatabase : IContentDatabase
  50. {
  51. #region Static Fields
  52. public static float TimeToDownload = 0;
  53. public static float TimeToSave = 0;
  54. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  55. #endregion Static Fields
  56. #region Fields
  57. private string m_repodir = null;
  58. private Dictionary<UUID, Scene> m_scenes = new Dictionary<UUID, Scene>();
  59. private Dictionary<UUID, IRegionSerialiserModule> m_serialiser = new Dictionary<UUID, IRegionSerialiserModule>();
  60. #endregion Fields
  61. #region Constructors
  62. public FileSystemDatabase()
  63. {
  64. }
  65. #endregion Constructors
  66. #region Private Methods
  67. // called by postinitialise
  68. private void CreateDirectory()
  69. {
  70. string scenedir;
  71. if (!Directory.Exists(m_repodir))
  72. Directory.CreateDirectory(m_repodir);
  73. foreach (UUID region in m_scenes.Keys)
  74. {
  75. scenedir = m_repodir + Slash.DirectorySeparatorChar + region + Slash.DirectorySeparatorChar;
  76. if (!Directory.Exists(scenedir))
  77. Directory.CreateDirectory(scenedir);
  78. }
  79. }
  80. // called by postinitialise
  81. private void SetupSerialiser()
  82. {
  83. if (m_serialiser.Count == 0)
  84. {
  85. foreach (UUID region in m_scenes.Keys)
  86. {
  87. m_serialiser.Add(region, m_scenes[region].RequestModuleInterface<IRegionSerialiserModule>());
  88. }
  89. }
  90. }
  91. #endregion Private Methods
  92. #region Public Methods
  93. public int GetMostRecentRevision(UUID regionid)
  94. {
  95. return NumOfRegionRev(regionid);
  96. }
  97. public string GetRegionObjectHeightMap(UUID regionid)
  98. {
  99. String filename = m_repodir + Slash.DirectorySeparatorChar + regionid +
  100. Slash.DirectorySeparatorChar + "heightmap.r32";
  101. FileStream fs = new FileStream( filename, FileMode.Open);
  102. StreamReader sr = new StreamReader(fs);
  103. String result = sr.ReadToEnd();
  104. sr.Close();
  105. fs.Close();
  106. return result;
  107. }
  108. public string GetRegionObjectHeightMap(UUID regionid, int revision)
  109. {
  110. String filename = m_repodir + Slash.DirectorySeparatorChar + regionid +
  111. Slash.DirectorySeparatorChar + "heightmap.r32";
  112. FileStream fs = new FileStream( filename, FileMode.Open);
  113. StreamReader sr = new StreamReader(fs);
  114. String result = sr.ReadToEnd();
  115. sr.Close();
  116. fs.Close();
  117. return result;
  118. }
  119. public System.Collections.ArrayList GetRegionObjectXMLList(UUID regionid, int revision)
  120. {
  121. System.Collections.ArrayList objectList = new System.Collections.ArrayList();
  122. string filename = m_repodir + Slash.DirectorySeparatorChar + regionid + Slash.DirectorySeparatorChar +
  123. + revision + Slash.DirectorySeparatorChar + "objects.xml";
  124. XmlDocument doc = new XmlDocument();
  125. XmlNode rootNode;
  126. //int primCount = 0;
  127. //SceneObjectGroup obj = null;
  128. if (File.Exists(filename))
  129. {
  130. XmlTextReader reader = new XmlTextReader(filename);
  131. reader.WhitespaceHandling = WhitespaceHandling.None;
  132. doc.Load(reader);
  133. reader.Close();
  134. rootNode = doc.FirstChild;
  135. foreach (XmlNode aPrimNode in rootNode.ChildNodes)
  136. {
  137. objectList.Add(aPrimNode.OuterXml);
  138. }
  139. return objectList;
  140. }
  141. return null;
  142. }
  143. public System.Collections.ArrayList GetRegionObjectXMLList(UUID regionid)
  144. {
  145. int revision = NumOfRegionRev(regionid);
  146. m_log.Info("[FSDB]: found revisions:" + revision);
  147. System.Collections.ArrayList xmlList = new System.Collections.ArrayList();
  148. string filename = m_repodir + Slash.DirectorySeparatorChar + regionid + Slash.DirectorySeparatorChar +
  149. + revision + Slash.DirectorySeparatorChar + "objects.xml";
  150. XmlDocument doc = new XmlDocument();
  151. XmlNode rootNode;
  152. m_log.Info("[FSDB]: Checking if " + filename + " exists.");
  153. if (File.Exists(filename))
  154. {
  155. Stopwatch x = new Stopwatch();
  156. x.Start();
  157. XmlTextReader reader = new XmlTextReader(filename);
  158. reader.WhitespaceHandling = WhitespaceHandling.None;
  159. doc.Load(reader);
  160. reader.Close();
  161. rootNode = doc.FirstChild;
  162. foreach (XmlNode aPrimNode in rootNode.ChildNodes)
  163. {
  164. xmlList.Add(aPrimNode.OuterXml);
  165. }
  166. x.Stop();
  167. TimeToDownload += x.ElapsedMilliseconds;
  168. m_log.Info("[FileSystemDatabase] Time spent retrieving xml files so far: " + TimeToDownload);
  169. return xmlList;
  170. }
  171. return null;
  172. }
  173. public void Initialise(Scene scene, string dir)
  174. {
  175. lock (this)
  176. {
  177. if (m_repodir == null)
  178. m_repodir = dir;
  179. }
  180. lock (m_scenes)
  181. m_scenes.Add(scene.RegionInfo.RegionID, scene);
  182. }
  183. public System.Collections.Generic.SortedDictionary<string, string> ListOfRegionRevisions(UUID regionid)
  184. {
  185. SortedDictionary<string, string> revisionDict = new SortedDictionary<string,string>();
  186. string scenedir = m_repodir + Slash.DirectorySeparatorChar + regionid + Slash.DirectorySeparatorChar;
  187. string[] directories = Directory.GetDirectories(scenedir);
  188. FileStream fs = null;
  189. StreamReader sr = null;
  190. String logMessage = "";
  191. String logLocation = "";
  192. foreach (string revisionDir in directories)
  193. {
  194. try
  195. {
  196. logLocation = revisionDir + Slash.DirectorySeparatorChar + "log";
  197. fs = new FileStream( logLocation, FileMode.Open);
  198. sr = new StreamReader(fs);
  199. logMessage = sr.ReadToEnd();
  200. sr.Close();
  201. fs.Close();
  202. revisionDict.Add(revisionDir, logMessage);
  203. }
  204. catch (Exception)
  205. {
  206. }
  207. }
  208. return revisionDict;
  209. }
  210. public int NumOfRegionRev(UUID regionid)
  211. {
  212. string scenedir = m_repodir + Slash.DirectorySeparatorChar + regionid + Slash.DirectorySeparatorChar;
  213. m_log.Info("[FSDB]: Reading scene dir: " + scenedir);
  214. string[] directories = Directory.GetDirectories(scenedir);
  215. return directories.Length;
  216. }
  217. // Run once and only once.
  218. public void PostInitialise()
  219. {
  220. SetupSerialiser();
  221. m_log.Info("[FSDB]: Creating repository in " + m_repodir + ".");
  222. CreateDirectory();
  223. }
  224. public void SaveRegion(UUID regionid, string regionName, string logMessage)
  225. {
  226. m_log.Info("[FSDB]: ...............................");
  227. string scenedir = m_repodir + Slash.DirectorySeparatorChar + regionid + Slash.DirectorySeparatorChar;
  228. m_log.Info("[FSDB]: checking if scene directory exists: " + scenedir);
  229. if (!Directory.Exists(scenedir))
  230. Directory.CreateDirectory(scenedir);
  231. int newRevisionNum = GetMostRecentRevision(regionid)+1;
  232. string revisiondir = scenedir + newRevisionNum + Slash.DirectorySeparatorChar;
  233. m_log.Info("[FSDB]: checking if revision directory exists: " + revisiondir);
  234. if (!Directory.Exists(revisiondir))
  235. Directory.CreateDirectory(revisiondir);
  236. try {
  237. Stopwatch x = new Stopwatch();
  238. x.Start();
  239. if (m_scenes.ContainsKey(regionid))
  240. {
  241. m_serialiser[regionid].SerialiseRegion(m_scenes[regionid], revisiondir);
  242. }
  243. x.Stop();
  244. TimeToSave += x.ElapsedMilliseconds;
  245. m_log.Info("[FileSystemDatabase] Time spent serialising regions to files on disk for " + regionName + ": " + x.ElapsedMilliseconds);
  246. m_log.Info("[FileSystemDatabase] Time spent serialising regions to files on disk so far: " + TimeToSave);
  247. }
  248. catch (Exception e)
  249. {
  250. m_log.ErrorFormat("[FSDB]: Serialisation of region failed: " + e);
  251. return;
  252. }
  253. try {
  254. // Finish by writing log message.
  255. FileStream file = new FileStream(revisiondir + "log", FileMode.Create, FileAccess.ReadWrite);
  256. StreamWriter sw = new StreamWriter(file);
  257. sw.Write(logMessage);
  258. sw.Close();
  259. }
  260. catch (Exception e)
  261. {
  262. m_log.ErrorFormat("[FSDB]: Failed trying to save log file " + e);
  263. return;
  264. }
  265. }
  266. #endregion Public Methods
  267. }
  268. }