Record Management System
by, 11-22-2011 at 06:14 PM (1757 Views)
RMS is a database (flat file) which MIDlets can use to persist data. In this post, I will introduce the audience with RMS.
The device platform maintains the integrity of the MIDlet's record stores throughout the normal use. The reboots, battery changes, etc. won’t affect the RMS. An interesting question developers ask is, where is this Record Store created? It is created at platformdependent location, like nonvolatile device memory, and is not directly exposed to the MIDlets.
an important thing to note is that that record store implementations ensure that all record store operations are atomic, synchronous, and serialized, so no corruption of data will occur during multiple accesses.
The record store maintains the following information:
- timestamp to indicate when it was modified.
- version information (integer) which is incremented for each operation that modifies the contents of the record store.
The version and timestamp info is of great use when you want to start the synchronization process. An interesting info for you is that each record in a Record Store has an array of bytes with a unique integer identifier.
There arises an interesting question: What if a MIDlet uses multiple threads to access a record store? You as a developer should not address this. Its MIDlet's responsibility to coordinate this access; and if it fails to do so, unintended consequences may result. Also note that if a platform performs a synchronization of a record store with multiple threads trying to access the record store simultaneously, the platform will manage everything.
Do read the first part of this post. This post is code specific. I will present some sode snippets to make things obvious.
Opening the Record Set is very easy.
To add a record, javax.microedition.rms.RecordStore provides the followig method:Java Code:RecordStore rs = RecordStore.openRecordStore("MyContact",true);
Example follows below:Java Code:public int addRecord(byte data, int offset, int numBytes)
Let me show you how to delete a record from a Record Store:Java Code:String appt = "new record"; byte bytes = appt.getBytes(); rs.addRecord(bytes,0,bytes.length);
To update a record, you first have to get that record using the following method:
It will return the data stored in the given record in form of byte arrayJava Code:public int getRecord(int recordId, byte buffer, int offset)
Now use the setRecord method to update the record:
Example follows below:Java Code:public void setRecord(int recordId, byte newData, int offset, int numBytes)
Java Code:tring newappt = "update record"; Byte data = newappt.getBytes(); Rs.setRecord(1, data, 0, data.length());