From 8b4441d940a55da90645580477ece33d15849078 Mon Sep 17 00:00:00 2001 From: Oren Hurvitz Date: Tue, 22 Jan 2013 08:41:32 +0200 Subject: [PATCH] Changed DAMap to be the container of "data stores", which are OSDMaps. Store names must have at least 4 characters. --- OpenSim/Framework/DAMap.cs | 104 ++++++++++++++---- .../DynamicAttributes/DAExampleModule.cs | 25 +++-- 2 files changed, 97 insertions(+), 32 deletions(-) diff --git a/OpenSim/Framework/DAMap.cs b/OpenSim/Framework/DAMap.cs index 7551a10fe4..c25623065a 100644 --- a/OpenSim/Framework/DAMap.cs +++ b/OpenSim/Framework/DAMap.cs @@ -39,10 +39,19 @@ using OpenMetaverse.StructuredData; namespace OpenSim.Framework { /// - /// This is the map for storing and retrieving dynamic attributes. + /// This class stores and retrieves dynamic attributes. /// - public class DAMap : IDictionary, IXmlSerializable - { + /// + /// Modules that want to use dynamic attributes need to do so in a private data store + /// which is accessed using a unique name. DAMap provides access to the data stores, + /// each of which is an OSDMap. Modules are free to store any type of data they want + /// within their data store. However, avoid storing large amounts of data because that + /// would slow down database access. + /// + public class DAMap : IDictionary, IXmlSerializable + { + private static readonly int MIN_STORE_NAME_LENGTH = 4; + protected OSDMap m_map; public DAMap() { m_map = new OSDMap(); } @@ -79,12 +88,42 @@ namespace OpenSim.Framework { writer.WriteRaw(ToXml()); } - + + /// + /// Returns the number of data stores. + /// public int Count { get { lock (this) { return m_map.Count; } } } + public bool IsReadOnly { get { return false; } } + + /// + /// Returns the names of the data stores. + /// public ICollection Keys { get { lock (this) { return m_map.Keys; } } } - public ICollection Values { get { lock (this) { return m_map.Values; } } } - public OSD this[string key] + + /// + /// Returns all the data stores. + /// + public ICollection Values + { + get + { + lock (this) + { + List stores = new List(m_map.Count); + foreach (OSD llsd in m_map.Values) + stores.Add((OSDMap)llsd); + return stores; + } + } + } + + /// + /// Gets or sets one data store. + /// + /// Store name + /// + public OSDMap this[string key] { get { @@ -93,13 +132,25 @@ namespace OpenSim.Framework lock (this) { if (m_map.TryGetValue(key, out llsd)) - return llsd; + return (OSDMap)llsd; else return null; } } - set { lock (this) { m_map[key] = value; } } - } + + set + { + ValidateKey(key); + lock (this) + m_map[key] = value; + } + } + + private static void ValidateKey(string key) + { + if (key.Length < MIN_STORE_NAME_LENGTH) + throw new Exception("Minimum store name length is " + MIN_STORE_NAME_LENGTH); + } public bool ContainsKey(string key) { @@ -107,13 +158,14 @@ namespace OpenSim.Framework return m_map.ContainsKey(key); } - public void Add(string key, OSD llsd) - { + public void Add(string key, OSDMap store) + { + ValidateKey(key); lock (this) - m_map.Add(key, llsd); + m_map.Add(key, store); } - public void Add(KeyValuePair kvp) + public void Add(KeyValuePair kvp) { lock (this) m_map.Add(kvp.Key, kvp.Value); @@ -125,10 +177,22 @@ namespace OpenSim.Framework return m_map.Remove(key); } - public bool TryGetValue(string key, out OSD llsd) - { + public bool TryGetValue(string key, out OSDMap store) + { lock (this) - return m_map.TryGetValue(key, out llsd); + { + OSD llsd; + if (m_map.TryGetValue(key, out llsd)) + { + store = (OSDMap)llsd; + return true; + } + else + { + store = null; + return false; + } + } } public void Clear() @@ -137,18 +201,18 @@ namespace OpenSim.Framework m_map.Clear(); } - public bool Contains(KeyValuePair kvp) + public bool Contains(KeyValuePair kvp) { lock (this) return m_map.ContainsKey(kvp.Key); } - public void CopyTo(KeyValuePair[] array, int index) + public void CopyTo(KeyValuePair[] array, int index) { throw new NotImplementedException(); } - public bool Remove(KeyValuePair kvp) + public bool Remove(KeyValuePair kvp) { lock (this) return m_map.Remove(kvp.Key); @@ -160,7 +224,7 @@ namespace OpenSim.Framework return m_map.GetEnumerator(); } - IEnumerator> IEnumerable>.GetEnumerator() + IEnumerator> IEnumerable>.GetEnumerator() { return null; } diff --git a/OpenSim/Region/CoreModules/Framework/DynamicAttributes/DAExampleModule.cs b/OpenSim/Region/CoreModules/Framework/DynamicAttributes/DAExampleModule.cs index d6fb15bfbe..084fb5f70b 100644 --- a/OpenSim/Region/CoreModules/Framework/DynamicAttributes/DAExampleModule.cs +++ b/OpenSim/Region/CoreModules/Framework/DynamicAttributes/DAExampleModule.cs @@ -75,22 +75,23 @@ namespace OpenSim.Region.Framework.DynamicAttributes.DAExampleModule protected bool OnSceneGroupMove(UUID groupId, Vector3 delta) { + OSDMap attrs = null; SceneObjectPart sop = m_scene.GetSceneObjectPart(groupId); - DAMap attrs = sop.DynAttrs; + if (!sop.DynAttrs.TryGetValue(Name, out attrs)) + attrs = new OSDMap(); - lock (attrs) - { - OSDInteger newValue; + OSDInteger newValue; - if (!attrs.ContainsKey("moves")) - newValue = new OSDInteger(1); - else - newValue = new OSDInteger(((OSDInteger)attrs["moves"]).AsInteger() + 1); + if (!attrs.ContainsKey("moves")) + newValue = new OSDInteger(1); + else + newValue = new OSDInteger(((OSDInteger)attrs["moves"]).AsInteger() + 1); - attrs["moves"] = newValue; - - m_dialogMod.SendGeneralAlert(string.Format("{0} {1} moved {2} times", sop.Name, sop.UUID, newValue)); - } + attrs["moves"] = newValue; + + sop.DynAttrs[Name] = attrs; + + m_dialogMod.SendGeneralAlert(string.Format("{0} {1} moved {2} times", sop.Name, sop.UUID, newValue)); return true; }