• @allObjects

    Excellent! - Very very cool... What about delete? ...and garbage collect?

    At this stage I've decided to keep it very simple.
    The likes of the ESP12 with 4Mb means that we have oodles of space.

    This is the structure used to store items:

    ={ "addr": 507904,
      "flash": function () { [native code] },
      "page_size": 4096,
      "items": {
        "_root": { "page": 0, "next_page": 14, "length": 743,
          "mime": "application/json"
         },
        "DS18B20": { "page": 1, "length": 1307,
          "mime": "application/javascript; charset=utf-8"
         },
        "/": { "page": 2, "length": 751,
          "mime": "text/html"
         },
        "/js/app.js": { "page": 3, "length": 627,
          "mime": "application/javascript"
         },
        "/favicon.ico": { "page": 4, "length": 1150,
          "mime": "image/x-icon"
         },
        "clock": { "page": 7, "length": 413,
          "mime": "application/javascript; charset=utf-8"
         },
        "Clock": { "page": 8, "length": 413,
          "mime": "application/javascript; charset=utf-8"
         },
        "DS18S20": { "page": 9, "length": 2,
          "mime": "application/json"
         },
        "/images/logo.png": { "page": 10, "length": 4726,
          "mime": "image/png"
         },
        "config": { "page": 12, "length": 36,
          "mime": "application/json"
         },
        "/js.app.js": { "page": 13, "length": 624,
          "mime": "application/javascript"
         }
       }
     }
    

    The magic is in the _root object, this is stored in JSON.stringify format in the first page of the flash, and is restored in the FileStorage constructor.

    The page size is 4K - currently each object takes up at least on page. This seems wasteful, however the trade off is speed.
    An existing object can get appended too - as long as it fits in the space allocated to it.

    I did not think it was worth the extra trouble of storing intra page and managing indexes.

    Given that you can store a complex js structure, you could pack everything you wanted in a structure and store that.

    What about delete?

    The fs.item('my_key').delete(); method removes an item. At this stage it is removed from the list of objects. The page is wasted, as the only management at this stage is the items._root.next_page.

    A future enhancement could be to have an array of free pages, and this could be checked and used for a new write.

    ...and garbage collect?

    In my case, I wanted to store web pages, and modules. Both of these are easy to re-populate, so rather than garbage collect and move pages around (with limited available memory), it is more straightforward to call fs.erase() and re-populate everything again.

    I wanted the FileStore module to be as small as possible, so that the end application has as much memory without bloating the module with methods. This is why the FileStoreWrite inherits it's methods from FileStore and adds the writing methods.

About

Avatar for Wilberforce @Wilberforce started