Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 37

i hc Khoa hc T nhin HQG-HCM Khoa Cng Ngh Thng Tin

Record Management System (RMS)


ThS. Trn Minh Trit

Ni dung
Lu tr d liu ti server t xa Lu tr cc b vi Record Management System Phm vi qun l ca Record Management Qun l Record Store nh ngha, nh dng, thm vo Record Stores Truy cp v xa trong Record Store Truy vn trn Record Store

Lu tr t xa
C th s dng lu tr t xa (remote storage) trn server u: Kch thc ng dng nh Ch lu nhng d liu cn thit Ni dung ng Thng dng trong lp trnh game Khuyt: Khng ng tin cy Tn thi gian v chi ph Chi ph server

Record Store?
Trong MIDP, vic lu tr s dng record store Mt record store l 1 tp hp cc record Vic t chc lu tr tht s do ci t ca MIDP Record store c th cha trn l hard disk, batterybacked RAM, flash ROM Mi record l 1 mng cc byte v c s nh danh duy nht Ni dung ca mng byte do ng dng t quy nh Cn dng gi javax.microedition.rms

RMS
MIDP lu tr thng tin bng cch dng h thng lu tr (Record Management System - RMS). RMS cho php c, ghi d liu, sp xp, tm kim v ly thng tin theo nhiu ty chn khc nhau

Record Store trong MIDP

D liu trong Record Store


Record l mng byte c nh dng theo nh ring

Cc record trong 1 record store c t chc l mng cc byte Cc mng byte khng cn cng di Mi record c 1 nh danh (ID) ring

Record c th khng lin tip v 1 s record b xa


7

Qun l Record trong MIDP


Mi Record store u c tn v c phn bit thng qua tn Tn gm ti a 32 k t (case sensitive) Hai record store trong cng 1 MIDlet suite khng c trng tn

Qun l RecordStore
S dng phng thc static m 1 record store: public static RecordStore openRecordStore( String recordStoreName, boolean createIfNecessary) throws RecordStoreException, RecordStoreFullException, RecordStoreNotFoundException ng recordstore bng phng thc: closeRecordStore() Xa record store bng phng thc static deleteRecordStore() Ly danh sch Recordstore bng phng thc static listRecordStores()
9

To record store
RecordStore openRecordStore( String recordStoreName, boolean createIfNecessary) RecordStore openRecordStore( String recordStoreName, boolean createIfNecessary, int authmode, boolean writable) RecordStore openRecordStore( String recordStoreName, String vendorName, String suiteName)

10

To record store
createIfNecessary yu cu to record store nu cha c) authmode: AUTHMODE_PRIVATE AUTHMODE_ANY writable - true nu RecordStore cho php cc MIDlet suite khc c th ghi (nu cp quyn) vendorName tn NSX ca MIDlet suite suiteName - tnMIDlet suite

11

RecordStore API
void closeRecordStore() void deleteRecordStore(String recordStoreName) String[] listRecordStores() int addRecord(byte[] data, int offset, int numBytes) void setRecord (int recordId, byte[] newData, int offset, int numBytes) void deleteRecord (int recordId)

12

Truy cp 1 record store


byte[] getRecord (int recordId) int getRecord (int recordId, byte[] buffer, int offset) int getRecordSize (int recordId) int getNextRecordID() int getNumRecords() long getLastModified()

13

Thao tc c v ghi
Cp pht stream Ghi d liu Flush d liu trong stream Chuyn stream d liu thnh mng Ghi mng vo record store ng cc stream

14

