That asynchronous storage API I’ve been working on for a while has finally been pushed to mozilla-central. That means you can now run database queries off the main thread without blocking the UI. This includes both read and write statements.
This may not seem like a big deal, but there is a big benefit to using this API over the existing synchronous API. SQLite performs a file system operation called fsync which pushes the data in the file system’s cache to the disk. This operation is inherently synchronous, and on some file systems (like ext3), can take substantial amount of time given the right circumstances. If this is ran on the main thread, the UI is locked up the whole time. By using this new asynchronous API, you won’t have to worry about that fsync holding up the main thread at all!
Perhaps the best part about this new API is that it doesn’t require many code changes. You still create SQL statements the same way, but instead of calling execute
or executeStep
on the prepared statement, you just have to call executeAsync
. The method takes one parameter – a callback that notifies on completion, error, and results. The callback is optional on the off chance that consumers don’t care if something finishes successfully or not.
Iterating through results is not much different from before either. The only difference is that results may be chunked, so the callback may get notified about results several times (with only the new data). Some good example code can be found in the tests that landed with this new API.
I’d really like people to try it out and see if they have any issues with the API. There are already a few refinements with bugs filed, and a few more up in my head that we might want if the need arises.
6 replies on “Asynchronous Storage API”
will this feature make it into the 3.1 release?
@Paula
Sure will. It is appearing right now in 3.1a1pre nightlies!
Is ext4 any better in that regard?
@funtomas
It’s my understanding that ext4 fixes this issue with fsync.
[…] been a bit more than three months since I wrote about the asynchronous storage API landing in mozilla-central. As far as I know, there are still no consumers of it in mozilla-central, but I do think that the […]
[…] In order to not block the main thread while reading from disk we either need to make SQLite use non-blocking read system calls, or call into SQLite off of the main thread. Changing the SQLite code isn’t something we want to do, so that solution was out of the question. Luckily, we had solved a similar problem with writes and fsyncs earlier in the Firefox 3.5 development with the asynchronous Storage API. […]