Per discussions with justincc... split the JsonStore type

functions into one for node type and one for value type.
Define and export constants for both nodes and values.
0.7.4-extended
Mic Bowman 2013-03-05 20:33:17 -08:00 committed by Justin Clark-Casey (justincc)
parent e6a97e9864
commit c5a6b61218
5 changed files with 143 additions and 35 deletions

View File

@ -41,6 +41,16 @@ namespace OpenSim.Region.Framework.Interfaces
Value = 3 Value = 3
} }
public enum JsonStoreValueType
{
Undefined = 0,
Boolean = 1,
Integer = 2,
Float = 3,
String = 4,
UUID = 5
}
public delegate void TakeValueCallback(string s); public delegate void TakeValueCallback(string s);
public interface IJsonStoreModule public interface IJsonStoreModule
@ -48,7 +58,9 @@ namespace OpenSim.Region.Framework.Interfaces
bool CreateStore(string value, ref UUID result); bool CreateStore(string value, ref UUID result);
bool DestroyStore(UUID storeID); bool DestroyStore(UUID storeID);
JsonStoreNodeType GetPathType(UUID storeID, string path); JsonStoreNodeType GetNodeType(UUID storeID, string path);
JsonStoreValueType GetValueType(UUID storeID, string path);
bool TestStore(UUID storeID); bool TestStore(UUID storeID);
bool SetValue(UUID storeID, string path, string value, bool useJson); bool SetValue(UUID storeID, string path, string value, bool useJson);

View File

@ -130,7 +130,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
/// ///
/// </summary> /// </summary>
// ----------------------------------------------------------------- // -----------------------------------------------------------------
public JsonStoreNodeType PathType(string expr) public JsonStoreNodeType GetNodeType(string expr)
{ {
Stack<string> path; Stack<string> path;
if (! ParsePathExpression(expr,out path)) if (! ParsePathExpression(expr,out path))
@ -153,6 +153,43 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
return JsonStoreNodeType.Undefined; return JsonStoreNodeType.Undefined;
} }
// -----------------------------------------------------------------
/// <summary>
///
/// </summary>
// -----------------------------------------------------------------
public JsonStoreValueType GetValueType(string expr)
{
Stack<string> path;
if (! ParsePathExpression(expr,out path))
return JsonStoreValueType.Undefined;
OSD result = ProcessPathExpression(ValueStore,path);
if (result == null)
return JsonStoreValueType.Undefined;
if (result is OSDMap)
return JsonStoreValueType.Undefined;
if (result is OSDArray)
return JsonStoreValueType.Undefined;
if (result is OSDBoolean)
return JsonStoreValueType.Boolean;
if (result is OSDInteger)
return JsonStoreValueType.Integer;
if (result is OSDReal)
return JsonStoreValueType.Float;
if (result is OSDString)
return JsonStoreValueType.String;
return JsonStoreValueType.Undefined;
}
// ----------------------------------------------------------------- // -----------------------------------------------------------------
/// <summary> /// <summary>
/// ///

View File

