diff options
Diffstat (limited to 'OpenSim/Framework/DAMap.cs')
-rw-r--r-- | OpenSim/Framework/DAMap.cs | 300 |
1 files changed, 178 insertions, 122 deletions
diff --git a/OpenSim/Framework/DAMap.cs b/OpenSim/Framework/DAMap.cs index df4a6bc..a57393b 100644 --- a/OpenSim/Framework/DAMap.cs +++ b/OpenSim/Framework/DAMap.cs | |||
@@ -29,10 +29,12 @@ using System; | |||
29 | using System.Collections; | 29 | using System.Collections; |
30 | using System.Collections.Generic; | 30 | using System.Collections.Generic; |
31 | using System.IO; | 31 | using System.IO; |
32 | using System.Reflection; | ||
32 | using System.Text; | 33 | using System.Text; |
33 | using System.Xml; | 34 | using System.Xml; |
34 | using System.Xml.Schema; | 35 | using System.Xml.Schema; |
35 | using System.Xml.Serialization; | 36 | using System.Xml.Serialization; |
37 | using log4net; | ||
36 | using OpenMetaverse; | 38 | using OpenMetaverse; |
37 | using OpenMetaverse.StructuredData; | 39 | using OpenMetaverse.StructuredData; |
38 | 40 | ||
@@ -48,13 +50,20 @@ namespace OpenSim.Framework | |||
48 | /// within their data store. However, avoid storing large amounts of data because that | 50 | /// within their data store. However, avoid storing large amounts of data because that |
49 | /// would slow down database access. | 51 | /// would slow down database access. |
50 | /// </remarks> | 52 | /// </remarks> |
51 | public class DAMap : IDictionary<string, OSDMap>, IXmlSerializable | 53 | public class DAMap : IXmlSerializable |
52 | { | 54 | { |
53 | private static readonly int MIN_STORE_NAME_LENGTH = 4; | 55 | // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
54 | 56 | ||
55 | protected OSDMap m_map; | 57 | private static readonly int MIN_NAMESPACE_LENGTH = 4; |
56 | 58 | ||
57 | public DAMap() { m_map = new OSDMap(); } | 59 | private OSDMap m_map = new OSDMap(); |
60 | |||
61 | // WARNING: this is temporary for experimentation only, it will be removed!!!! | ||
62 | public OSDMap TopLevelMap | ||
63 | { | ||
64 | get { return m_map; } | ||
65 | set { m_map = value; } | ||
66 | } | ||
58 | 67 | ||
59 | public XmlSchema GetSchema() { return null; } | 68 | public XmlSchema GetSchema() { return null; } |
60 | 69 | ||
@@ -64,39 +73,34 @@ namespace OpenSim.Framework | |||
64 | map.ReadXml(rawXml); | 73 | map.ReadXml(rawXml); |
65 | return map; | 74 | return map; |
66 | } | 75 | } |
67 | 76 | ||
77 | public void ReadXml(XmlReader reader) | ||
78 | { | ||
79 | ReadXml(reader.ReadInnerXml()); | ||
80 | } | ||
81 | |||
68 | public void ReadXml(string rawXml) | 82 | public void ReadXml(string rawXml) |
69 | { | 83 | { |
70 | // System.Console.WriteLine("Trying to deserialize [{0}]", rawXml); | 84 | // System.Console.WriteLine("Trying to deserialize [{0}]", rawXml); |
71 | 85 | ||
72 | lock (this) | 86 | lock (this) |
87 | { | ||
73 | m_map = (OSDMap)OSDParser.DeserializeLLSDXml(rawXml); | 88 | m_map = (OSDMap)OSDParser.DeserializeLLSDXml(rawXml); |
89 | SanitiseMap(this); | ||
90 | } | ||
74 | } | 91 | } |
75 | 92 | ||
76 | // WARNING: this is temporary for experimentation only, it will be removed!!!! | 93 | public void WriteXml(XmlWriter writer) |
77 | public OSDMap TopLevelMap | ||
78 | { | 94 | { |
79 | get { return m_map; } | 95 | writer.WriteRaw(ToXml()); |
80 | set { m_map = value; } | ||
81 | } | 96 | } |
82 | |||
83 | 97 | ||
84 | public void ReadXml(XmlReader reader) | ||
85 | { | ||
86 | ReadXml(reader.ReadInnerXml()); | ||
87 | } | ||
88 | |||
89 | public string ToXml() | 98 | public string ToXml() |
90 | { | 99 | { |
91 | lock (this) | 100 | lock (this) |
92 | return OSDParser.SerializeLLSDXmlString(m_map); | 101 | return OSDParser.SerializeLLSDXmlString(m_map); |
93 | } | 102 | } |
94 | 103 | ||
95 | public void WriteXml(XmlWriter writer) | ||
96 | { | ||
97 | writer.WriteRaw(ToXml()); | ||
98 | } | ||
99 | |||
100 | public void CopyFrom(DAMap other) | 104 | public void CopyFrom(DAMap other) |
101 | { | 105 | { |
102 | // Deep copy | 106 | // Deep copy |
@@ -104,7 +108,7 @@ namespace OpenSim.Framework | |||
104 | string data = null; | 108 | string data = null; |
105 | lock (other) | 109 | lock (other) |
106 | { | 110 | { |
107 | if (other.Count > 0) | 111 | if (other.CountNamespaces > 0) |
108 | { | 112 | { |
109 | data = OSDParser.SerializeLLSDXmlString(other.m_map); | 113 | data = OSDParser.SerializeLLSDXmlString(other.m_map); |
110 | } | 114 | } |
@@ -120,59 +124,133 @@ namespace OpenSim.Framework | |||
120 | } | 124 | } |
121 | 125 | ||
122 | /// <summary> | 126 | /// <summary> |
123 | /// Returns the number of data stores. | 127 | /// Sanitise the map to remove any namespaces or stores that are not OSDMap. |
124 | /// </summary> | 128 | /// </summary> |
125 | public int Count { get { lock (this) { return m_map.Count; } } } | 129 | /// <param name='map'> |
126 | 130 | /// </param> | |
127 | public bool IsReadOnly { get { return false; } } | 131 | public static void SanitiseMap(DAMap daMap) |
128 | 132 | { | |
133 | List<string> keysToRemove = null; | ||
134 | |||
135 | // Hard-coded special case that needs to be removed in the future. Normally, modules themselves should | ||
136 | // handle reading data from old locations | ||
137 | bool osMaterialsMigrationRequired = false; | ||
138 | |||
139 | OSDMap namespacesMap = daMap.m_map; | ||
140 | |||
141 | foreach (string key in namespacesMap.Keys) | ||
142 | { | ||
143 | // Console.WriteLine("Processing ns {0}", key); | ||
144 | if (!(namespacesMap[key] is OSDMap)) | ||
145 | { | ||
146 | if (keysToRemove == null) | ||
147 | keysToRemove = new List<string>(); | ||
148 | |||
149 | keysToRemove.Add(key); | ||
150 | } | ||
151 | else if (key == "OS:Materials") | ||
152 | { | ||
153 | osMaterialsMigrationRequired = true; | ||
154 | } | ||
155 | } | ||
156 | |||
157 | if (keysToRemove != null) | ||
158 | { | ||
159 | foreach (string key in keysToRemove) | ||
160 | { | ||
161 | // Console.WriteLine ("Removing bad ns {0}", key); | ||
162 | namespacesMap.Remove(key); | ||
163 | } | ||
164 | } | ||
165 | |||
166 | // Hard-coded special case that needs to be removed in the future. Normally, modules themselves should | ||
167 | // handle reading data from old locations | ||
168 | if (osMaterialsMigrationRequired) | ||
169 | daMap.SetStore("OpenSim", "Materials", (OSDMap)namespacesMap["OS:Materials"]); | ||
170 | |||
171 | foreach (OSD nsOsd in namespacesMap.Values) | ||
172 | { | ||
173 | OSDMap nsOsdMap = (OSDMap)nsOsd; | ||
174 | keysToRemove = null; | ||
175 | |||
176 | foreach (string key in nsOsdMap.Keys) | ||
177 | { | ||
178 | if (!(nsOsdMap[key] is OSDMap)) | ||
179 | { | ||
180 | if (keysToRemove == null) | ||
181 | keysToRemove = new List<string>(); | ||
182 | |||
183 | keysToRemove.Add(key); | ||
184 | } | ||
185 | } | ||
186 | |||
187 | if (keysToRemove != null) | ||
188 | foreach (string key in keysToRemove) | ||
189 | nsOsdMap.Remove(key); | ||
190 | } | ||
191 | } | ||
192 | |||
129 | /// <summary> | 193 | /// <summary> |
130 | /// Returns the names of the data stores. | 194 | /// Get the number of namespaces |
131 | /// </summary> | 195 | /// </summary> |
132 | public ICollection<string> Keys { get { lock (this) { return m_map.Keys; } } } | 196 | public int CountNamespaces { get { lock (this) { return m_map.Count; } } } |
133 | 197 | ||
134 | /// <summary> | 198 | /// <summary> |
135 | /// Returns all the data stores. | 199 | /// Get the number of stores. |
136 | /// </summary> | 200 | /// </summary> |
137 | public ICollection<OSDMap> Values | 201 | public int CountStores |
138 | { | 202 | { |
139 | get | 203 | get |
140 | { | 204 | { |
205 | int count = 0; | ||
206 | |||
141 | lock (this) | 207 | lock (this) |
142 | { | 208 | { |
143 | List<OSDMap> stores = new List<OSDMap>(m_map.Count); | 209 | foreach (OSD osdNamespace in m_map) |
144 | foreach (OSD llsd in m_map.Values) | 210 | { |
145 | stores.Add((OSDMap)llsd); | 211 | count += ((OSDMap)osdNamespace).Count; |
146 | return stores; | 212 | } |
147 | } | 213 | } |
214 | |||
215 | return count; | ||
148 | } | 216 | } |
149 | } | 217 | } |
150 | 218 | ||
151 | /// <summary> | 219 | public OSDMap GetStore(string ns, string storeName) |
152 | /// Gets or sets one data store. | 220 | { |
153 | /// </summary> | 221 | OSD namespaceOsd; |
154 | /// <param name="key">Store name</param> | 222 | |
155 | /// <returns></returns> | 223 | lock (this) |
156 | public OSDMap this[string key] | 224 | { |
157 | { | 225 | if (m_map.TryGetValue(ns, out namespaceOsd)) |
158 | get | ||
159 | { | ||
160 | OSD llsd; | ||
161 | |||
162 | lock (this) | ||
163 | { | 226 | { |
164 | if (m_map.TryGetValue(key, out llsd)) | 227 | OSD store; |
165 | return (OSDMap)llsd; | 228 | |
166 | else | 229 | if (((OSDMap)namespaceOsd).TryGetValue(storeName, out store)) |
167 | return null; | 230 | return (OSDMap)store; |
168 | } | 231 | } |
169 | } | 232 | } |
170 | 233 | ||
171 | set | 234 | return null; |
235 | } | ||
236 | |||
237 | public void SetStore(string ns, string storeName, OSDMap store) | ||
238 | { | ||
239 | ValidateNamespace(ns); | ||
240 | OSDMap nsMap; | ||
241 | |||
242 | lock (this) | ||
172 | { | 243 | { |
173 | ValidateKey(key); | 244 | if (!m_map.ContainsKey(ns)) |
174 | lock (this) | 245 | { |
175 | m_map[key] = value; | 246 | nsMap = new OSDMap(); |
247 | m_map[ns] = nsMap; | ||
248 | } | ||
249 | |||
250 | nsMap = (OSDMap)m_map[ns]; | ||
251 | |||
252 | // m_log.DebugFormat("[DA MAP]: Setting store to {0}:{1}", ns, storeName); | ||
253 | nsMap[storeName] = store; | ||
176 | } | 254 | } |
177 | } | 255 | } |
178 | 256 | ||
@@ -180,54 +258,46 @@ namespace OpenSim.Framework | |||
180 | /// Validate the key used for storing separate data stores. | 258 | /// Validate the key used for storing separate data stores. |
181 | /// </summary> | 259 | /// </summary> |
182 | /// <param name='key'></param> | 260 | /// <param name='key'></param> |
183 | public static void ValidateKey(string key) | 261 | public static void ValidateNamespace(string ns) |
184 | { | 262 | { |
185 | if (key.Length < MIN_STORE_NAME_LENGTH) | 263 | if (ns.Length < MIN_NAMESPACE_LENGTH) |
186 | throw new Exception("Minimum store name length is " + MIN_STORE_NAME_LENGTH); | 264 | throw new Exception("Minimum namespace length is " + MIN_NAMESPACE_LENGTH); |
187 | } | 265 | } |
188 | 266 | ||
189 | public bool ContainsKey(string key) | 267 | public bool ContainsStore(string ns, string storeName) |
190 | { | 268 | { |
191 | lock (this) | 269 | OSD namespaceOsd; |
192 | return m_map.ContainsKey(key); | ||
193 | } | ||
194 | |||
195 | public void Add(string key, OSDMap store) | ||
196 | { | ||
197 | ValidateKey(key); | ||
198 | lock (this) | ||
199 | m_map.Add(key, store); | ||
200 | } | ||
201 | 270 | ||
202 | public void Add(KeyValuePair<string, OSDMap> kvp) | ||
203 | { | ||
204 | ValidateKey(kvp.Key); | ||
205 | lock (this) | 271 | lock (this) |
206 | m_map.Add(kvp.Key, kvp.Value); | 272 | { |
207 | } | 273 | if (m_map.TryGetValue(ns, out namespaceOsd)) |
274 | { | ||
275 | return ((OSDMap)namespaceOsd).ContainsKey(storeName); | ||
276 | } | ||
277 | } | ||
208 | 278 | ||
209 | public bool Remove(string key) | 279 | return false; |
210 | { | 280 | } |
211 | lock (this) | ||
212 | return m_map.Remove(key); | ||
213 | } | ||
214 | 281 | ||
215 | public bool TryGetValue(string key, out OSDMap store) | 282 | public bool TryGetStore(string ns, string storeName, out OSDMap store) |
216 | { | 283 | { |
284 | OSD namespaceOsd; | ||
285 | |||
217 | lock (this) | 286 | lock (this) |
218 | { | 287 | { |
219 | OSD llsd; | 288 | if (m_map.TryGetValue(ns, out namespaceOsd)) |
220 | if (m_map.TryGetValue(key, out llsd)) | ||
221 | { | ||
222 | store = (OSDMap)llsd; | ||
223 | return true; | ||
224 | } | ||
225 | else | ||
226 | { | 289 | { |
227 | store = null; | 290 | OSD storeOsd; |
228 | return false; | 291 | |
292 | bool result = ((OSDMap)namespaceOsd).TryGetValue(storeName, out storeOsd); | ||
293 | store = (OSDMap)storeOsd; | ||
294 | |||
295 | return result; | ||
229 | } | 296 | } |
230 | } | 297 | } |
298 | |||
299 | store = null; | ||
300 | return false; | ||
231 | } | 301 | } |
232 | 302 | ||
233 | public void Clear() | 303 | public void Clear() |
@@ -235,39 +305,25 @@ namespace OpenSim.Framework | |||
235 | lock (this) | 305 | lock (this) |
236 | m_map.Clear(); | 306 | m_map.Clear(); |
237 | } | 307 | } |
238 | |||
239 | public bool Contains(KeyValuePair<string, OSDMap> kvp) | ||
240 | { | ||
241 | lock (this) | ||
242 | return m_map.ContainsKey(kvp.Key); | ||
243 | } | ||
244 | 308 | ||
245 | public void CopyTo(KeyValuePair<string, OSDMap>[] array, int index) | 309 | public bool RemoveStore(string ns, string storeName) |
246 | { | 310 | { |
247 | throw new NotImplementedException(); | 311 | OSD namespaceOsd; |
248 | } | ||
249 | 312 | ||
250 | public bool Remove(KeyValuePair<string, OSDMap> kvp) | ||
251 | { | ||
252 | lock (this) | ||
253 | return m_map.Remove(kvp.Key); | ||
254 | } | ||
255 | |||
256 | public System.Collections.IDictionaryEnumerator GetEnumerator() | ||
257 | { | ||
258 | lock (this) | 313 | lock (this) |
259 | return m_map.GetEnumerator(); | 314 | { |
260 | } | 315 | if (m_map.TryGetValue(ns, out namespaceOsd)) |
316 | { | ||
317 | OSDMap namespaceOsdMap = (OSDMap)namespaceOsd; | ||
318 | namespaceOsdMap.Remove(storeName); | ||
261 | 319 | ||
262 | IEnumerator<KeyValuePair<string, OSDMap>> IEnumerable<KeyValuePair<string, OSDMap>>.GetEnumerator() | 320 | // Don't keep empty namespaces around |
263 | { | 321 | if (namespaceOsdMap.Count <= 0) |
264 | return null; | 322 | m_map.Remove(ns); |
265 | } | 323 | } |
324 | } | ||
266 | 325 | ||
267 | IEnumerator IEnumerable.GetEnumerator() | 326 | return false; |
268 | { | 327 | } |
269 | lock (this) | ||
270 | return m_map.GetEnumerator(); | ||
271 | } | ||
272 | } | 328 | } |
273 | } \ No newline at end of file | 329 | } \ No newline at end of file |