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.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
@allObjects
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:
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.
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 theitems._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.
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 theFileStoreWrite
inherits it's methods fromFileStore
and adds the writing methods.