@ -234,7 +234,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
/// ///
/// </summary> /// </summary>
// ----------------------------------------------------------------- // -----------------------------------------------------------------
public JsonStoreNodeType GetPathType(UUID storeID, string path) public JsonStoreNodeType GetNodeType(UUID storeID, string path)
{ {
if (! m_enabled) return JsonStoreNodeType.Undefined; if (! m_enabled) return JsonStoreNodeType.Undefined;
@ -251,7 +251,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
try try
{ {
lock (map) lock (map)
return map.PathType(path); return map.GetNodeType(path);
} }
catch (Exception e) catch (Exception e)
{ {
@ -261,6 +261,38 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
return JsonStoreNodeType.Undefined; return JsonStoreNodeType.Undefined;
} }
// -----------------------------------------------------------------
/// <summary>
///
/// </summary>
// -----------------------------------------------------------------
public JsonStoreValueType GetValueType(UUID storeID, string path)
{
if (! m_enabled) return JsonStoreValueType.Undefined;
JsonStore map = null;
lock (m_JsonValueStore)
{
if (! m_JsonValueStore.TryGetValue(storeID,out map))
{
m_log.InfoFormat("[JsonStore] Missing store {0}",storeID);
return JsonStoreValueType.Undefined;
}
}
try
{
lock (map)
return map.GetValueType(path);
}
catch (Exception e)
{
m_log.Error(string.Format("[JsonStore]: Path test failed for {0} in {1}", path, storeID), e);
}
return JsonStoreValueType.Undefined;
}
// ----------------------------------------------------------------- // -----------------------------------------------------------------
/// <summary> /// <summary>
/// ///

View File

@ -192,16 +192,32 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
#region ScriptConstantsInterface #region ScriptConstantsInterface
[ScriptConstant] [ScriptConstant]
public static readonly int JSON_TYPE_UNDEF = (int)JsonStoreNodeType.Undefined; public static readonly int JSON_NODETYPE_UNDEF = (int)JsonStoreNodeType.Undefined;
[ScriptConstant] [ScriptConstant]
public static readonly int JSON_TYPE_OBJECT = (int)JsonStoreNodeType.Object; public static readonly int JSON_NODETYPE_OBJECT = (int)JsonStoreNodeType.Object;
[ScriptConstant] [ScriptConstant]
public static readonly int JSON_TYPE_ARRAY = (int)JsonStoreNodeType.Array; public static readonly int JSON_NODETYPE_ARRAY = (int)JsonStoreNodeType.Array;
[ScriptConstant] [ScriptConstant]
public static readonly int JSON_TYPE_VALUE = (int)JsonStoreNodeType.Value; public static readonly int JSON_NODETYPE_VALUE = (int)JsonStoreNodeType.Value;
[ScriptConstant]
public static readonly int JSON_VALUETYPE_UNDEF = (int)JsonStoreValueType.Undefined;
[ScriptConstant]
public static readonly int JSON_VALUETYPE_BOOLEAN = (int)JsonStoreValueType.Boolean;
[ScriptConstant]
public static readonly int JSON_VALUETYPE_INTEGER = (int)JsonStoreValueType.Integer;
[ScriptConstant]
public static readonly int JSON_VALUETYPE_FLOAT = (int)JsonStoreValueType.Float;
[ScriptConstant]
public static readonly int JSON_VALUETYPE_STRING = (int)JsonStoreValueType.String;
#endregion #endregion
@ -295,9 +311,20 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
/// </summary> /// </summary>
// ----------------------------------------------------------------- // -----------------------------------------------------------------
[ScriptInvocation] [ScriptInvocation]
public int JsonGetPathType(UUID hostID, UUID scriptID, UUID storeID, string path) public int JsonGetNodeType(UUID hostID, UUID scriptID, UUID storeID, string path)
{ {
return (int)m_store.GetPathType(storeID,path); return (int)m_store.GetNodeType(storeID,path);
}
// -----------------------------------------------------------------
/// <summary>
///
/// </summary>
// -----------------------------------------------------------------
[ScriptInvocation]
public int JsonGetValueType(UUID hostID, UUID scriptID, UUID storeID, string path)
{
return (int)m_store.GetValueType(storeID,path);
} }
// ----------------------------------------------------------------- // -----------------------------------------------------------------

View File

@ -158,8 +158,8 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore.Tests
Assert.That(dsrv, Is.EqualTo(1)); Assert.That(dsrv, Is.EqualTo(1));
int tprv = (int)InvokeOp("JsonGetPathType", storeId, "Hello"); int tprv = (int)InvokeOp("JsonGetNodeType", storeId, "Hello");
Assert.That(tprv, Is.EqualTo(JsonStoreScriptModule.JSON_TYPE_UNDEF)); Assert.That(tprv, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_UNDEF));
} }
[Test] [Test]
@ -277,8 +277,8 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore.Tests
int returnValue = (int)InvokeOp( "JsonRemoveValue", storeId, "Hello"); int returnValue = (int)InvokeOp( "JsonRemoveValue", storeId, "Hello");
Assert.That(returnValue, Is.EqualTo(1)); Assert.That(returnValue, Is.EqualTo(1));
int result = (int)InvokeOp("JsonGetPathType", storeId, "Hello"); int result = (int)InvokeOp("JsonGetNodeType", storeId, "Hello");
Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_TYPE_UNDEF)); Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_UNDEF));
string returnValue2 = (string)InvokeOp("JsonGetValue", storeId, "Hello"); string returnValue2 = (string)InvokeOp("JsonGetValue", storeId, "Hello");
Assert.That(returnValue2, Is.EqualTo("")); Assert.That(returnValue2, Is.EqualTo(""));
@ -291,8 +291,8 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore.Tests
int returnValue = (int)InvokeOp( "JsonRemoveValue", storeId, "Hello"); int returnValue = (int)InvokeOp( "JsonRemoveValue", storeId, "Hello");
Assert.That(returnValue, Is.EqualTo(1)); Assert.That(returnValue, Is.EqualTo(1));
int result = (int)InvokeOp("JsonGetPathType", storeId, "Hello"); int result = (int)InvokeOp("JsonGetNodeType", storeId, "Hello");
Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_TYPE_UNDEF)); Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_UNDEF));
string returnValue2 = (string)InvokeOp("JsonGetJson", storeId, "Hello"); string returnValue2 = (string)InvokeOp("JsonGetJson", storeId, "Hello");
Assert.That(returnValue2, Is.EqualTo("")); Assert.That(returnValue2, Is.EqualTo(""));
@ -306,11 +306,11 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore.Tests
int returnValue = (int)InvokeOp( "JsonRemoveValue", storeId, "Hello[0]"); int returnValue = (int)InvokeOp( "JsonRemoveValue", storeId, "Hello[0]");
Assert.That(returnValue, Is.EqualTo(1)); Assert.That(returnValue, Is.EqualTo(1));
int result = (int)InvokeOp("JsonGetPathType", storeId, "Hello[0]"); int result = (int)InvokeOp("JsonGetNodeType", storeId, "Hello[0]");
Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_TYPE_VALUE)); Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_VALUE));
result = (int)InvokeOp("JsonGetPathType", storeId, "Hello[1]"); result = (int)InvokeOp("JsonGetNodeType", storeId, "Hello[1]");
Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_TYPE_UNDEF)); Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_UNDEF));
string stringReturnValue = (string)InvokeOp("JsonGetValue", storeId, "Hello[0]"); string stringReturnValue = (string)InvokeOp("JsonGetValue", storeId, "Hello[0]");
Assert.That(stringReturnValue, Is.EqualTo("value2")); Assert.That(stringReturnValue, Is.EqualTo("value2"));
@ -433,7 +433,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore.Tests
} }
[Test] [Test]
public void TestJsonGetPathType() public void TestJsonGetNodeType()
{ {
TestHelpers.InMethod(); TestHelpers.InMethod();
// TestHelpers.EnableLogging(); // TestHelpers.EnableLogging();
@ -441,41 +441,41 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore.Tests
UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello' : { 'World' : [ 'one', 2 ] } }"); UUID storeId = (UUID)InvokeOp("JsonCreateStore", "{ 'Hello' : { 'World' : [ 'one', 2 ] } }");
{ {
int result = (int)InvokeOp("JsonGetPathType", storeId, "."); int result = (int)InvokeOp("JsonGetNodeType", storeId, ".");
Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_TYPE_OBJECT)); Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_OBJECT));
} }
{ {
int result = (int)InvokeOp("JsonGetPathType", storeId, "Hello"); int result = (int)InvokeOp("JsonGetNodeType", storeId, "Hello");
Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_TYPE_OBJECT)); Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_OBJECT));
} }
{ {
int result = (int)InvokeOp("JsonGetPathType", storeId, "Hello.World"); int result = (int)InvokeOp("JsonGetNodeType", storeId, "Hello.World");
Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_TYPE_ARRAY)); Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_ARRAY));
} }
{ {
int result = (int)InvokeOp("JsonGetPathType", storeId, "Hello.World[0]"); int result = (int)InvokeOp("JsonGetNodeType", storeId, "Hello.World[0]");
Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_TYPE_VALUE)); Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_VALUE));
} }
{ {
int result = (int)InvokeOp("JsonGetPathType", storeId, "Hello.World[1]"); int result = (int)InvokeOp("JsonGetNodeType", storeId, "Hello.World[1]");
Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_TYPE_VALUE)); Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_VALUE));
} }
// Test for non-existant path // Test for non-existant path
{ {
int result = (int)InvokeOp("JsonGetPathType", storeId, "foo"); int result = (int)InvokeOp("JsonGetNodeType", storeId, "foo");
Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_TYPE_UNDEF)); Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_UNDEF));
} }
// Test for non-existant store // Test for non-existant store
{ {
UUID fakeStoreId = TestHelpers.ParseTail(0x500); UUID fakeStoreId = TestHelpers.ParseTail(0x500);
int result = (int)InvokeOp("JsonGetPathType", fakeStoreId, "."); int result = (int)InvokeOp("JsonGetNodeType", fakeStoreId, ".");
Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_TYPE_UNDEF)); Assert.That(result, Is.EqualTo(JsonStoreScriptModule.JSON_NODETYPE_UNDEF));
} }
} }