aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/DAMap.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/DAMap.cs')
-rw-r--r--OpenSim/Framework/DAMap.cs295
1 files changed, 175 insertions, 120 deletions
diff --git a/OpenSim/Framework/DAMap.cs b/OpenSim/Framework/DAMap.cs
index df4a6bc..4995a92 100644
--- a/OpenSim/Framework/DAMap.cs
+++ b/OpenSim/Framework/DAMap.cs
@@ -29,10 +29,12 @@ using System;
29using System.Collections; 29using System.Collections;
30using System.Collections.Generic; 30using System.Collections.Generic;
31using System.IO; 31using System.IO;
32using System.Reflection;
32using System.Text; 33using System.Text;
33using System.Xml; 34using System.Xml;
34using System.Xml.Schema; 35using System.Xml.Schema;
35using System.Xml.Serialization; 36using System.Xml.Serialization;
37using log4net;
36using OpenMetaverse; 38using OpenMetaverse;
37using OpenMetaverse.StructuredData; 39using 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,132 @@ 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 OSDMap namespacesMap = daMap.m_map;
136
137 foreach (string key in namespacesMap.Keys)
138 {
139// Console.WriteLine("Processing ns {0}", key);
140 if (!(namespacesMap[key] is OSDMap))
141 {
142 if (keysToRemove == null)
143 keysToRemove = new List<string>();
144
145 keysToRemove.Add(key);
146 }
147 }
148
149 if (keysToRemove != null)
150 {
151 foreach (string key in keysToRemove)
152 {
153// Console.WriteLine ("Removing bad ns {0}", key);
154 namespacesMap.Remove(key);
155 }
156 }
157
158 foreach (OSD nsOsd in namespacesMap.Values)
159 {
160 OSDMap nsOsdMap = (OSDMap)nsOsd;
161 keysToRemove = null;
162
163 foreach (string key in nsOsdMap.Keys)
164 {
165 if (!(nsOsdMap[key] is OSDMap))
166 {
167 if (keysToRemove == null)
168 keysToRemove = new List<string>();
169
170 keysToRemove.Add(key);
171 }
172 }
173
174 if (keysToRemove != null)
175 foreach (string key in keysToRemove)
176 nsOsdMap.Remove(key);
177 }
178 }
179
129 /// <summary> 180 /// <summary>
130 /// Returns the names of the data stores. 181 /// Get the number of namespaces
131 /// </summary> 182 /// </summary>
132 public ICollection<string> Keys { get { lock (this) { return m_map.Keys; } } } 183 public int CountNamespaces { get { lock (this) { return m_map.Count; } } }
133 184
134 /// <summary> 185 /// <summary>
135 /// Returns all the data stores. 186 /// Get the number of stores.
136 /// </summary> 187 /// </summary>
137 public ICollection<OSDMap> Values 188 public int CountStores
138 { 189 {
139 get 190 get
140 { 191 {
192 int count = 0;
193
141 lock (this) 194 lock (this)
142 { 195 {
143 List<OSDMap> stores = new List<OSDMap>(m_map.Count); 196 foreach (OSD osdNamespace in m_map)
144 foreach (OSD llsd in m_map.Values) 197 {
145 stores.Add((OSDMap)llsd); 198 count += ((OSDMap)osdNamespace).Count;
146 return stores; 199 }
147 } 200 }
201
202 return count;
148 } 203 }
149 } 204 }
150 205
151 /// <summary> 206 /// <summary>
152 /// Gets or sets one data store. 207 /// Retrieve a Dynamic Attribute store
153 /// </summary> 208 /// </summary>
154 /// <param name="key">Store name</param> 209 /// <param name="ns">namespace for the store - use "OpenSim" for in-core modules</param>
155 /// <returns></returns> 210 /// <param name="storeName">name of the store within the namespace</param>
156 public OSDMap this[string key] 211 /// <returns>an OSDMap representing the stored data, or null if not found</returns>
157 { 212 public OSDMap GetStore(string ns, string storeName)
158 get 213 {
159 { 214 OSD namespaceOsd;
160 OSD llsd; 215
161 216 lock (this)
162 lock (this) 217 {
218 if (m_map.TryGetValue(ns, out namespaceOsd))
163 { 219 {
164 if (m_map.TryGetValue(key, out llsd)) 220 OSD store;
165 return (OSDMap)llsd; 221
166 else 222 if (((OSDMap)namespaceOsd).TryGetValue(storeName, out store))
167 return null; 223 return (OSDMap)store;
168 } 224 }
169 } 225 }
170 226
171 set 227 return null;
228 }
229
230 /// <summary>
231 /// Saves a Dynamic attribute store
232 /// </summary>
233 /// <param name="ns">namespace for the store - use "OpenSim" for in-core modules</param>
234 /// <param name="storeName">name of the store within the namespace</param>
235 /// <param name="store">an OSDMap representing the data to store</param>
236 public void SetStore(string ns, string storeName, OSDMap store)
237 {
238 ValidateNamespace(ns);
239 OSDMap nsMap;
240
241 lock (this)
172 { 242 {
173 ValidateKey(key); 243 if (!m_map.ContainsKey(ns))
174 lock (this) 244 {
175 m_map[key] = value; 245 nsMap = new OSDMap();
246 m_map[ns] = nsMap;
247 }
248
249 nsMap = (OSDMap)m_map[ns];
250
251// m_log.DebugFormat("[DA MAP]: Setting store to {0}:{1}", ns, storeName);
252 nsMap[storeName] = store;
176 } 253 }
177 } 254 }
178 255
@@ -180,54 +257,46 @@ namespace OpenSim.Framework
180 /// Validate the key used for storing separate data stores. 257 /// Validate the key used for storing separate data stores.
181 /// </summary> 258 /// </summary>
182 /// <param name='key'></param> 259 /// <param name='key'></param>
183 public static void ValidateKey(string key) 260 public static void ValidateNamespace(string ns)
184 { 261 {
185 if (key.Length < MIN_STORE_NAME_LENGTH) 262 if (ns.Length < MIN_NAMESPACE_LENGTH)
186 throw new Exception("Minimum store name length is " + MIN_STORE_NAME_LENGTH); 263 throw new Exception("Minimum namespace length is " + MIN_NAMESPACE_LENGTH);
187 } 264 }
188 265
189 public bool ContainsKey(string key) 266 public bool ContainsStore(string ns, string storeName)
190 { 267 {
191 lock (this) 268 OSD namespaceOsd;
192 return m_map.ContainsKey(key);
193 }
194 269
195 public void Add(string key, OSDMap store)
196 {
197 ValidateKey(key);
198 lock (this)
199 m_map.Add(key, store);
200 }
201
202 public void Add(KeyValuePair<string, OSDMap> kvp)
203 {
204 ValidateKey(kvp.Key);
205 lock (this) 270 lock (this)
206 m_map.Add(kvp.Key, kvp.Value); 271 {
207 } 272 if (m_map.TryGetValue(ns, out namespaceOsd))
273 {
274 return ((OSDMap)namespaceOsd).ContainsKey(storeName);
275 }
276 }
208 277
209 public bool Remove(string key) 278 return false;
210 { 279 }
211 lock (this)
212 return m_map.Remove(key);
213 }
214 280
215 public bool TryGetValue(string key, out OSDMap store) 281 public bool TryGetStore(string ns, string storeName, out OSDMap store)
216 { 282 {
283 OSD namespaceOsd;
284
217 lock (this) 285 lock (this)
218 { 286 {
219 OSD llsd; 287 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 { 288 {
227 store = null; 289 OSD storeOsd;
228 return false; 290
291 bool result = ((OSDMap)namespaceOsd).TryGetValue(storeName, out storeOsd);
292 store = (OSDMap)storeOsd;
293
294 return result;
229 } 295 }
230 } 296 }
297
298 store = null;
299 return false;
231 } 300 }
232 301
233 public void Clear() 302 public void Clear()
@@ -235,39 +304,25 @@ namespace OpenSim.Framework
235 lock (this) 304 lock (this)
236 m_map.Clear(); 305 m_map.Clear();
237 } 306 }
238
239 public bool Contains(KeyValuePair<string, OSDMap> kvp)
240 {
241 lock (this)
242 return m_map.ContainsKey(kvp.Key);
243 }
244
245 public void CopyTo(KeyValuePair<string, OSDMap>[] array, int index)
246 {
247 throw new NotImplementedException();
248 }
249 307
250 public bool Remove(KeyValuePair<string, OSDMap> kvp) 308 public bool RemoveStore(string ns, string storeName)
251 { 309 {
252 lock (this) 310 OSD namespaceOsd;
253 return m_map.Remove(kvp.Key);
254 }
255 311
256 public System.Collections.IDictionaryEnumerator GetEnumerator()
257 {
258 lock (this) 312 lock (this)
259 return m_map.GetEnumerator(); 313 {
260 } 314 if (m_map.TryGetValue(ns, out namespaceOsd))
315 {
316 OSDMap namespaceOsdMap = (OSDMap)namespaceOsd;
317 namespaceOsdMap.Remove(storeName);
261 318
262 IEnumerator<KeyValuePair<string, OSDMap>> IEnumerable<KeyValuePair<string, OSDMap>>.GetEnumerator() 319 // Don't keep empty namespaces around
263 { 320 if (namespaceOsdMap.Count <= 0)
264 return null; 321 m_map.Remove(ns);
265 } 322 }
323 }
266 324
267 IEnumerator IEnumerable.GetEnumerator() 325 return false;
268 { 326 }
269 lock (this)
270 return m_map.GetEnumerator();
271 }
272 } 327 }
273} \ No newline at end of file 328} \ No newline at end of file