Redis(In memory DB)¶
In memory DB (Redis) is used to store meta-data. This data can be used in the rule during its execution and evaluation. User can read meta-data and do filtering in the if-then-else evaluation. To know more about redis click Redis and for commands click Redis Commands.
There are following three operations implemented in MR for Redis:
1) Reading meta-data stored in memory DB
2) Writing meta-data in memory DB
3) Execution of Lua script on in memory DB
Reading Meta-data Stored in Memory DB¶
Below is the create rule JSON for reading meta-data stored in Redis.
{
"RuleName": "Test Rule",
"RuleDescription": "Test Rule for in memory DB read",
"input": {
"DirectExchange": "Direct.Exchange",
"RoutingKey": "uKK6WjaZZnnYBj1G6rONQgNAIuc="
},
"params": [
{
"name": "feature",
"value": ""
},
{
"name": "sensorID",
"value": "blood-sensor-1"
},
{
"name": "time",
"value": {
"starttime": "",
"endtime": ""
}
},
{
"name": "expression",
"value": {
"if": "systolic> (1)*tcup.CacheController.LookUp(\"get subrata:systolic\") ",
"then": "return \"high\"; ",
"else": "if ( systolic< 80 && systolic > 50 ) {return \"medium\";} else {if (systolic > 20){return \"low\";} } "
}
},
{
"name": "ObservedProperties",
"value": [
{
"name": "systolic",
"value": "!=",
"type": "quantity",
"operator": "80"
}
]
}
],
"output": {
"TopicExchange": "testTopic",
"Topic": "redisR.*"
}
}
As we see in above create rule JSON systolic parameter value is being checked with value stored in Redis with key subrata:systolic which prints the output of expression evaluation accordingly.
Note
tcup.CacheController.LookUp() will lookup the key values from the in memory DB. This function accepts all the Redis supported commands to read different data types. In the above example it will look up for “subrata:systolic” string data type from Redis. Necessary type conversion has to be done in the rule itself like string values get converted to integer by being multiplied with 1.
Writing meta-data in memory DB¶
Below is the create rule JSON for writing meta-data stored in Redis.
Through rule, it can update Redis DB during its execution and evaluation. User can write data through the if-then-else evaluation. It is necessary to know Redis related command and data types to write to in memory DB.
{
"RuleName": "Test Rule",
"RuleDescription": "Test Rule for in memory data write",
"input": {
"DirectExchange": "Direct.Exchange",
"RoutingKey": "uKK6WjaZZnnYBj1G6rONQgNAIuc="
},
"params": [
{
"name": "feature",
"value": ""
},
{
"name": "sensorID",
"value": "blood-sensor-1"
},
{
"name": "time",
"value": {
"starttime": "",
"endtime": ""
}
},
{
"name": "expression",
"value": {
"if": "systolic> 150 ",
"then": "tcup.CacheController.Write(\"set subrata:systolic \" + systolic);return \"high\"; ",
"else": ""
}
},
{
"name": "ObservedProperties",
"value": [
{
"name": "systolic",
"value": "!=",
"type": "quantity",
"operator": "80"
}
]
}
],
"output": {
"TopicExchange": "testTopic",
"Topic": "redisW.*"
}
}
Here we can see that systolic property value has been used to write the systolic value in Redis using the command set subrata:systolic only when if condition is true.
Note
tcup.CacheController.Write() will write the key values in memory DB. This function accepts all the Redis supported commands to create/update any keys in Redis. This key naming format should be ideally formed as URN naming methodology.
Execution of Lua script on in memory DB¶
Below is the create rule JSON for execution of Lua script on Redis.
Through rule, it can execute Lua script on Redis. Lua script can be executed through the if-then-else evaluation.
Example, multiple keys deletion on in memory DB is not supported currently but below Lua script allows to delete multiple keys starting with abc:
EVAL “return redis.call(‘del’, unpack(redis.call(‘keys’, ARGV[1])))” 0 abc:*
Above Lua script can be executed through Rule using tcup.CacheController.redisLuaScript() method as shown below:
{
"Status": "Running",
"RuleID": 81,
"RuleName": "test5",
"RuleDescription": "test5 for humidity",
"parallelismFactor": 1,
"input": {
"type": "",
"DirectExchange": "Direct.Exchange",
"RoutingKey": "uKK6WjaZZnnYBj1G6rONQgNAIuc=",
"MQName": ""
},
"params": [{
"name": "feature",
"value": "abc"
},
{
"name": "sensorID",
"value": ""
},
{
"name": "time",
"value": {
"starttime": "",
"endtime": ""
}
},
{
"name": "expression",
"value": {
"if": "true",
"then": "ret=(1)*tcup.CacheController.redisLuaScript(\"return redis.call('del', unpack(redis.call('keys', ARGV[1])))\",0,key);return ret+\" keys deleted\";",
"else": ""
}
},
{
"name": "ObservedProperties",
"value": []
}
],
"output": {
"TopicExchange": "testEx2",
"Topic": "test2.*"
}
}
Here above Lua script deletes multiple keys with pattern defined in key value. It will return number of keys deleted from in memory DB.
Note
In memory database should have key defined as per the pattern defined in key property shown below:
The below sample data send through sendRequest() has key pattern to be deleted.
{
"version": "1.0.1",
"observations": [{
"sensor": "abc",
"feature": "abc",
"record": [{
"starttime": "23-JUL-2013 15:30:00 IST",
"position-global": {
"latitude": "88.474523",
"longitude": "22.590957",
"altitude": "2m"
},
"output": [{
"name": "key",
"value": "abc1:*"
}]
}]
}]
}