blackberry persistent storage models

52
1 BlackBerry Persistent Storage Models Persistent Storage APIs and Record Management System

Upload: carly-walls

Post on 31-Dec-2015

42 views

Category:

Documents


0 download

DESCRIPTION

BlackBerry Persistent Storage Models. Persistent Storage APIs and Record Management System. Overview. Part 1- Persistent Storage APIs Part 2- MIDP Record Store (RMS). PART 1 – Persistent storage APIs. PersistentStore class. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: BlackBerry Persistent Storage Models

1

BlackBerry Persistent Storage Models

Persistent Storage APIs

and

Record Management System

Page 2: BlackBerry Persistent Storage Models

2http://cmer.cis.uoguelph.ca

Overview

• Part 1-– Persistent Storage APIs

• Part 2- – MIDP Record Store (RMS)

Page 3: BlackBerry Persistent Storage Models

3http://cmer.cis.uoguelph.ca

PART 1 – PERSISTENT STORAGE APIS

Page 4: BlackBerry Persistent Storage Models

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.

Page 5: BlackBerry Persistent Storage Models

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 );

}

Page 6: BlackBerry Persistent Storage Models

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.

Page 7: BlackBerry Persistent Storage Models

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.

Page 8: BlackBerry Persistent Storage Models

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

Page 9: BlackBerry Persistent Storage Models

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;

}

……

}

Page 10: BlackBerry Persistent Storage Models

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.

Page 11: BlackBerry Persistent Storage Models

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.

Page 12: BlackBerry Persistent Storage Models

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”

Page 13: BlackBerry Persistent Storage Models

13http://cmer.cis.uoguelph.ca

PersistentObject class (Cont.)

Page 14: BlackBerry Persistent Storage Models

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)

Page 15: BlackBerry Persistent Storage Models

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

Page 16: BlackBerry Persistent Storage Models

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();

}

Page 17: BlackBerry Persistent Storage Models

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();

}

}

Page 18: BlackBerry Persistent Storage Models

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();

}

}

};

Page 19: BlackBerry Persistent Storage Models

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.

Page 20: BlackBerry Persistent Storage Models

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();

}

Page 21: BlackBerry Persistent Storage Models

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.

Page 22: BlackBerry Persistent Storage Models

22http://cmer.cis.uoguelph.ca

Example- Batches of Transaction

synchronized (PersistentStore.getSynchObject()) {

…..

store.commit();

}

Page 23: BlackBerry Persistent Storage Models

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.

Page 24: BlackBerry Persistent Storage Models

24http://cmer.cis.uoguelph.ca

PART 2 – RMS (RECORD MANAGEMENT SYSTEM)

Page 25: BlackBerry Persistent Storage Models

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

Page 26: BlackBerry Persistent Storage Models

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

Page 27: BlackBerry Persistent Storage Models

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

Page 28: BlackBerry Persistent Storage Models

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

Page 29: BlackBerry Persistent Storage Models

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.

Page 30: BlackBerry Persistent Storage Models

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.

Page 31: BlackBerry Persistent Storage Models

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

Page 32: BlackBerry Persistent Storage Models

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.

Page 33: BlackBerry Persistent Storage Models

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.

Page 34: BlackBerry Persistent Storage Models

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)

Page 35: BlackBerry Persistent Storage Models

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

Page 36: BlackBerry Persistent Storage Models

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

Page 37: BlackBerry Persistent Storage Models

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

Page 38: BlackBerry Persistent Storage Models

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());

}

 

Page 39: BlackBerry Persistent Storage Models

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());

  }

Page 40: BlackBerry Persistent Storage Models

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;

}

Page 41: BlackBerry Persistent Storage Models

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());

  }

Page 42: BlackBerry Persistent Storage Models

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

Page 43: BlackBerry Persistent Storage Models

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.

Page 44: BlackBerry Persistent Storage Models

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;

}

Page 45: BlackBerry Persistent Storage Models

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.

 

Page 46: BlackBerry Persistent Storage Models

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;

}

}

Page 47: BlackBerry Persistent Storage Models

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.

Page 48: BlackBerry Persistent Storage Models

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

Page 49: BlackBerry Persistent Storage Models

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) {}

}

Page 50: BlackBerry Persistent Storage Models

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

Page 51: BlackBerry Persistent Storage Models

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.

Page 52: BlackBerry Persistent Storage Models

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());

}

}