150 likes | 392 Views
RMS - Record Management System. Record Store. En Record er et bytearray af vilkårlig størrelse En RecordStore er et antal Records nummerede 1, 2, 3, ... Når en ny Record indsættes anvendes næste endnu ikke anvendte index (ingen genbrug af indexværdier). Oprette en RecordStore. ...
E N D
Record Store • En Record er et bytearray af vilkårlig størrelse • En RecordStore er et antal Records nummerede 1, 2, 3, ... • Når en ny Record indsættes anvendes næste endnu ikke anvendte index (ingen genbrug af indexværdier) ITJEM1/jrt
Oprette en RecordStore ... // Create a record store RecordStore rs = null; try { rs = RecordStore.openRecordStore( "myrs", true ); } catch( RecordStoreException e ){ // couldn't open it or create it } ... ITJEM1/jrt
Indsætte og opdatere Records // Indsætte en ny Record byte[] data = new byte[]{0, 1, 2, 3 }; int recordID; recordID = rs.addRecord(data, 0, data.length); // Opdatere en eksisterende Record int recordID = ...; // some record ID byte[] data = new byte[]{ 0, 10, 20, 30}; rs.setRecord(recordID, data, 1, 2); // replaces all data in record with 10, 20 ITJEM1/jrt
Læse og fjerne Records ... int recordID = .... // some record ID byte[] data = rs.getRecord(recordID); ... ... int recordID = ...; // some record ID rs.deleteRecord(recordID); ... Once a record is deleted, any attempt to use it throws an InvalidRecordIDException. ITJEM1/jrt
Slette en RecordStore ... try { RecordStore.deleteRecordStore("myrs"); } catch(RecordStoreNotFoundException e){ // no such record store } catch(RecordStoreException e){ // somebody has it open } ... ITJEM1/jrt
Flere RecordStore operationer • getLastModified() returns the time of the last modification • getName() returns the name of the record store. • getNumRecords() returns the number of records in the record store. • getSize() returns the total size of the record store, in bytes. • getSizeAvailable() returns the number of bytes available for growth. • getVersion() returns the version number of the record store. ITJEM1/jrt
RMS Exceptions • InvalidRecordIDException is thrown when an operation cannot be performed because the record ID is invalid. • RecordStoreFullException is thrown when no more space is available in the record store. • RecordStoreNotFoundException is thrown when the application tries to open a non-existent record store. • RecordStoreNotOpenException is thrown when an application tries to access a record store that has already been closed. • RecordStoreException is the superclass of the other four, and is thrown for general errors they don't cover. ITJEM1/jrt
Datamapping 1 public void fromByteArray(byte[] data) throws IOException { ByteArrayInputStream bin = new ByteArrayInputStream(data); DataInputStream din = new DataInputStream(bin); _firstName = din.readUTF(); _lastName = din.readUTF(); _phoneNumber = din.readUTF(); din.close(); } public byte[] toByteArray() throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream dout = new DataOutputStream(bout); dout.writeUTF(getFirstName()); dout.writeUTF(getLastName()); dout.writeUTF(getPhoneNumber()); dout.close(); return bout.toByteArray(); } ITJEM1/jrt
Enumerations 1 RecordStore rs = ... // an open record store RecordEnumeration enum = rs.enumerateRecords( filter, comperator, tracking_updates ); ... // use the enumeration here enum.destroy(); // always clean it up! enum.hasNextElement() enum.nextRecordId() enum.nextRecord() osv Rækkefølgen i enum er udefineret ITJEM1/jrt
Enumerations 2 RecordFilter public class MyFilter implements RecordFilter { public boolean matches( byte[] recordData ) { ... // matching code here } } ITJEM1/jrt
Enumerations 3 RecordComparator public class MyComparator implements RecordComparator { public int compare(byte[] r1, byte[] r2){ int salary1 = getSalary(r1); int salary2 = getSalary(r2); if(salary1 < salary2){ return PRECEDES; } else if(salary1 == salary2){ return EQUIVALENT; } else { return FOLLOWS; } } } ITJEM1/jrt
Alternativer • RMS • Filsystem (FileConnection API) • PIM (Personal Information Management API) RMS er tilgængelig på de fleste apparater Filesystem og PIM er ikke så tilgængelige som RMS, og kræver endvidere certificering ITJEM1/jrt
Afsluttende bemærkninger • Alle RMS operationer er trådsikre • Attributten MIDlet-Data-Size (minimumsværdi) bør sættes i såvel jad som manifest filer • Det vil være rart at kunne arbejde på "objektniveau" • Der er behov for at kunne søge effektivt i en RecordStore ITJEM1/jrt