Scmad Chapter08

1,036 views

Published on

Chapter 08 - RMS . This material is only for study purposes.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,036
On SlideShare
0
From Embeds
0
Number of Embeds
36
Actions
Shares
0
Downloads
61
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Scmad Chapter08

  1. 1. By Marcel Caraciolo http://mobideia.blogspot.com Chapter 08– Record Management System (RMS) SCMAD Certification 45mm 61mm
  2. 2. Agenda <ul><li>MIDP - RMS </li></ul><ul><ul><li>Record Management System </li></ul></ul><ul><ul><li>Record Store </li></ul></ul><ul><ul><li>Record Listener </li></ul></ul><ul><ul><li>RecordEnumeration </li></ul></ul><ul><ul><li>RecordComparator </li></ul></ul><ul><ul><li>RecordFilter </li></ul></ul><ul><ul><li>Exceptions </li></ul></ul>
  3. 3. Record Management System <ul><li>Data may be saved to non-volatile memory on the device. This data is no lost when the application is finished or when the device is restarted. Device shall perform a best-effort service to maintain data durability. </li></ul><ul><li>RMS is a record-oriented system (byte arrays). A record store keeps a collection of records. </li></ul><ul><li>An id is created and linked to every record added to the database (record “ ids ” starts at 1). </li></ul>
  4. 4. Record Management System
  5. 5. Record Management System <ul><li>Record Stores may be shared between MIDlets and MIDlet suites, and they belong to one midlet suite. Every record store has a name (1 to 32 characters string). The Record Store belongs to the MIDlet Suite. Two records stores may have the same name as long as they belong to different suites. A suite may only remove its own record stores. </li></ul><ul><li>Record Stores must be opened before being used. Several calls to “open” may be done, and all point to the same record store. When a record store is not going to be used anymore, it shall be closed. To effectively close a record store we need to perform “close” the same number of opens we did. </li></ul><ul><li>If a Record Store belongs to the suite, we jus need its name to open it. If it belongs to other suite, we need its name, the suite name and the suite’s vendor name. </li></ul>
  6. 6. Record Management System <ul><li>Data Serialization shall be performed by the application. To convert data to byte arrays and vice-versa, the following classes may be used: </li></ul><ul><ul><li>DataArrayOutputStream </li></ul></ul><ul><ul><li>ByteArrayInputStream </li></ul></ul><ul><ul><li>DataOutputStream </li></ul></ul><ul><ul><li>DataInputStream </li></ul></ul><ul><li>Data may be extracted from the database with RecordEnumeration, RecordComparator and RecordFilter. These classes have features similar to database SELECT statement. RecordEnumerations stands for a cursor, RecordComparator is useful for sorting and RecordFilter is useful for filtering. </li></ul>
  7. 7. Record Management System <ul><li>A listener may be added to a Record Store so that database events (add, deletes, updates) are informed. </li></ul><ul><li>There’s no API to lock records. Accesses are serialized (and hence atomic and isolated). Concurrent operations will not corrupt the database: When two threads call setRecord over the same record at the same time, the second record overwrites the first. </li></ul><ul><li>When a suite is uninstalled, all its records are removed. </li></ul><ul><li>Classes at javax.microedition.rms package. </li></ul>
  8. 8. Record Store <ul><li>Both “Record Store” and “factory” </li></ul><ul><li>Static methods: </li></ul><ul><ul><li>openRecordStore(recordStoreName, createIfNecessary): Opens the record store. If it does not exist and createIfNecessary is true, then a record store is created. If it does not exist and createIfNecessary is false, an exception is thrown. </li></ul></ul><ul><ul><li>deteleRecordStore(recordStoreName): Deletes a record store. It shall be closed before being removed. </li></ul></ul>
  9. 9. Record Store <ul><li>Static methods (Continuation): </li></ul><ul><ul><li>openRecordStore(recordStoreName,createIfNecessary,authMode,writable): Opens a shared record store. Authmode is either AUTHMODE_PRIVATE or AUTHMODE_ANY, which allows sharing. If “writable” is true the other suites may write to this record store. </li></ul></ul><ul><ul><li>openRecordStore(recordStoreName, vendorName, suiteName): Opens a record store created by other midlet suite. </li></ul></ul><ul><ul><li>listRecordStores(): Returns a string array with the names of all record stores that belong to this suite. </li></ul></ul>
  10. 10. Record Store <ul><li>Static methods (Continuation): </li></ul><ul><ul><li>openRecordStore(recordStoreName,createIfNecessary,authMode,writable): Opens a shared record store. Authmode is either AUTHMODE_PRIVATE or AUTHMODE_ANY, which allows sharing. If “writable” is true the other suites may write to this record store. </li></ul></ul><ul><ul><li>openRecordStore(recordStoreName, vendorName, suiteName): Opens a record store created by other midlet suite. </li></ul></ul><ul><ul><li>listRecordStores(): Returns a string array with the names of all record stores that belong to this suite. </li></ul></ul>
  11. 11. Record Store: Instance methods <ul><li>getName(): Record Store Name </li></ul><ul><li>closeRecordStore(): Closes a record store. This must be called the same number of times that open was called to effectively close . When the record store is closed, all its listeners are removed and all its enumerations are invalidated. </li></ul><ul><li>setMode(authMode,writable): Changes the sharing permissions </li></ul><ul><li>addRecord(data[],offset,numBytes): Adds a new record to the database. The created index is returned. This operations is synchronized, blocking, atomic and durable. </li></ul><ul><li>getRecordSize(recordId): Size (in bytes) of a record </li></ul><ul><li>getRecord(recordId): Returns a byte array with the record contents. </li></ul>
  12. 12. Record Store: Instance methods <ul><li>getRecord(recordId,buffer[],offset): Reads the record and saves its content on the array passed as parameter (this may be more memory-efficient than calling getRecord( recordId), because it will not create a new byte array for every call). </li></ul><ul><li>enumerateRecords(filter,comparator,keepUpdated): Gets a set of records that are approved by the filter, sorted by comparator. If filter is null, all records are returned. If comparator is null, no sorting is defined. If keepUpdated is true, changes to the database are propagated to the enumeration. There is a performance impact when this option is used. </li></ul><ul><li>setRecord(recordId, newData[], offset, numBytes): Overwrites the informed record with the new data. </li></ul>
  13. 13. Record Store: Instance methods <ul><li>deleteRecord(recordId): Removes a record. This id will not be reused. (you can’t call setRecord with this ID again). </li></ul><ul><li>getNumRecords(): Returns the number of records on the database </li></ul><ul><li>getSize(): Returns how many bytes are used by the database. Includes not only the space consumed by the records, but also the overhead of handling the records. </li></ul><ul><li>getSizeAvailable(): Size (in bytes) available to persistence. This values does note represent exactly the amount available to new records, since we must also consider the overhead of handling records. </li></ul>
  14. 14. Record Store: Instance methods <ul><li>getNextRecordID(): Returns the ID that will be used for the next addRecord </li></ul><ul><li>getVersion(): When the database is changed (by addRecord, deleteRecord or setRecord) this value is incremented. This can be used to check if the database was modified. </li></ul><ul><li>getLastModified(): Time when the last modification happened to the database, on System.currentTimeInMilis() format. </li></ul><ul><li>addRecordListener(listener): Registers a record listener </li></ul><ul><li>removeRecordListener(listener): Removes a record listener </li></ul>
  15. 15. RecordListener <ul><li>Defines an object that will receive database change notifications </li></ul><ul><li>Methods: </li></ul><ul><ul><li>recordAdded(recordStore, recordID): A record with the informed id was added to the record store </li></ul></ul><ul><ul><li>recordChanged(recordStore,recordID): A record with the informed id was modified on the record store. This is called after the modification was happened. </li></ul></ul><ul><ul><li>recordDeleted(recordStore,recordID): A record with the informed id was removed from the record store. This is called after the removal has happened. </li></ul></ul>
  16. 16. RecordEnumeration <ul><li>Iterator over a record collection from a record store. Iterates forward and backwards. </li></ul><ul><li>Records may be filtered and sorted, according to how this enumeration was created. </li></ul><ul><li>It may be linked to the record store so that database modifcations are reflected on this enumeration. This impacts performance. </li></ul><ul><li>If right after its creation, the first method called is previousRecord or previousRecordID, the first item to be returned is the last of this list. This can be useful for iterating from the last to the first item. </li></ul>
  17. 17. RecordEnumeration: Methods <ul><li>numRecords(): Calculates how many records are available on this enumeration. This enumeration may not be implemented as an in-memory collection, so calling this method may be a costly operation since it may force loading all data from persistence. </li></ul><ul><li>nextRecord(): Returns data from the next record. Call to this method to move the iterator. If it’s called at the end of the enumeration an exception is thrown. </li></ul><ul><li>nextRecordId(): Returns the record id of the next record. Calls to this method move the iterator. If it’s called at the end of the enumeration, an exception is thrown. </li></ul>
  18. 18. RecordEnumeration: Methods <ul><li>previousRecord(): Returns data from the previous record. Calls to this method move the iterator. If it’s called at the beginning of the enumeration, an exception is thrown. </li></ul><ul><li>previousRecordId(): Returns the id from the previous record. Calls to this method move the iterator. If it’s called at the beginning of the enumeration, an exception is thrown. </li></ul><ul><li>reset(): Returns the enumeration to the state it was created </li></ul><ul><li>rebuild(): Rebuilds the enumeration. Similar to call enumerateRecords again. </li></ul>
  19. 19. RecordEnumeration: Methods <ul><li>hasNextElement(), hasPreviousElement(): Returns true If there are more records on the direction </li></ul><ul><li>keepUpdated(booleankeepUpdated): Changes the update policy </li></ul><ul><li>isKeepUpdated(): Returns true if this is a database synchronized enumeration. </li></ul><ul><li>detroy(): Destroys the enumeration and frees all the resources it consumed. </li></ul>
  20. 20. RecordComparator <ul><li>Defines comparators between records. Similar to java.util’s Comparator. Used when sorting records when a RecordEnumeration is created. </li></ul><ul><li>Method: compare(rec1[], rec2[]) : Compare both elements. Shall returns PRECEDES if rec1 precedes rec2, FOLLOWS if rec1 follows rec2, and EQUIVALENT if the records have the same priority. </li></ul>
  21. 21. RecordFilter <ul><li>Filter elements when a RecordEnumeration is created </li></ul><ul><li>Method: matches(candidate[]) : Shall return true if the record shall be included on the enumeration. </li></ul>
  22. 22. Exceptions <ul><li>RecordStoreException: Generic database errors. All RMS exceptions extend this one. </li></ul><ul><li>RecordStoreFullException: No more space </li></ul><ul><li>RecordStoreNotFoundException: Invalid database name </li></ul><ul><li>RecordStoreNotOpenException: Record Store is closed </li></ul><ul><li>Invalid RecordIDException: Operation performed over an invalid record id. </li></ul>
  23. 23. Example Codes <ul><li>Some examples and MIDlets samples are available for reference and studying at this link: </li></ul><ul><ul><li>http://www.dsc.upe.br/~mpc/chapter8.rar </li></ul></ul><ul><li>The source codes include: </li></ul><ul><ul><li>RMSMIDlet </li></ul></ul>
  24. 24. Future Work <ul><li>Next Chapter: </li></ul><ul><ul><li>CLDC - GCF </li></ul></ul><ul><ul><ul><li>Generic Connection Framework </li></ul></ul></ul><ul><ul><ul><li>Connector </li></ul></ul></ul><ul><ul><ul><li>Connection, InputConnection, OutputConnection </li></ul></ul></ul><ul><ul><ul><li>StreamConnection, ContentConnection </li></ul></ul></ul><ul><ul><ul><li>DatagramConnection </li></ul></ul></ul><ul><ul><ul><li>Datagram </li></ul></ul></ul><ul><ul><ul><li>StreamConnectionNotifier </li></ul></ul></ul><ul><ul><ul><li>Exceptions </li></ul></ul></ul>
  25. 25. References <ul><li>ALVES F. Eduardo. SCMAD Study Guide, </li></ul><ul><li>27/04/2008. </li></ul><ul><li>JAKL Andreas, Java Platform, Micro Edition Part </li></ul><ul><li>01 slides, 12/2007. </li></ul><ul><li>Sun Certification Mobile Application Developer </li></ul><ul><li>Website: [http://www.sun.com/training/certification/java/scmad.xml]. </li></ul>

×