View RSS Feed

My Java Tips

Record Management System

Rate this Entry
by , 11-22-2011 at 06:14 PM (2777 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.
Java Code:
RecordStore rs = RecordStore.openRecordStore("MyContact",true);
To add a record, javax.microedition.rms.RecordStore provides the followig method:

Java Code:
public int addRecord(byte[] data, int offset, int numBytes)
Example follows below:

Java Code:
String appt = "new record";
byte bytes[] = appt.getBytes();
Let me show you how to delete a record from a Record Store:
To update a record, you first have to get that record using the following method:

Java Code:
public int getRecord(int recordId, byte[] buffer, int offset)
It will return the data stored in the given record in form of byte array

Now use the setRecord method to update the record:

Java Code:
public void setRecord(int recordId, byte[] newData, int offset, int numBytes)
Example follows below:

Java Code:
tring newappt = "update record";
Byte data = newappt.getBytes();
Rs.setRecord(1, data, 0, data.length());

Submit "Record Management System" to Facebook Submit "Record Management System" to Digg Submit "Record Management System" to Submit "Record Management System" to StumbleUpon Submit "Record Management System" to Google

Tags: midlet, rms, timestamp Add / Edit Tags
Java ME