Global Blob (glob)

Global blob is a persistent key value store which can be used across templates

Since the scope of the state variable is limited during the scope of the instance lifetime, glob offers a permanent storage for your test needs. Glob is basically a global key value store, which is persistent across the simulation and is visible to all template, upon every run. This means that any value set in the glob can be retrieved later by any other template instance, as long as the key is known.

Think of glob as your internal database shared across the templates. Two templates running in parallel could also use the glob to exchange pre determined information with each other.

Lets take an example

{
// try to retrieve the unique id for client 1
var my_unique_id = glob.get("client"+this.clientId+"_id");
if (!my_unique_id){
// permanent unique ID not found. Lets create a new one and store in glob
my_unique_id = chance.guid();
glob.set("client"+this.clientId+"_id", my_unique_id);
console.log("Unique id not found. Creating one ", my_unique_id);
}
else
{
console.log("Retrieved unique id from glob", my_unique_id);
}
}

In the above example, we first call glob.get api with a unique identifier to see if we have previously stored any unique client ID. If it is found, we will use one. If it can not be retrieved, we will create a new one and store in glob for future reference.

Glob object provides 3 simple functions

glob.set(key, value, function(success){})

key: Must be a string

value: Could be a string, JSON, array or any other object.

callback: where success is true or false, indicating the result of operation.

The success returns true if glob can be stored successfully , else false.

Note: If callback is ommitted, the API performs synchronously and returns the success bool. Synchronous calling slows down the simulation speed drastically and should be avoided.

glob.get(key, function(result){})

key: Must be a string

callback: passed with the blob value in result. null if the key can not be found.

The API returns the value stored in glob object if found. else false. The function try to JSON.parse() the return value so the objects are automatically converted, if possible.

If key can not be found, the result is null

Note: If callback is ommitted, the API performs synchronously and returns the stored value. Synchronous calling slows down the simulation speed drastically and should be avoided.

glob.delete(key, function(success){})

key: Must be a string

callback: passed with the success of operation as result. false if the key can not be found.

The API erases the key stored in glob object if found and return true else false.

It’s a good idea to clean up glob value after you finish the your template.

Note: If callback is ommitted, the API performs synchronously and returns the success bool. Synchronous calling slows down the simulation speed drastically and should be avoided.

How could I use glob?

There are a variety of use cases of glob. Let’s consider following.

When you simulate a moving truck in IoTIFY template, it always start from the same location every time you run the template. There is no way you could remember the last position of your truck and start from there.
With glob, this problem becomes easy to solve. Here is an example of smart truck template which has been modified to use glob. This new template remembers its last location and starts exactly where it ended last time.

{
if (state.start === undefined) {
var pos = glob.get("lastPos"+_meta.clientId);
if (!pos){
state.start = "Zurich,CH";
}
else
{
state.start = pos;
}
}
state.path = drive({start:state.start,end:'Hamburg,DE',accuracy:5});
var retval = {};
retval.time = { "$date" : moment.now()};
retval.data = {};
retval.data.location = state.path.latitude + ","+ state.path.longitude;
glob.set("lastPos"+_meta.clientId, retval.data.location, function(){});
retval.data.speed = state.path.speed ;
retval.data.temperature = chance.integer({min:10, max:45});
//return a string value which will be sent as the message payload
return JSON.stringify(retval, null, 2);
}

The only difference here is that instead of always starting from a fixed location, the template finds the last saved starting position in glob and then uses it as a starting position.