Record Management System (RMS) - ThS. Trần Minh Triết

ppt 37 trang phuongnguyen 2300
Bạn đang xem 20 trang mẫu của tài liệu "Record Management System (RMS) - ThS. Trần Minh Triết", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pptrecord_management_system_rms_ths_tran_minh_triet.ppt

Nội dung text: Record Management System (RMS) - ThS. Trần Minh Triết

  1. Đại học Khoa học Tự nhiên ĐHQG-HCM Khoa Công Nghệ Thông Tin Record Management System (RMS) ThS. Trần Minh Triết 1
  2. Nội dung Lưu trữ dữ liệu tại server từ xa Lưu trữ cục bộ với Record Management System Phạm vi quản lý của Record Management Quản lý Record Store Định nghĩa, định dạng, thêm vào Record Stores Truy cập và xóa trong Record Store Truy vấn trên Record Store 2
  3. Lưu trữ từ xa Có thể sử dụng lưu trữ từ xa (remote storage) trên server Ưu: Kích thước ứng dụng nhỏ Chỉ lưu những dữ liệu cần thiết Nội dung động Thường dùng trong lập trình game Khuyết: Không đáng tin cậy Tốn thời gian và chi phí Chi phí server 3
  4. Record Store? Trong MIDP, việc lưu trữ sử dụng record store Một record store là 1 tập hợp các record Việc tổ chức lưu trữ thật sự do cài đặt của MIDP Record store có thể chứa trên là hard disk, battery- backed RAM, flash ROM Mỗi record là 1 mảng các byte và có số định danh duy nhất Nội dung của mảng byte do ứng dụng tự quy định Cần dùng gói javax.microedition.rms 4
  5. RMS MIDP lưu trữ thông tin bằng cách dùng hệ thống lưu trữ (Record Management System - RMS). RMS cho phép đọc, ghi dữ liệu, sắp xếp, tìm kiếm và lấy thông tin theo nhiều tùy chọn khác nhau 5
  6. Record Store trong MIDP 6
  7. Dữ liệu trong Record Store Record là mảng byte được định dạng theo ý định riêng Các record trong 1 record store được tổ chức là mảng các byte Các mảng byte không cần cùng độ dài Mỗi record có 1 định danh (ID) riêng Record có thể không liên tiếp vì 1 số record đã bị xóa 7
  8. Quản lý Record trong MIDP Mỗi Record store đều có tên và được phân biệt thông qua tên Tên gồm tối đa 32 ký tự (case sensitive) Hai record store trong cùng 1 MIDlet suite không được trùng tên 8
  9. Quản lý RecordStore Sử dụng phương thức static để mở 1 record store: public static RecordStore openRecordStore( String recordStoreName, boolean createIfNecessary) throws RecordStoreException, RecordStoreFullException, RecordStoreNotFoundException Đóng recordstore bằng phương thức: closeRecordStore() Xóa record store bằng phương thức static deleteRecordStore() Lấy danh sách Recordstore bằng phương thức static listRecordStores() 9
  10. Tạo 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
  11. Tạo record store createIfNecessary – yêu cầu tạo record store nếu chưa có) authmode: AUTHMODE_PRIVATE AUTHMODE_ANY writable - true nếu RecordStore cho phép các MIDlet suite khác có thể ghi (nếu cấp quyền) vendorName – tên NSX của MIDlet suite suiteName - tênMIDlet suite 11
  12. 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. Truy cập 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
  14. Thao tác Đọc và ghi Cấp phát stream Ghi dữ liệu Flush dữ liệu trong stream Chuyển stream dữ liệu thành mảng Ghi mảng vào record store Đóng các stream 14
  15. Thao tác Đọ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
  16. Thao tác Đọ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
  17. Thao tác Đọc và ghi (#3) strmBytes.close(); strmDataType.close(); } 17
  18. Định dạng và thêm Record Trước khi thêm record, cần định dạng record 18
  19. Lấy Record Lấy record public byte[] getRecord(int recordId) throws RecordStoreException, RecordStoreNotOpenException, InvalidRecordIDException 19
  20. Xóa Record Khi xóa Record, cần truyền record ID# vào hàm deleteRecord() của đối tượng RecordStore. record ID# đã bị xóa sẽ không được dùng lại 20
  21. Xóa Record 21
  22. Truy vấn Record Interface RecordFilter cho phép lọc các record theo các tiêu chí khác nhau. Phương thức matches() được dùng để định nghĩa điều kiện lọc Có thể dùng RecordEnumeration để duyệt tuần tự qua các record để kiểm tra record nào thỏa điều kiện lọc. Interface RecordComparator cung cấp phương thức compare() cho phép định nghĩa cách so sánh 2 record khi sắp xếp 22
  23. Lọc Record Interface RecordFilter cho phép lọc các record theo các tiêu chí khác nhau. Phương thức matches() được dùng để định nghĩa điều kiện lọc 23
  24. 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
  25. 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
  26. Sắp xếp Record Để sắp xếp cần dùng interface RecordComparator. Cần cài đặt phương thức compare() để so sánh 2 record 26
  27. RecordComparator API int compare(byte[] rec1, byte[] rec2) static int EQUIVALENT static int FOLLOWS static int PRECEDES 27
  28. 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
  29. 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
  30. Duyệt Record Có thể duyệt qua các record bằng interface RecordEnumeration Xác định record nào được chọn Thứ tự sắp xếp Trả về các record được chọn theo thứ tự quy định Trả về tất cả record (không sắp thứ tự) 30
  31. RecordEnumeration API int numRecords() byte[] nextRecord() int nextRecordId() byte[] previousRecord() int previousRecordId() boolean hasNextElement() 31
  32. RecordEnumeration API boolean hasPreviousElement() void keepUpdated(boolean keepUpdated) boolean isKeptUpdated() void rebuild() void reset() void destroy() 32
  33. Phát hiện sự thay đổi của Record Có thể đăng ký event listener cho 1 RecordStore Sử dụng phương thức addRecordListener() và removeRecordListener() RecordListener có 3 hàm callback: public void recordAdded(RecordStore rs, int id) public void recordChanged(RecordStore rs, int id) public void recordDeleted(RecordStore rs, int id) 33
  34. RecordListener API RecordListener cho phép nhận để xử lý sự kiện khi có sự thay đổi, thêm, xóa trong record store. Các bước sử dụng RecordListener: Mở (hay tạo)record store. Tạo 1 listener mới Cài đặt các phương thức của interface RecordListener 34
  35. RecordListener API void recordAdded( RecordStore recordStore, int recordId) void recordChanged( RecordStore recordStore, int recordId) void recordDeleted( RecordStore recordStore, int recordId) 35
  36. Tạo 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. Sử dụng file Resource Có thể sử dụng lưu trữ trong MIDP: đọc các file resource trong file JAR của MIDlet suite Dùng phương thức getResourceAsStream(). 37