Professional Documents
Culture Documents
Lap Trinh Java05-RMS
Lap Trinh Java05-RMS
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
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
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
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
15
16
17
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
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
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
37