blackberry persistent storage models
Post on 31-Dec-2015
43 Views
Preview:
DESCRIPTION
TRANSCRIPT
1
BlackBerry Persistent Storage Models
Persistent Storage APIs
and
Record Management System
2http://cmer.cis.uoguelph.ca
Overview
• Part 1-– Persistent Storage APIs
• Part 2- – MIDP Record Store (RMS)
3http://cmer.cis.uoguelph.ca
PART 1 – PERSISTENT STORAGE APIS
4http://cmer.cis.uoguelph.ca
PersistentStore class
• PersistentStore provides a means for storing persistent objects
• The class to work with persistent store is net.rim.device.api.system.PersistentStore
• Persistent objects consisting of a key-value pair, are committed to the persistent store.
• You can retrieve them from the persistent store by their key.
5http://cmer.cis.uoguelph.ca
PersistentStore class (Cont.)
• To retrieve them, invoke PersistentStore.getPersistentObject(long key) method.
• Example:// Hash of cmer.project.persistentobject.uniquekey
long KEY = 0x5602c4fba32d702L ;
static PersistentObject store;
Static{
store = PersistentStore.getPersistentObject( KEY );
}
6http://cmer.cis.uoguelph.ca
PersistentStore class (Cont.)
• In the previous example, using a static constructor means that the PersistentObject is created only once.
• The key is a hash of a string. The string is typically the name of your package.
7http://cmer.cis.uoguelph.ca
Persistent Objects
• Persistent Objects are objects whose content remains persistent between device resets.
• There are two types of persistent objects:– PersistentObject– Implicit persistence
• Persistent objects are stored permanently in a PersistentStore object.
8http://cmer.cis.uoguelph.ca
Implicit persistence
• A custom data types are implicitly persistable if they implement a Persistable interface.
• These data types are:– Boolean -Vector– Byte - Hashtable– Integer - Object– Long - Short– String
9http://cmer.cis.uoguelph.ca
Example- Implicit Persistence
private static final class Employee implements Persistable {
private String name;
private String address;
public Employee() {
}
public String getName() {
return this.name;
}
public void setName(String value) {
this.name = value;
}
……
}
10http://cmer.cis.uoguelph.ca
PersistentObject Class
• The class to work with Persistent Object is net.rim.device.api.system.PersistentObject
• Persistent objects are stored in a PersistentStore object.
• When a persistent object is stored to persistent store, the persistent object is stored in flash memory.
11http://cmer.cis.uoguelph.ca
PersistentObject class (Cont.)
• A persistent Object consists of a key-value pair.
• Each PersistentObject has a unique long key.
• The unique key acts as an identifier for an object meaning that objects can be retrieved from a PersistentStore via this key.
12http://cmer.cis.uoguelph.ca
PersistentObject class (Cont.)
• In BlackBerry JDE, you are able to build a unique key.
• You can do it by writing a string in IDE (any string) such as “cmer.project.persistentobject.uniquekey”
• Then, Right-click this string and select “convert to long”
13http://cmer.cis.uoguelph.ca
PersistentObject class (Cont.)
14http://cmer.cis.uoguelph.ca
PersistentObject class (Cont.)
• You can get and set the contents of the object using these methods:– void setContents( Object contents) – Object getContents()
• When you set the content of the object, you may want to store the object in the persistent store. This is done by invoking commit().
• Commit() method writes the contents of the persistent object to flash memory (persistent memory)
15http://cmer.cis.uoguelph.ca
Steps for Storing Data in Persistent Store
1. Create a unique key
2. Retrieve a persistentObject from the persistent store by the key
3. Create, change or update the object’s value. The object can be any data type such as String, int, String[], …
4. Set the contents of the persistentObject to the object you have in step 3
5. Commit the persistentObject to the Persistent Store
16http://cmer.cis.uoguelph.ca
Example- Create a Persistent Object
static {
//Create a long key – Key: CMER.lab1.persistentAPI
long dbkey = 0x6b5fddcbb010cf0cL;
store = PersistentStore.getPersistentObject(dbkey);
synchronized (store) {
if (store.getContents() == null) {
store.setContents(new Vector());
store.commit();
}
}
table = new Vector();
table = (Vector) store.getContents();
}
17http://cmer.cis.uoguelph.ca
Example- Store a Persistent Object
private static Vector table;
private static PersistentObject store;
……
private MenuItem _save = new MenuItem("Save", 110, 10) {
public void run() {
table.addElement(favor);
synchronized (store) {
store.setContents(table);
store.commit();
}
}
18http://cmer.cis.uoguelph.ca
Example- Retrieve a Persistent Object
private static Vector table;
private static PersistentObject store;
……
private MenuItem _retrieve = new MenuItem("Retrieve", 110, 11) {
public void run() {
synchronized (store) {
table = (Vector) store.getContents();
}
}
};
19http://cmer.cis.uoguelph.ca
Transactions in Persistent Storage Model
• There are two common ways to commit objects to the persistent store:– Single transaction– Batches of transactions
• In single transaction, commit() and forceCommit()
are used to commit a single object to the persistent store.
20http://cmer.cis.uoguelph.ca
Transactions in Persistent Storage Model (Cont.)
• These two methods do the same thing. However, forceCommit() commits the object immediately to the persistent store.
• Example:synchronized (store) {
store.setContents(table);
store.commit();
}
21http://cmer.cis.uoguelph.ca
Batches of Transaction
• A batch transaction commits objects to the persistent store.
• To do this:– Invoke PersistentStore.getSyncObject()
• This method retrieves the persistent store monitor to lock the object.
– Synchronize on the object– Invoke commit() as necessary.
22http://cmer.cis.uoguelph.ca
Example- Batches of Transaction
synchronized (PersistentStore.getSynchObject()) {
…..
store.commit();
}
23http://cmer.cis.uoguelph.ca
Delete a Database
• To delete a database, invoke – PersistentStore.destroyPersistentObject(long key)
• It removes the persistent object from the store by key.
• By deleting the PersistentObject, you permanently remove all persistent data that your application has stored.
24http://cmer.cis.uoguelph.ca
PART 2 – RMS (RECORD MANAGEMENT SYSTEM)
25http://cmer.cis.uoguelph.ca
Record Management System
• MIDP provides a mechanism for Midlets to persistently store data and retrieve it later to/from small device.
• This is called Record Management System (RMS)
• It is a Java ME package. However, BlackBerry devices support RMS. Although, it is limited in capabilities
26http://cmer.cis.uoguelph.ca
RMS (Cont.)
• RMS library package is javax.microedition.rms• A Record Store is a collection of persistent
records• Each Record Store has a unique name which is
case-sensitive and between 1-32 unicode characters
Record ID 0
Record ID 1
RecordStore
27http://cmer.cis.uoguelph.ca
Record
• A Record is an array of Bytes• Each record in a record store can have a
different length and different types• Each record has a unique identifier called
recordID– It is used to retrieve a record from record store– It is assigned automatically by an increasing-by-
one algorithm
28http://cmer.cis.uoguelph.ca
Methods
• public static RecordStore openRecordStore( String recordStoreName , boolean createIfNecessary) throws RecordStoreException, RecordStoreFullException, RecordStoreNotFoundException
Opens a record store and returns a RecordStore object. For any access to record store, you should call it first
29http://cmer.cis.uoguelph.ca
Methods (Cont.)
• public String getName() throws RecordStoreNotOpenException
Returns the name of the record store opened or created
• public int getNumRecords() throws RecordStoreNotOpenException
Returns the number of records currently in the record store.
30http://cmer.cis.uoguelph.ca
Methods (Cont.)
• public static void deleteRecordStore(String recordStoreName)
throws RecordStoreException, RecordStoreNotFoundException
Deletes the named record store. MIDlet suites are only allowed to delete their own record stores. A record must be closed before it can be deleted.
31http://cmer.cis.uoguelph.ca
Methods (Cont.)
• public void closeRecordStore() throws RecordStoreNotOpenException, RecordStoreException
This method is called when the MIDlet requests to close the record store. Note that the record store will be closed since closeRecordStore() is called as many times as openRecordStore() is called
32http://cmer.cis.uoguelph.ca
Sharing Record Store
• Midlets within a MIDlet suit can share their record store.
• If multiple midlet within a MIDlet suit want to access a record store, the synchronization of the access operation must be implemented.
• In MIDP 2.0, Midlets whitin a midlet suit can access to the record stores of the other midlet suits on the device. In MIDP 1.0, this capability is not supported.
33http://cmer.cis.uoguelph.ca
Sharing Record Store (Cont.)
• To share a record store, you should set options when you create a record store using openRecordStore( )– authmod: it can be AUTHMODE_PRIVATE meaning
that only midlet suit that created the record store can access to it OR AUTHMODE_ANY allows any midlet to access the record store
– writable: also , you can identify the type of access whether it is read-only or it allows others to write to it.
34http://cmer.cis.uoguelph.ca
Manipulating Records
• public int addRecord(byte[] data, int offset, int numBytes) throws RecordStoreNotOpenException, RecordStoreException, RecordStoreFullExceptionAdds a new record to the record store. The recordID for this new
record is returned.
data - the data to be stored in this record.
offset - the index into the data buffer of the first relevant byte for this record
numBytes - the number of bytes of the data buffer used for this record (may be zero)
35http://cmer.cis.uoguelph.ca
Manipulating Records (Cont.)
• public void deleteRecord(int recordId) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException
To delete a record from the record store
• public byte[] getRecord(int recordId) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException
Returns a record
36http://cmer.cis.uoguelph.ca
Manipulating Records (Cont.)
• public int getRecord(int recordId, byte[] buffer, int offset) throws RecordStoreException, RecordStoreNotOpenException, InvalidRecordIDException, Returns the record in the given record store,
recordId - the ID of the record to be retrieved
buffer - the byte array in which to copy the data
offset - the index into the buffer in which to start copying
37http://cmer.cis.uoguelph.ca
Manipulating Records (Cont.)
• public void setRecord(int recordId, byte[] newData, int offset, int numBytes) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, RecordStoreFullException Override the record identified by recordId
38http://cmer.cis.uoguelph.ca
Example- Open & Close a Record Store
try{
// To open a record store
RecordStore rs = RecordStore.openRecordStore(recordStore_name,true);
….
//Close a record store
rs.closeRecordStore();
}catch(Exception e)
{
System.err.println(e.toString());
}
39http://cmer.cis.uoguelph.ca
Example- Add & Delete a Record
byte[] rec = str.getBytes();
try {
//Add a new record
rs.addRecord(rec, 0, rec.length);
….
//Delete a record
rs.deleteRecord(recId);
}catch (Exception e)
{
System.err.println(e.toString());
}
40http://cmer.cis.uoguelph.ca
Example- Retrieve & Update a Record
byte[] recData= null;
try {
//Retrieve a record from the record store
recData = rs.getRecord(recId);
String retrievedRecord = new String(recData);
…..
//Update a record in the record store
rs.setRecord(recordId, str.getBytes(), 0, str.length());
} catch (Exception e) {
System.err.println(e.toString());
return null;
}
41http://cmer.cis.uoguelph.ca
Example- Delete a Record Store
if (RecordStore.listRecordStores() != null)
{
try {
//Delete a record store
RecordStore.deleteRecordStore(recordStore_name);
} catch (Exception e) {
System.err.println(e.toString());
}
42http://cmer.cis.uoguelph.ca
Filtering, Comparing, Listening and Enumerating Records
• RMS provides 4 Java interfaces used to filter, search and sort a record store:– RecordFilter Interface– RecordComparator Interface– RecordListener Interface– RecordEnumerator Interface
43http://cmer.cis.uoguelph.ca
RecordFilter Interface
• Allows to define filter for searching records It has the following method:
public boolean matches(byte[] record)
Returns true if the candidate matches the user-defined search criteria.
record - the record to consider.
44http://cmer.cis.uoguelph.ca
Example- RecordFilter
public class SearchFilter implements RecordFilter {
private String searchText = null;
public SearchFilter(String searchText) {
this.searchText = searchText.toLowerCase(); // This is the text to search for
}
public boolean matches(byte[] candidate) {
String str = new String(candidate).toLowerCase(); // Look for a match
if (searchText != null && str.indexOf(searchText) != -1)
return true;
else
return false;
}
45http://cmer.cis.uoguelph.ca
RecordComparator Interface
• It is used to compare two records to check if they match
public int compare(byte[] rec1, byte[] rec2)
rec1- the first record to use for comparison.
rec2 - the second record to use for comparison.
Return value:
EQUIVALENT: two records are the same in terms of the search
FOLLOWS: the first record follows the second record in terms of search or search order
PRECEDES: the first record precedes the right record in terms of search or sort order.
46http://cmer.cis.uoguelph.ca
Example- RecordComparator
public class SortCompare implements RecordComparator {
public int compare(byte[] rec1, byte[] rec2){
String str1 = new String(rec1);
String str2 = new String(rec2);
int result = str1.compareTo(str2);
if (result == 0)
return RecordComparator.EQUIVALENT;
else if (result < 0)
return RecordComparator.PRECEDES;
else
return RecordComparator.FOLLOWS;
}
}
47http://cmer.cis.uoguelph.ca
RecordListener Interface
• It is used to monitor the manipulating records such as add, delete, …– public void recordAdded(RecordStore recordStore,
int recordId)
it is called when a record is added to the record store– public void recordChanged(RecordStore recordStore,
int recordId)
it is called after a record in a record store has been changed.
48http://cmer.cis.uoguelph.ca
RecordListener Interface (Cont.)
– public void recordDeleted( RecordStore recordStore, int recordId)
it is called after a record has been deleted from a record store. If the implementation of this method tries to retrieve the record from the record store, an InvalidRecordIDException will be thrown
49http://cmer.cis.uoguelph.ca
Example- RecordListener
public class Lab10RecordListner implements RecordListener{
public void recordAdded(RecordStore recordStore, int recordId) {
try {
System.out.println("Record with ID#: " + recordId +
" added to RecordStore: " + recordStore.getName());
} catch (Exception e) { System.err.println(e); }
}
public void recordDeleted(RecordStore recordStore, int recordId) {
try {
System.out.println("Record with ID#: " + recordId +
" deleted from RecordStore: " + recordStore.getName());
} catch (Exception e) { System.err.println(e); }
}
public void recordChanged(RecordStore recordStore, int recordId) {}
}
50http://cmer.cis.uoguelph.ca
RecordEnumerator Interface
• It provides a method to return an enumeration for traversing a set of records in the record store
• public RecordEnumeration enumerateRecords( RecordFilter filter, RecordComparator comparator, boolean keepUpdated) throws RecordStoreNotOpenException
filter :
- if non-null, it determines what subset of the record store records will be used
Comparator:
- if non-null, it will be used to determine the order in which the records are returned
51http://cmer.cis.uoguelph.ca
RecordEnumerator Interface (Cont.)
keepUpdated
- if true, the enumerator will keep its enumeration current with any changes in the records of the record store.
- If false the enumeration will not be kept current and may return recordIds for records that have been deleted or miss records that are added later.
52http://cmer.cis.uoguelph.ca
Example- Sort
• Sort using Record Enumerator public void sortingReadRMS(){
try{
if (rs.getNumRecords() > 0){
SortCompare comp = new SortCompare();
RecordEnumeration re=rs.enumerateRecords(null, comp, false);
list1.deleteAll();
while (re.hasNextElement()){
String str = new String(re.nextRecord());
list1.append(str, null);
}
}
} catch (Exception e) {
System.err.println(e.toString());
}
}
top related