Lap Trinh Java05-RMS

Embed Size (px)

Citation preview

  • 8/3/2019 Lap Trinh Java05-RMS

    1/37

    1

    Record Management System

    (RMS)ThS. Trn Minh Trit

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

  • 8/3/2019 Lap Trinh Java05-RMS

    2/37

    2

    Ni dung

    Lu tr d liu ti server t xaLu 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 StoresTruy cp v xa trong Record Store

    Truy vn trn Record Store

  • 8/3/2019 Lap Trinh Java05-RMS

    3/37

    3

    Lu tr t xa

    C th s dng lu tr t xa (remote storage) trn serveru:

    Kch thc ng dng nh

    Ch lu nhng d liu cn thit

    Ni dung ngThng dng trong lp trnh game

    Khuyt:

    Khng ng tin cy

    Tn thi gian v chi ph

    Chi ph server

  • 8/3/2019 Lap Trinh Java05-RMS

    4/37

    4

    Record Store?

    Trong MIDP, vic lu tr s dng record storeMt 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, battery-

    backed RAM, flash ROMMi record l 1 mng cc byte v c s nh danh duynht

    Ni dung ca mng byte do ng dng t quy nh

    Cn dng gijavax.microedition.rms

  • 8/3/2019 Lap Trinh Java05-RMS

    5/37

    5

    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 lythng tin theo nhiu ty chn khc nhau

  • 8/3/2019 Lap Trinh Java05-RMS

    6/37

    6

    Record Store trong MIDP

  • 8/3/2019 Lap Trinh Java05-RMS

    7/37

    7

    D liu trong Record Store

    Cc record trong 1record store ct chc l mngcc byte

    Cc mng byte

    khng cn cng di

    Mi record c 1nh danh (ID)ring

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

    Record l mng byte c nh dng theo nh ring

  • 8/3/2019 Lap Trinh Java05-RMS

    8/37

    8

    Qun l Record trong MIDP

    Mi Record store u c tn v c phn bit thng quatn

    Tn gm ti a 32 k t (case sensitive)

    Hai record store trong cng 1 MIDlet suite khng c

    trng tn

  • 8/3/2019 Lap Trinh Java05-RMS

    9/37

    9

    Qun l RecordStore

    S dng phng thc static m 1 record store:publicstaticRecordStoreopenRecordStore(

    String recordStoreName,

    boolean createIfNecessary)

    throwsRecordStoreException,RecordStoreFullException,RecordStoreNotFoundException

    ng recordstore bng phng thc:

    closeRecordStore()Xa record store bng phng thc staticdeleteRecordStore()

    Ly danh sch Recordstore bng phng thc static

    listRecordStores()

  • 8/3/2019 Lap Trinh Java05-RMS

    10/37

    10

    To record store

    RecordStoreopenRecordStore(String recordStoreName,

    boolean createIfNecessary)

    RecordStoreopenRecordStore(String recordStoreName,

    boolean createIfNecessary,

    int authmode, boolean writable)

    RecordStoreopenRecordStore(

    String recordStoreName,

    String vendorName, String suiteName)

  • 8/3/2019 Lap Trinh Java05-RMS

    11/37

    11

    To record store

    createIfNecessary yu cu to record store nu chac)

    authmode:

    AUTHMODE_PRIVATE

    AUTHMODE_ANYwritable - true nu RecordStore cho php cc MIDletsuite khc c th ghi (nu cp quyn)

    vendorName tn NSX ca MIDlet suite

    suiteName - tnMIDlet suite

  • 8/3/2019 Lap Trinh Java05-RMS

    12/37

    12

    RecordStore API

    voidcloseRecordStore()voiddeleteRecordStore(String recordStoreName)

    String[] listRecordStores()

    intaddRecord(byte[] data, int offset, int numBytes)

    voidsetRecord (int recordId, byte[] newData,int offset, int numBytes)

    voiddeleteRecord (int recordId)

  • 8/3/2019 Lap Trinh Java05-RMS

    13/37

    13

    Truy cp 1 record store

    byte[] getRecord (int recordId)intgetRecord (int recordId, byte[] buffer, int offset)

    intgetRecordSize (int recordId)

    intgetNextRecordID()

    intgetNumRecords()longgetLastModified()

  • 8/3/2019 Lap Trinh Java05-RMS

    14/37

    14

    Thao tc c v ghi

    Cp pht streamGhi d liu

    Flush d liu trong stream

    Chuyn stream d liu thnh mng

    Ghi mng vo record storeng cc stream

  • 8/3/2019 Lap Trinh Java05-RMS

    15/37

    15

    Thao tc c v ghi (#1)

    publicvoidwriteStream(boolean[] bData, int[] iData, String[] sData) {

    // Write data into an internal byte array

    ByteArrayOutputStream strmBytes =

    newByteArrayOutputStream();// Write Java data types into the above byte array

    DataOutputStream strmDataType =

    newDataOutputStream(strmBytes);

  • 8/3/2019 Lap Trinh Java05-RMS

    16/37

    16

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

  • 8/3/2019 Lap Trinh Java05-RMS

    17/37

    17

    Thao tc c v ghi (#3)

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

    }

  • 8/3/2019 Lap Trinh Java05-RMS

    18/37

    18

    nh dng v thm RecordTrc khi thm record, cn nh dng record

  • 8/3/2019 Lap Trinh Java05-RMS

    19/37

    19

    Ly Record

    Ly recordpublicbyte[] getRecord(int recordId)

    throwsRecordStoreException,

    RecordStoreNotOpenException,

    InvalidRecordIDException

  • 8/3/2019 Lap Trinh Java05-RMS

    20/37

    20

    Xa Record

    Khi xa Record, cn truyn record ID# vo hmdeleteRecord() ca i tng RecordStore.

    record ID# b xa s khng c dng li

  • 8/3/2019 Lap Trinh Java05-RMS

    21/37

    21

    Xa Record

  • 8/3/2019 Lap Trinh Java05-RMS

    22/37

    22

    Truy vn Record

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

    C th dng RecordEnumeration duyt tun t quacc record kim tra record no tha iu kin lc.

    Interface RecordComparator cung cp phng thccompare() cho php nh ngha cch so snh 2 record khisp xp

  • 8/3/2019 Lap Trinh Java05-RMS

    23/37

    23

    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

  • 8/3/2019 Lap Trinh Java05-RMS

    24/37

    24

    RecordFilter APIclassSearchFilterimplementsRecordFilter {

    privateString searchText = null;

    publicSearchFilter(String searchText) {// Text to findthis.searchText = searchText.toLowerCase();}

    publicbooleanmatches(byte[] candidate) {String str = newString(candidate).toLowerCase();// Does the text exist?if (searchText != null&&str.indexOf(searchText)!=-1) { returntrue;

    } else { returnfalse;}

    }

    }

  • 8/3/2019 Lap Trinh Java05-RMS

    25/37

    25

    RecordFilter API

    // Create search filterSearchFilter 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... ...

    }

  • 8/3/2019 Lap Trinh Java05-RMS

    26/37

    26

    Sp xp Record sp xp cn dng interface RecordComparator.

    Cn ci t phng thc compare() so snh 2 record

  • 8/3/2019 Lap Trinh Java05-RMS

    27/37

    27

    RecordComparator API

    intcompare(byte[] rec1, byte[] rec2)staticintEQUIVALENT

    staticintFOLLOWS

    staticintPRECEDES

  • 8/3/2019 Lap Trinh Java05-RMS

    28/37

    28

    RecordComparator API

    publicclasscomparatorimplementsRecordComparator {publicintcompare(byte[] rec1, byte[] rec2) {

    String str1 = newString(rec1),str2 = newString(rec2);

    int result = str1.compareTo(str2);if (result == 0) {return RecordComparator.EQUIVALENT;} elseif (result < 0) {returnRecordComparator.PRECEDES;

    } else {returnRecordComparator.FOLLOWS;

    }}

    }

  • 8/3/2019 Lap Trinh Java05-RMS

    29/37

    29

    RecordComparator API

    // Create a new comparator for sortingComparator comp = newComparator();

    // Reference the comparator when creating the result

    setRecordEnumeration re = rs.enumerateRecords

    (null, comp, false);

    // Retrieve each record in sorted orderwhile (re.hasNextElement()) {

    String str = newString(re.nextRecord()); ...

    }

  • 8/3/2019 Lap Trinh Java05-RMS

    30/37

    30

    Duyt Record

    C th duyt qua cc record bng interfaceRecordEnumeration

    Xc nh recordno c chn

    Th t sp xp

    Tr v cc recordc chn theoth t quy nh

    Tr v tt c record(khng sp th t)

  • 8/3/2019 Lap Trinh Java05-RMS

    31/37

    31

    RecordEnumeration API

    intnumRecords()byte[] nextRecord()

    intnextRecordId()

    byte[] previousRecord()

    intpreviousRecordId()booleanhasNextElement()

  • 8/3/2019 Lap Trinh Java05-RMS

    32/37

    32

    RecordEnumeration API

    booleanhasPreviousElement()voidkeepUpdated(boolean keepUpdated)

    booleanisKeptUpdated()

    voidrebuild()

    voidreset()voiddestroy()

  • 8/3/2019 Lap Trinh Java05-RMS

    33/37

    33

    Pht hin s thay i ca Record

    C th ng k eventlistener cho 1 RecordStoreS dng phng thc addRecordListener() vremoveRecordListener()

    RecordListener c 3 hm callback:

    publicvoidrecordAdded(RecordStore rs, int id)publicvoidrecordChanged(RecordStore rs, int id)

    publicvoidrecordDeleted(RecordStore rs, int id)

  • 8/3/2019 Lap Trinh Java05-RMS

    34/37

    34

    RecordListener API

    RecordListener cho php nhn x l s kin khi c sthay 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

  • 8/3/2019 Lap Trinh Java05-RMS

    35/37

    35

    RecordListener API

    voidrecordAdded( RecordStore recordStore,int recordId)

    voidrecordChanged( RecordStore recordStore,

    int recordId)

    voidrecordDeleted( RecordStore recordStore,int recordId)

  • 8/3/2019 Lap Trinh Java05-RMS

    36/37

    36

    To 1 record store// Open record storers = RecordStore.openRecordStore(REC_STORE, true);

    ...// Using handle to open record store, create a listenerrs.addRecordListener(newDemoRecordListener());...classDemoRecordListenerimplementsRecordListener {

    publicvoidrecordAdded(RecordStore recordStore,int recordId)

    { System.out.println("Record added"); }publicvoidrecordDeleted(RecordStore recordStore,

    int recordId){ System.out.println("Record deleted"); }publicvoid recordChanged(RecordStore recordStore,

    int recordId){ System.out.println("Record changed"); }

    }

  • 8/3/2019 Lap Trinh Java05-RMS

    37/37

    37

    S dng file Resource

    C th s dng lu tr trong MIDP: c cc file resourcetrong file JAR ca MIDlet suite

    Dng phng thc getResourceAsStream().