does CIQ simulator have the same storage as the device?

I am implementing a FIFO buffer using the Application.Storage module. My code involves deleting the first-in value when the storage is full by catch the Lang.StorageFullException. I wanted to test if this exception is properly caught and the code is executed properly, but I am not sure if CIQ simulator has the same storage capacity as the device (128 KB). I wanted to test it through running my program until the storage was full and observing its behavior. This is the snippet of my code that implements the buffer:

    function storeUnsentData(location, timestamp){
        //Try storing unsent data with unique keys
        try {
            Application.Storage.setValue("t"+head, timestamp);
            Application.Storage.setValue("l"+head, location);
            head = head + 1;
            System.println("Storing values" + head);
        //Catch storage full error and pop the tail then retry storing
        } catch (ex instanceof Lang.StorageFullException) {
            System.println("Storage full");
            if (tail < head){
                Application.Storage.deleteValue("t"+tail);
                Application.Storage.deleteValue("l"+tail);
                tail = tail + 1;
                storeUnsentData(location, timestamp);
            } else{
                System.println("Error: no values to clear");
            }
        }
    }
  • Some thoughts:

    1. I think it's the same, but even if not, then you should be able to test it, just maybe it'll take more time to fill it

    2. when testing in simulator use a device with the smallest possible storage capacity

    3. IMHO every time you increase either head or tail you need to store it as well, otherwise how will you be able to know it if the app is restarted?

    4. There's a logical error in your code: when you are in the catch and you call yourself recursively, then you'l AGAIN increase head. This will cause you an error later, when you'll try to read the data from the position, where the error occurred. When you are in the catch, then you're not supposed to increase head again, but try to store to the already increased head position. Maybe the easiest way to fix this is to add this line as the 1st line in the catch block: head = head - 1;

    5. maybe in the last line instead of (or after) you log :no values to clear you should throw the original exception. (though it's OK, if you just want the app to continue and skip some of the data)

  • Those are all very good points. I was also wondering about the recursive function, but my priorities are:

    1- retain as much data as possible.

    2- store the most recent data.

    Therefore, I have no problem getting rid of the first-in data point. My question then is, why do I need to decrement the head if I am incrementing the tail in the catch block? I don't see why there will be an error if I already cleared space in the storage

  • because you haven't actually managed to save to t23 and/or l23, so you have there "null" (but not really, because you'll get another exception because you'll try to read a non existant key) so when the connection to your phone is back then you'll try to read all the data between tail and head, but when you get to l23 you'll crash

  • Does that mean that the line 'head = head + 1;' in the try block is executed even if there is a StorageFullException?

  • ah no, my bad! For some reason I didn't realized you do it after the successful storage. You're right. Ignore my #4

  • No problem. Thanks for your help!