Hey,

We recently landed a big change in the DB upgrade process for the
mozContacts API. I thought it could interest some of you, so here are
the relevant links with the patches:

[1] Bug 951806: changes the upgrade code
[2] Bug 932803: tests the upgrade code path


# The problem


The problem was that between Firefox OS versions 1.2 and 1.3 we
increased the DB version several times, with each time an upgrade step
involving looping all values to change ("fix") them. At the end of the
day, this added up to last between 15 and 30 minutes on a Buri, which
was not acceptable.

The issue was simple: in each step, we were looping all values using a
cursor, and calling a "next" function to do the next upgrade step once
all values were visited.


# The solution


The solution was simple too: we need to loop all values only once. We
kept the separate upgrade step functions, but instead of looping and
running them right away, we're pushing them in an array. And then, after
having executed all upgrade steps, we're looping through the values, and
executing all functions that are in the array. And the result is that
the upgrade process now takes ~ 2min30 on the Buri.

In addition to the performance improvement, this leads to a simpler and
clearer code, with less possible mistakes. It's just too easy to miss a
"cursor.continue()", but now, with the new process, future code won't
need to do this.


# Upgrade testing


But we were feeling unsafe because, after all, we were fiddling with
some upgrade code in a 1.3+ bug, and that's why we also introduced the
DB migration tests. Here again, the idea is simple: prepopulate the
profile with an old indexed db, and run mozContacts functions to trigger
the db upgrade and check everything looks fine.

But I didn't want to push an opaque binary or something, so I wanted to
initialize some indexed db code running in chrome, and then test it in
content.

That was the perfect job for mochitest-plain and loadChromeScript (see
[3] for the chrome script). The good thing with this is that it also
runs in B2G Desktop [4] and the B2G Emulator, whereas mochitest-chrome
tests don't. Also, I actually find very readable to separate the
DB-setup code from the testing code.

I've made the chrome script quite generic and therefore it should be
easy to reuse it for other APIs.



[1] https://bugzilla.mozilla.org/show_bug.cgi?id=951806
[2] https://bugzilla.mozilla.org/show_bug.cgi?id=932803
[3]
https://mxr.mozilla.org/mozilla-central/source/dom/contacts/tests/test_migration_chrome.js
[4] loadChromeScript does not work in B2G Desktop in the v1.3 branch,
for some reason creating a scriptableinputstream fails, but it works
fine in central. I found several uses of loadChromeScript in tests that
are disabled for B2G Desktop, and maybe we can reenable it on central now.


Hope it was useful,

Regards,
-- 
Julien

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
dev-b2g mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-b2g

Reply via email to