Changed DAMap to be the container of "data stores", which are OSDMaps. Store names must have at least 4 characters.

user_profiles
Oren Hurvitz 2013-01-22 08:41:32 +02:00 committed by Justin Clark-Casey (justincc)
parent 918b062866
commit 8b4441d940
2 changed files with 97 additions and 32 deletions

View File

@ -39,10 +39,19 @@ using OpenMetaverse.StructuredData;
namespace OpenSim.Framework namespace OpenSim.Framework
{ {
/// <summary> /// <summary>
/// This is the map for storing and retrieving dynamic attributes. /// This class stores and retrieves dynamic attributes.
/// </summary> /// </summary>
public class DAMap : IDictionary<string, OSD>, IXmlSerializable /// <remarks>
/// 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.
/// </remarks>
public class DAMap : IDictionary<string, OSDMap>, IXmlSerializable
{ {
private static readonly int MIN_STORE_NAME_LENGTH = 4;
protected OSDMap m_map; protected OSDMap m_map;
public DAMap() { m_map = new OSDMap(); } public DAMap() { m_map = new OSDMap(); }
@ -80,11 +89,41 @@ namespace OpenSim.Framework
writer.WriteRaw(ToXml()); writer.WriteRaw(ToXml());
} }
/// <summary>
/// Returns the number of data stores.
/// </summary>
public int Count { get { lock (this) { return m_map.Count; } } } public int Count { get { lock (this) { return m_map.Count; } } }
public bool IsReadOnly { get { return false; } } public bool IsReadOnly { get { return false; } }
/// <summary>
/// Returns the names of the data stores.
/// </summary>
public ICollection<string> Keys { get { lock (this) { return m_map.Keys; } } } public ICollection<string> Keys { get { lock (this) { return m_map.Keys; } } }
public ICollection<OSD> Values { get { lock (this) { return m_map.Values; } } }
public OSD this[string key] /// <summary>
/// Returns all the data stores.
/// </summary>
public ICollection<OSDMap> Values
{
get
{
lock (this)
{
List<OSDMap> stores = new List<OSDMap>(m_map.Count);
foreach (OSD llsd in m_map.Values)
stores.Add((OSDMap)llsd);
return stores;
}
}
}
/// <summary>
/// Gets or sets one data store.
/// </summary>
/// <param name="key">Store name</param>
/// <returns></returns>
public OSDMap this[string key]
{ {
get get
{ {
@ -93,12 +132,24 @@ namespace OpenSim.Framework
lock (this) lock (this)
{ {
if (m_map.TryGetValue(key, out llsd)) if (m_map.TryGetValue(key, out llsd))
return llsd; return (OSDMap)llsd;
else else
return null; 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) public bool ContainsKey(string key)
@ -107,13 +158,14 @@ namespace OpenSim.Framework
return m_map.ContainsKey(key); return m_map.ContainsKey(key);
} }
public void Add(string key, OSD llsd) public void Add(string key, OSDMap store)
{ {
ValidateKey(key);
lock (this) lock (this)
m_map.Add(key, llsd); m_map.Add(key, store);
} }
public void Add(KeyValuePair<string, OSD> kvp) public void Add(KeyValuePair<string, OSDMap> kvp)
{ {
lock (this) lock (this)
m_map.Add(kvp.Key, kvp.Value); m_map.Add(kvp.Key, kvp.Value);
@ -125,10 +177,22 @@ namespace OpenSim.Framework
return m_map.Remove(key); return m_map.Remove(key);
} }
public bool TryGetValue(string key, out OSD llsd) public bool TryGetValue(string key, out OSDMap store)
{ {
lock (this) 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() public void Clear()
@ -137,18 +201,18 @@ namespace OpenSim.Framework
m_map.Clear(); m_map.Clear();
} }
public bool Contains(KeyValuePair<string, OSD> kvp) public bool Contains(KeyValuePair<string, OSDMap> kvp)
{ {
lock (this) lock (this)
return m_map.ContainsKey(kvp.Key); return m_map.ContainsKey(kvp.Key);
} }
public void CopyTo(KeyValuePair<string, OSD>[] array, int index) public void CopyTo(KeyValuePair<string, OSDMap>[] array, int index)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public bool Remove(KeyValuePair<string, OSD> kvp) public bool Remove(KeyValuePair<string, OSDMap> kvp)
{ {
lock (this) lock (this)
return m_map.Remove(kvp.Key); return m_map.Remove(kvp.Key);
@ -160,7 +224,7 @@ namespace OpenSim.Framework
return m_map.GetEnumerator(); return m_map.GetEnumerator();
} }
IEnumerator<KeyValuePair<string, OSD>> IEnumerable<KeyValuePair<string, OSD>>.GetEnumerator() IEnumerator<KeyValuePair<string, OSDMap>> IEnumerable<KeyValuePair<string, OSDMap>>.GetEnumerator()
{ {
return null; return null;
} }

View File

@ -75,11 +75,11 @@ namespace OpenSim.Region.Framework.DynamicAttributes.DAExampleModule
protected bool OnSceneGroupMove(UUID groupId, Vector3 delta) protected bool OnSceneGroupMove(UUID groupId, Vector3 delta)
{ {
OSDMap attrs = null;
SceneObjectPart sop = m_scene.GetSceneObjectPart(groupId); 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")) if (!attrs.ContainsKey("moves"))
@ -89,8 +89,9 @@ namespace OpenSim.Region.Framework.DynamicAttributes.DAExampleModule
attrs["moves"] = newValue; attrs["moves"] = newValue;
sop.DynAttrs[Name] = attrs;
m_dialogMod.SendGeneralAlert(string.Format("{0} {1} moved {2} times", sop.Name, sop.UUID, newValue)); m_dialogMod.SendGeneralAlert(string.Format("{0} {1} moved {2} times", sop.Name, sop.UUID, newValue));
}
return true; return true;
} }