aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Modules/ContentManagementSystem/FileSystemDatabase.cs
diff options
context:
space:
mode:
authorSean Dague2008-09-03 18:04:37 +0000
committerSean Dague2008-09-03 18:04:37 +0000
commit12beaccec79319186ae2a7bf3663510584117a63 (patch)
tree0dadf7a61269ae5215d1c007a2c8b7749e84e434 /OpenSim/Region/Environment/Modules/ContentManagementSystem/FileSystemDatabase.cs
parentUpdate svn properties. (diff)
downloadopensim-SC-12beaccec79319186ae2a7bf3663510584117a63.zip
opensim-SC-12beaccec79319186ae2a7bf3663510584117a63.tar.gz
opensim-SC-12beaccec79319186ae2a7bf3663510584117a63.tar.bz2
opensim-SC-12beaccec79319186ae2a7bf3663510584117a63.tar.xz
Merge branch 'cms' of http://pokgsa.ibm.com/~jbongio/public/opensim
Diffstat (limited to 'OpenSim/Region/Environment/Modules/ContentManagementSystem/FileSystemDatabase.cs')
-rw-r--r--OpenSim/Region/Environment/Modules/ContentManagementSystem/FileSystemDatabase.cs260
1 files changed, 260 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Modules/ContentManagementSystem/FileSystemDatabase.cs b/OpenSim/Region/Environment/Modules/ContentManagementSystem/FileSystemDatabase.cs
new file mode 100644
index 0000000..e1f519b
--- /dev/null
+++ b/OpenSim/Region/Environment/Modules/ContentManagementSystem/FileSystemDatabase.cs
@@ -0,0 +1,260 @@
1// FileSystemDatabase.cs
2// User: bongiojp
3
4using System;
5using System.Collections.Generic;
6using System.IO;
7using System.Reflection;
8using System.Xml;
9using libsecondlife;
10using Nini.Config;
11using OpenSim.Framework;
12using OpenSim.Region.Environment.Interfaces;
13using OpenSim.Region.Environment.Modules.World.Serialiser;
14using OpenSim.Region.Environment.Modules.World.Terrain;
15using OpenSim.Region.Environment.Scenes;
16using log4net;
17using OpenSim.Region.Physics.Manager;
18using Axiom.Math;
19using Slash=System.IO.Path;
20using System.Diagnostics;
21
22namespace OpenSim.Region.Environment.Modules.ContentManagement
23{
24 public class FileSystemDatabase : IContentDatabase
25 {
26 public static float TimeToDownload = 0;
27 public static float TimeToSave = 0;
28
29 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
30
31 private string m_repodir = null;
32 private Dictionary<LLUUID, IRegionSerialiser> m_serialiser = new Dictionary<LLUUID, IRegionSerialiser>();
33 private Dictionary<LLUUID, Scene> m_scenes = new Dictionary<LLUUID, Scene>();
34
35 public FileSystemDatabase()
36 {
37 }
38
39 public void Initialise(Scene scene, string dir)
40 {
41 lock(this)
42 {
43 if (m_repodir == null)
44 m_repodir = dir;
45 }
46 lock(m_scenes)
47 m_scenes.Add(scene.RegionInfo.RegionID, scene);
48 }
49
50
51 // Run once and only once.
52 public void PostInitialise()
53 {
54 SetupSerialiser();
55
56 m_log.Info("[FSDB]: Creating repository in " + m_repodir + ".");
57 CreateDirectory();
58 }
59
60 // called by postinitialise
61 private void SetupSerialiser()
62 {
63 if (m_serialiser.Count == 0)
64 foreach(LLUUID region in m_scenes.Keys)
65 m_serialiser.Add(region,
66 m_scenes[region].RequestModuleInterface<IRegionSerialiser>()
67 );
68 }
69
70 // called by postinitialise
71 private void CreateDirectory()
72 {
73 string scenedir;
74 if (!Directory.Exists(m_repodir))
75 Directory.CreateDirectory(m_repodir);
76
77 foreach (LLUUID region in m_scenes.Keys)
78 {
79 scenedir = m_repodir + Slash.DirectorySeparatorChar + region + Slash.DirectorySeparatorChar;
80 if (!Directory.Exists(scenedir))
81 Directory.CreateDirectory(scenedir);
82 }
83 }
84
85 public int NumOfRegionRev(LLUUID regionid)
86 {
87 string scenedir = m_repodir + Slash.DirectorySeparatorChar + regionid + Slash.DirectorySeparatorChar;
88 m_log.Info("[FSDB]: Reading scene dir: " + scenedir);
89 string[] directories = Directory.GetDirectories(scenedir);
90 return directories.Length;
91 }
92
93 public int GetMostRecentRevision(LLUUID regionid)
94 {
95 return NumOfRegionRev(regionid);
96 }
97
98 public void SaveRegion(LLUUID regionid, string regionName, string logMessage)
99 {
100 m_log.Info("[FSDB]: ...............................");
101 string scenedir = m_repodir + Slash.DirectorySeparatorChar + regionid + Slash.DirectorySeparatorChar;
102
103 m_log.Info("[FSDB]: checking if scene directory exists: " + scenedir);
104 if (!Directory.Exists(scenedir))
105 Directory.CreateDirectory(scenedir);
106
107 int newRevisionNum = GetMostRecentRevision(regionid)+1;
108 string revisiondir = scenedir + newRevisionNum + Slash.DirectorySeparatorChar;
109
110 m_log.Info("[FSDB]: checking if revision directory exists: " + revisiondir);
111 if (!Directory.Exists(revisiondir))
112 Directory.CreateDirectory(revisiondir);
113
114 try {
115 Stopwatch x = new Stopwatch();
116 x.Start();
117 if (m_scenes.ContainsKey(regionid))
118 {
119 m_serialiser[regionid].SerialiseRegion(m_scenes[regionid], revisiondir);
120 }
121 x.Stop();
122 TimeToSave += x.ElapsedMilliseconds;
123 m_log.Info("[FileSystemDatabase] Time spent serialising regions to files on disk for " + regionName + ": " + x.ElapsedMilliseconds);
124 m_log.Info("[FileSystemDatabase] Time spent serialising regions to files on disk so far: " + TimeToSave);
125 }
126 catch (Exception e)
127 {
128 m_log.ErrorFormat("[FSDB]: Serialisation of region failed: " + e);
129 return;
130 }
131
132 try {
133 // Finish by writing log message.
134 FileStream file = new FileStream(revisiondir + "log", FileMode.Create, FileAccess.ReadWrite);
135 StreamWriter sw = new StreamWriter(file);
136 sw.Write(logMessage);
137 sw.Close();
138 }
139 catch (Exception e)
140 {
141 m_log.ErrorFormat("[FSDB]: Failed trying to save log file " + e);
142 return;
143 }
144 }
145
146 public System.Collections.ArrayList GetRegionObjectXMLList(LLUUID regionid, int revision)
147 {
148 System.Collections.ArrayList objectList = new System.Collections.ArrayList();
149 string filename = m_repodir + Slash.DirectorySeparatorChar + regionid + Slash.DirectorySeparatorChar +
150 + revision + Slash.DirectorySeparatorChar + "objects.xml";
151 XmlDocument doc = new XmlDocument();
152 XmlNode rootNode;
153 //int primCount = 0;
154 //SceneObjectGroup obj = null;
155
156 if(File.Exists(filename))
157 {
158 XmlTextReader reader = new XmlTextReader(filename);
159 reader.WhitespaceHandling = WhitespaceHandling.None;
160 doc.Load(reader);
161 reader.Close();
162 rootNode = doc.FirstChild;
163 foreach (XmlNode aPrimNode in rootNode.ChildNodes)
164 {
165 objectList.Add(aPrimNode.OuterXml);
166 }
167 return objectList;
168 }
169 return null;
170 }
171
172 public System.Collections.ArrayList GetRegionObjectXMLList(LLUUID regionid)
173 {
174 int revision = NumOfRegionRev(regionid);
175 m_log.Info("[FSDB]: found revisions:" + revision);
176 System.Collections.ArrayList xmlList = new System.Collections.ArrayList();
177 string filename = m_repodir + Slash.DirectorySeparatorChar + regionid + Slash.DirectorySeparatorChar +
178 + revision + Slash.DirectorySeparatorChar + "objects.xml";
179 XmlDocument doc = new XmlDocument();
180 XmlNode rootNode;
181
182
183 m_log.Info("[FSDB]: Checking if " + filename + " exists.");
184 if(File.Exists(filename))
185 {
186 Stopwatch x = new Stopwatch();
187 x.Start();
188
189 XmlTextReader reader = new XmlTextReader(filename);
190 reader.WhitespaceHandling = WhitespaceHandling.None;
191 doc.Load(reader);
192 reader.Close();
193 rootNode = doc.FirstChild;
194
195 foreach (XmlNode aPrimNode in rootNode.ChildNodes)
196 xmlList.Add(aPrimNode.OuterXml);
197
198 x.Stop();
199 TimeToDownload += x.ElapsedMilliseconds;
200 m_log.Info("[FileSystemDatabase] Time spent retrieving xml files so far: " + TimeToDownload);
201
202 return xmlList;
203 }
204 return null;
205 }
206
207 public string GetRegionObjectHeightMap(LLUUID regionid)
208 {
209 String filename = m_repodir + Slash.DirectorySeparatorChar + regionid +
210 Slash.DirectorySeparatorChar + "heightmap.r32";
211 FileStream fs = new FileStream( filename, FileMode.Open);
212 StreamReader sr = new StreamReader(fs);
213 String result = sr.ReadToEnd();
214 sr.Close();
215 fs.Close();
216 return result;
217 }
218
219 public string GetRegionObjectHeightMap(LLUUID regionid, int revision)
220 {
221 String filename = m_repodir + Slash.DirectorySeparatorChar + regionid +
222 Slash.DirectorySeparatorChar + "heightmap.r32";
223 FileStream fs = new FileStream( filename, FileMode.Open);
224 StreamReader sr = new StreamReader(fs);
225 String result = sr.ReadToEnd();
226 sr.Close();
227 fs.Close();
228 return result;
229 }
230
231 public System.Collections.Generic.SortedDictionary<string, string> ListOfRegionRevisions(LLUUID regionid)
232 {
233 SortedDictionary<string, string> revisionDict = new SortedDictionary<string,string>();
234
235 string scenedir = m_repodir + Slash.DirectorySeparatorChar + regionid + Slash.DirectorySeparatorChar;
236 string[] directories = Directory.GetDirectories(scenedir);
237
238 FileStream fs = null;
239 StreamReader sr = null;
240 String logMessage = "";
241 String logLocation = "";
242 foreach(string revisionDir in directories)
243 {
244 try {
245 logLocation = revisionDir + Slash.DirectorySeparatorChar + "log";
246 fs = new FileStream( logLocation, FileMode.Open);
247 sr = new StreamReader(fs);
248 logMessage = sr.ReadToEnd();
249 sr.Close();
250 fs.Close();
251 revisionDict.Add(revisionDir, logMessage);
252 }
253 catch (Exception)
254 {}
255 }
256
257 return revisionDict;
258 }
259 }
260}