Thao tc c v ghi (#1)


public void writeStream( boolean[] bData, int[] iData, String[] sData) { // Write data into an internal byte array ByteArrayOutputStream strmBytes = new ByteArrayOutputStream(); // Write Java data types into the above byte array DataOutputStream strmDataType = new DataOutputStream(strmBytes);

15

Thao tc c v ghi (#2)


byte[] record; for (int i = 0; i < sData.length; i++) { // Write Java data types strmDataType.writeBoolean(bData[i]); strmDataType.writeInt(iData[i]); strmDataType.writeUTF(sData[i]); // Clear any buffered data strmDataType.flush(); // Get stream data into byte array and write record record = strmBytes.toByteArray(); rs.addRecord(record, 0, record.length); // Toss any data in the internal array so writes // starts at beginning (of the internal array) strmBytes.reset(); }

16

Thao tc c v ghi (#3)


strmBytes.close(); strmDataType.close();

17

nh dng v thm Record


Trc khi thm record, cn nh dng record

18

Ly Record
Ly record public byte[] getRecord(int recordId) throws RecordStoreException, RecordStoreNotOpenException, InvalidRecordIDException

19

Xa Record
Khi xa Record, cn truyn record ID# vo hm deleteRecord() ca i tng RecordStore. record ID# b xa s khng c dng li

20

Xa Record

21

Truy vn Record
Interface RecordFilter cho php lc cc record theo cc tiu ch khc nhau. Phng thc matches() c dng nh ngha iu kin lc C th dng RecordEnumeration duyt tun t qua cc record kim tra record no tha iu kin lc. Interface RecordComparator cung cp phng thc compare() cho php nh ngha cch so snh 2 record khi sp xp

22

Lc Record
Interface RecordFilter cho php lc cc record theo cc tiu ch khc nhau. Phng thc matches() c dng nh ngha iu kin lc

23

RecordFilter API

class SearchFilter implements RecordFilter { private String searchText = null; public SearchFilter(String searchText) { // Text to find this.searchText = searchText.toLowerCase(); } public boolean matches(byte[] candidate) { String str = new String(candidate).toLowerCase(); // Does the text exist? if (searchText != null&&str.indexOf(searchText)!=-1) { return true; } else { return false; } } } 24

RecordFilter API
// Create search filter SearchFilter search = new SearchFilter("abc"); // Reference filter when creating the result set RecordEnumeration re = rs.enumerateRecords (search, null, false); // If there is at least one record in result set, // a match was found if (re.numRecords() > 0) { // At least one record in the result set... ... }

25

Sp xp Record
sp xp cn dng interface RecordComparator. Cn ci t phng thc compare() so snh 2 record

26

RecordComparator API
int compare(byte[] rec1, byte[] rec2) static int EQUIVALENT static int FOLLOWS static int PRECEDES

27

RecordComparator API
public class comparator implements RecordComparator { public int compare(byte[] rec1, byte[] rec2) { String str1 = new String(rec1), 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; } } }

28

RecordComparator API
// Create a new comparator for sorting Comparator comp = new Comparator(); // Reference the comparator when creating the result set RecordEnumeration re = rs.enumerateRecords (null, comp, false); // Retrieve each record in sorted order while (re.hasNextElement()) { String str = new String(re.nextRecord()); ... }
29

Duyt Record
C th duyt qua RecordEnumeration cc record bng interface

Xc nh record no c chn Th t sp xp

Tr v cc record c chn theo th t quy nh Tr v tt c record (khng sp th t)


30

RecordEnumeration API
int numRecords() byte[] nextRecord() int nextRecordId() byte[] previousRecord() int previousRecordId() boolean hasNextElement()

31

RecordEnumeration API
boolean hasPreviousElement() void keepUpdated(boolean keepUpdated) boolean isKeptUpdated() void rebuild() void reset() void destroy()

32

Pht hin s thay i ca Record


C th ng k event listener cho 1 RecordStore S dng phng thc addRecordListener() v removeRecordListener() RecordListener c 3 hm callback: public void recordAdded(RecordStore rs, int id) public void recordChanged(RecordStore rs, int id) public void recordDeleted(RecordStore rs, int id)

33

RecordListener API
RecordListener cho php nhn x l s kin khi c s thay i, thm, xa trong record store. Cc bc s dng RecordListener: M (hay to)record store. To 1 listener mi Ci t cc phng thc ca interface RecordListener

34

RecordListener API
void recordAdded( RecordStore recordStore, int recordId) void recordChanged( RecordStore recordStore, int recordId) void recordDeleted( RecordStore recordStore, int recordId)

35

To 1 record store
// Open record store rs = RecordStore.openRecordStore(REC_STORE, true); ... // Using handle to open record store, create a listener rs.addRecordListener(new DemoRecordListener()); ... class DemoRecordListener implements RecordListener { public void recordAdded(RecordStore recordStore, int recordId) { System.out.println("Record added"); } public void recordDeleted(RecordStore recordStore, int recordId) { System.out.println("Record deleted"); } public void recordChanged(RecordStore recordStore, int recordId) { System.out.println("Record changed"); } }

36

S dng file Resource


C th s dng lu tr trong MIDP: c cc file resource trong file JAR ca MIDlet suite Dng phng thc getResourceAsStream().

37

You might also like