Coming soon:

  • Tables Library
  • AggreGate Library
  • SNTP Library

Subscribe

You can subscribe to the Tibblog:

May 16 ’11

Flash Disk, Fortified

Everyone knows that flash memory hates abrupt power interrupts. Turn the power off in the wrong (right?) moment, and you may have to do fd.format again!

Well, not anymore! We’ve just extended our fd. object to support transactions (journaling).

First, format the disk with the new fd.formatj (“j” stands for “journaling”) method:

fd.formatj(fd.availableflashspace-100,16,100)

Now, when you want to write something to the disk, open the transaction first, do your writes, then commit the transaction:

fd.transactionstart 'transaction starts
fd.setdata("some data")
fd.transactioncommit 'transaction ends

(For clarity, this code does not check for errors — your real program should!).

Why bother encasing fd.setdata within a transaction? Because this simple method potentially alters multiple sectors on the disk. Turn the power off during this operation, and the disk may become corrupted… but not if you did this within the transaction.

Should the power fail, one of two things will happen: (a) no changes will be made to the disk; or (b) all changes will be made to the disk.

You can also apply transactions to operations on multiple files. For example, it makes sense to change the data table file and the index file in one transaction — this way, the data and its index will never fall out of sync.

To try out the fd object, download TiOS 3.23.01 (down the page) and the experimental libraries. Use transactions, folks. They will make your file operations bulletproof!