Kamis, 09 Desember 2010

Saving Data Safely

Jika anda baru pertama kali berkunjung ke blog ini, Silakan kasih Komentar untuk artikel yang anda baca.

With the advent of Gingerbread, were going to be streaming a series of posts in this space most the aspects of Android 2.3 that developers should tending about. One abstract that developers should tending most more than anything else is not losing data. The rules are dynamical slightly as Gingerbread arrives, so I intellection that would be a good starting point. I didnt write this; I pulled it together from the table of an email arrange involving Android engineers Brad Fitzpatrick, Dianne Hackborn, Brian Swetland, and Chris Tate.

The question is: how do you make rattling trusty your datas been written to persistent storage? The respond involves a low-level grouping call named fsync(). Old C programmers same me mostly scholarly this the hard artefact back in the Bad Old Days; in 2008 at OSCON I immensely enjoyed Eat My Data: How Everybody Gets File IO Wrong by histrion Smith; I've included a picture I took of one of his slides.

The think this should be of concern to Android developers is that with 2.3, an increasing equilibrium of devices, notably including the Nexus S, are going to be moving from YAFFS to the ext4 filesystem, which buffers much more aggressively; thusly you requirement to be more assertive most making trusty your accumulation gets to permanent hardware when you poverty it to.

If you just ingest SharedPreferences or SQLite, you crapper relax, because weve made trusty they Do The Right Thing most buffering. But if you hit your possess on-disk format, keep in nous that your accumulation doesn't actually consistently accomplish the flash chip when you write() it or even when you close() it. There are several layers of buffering between you and the hardware! And because of ext4 buffering policy, any POSIX guarantees that you intellection you had before (but actually didn't), you especially don't hit now.

Some Android devices are already streaming non-YAFFS filesystems, but as we brought up the Nexus S, buffering issues hit actually bitten us a couple of nowadays in framework code. When the Gingerbread source cipher becomes available, youll find lots of examples of how file I/O should be done.

To start with, for raw accumulation study using one of the coetaneous modes of java.io.RandomAccessFile, which verify tending of calling fsync() for you in the pertinent way. If you cant, youll poverty Java cipher that looks something same this.

public static boolean sync(FileOutputStream stream) { try { if (stream != null) { stream.getFD().sync(); } convey true; } grownup (IOException e) { } convey false;

In whatever applications, you strength even poverty to analyse the convey status of the close() call.

Now of course, there are digit sides to this story. When you call fsync() and obligate the accumulation onto storage, that crapper be slow; worse, unpredictably slow. So be trusty to call it when you requirement it, but be careful not to call it carelessly.

Background datum if you poverty to know more:

  • ext4 and accumulation loss by Jonathan Corbet.

  • ext4 vs fsync, my verify by Alexander Larsson.

  • Dont fear the fsync! by Ted Tso.


Thanks
basyar.com

Tidak ada komentar:

Posting Komentar